1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
This file defines all numerical functions
24
#include <drizzled/server_includes.h>
26
#include <mysys/my_bit.h>
27
#include <drizzled/drizzled_error_messages.h>
29
bool check_reserved_words(LEX_STRING *name)
31
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
32
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
33
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
41
true if item is a constant
45
eval_const_cond(COND *cond)
47
return ((Item_func*) cond)->val_int() ? true : false;
51
void Item_func::set_arguments(List<Item> &list)
54
arg_count=list.elements;
55
args= tmp_arg; // If 2 arguments
56
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
58
List_iterator_fast<Item> li(list);
60
Item **save_args= args;
65
with_sum_func|=item->with_sum_func;
68
list.empty(); // Fields are used
71
Item_func::Item_func(List<Item> &list)
77
Item_func::Item_func(THD *thd, Item_func *item)
78
:Item_result_field(thd, item),
79
allowed_arg_cols(item->allowed_arg_cols),
80
arg_count(item->arg_count),
81
used_tables_cache(item->used_tables_cache),
82
not_null_tables_cache(item->not_null_tables_cache),
83
const_item_cache(item->const_item_cache)
91
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
94
memcpy(args, item->args, sizeof(Item*)*arg_count);
100
Resolve references to table column for a function and its argument
105
ref Pointer to where this object is used. This reference
106
is used if we want to replace this object with another
107
one (for example in the summary functions).
110
Call fix_fields() for all arguments to the function. The main intention
111
is to allow all Item_field() objects to setup pointers to the table fields.
113
Sets as a side effect the following class variables:
114
maybe_null Set if any argument may return NULL
115
with_sum_func Set if any of the arguments contains a sum function
116
used_tables_cache Set to union of the tables used by arguments
118
str_value.charset If this is a string function, set this to the
119
character set for the first argument.
120
If any argument is binary, this is set to binary
122
If for any item any of the defaults are wrong, then this can
123
be fixed in the fix_length_and_dec() function that is called
124
after this one or by writing a specialized fix_fields() for the
129
true Got error. Stored with my_error().
133
Item_func::fix_fields(THD *thd, Item **ref __attribute__((unused)))
136
Item **arg,**arg_end;
137
void *save_thd_marker= thd->thd_marker;
138
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
140
used_tables_cache= not_null_tables_cache= 0;
143
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
144
return true; // Fatal error if flag is set!
146
{ // Print purify happy
147
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
151
We can't yet set item to *arg as fix_fields may change *arg
152
We shouldn't call fix_fields() twice, so check 'fixed' field first
154
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
155
return true; /* purecov: inspected */
158
if (allowed_arg_cols)
160
if (item->check_cols(allowed_arg_cols))
165
/* we have to fetch allowed_arg_cols from first argument */
166
assert(arg == args); // it is first argument
167
allowed_arg_cols= item->cols();
168
assert(allowed_arg_cols); // Can't be 0 any more
171
if (item->maybe_null)
174
with_sum_func= with_sum_func || item->with_sum_func;
175
used_tables_cache|= item->used_tables();
176
not_null_tables_cache|= item->not_null_tables();
177
const_item_cache&= item->const_item();
178
with_subselect|= item->with_subselect;
181
fix_length_and_dec();
182
if (thd->is_error()) // An error inside fix_length_and_dec occured
185
thd->thd_marker= save_thd_marker;
190
void Item_func::fix_after_pullout(st_select_lex *new_parent,
191
Item **ref __attribute__((unused)))
193
Item **arg,**arg_end;
195
used_tables_cache= not_null_tables_cache= 0;
200
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
202
(*arg)->fix_after_pullout(new_parent, arg);
205
used_tables_cache|= item->used_tables();
206
not_null_tables_cache|= item->not_null_tables();
207
const_item_cache&= item->const_item();
213
bool Item_func::walk(Item_processor processor, bool walk_subquery,
218
Item **arg,**arg_end;
219
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
221
if ((*arg)->walk(processor, walk_subquery, argument))
225
return (this->*processor)(argument);
228
void Item_func::traverse_cond(Cond_traverser traverser,
229
void *argument, traverse_order order)
233
Item **arg,**arg_end;
237
(*traverser)(this, argument);
238
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
240
(*arg)->traverse_cond(traverser, argument, order);
244
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
246
(*arg)->traverse_cond(traverser, argument, order);
248
(*traverser)(this, argument);
252
(*traverser)(this, argument);
257
Transform an Item_func object with a transformer callback function.
259
The function recursively applies the transform method to each
260
argument of the Item_func node.
261
If the call of the method for an argument item returns a new item
262
the old item is substituted for a new one.
263
After this the transformer is applied to the root node
264
of the Item_func object.
265
@param transformer the transformer callback function to be applied to
266
the nodes of the tree of the object
267
@param argument parameter to be passed to the transformer
270
Item returned as the result of transformation of the root node
273
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
277
Item **arg,**arg_end;
278
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
280
Item *new_item= (*arg)->transform(transformer, argument);
285
THD::change_item_tree() should be called only if the tree was
286
really transformed, i.e. when a new item has been created.
287
Otherwise we'll be allocating a lot of unnecessary memory for
288
change records at each execution.
290
if (*arg != new_item)
291
current_thd->change_item_tree(arg, new_item);
294
return (this->*transformer)(argument);
299
Compile Item_func object with a processor and a transformer
302
First the function applies the analyzer to the root node of
303
the Item_func object. Then if the analizer succeeeds (returns true)
304
the function recursively applies the compile method to each argument
305
of the Item_func node.
306
If the call of the method for an argument item returns a new item
307
the old item is substituted for a new one.
308
After this the transformer is applied to the root node
309
of the Item_func object.
311
@param analyzer the analyzer callback function to be applied to the
312
nodes of the tree of the object
313
@param[in,out] arg_p parameter to be passed to the processor
314
@param transformer the transformer callback function to be applied to the
315
nodes of the tree of the object
316
@param arg_t parameter to be passed to the transformer
319
Item returned as the result of transformation of the root node
322
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
323
Item_transformer transformer, uchar *arg_t)
325
if (!(this->*analyzer)(arg_p))
329
Item **arg,**arg_end;
330
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
333
The same parameter value of arg_p must be passed
334
to analyze any argument of the condition formula.
336
uchar *arg_v= *arg_p;
337
Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
338
if (new_item && *arg != new_item)
339
current_thd->change_item_tree(arg, new_item);
342
return (this->*transformer)(arg_t);
346
See comments in Item_cmp_func::split_sum_func()
349
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
352
Item **arg, **arg_end;
353
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
354
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, true);
358
void Item_func::update_used_tables()
362
for (uint i=0 ; i < arg_count ; i++)
364
args[i]->update_used_tables();
365
used_tables_cache|=args[i]->used_tables();
366
const_item_cache&=args[i]->const_item();
371
table_map Item_func::used_tables() const
373
return used_tables_cache;
377
table_map Item_func::not_null_tables() const
379
return not_null_tables_cache;
383
void Item_func::print(String *str, enum_query_type query_type)
385
str->append(func_name());
387
print_args(str, 0, query_type);
392
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
394
for (uint i=from ; i < arg_count ; i++)
398
args[i]->print(str, query_type);
403
void Item_func::print_op(String *str, enum_query_type query_type)
406
for (uint i=0 ; i < arg_count-1 ; i++)
408
args[i]->print(str, query_type);
410
str->append(func_name());
413
args[arg_count-1]->print(str, query_type);
418
bool Item_func::eq(const Item *item, bool binary_cmp) const
420
/* Assume we don't have rtti */
423
if (item->type() != FUNC_ITEM)
425
Item_func *item_func=(Item_func*) item;
426
Item_func::Functype func_type;
427
if ((func_type= functype()) != item_func->functype() ||
428
arg_count != item_func->arg_count ||
429
(func_type != Item_func::FUNC_SP &&
430
func_name() != item_func->func_name()) ||
431
(func_type == Item_func::FUNC_SP &&
432
my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
434
for (uint i=0; i < arg_count ; i++)
435
if (!args[i]->eq(item_func->args[i], binary_cmp))
441
Field *Item_func::tmp_table_field(TABLE *table)
445
switch (result_type()) {
447
if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
448
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
450
field= new Field_long(max_length, maybe_null, name, unsigned_flag);
453
field= new Field_double(max_length, maybe_null, name, decimals);
456
return make_string_field(table);
459
field= new Field_new_decimal(my_decimal_precision_to_length(decimal_precision(),
462
maybe_null, name, decimals, unsigned_flag);
466
// This case should never be chosen
477
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
480
int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
481
return decimal_value;
485
String *Item_real_func::val_str(String *str)
488
double nr= val_real();
490
return 0; /* purecov: inspected */
491
str->set_real(nr,decimals, &my_charset_bin);
496
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
499
double nr= val_real();
501
return 0; /* purecov: inspected */
502
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
503
return decimal_value;
507
void Item_func::fix_num_length_and_dec()
511
for (uint i=0 ; i < arg_count ; i++)
513
set_if_bigger(decimals,args[i]->decimals);
514
set_if_bigger(fl_length, args[i]->max_length);
516
max_length=float_length(decimals);
517
if (fl_length > max_length)
519
decimals= NOT_FIXED_DEC;
520
max_length= float_length(NOT_FIXED_DEC);
525
void Item_func_numhybrid::fix_num_length_and_dec()
530
Set max_length/decimals of function if function is fixed point and
531
result length/precision depends on argument ones.
534
void Item_func::count_decimal_length()
539
for (uint i=0 ; i < arg_count ; i++)
541
set_if_bigger(decimals, args[i]->decimals);
542
set_if_bigger(max_int_part, args[i]->decimal_int_part());
543
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
545
int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
546
max_length= my_decimal_precision_to_length(precision, decimals,
552
Set max_length of if it is maximum length of its arguments.
555
void Item_func::count_only_length()
559
for (uint i=0 ; i < arg_count ; i++)
561
set_if_bigger(max_length, args[i]->max_length);
562
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
568
Set max_length/decimals of function if function is floating point and
569
result length/precision depends on argument ones.
572
void Item_func::count_real_length()
577
for (uint i=0 ; i < arg_count ; i++)
579
if (decimals != NOT_FIXED_DEC)
581
set_if_bigger(decimals, args[i]->decimals);
582
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
584
set_if_bigger(max_length, args[i]->max_length);
586
if (decimals != NOT_FIXED_DEC)
590
if (length < max_length) // If previous operation gave overflow
591
max_length= UINT32_MAX;
599
void Item_func::signal_divide_by_null()
601
THD *thd= current_thd;
602
if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
603
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO,
604
ER(ER_DIVISION_BY_ZERO));
609
Item *Item_func::get_tmp_table_item(THD *thd)
611
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
612
return new Item_field(result_field);
613
return copy_or_same(thd);
616
double Item_int_func::val_real()
620
return unsigned_flag ? (double) ((uint64_t) val_int()) : (double) val_int();
624
String *Item_int_func::val_str(String *str)
627
int64_t nr=val_int();
630
str->set_int(nr, unsigned_flag, &my_charset_bin);
635
void Item_func_connection_id::fix_length_and_dec()
637
Item_int_func::fix_length_and_dec();
642
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
644
if (Item_int_func::fix_fields(thd, ref))
646
thd->thread_specific_used= true;
647
value= thd->variables.pseudo_thread_id;
653
Check arguments here to determine result's type for a numeric
654
function of two arguments.
657
void Item_num_op::find_num_type(void)
659
assert(arg_count == 2);
660
Item_result r0= args[0]->result_type();
661
Item_result r1= args[1]->result_type();
663
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
664
r0 == STRING_RESULT || r1 ==STRING_RESULT)
667
max_length= float_length(decimals);
668
hybrid_type= REAL_RESULT;
670
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
672
hybrid_type= DECIMAL_RESULT;
677
assert(r0 == INT_RESULT && r1 == INT_RESULT);
679
hybrid_type=INT_RESULT;
687
Set result type for a numeric function of one argument
688
(can be also used by a numeric function of many arguments, if the result
689
type depends only on the first argument)
692
void Item_func_num1::find_num_type()
694
switch (hybrid_type= args[0]->result_type()) {
696
unsigned_flag= args[0]->unsigned_flag;
700
hybrid_type= REAL_RESULT;
701
max_length= float_length(decimals);
712
void Item_func_num1::fix_num_length_and_dec()
714
decimals= args[0]->decimals;
715
max_length= args[0]->max_length;
719
void Item_func_numhybrid::fix_length_and_dec()
721
fix_num_length_and_dec();
726
String *Item_func_numhybrid::val_str(String *str)
729
switch (hybrid_type) {
732
my_decimal decimal_value, *val;
733
if (!(val= decimal_op(&decimal_value)))
734
return 0; // null is set
735
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
736
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
741
int64_t nr= int_op();
743
return 0; /* purecov: inspected */
744
str->set_int(nr, unsigned_flag, &my_charset_bin);
749
double nr= real_op();
751
return 0; /* purecov: inspected */
752
str->set_real(nr,decimals,&my_charset_bin);
756
return str_op(&str_value);
764
double Item_func_numhybrid::val_real()
767
switch (hybrid_type) {
770
my_decimal decimal_value, *val;
772
if (!(val= decimal_op(&decimal_value)))
773
return 0.0; // null is set
774
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
779
int64_t result= int_op();
780
return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
788
String *res= str_op(&str_value);
789
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
790
&end_not_used, &err_not_used) : 0.0);
799
int64_t Item_func_numhybrid::val_int()
802
switch (hybrid_type) {
805
my_decimal decimal_value, *val;
806
if (!(val= decimal_op(&decimal_value)))
807
return 0; // null is set
809
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
815
return (int64_t) rint(real_op());
820
if (!(res= str_op(&str_value)))
823
char *end= (char*) res->ptr() + res->length();
824
const CHARSET_INFO * const cs= str_value.charset();
825
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
834
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
836
my_decimal *val= decimal_value;
838
switch (hybrid_type) {
840
val= decimal_op(decimal_value);
844
int64_t result= int_op();
845
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
850
double result= (double)real_op();
851
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
857
if (!(res= str_op(&str_value)))
860
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
861
res->length(), res->charset(), decimal_value);
872
void Item_func_signed::print(String *str, enum_query_type query_type)
874
str->append(STRING_WITH_LEN("cast("));
875
args[0]->print(str, query_type);
876
str->append(STRING_WITH_LEN(" as signed)"));
881
int64_t Item_func_signed::val_int_from_str(int *error)
883
char buff[MAX_FIELD_WIDTH], *end, *start;
885
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
889
For a string result, we must first get the string and then convert it
893
if (!(res= args[0]->val_str(&tmp)))
900
start= (char *)res->ptr();
901
length= res->length();
904
value= my_strtoll10(start, &end, error);
905
if (*error > 0 || end != start+ length)
908
String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
909
err_tmp.copy(start, length, system_charset_info);
910
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
911
ER_TRUNCATED_WRONG_VALUE,
912
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
919
int64_t Item_func_signed::val_int()
924
if (args[0]->cast_to_int_type() != STRING_RESULT ||
925
args[0]->result_as_int64_t())
927
value= args[0]->val_int();
928
null_value= args[0]->null_value;
932
value= val_int_from_str(&error);
933
if (value < 0 && error == 0)
935
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
936
"Cast to signed converted positive out-of-range integer to "
937
"it's negative complement");
943
void Item_func_unsigned::print(String *str, enum_query_type query_type)
945
str->append(STRING_WITH_LEN("cast("));
946
args[0]->print(str, query_type);
947
str->append(STRING_WITH_LEN(" as unsigned)"));
952
int64_t Item_func_unsigned::val_int()
957
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
959
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
960
if (!(null_value= args[0]->null_value))
961
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
966
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
967
args[0]->result_as_int64_t())
969
value= args[0]->val_int();
970
null_value= args[0]->null_value;
974
value= val_int_from_str(&error);
976
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
977
"Cast to unsigned converted negative integer to it's "
978
"positive complement");
983
String *Item_decimal_typecast::val_str(String *str)
985
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
988
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
993
double Item_decimal_typecast::val_real()
995
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
999
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
1004
int64_t Item_decimal_typecast::val_int()
1006
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1010
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
1015
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
1017
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1021
if ((null_value= args[0]->null_value))
1023
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
1029
my_decimal_set_zero(dec);
1033
precision= my_decimal_length_to_precision(max_length,
1034
decimals, unsigned_flag);
1035
if (precision - decimals < (uint) my_decimal_intg(dec))
1037
max_my_decimal(dec, precision, decimals);
1044
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1045
ER_WARN_DATA_OUT_OF_RANGE,
1046
ER(ER_WARN_DATA_OUT_OF_RANGE),
1052
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
1054
char len_buf[20*3 + 1];
1057
uint precision= my_decimal_length_to_precision(max_length, decimals,
1059
str->append(STRING_WITH_LEN("cast("));
1060
args[0]->print(str, query_type);
1061
str->append(STRING_WITH_LEN(" as decimal("));
1063
end=int10_to_str(precision, len_buf,10);
1064
str->append(len_buf, (uint32_t) (end - len_buf));
1068
end=int10_to_str(decimals, len_buf,10);
1069
str->append(len_buf, (uint32_t) (end - len_buf));
1076
double Item_func_plus::real_op()
1078
double value= args[0]->val_real() + args[1]->val_real();
1079
if ((null_value=args[0]->null_value || args[1]->null_value))
1081
return fix_result(value);
1085
int64_t Item_func_plus::int_op()
1087
int64_t value=args[0]->val_int()+args[1]->val_int();
1088
if ((null_value=args[0]->null_value || args[1]->null_value))
1095
Calculate plus of two decimals.
1097
@param decimal_value Buffer that can be used to store result
1100
0 Value was NULL; In this case null_value is set
1102
\# Value of operation as a decimal
1105
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
1107
my_decimal value1, *val1;
1108
my_decimal value2, *val2;
1109
val1= args[0]->val_decimal(&value1);
1110
if ((null_value= args[0]->null_value))
1112
val2= args[1]->val_decimal(&value2);
1113
if (!(null_value= (args[1]->null_value ||
1114
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
1116
return decimal_value;
1121
Set precision of results for additive operations (+ and -)
1123
void Item_func_additive_op::result_precision()
1125
decimals= max(args[0]->decimals, args[1]->decimals);
1126
int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1127
args[1]->decimal_precision() - args[1]->decimals);
1128
int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1130
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1131
if (result_type() == INT_RESULT)
1132
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1134
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1135
max_length= my_decimal_precision_to_length(precision, decimals,
1141
The following function is here to allow the user to force
1142
subtraction of UNSIGNED BIGINT to return negative values.
1145
void Item_func_minus::fix_length_and_dec()
1147
Item_num_op::fix_length_and_dec();
1148
if (unsigned_flag &&
1149
(current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
1154
double Item_func_minus::real_op()
1156
double value= args[0]->val_real() - args[1]->val_real();
1157
if ((null_value=args[0]->null_value || args[1]->null_value))
1159
return fix_result(value);
1163
int64_t Item_func_minus::int_op()
1165
int64_t value=args[0]->val_int() - args[1]->val_int();
1166
if ((null_value=args[0]->null_value || args[1]->null_value))
1173
See Item_func_plus::decimal_op for comments.
1176
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
1178
my_decimal value1, *val1;
1179
my_decimal value2, *val2=
1181
val1= args[0]->val_decimal(&value1);
1182
if ((null_value= args[0]->null_value))
1184
val2= args[1]->val_decimal(&value2);
1185
if (!(null_value= (args[1]->null_value ||
1186
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
1188
return decimal_value;
1193
double Item_func_mul::real_op()
1196
double value= args[0]->val_real() * args[1]->val_real();
1197
if ((null_value=args[0]->null_value || args[1]->null_value))
1199
return fix_result(value);
1203
int64_t Item_func_mul::int_op()
1206
int64_t value=args[0]->val_int()*args[1]->val_int();
1207
if ((null_value=args[0]->null_value || args[1]->null_value))
1213
/** See Item_func_plus::decimal_op for comments. */
1215
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
1217
my_decimal value1, *val1;
1218
my_decimal value2, *val2;
1219
val1= args[0]->val_decimal(&value1);
1220
if ((null_value= args[0]->null_value))
1222
val2= args[1]->val_decimal(&value2);
1223
if (!(null_value= (args[1]->null_value ||
1224
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
1226
return decimal_value;
1231
void Item_func_mul::result_precision()
1233
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1234
if (result_type() == INT_RESULT)
1235
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1237
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1238
decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1239
int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
1240
(unsigned int)DECIMAL_MAX_PRECISION);
1241
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1245
double Item_func_div::real_op()
1248
double value= args[0]->val_real();
1249
double val2= args[1]->val_real();
1250
if ((null_value= args[0]->null_value || args[1]->null_value))
1254
signal_divide_by_null();
1257
return fix_result(value/val2);
1261
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
1263
my_decimal value1, *val1;
1264
my_decimal value2, *val2;
1267
val1= args[0]->val_decimal(&value1);
1268
if ((null_value= args[0]->null_value))
1270
val2= args[1]->val_decimal(&value2);
1271
if ((null_value= args[1]->null_value))
1273
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1274
val1, val2, prec_increment)) > 3)
1276
if (err == E_DEC_DIV_ZERO)
1277
signal_divide_by_null();
1281
return decimal_value;
1285
void Item_func_div::result_precision()
1287
uint precision=min(args[0]->decimal_precision() + prec_increment,
1288
(unsigned int)DECIMAL_MAX_PRECISION);
1289
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1290
if (result_type() == INT_RESULT)
1291
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1293
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1294
decimals= min(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
1295
max_length= my_decimal_precision_to_length(precision, decimals,
1300
void Item_func_div::fix_length_and_dec()
1302
prec_increment= current_thd->variables.div_precincrement;
1303
Item_num_op::fix_length_and_dec();
1304
switch(hybrid_type) {
1307
decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1308
set_if_smaller(decimals, NOT_FIXED_DEC);
1309
max_length=args[0]->max_length - args[0]->decimals + decimals;
1310
uint tmp=float_length(decimals);
1311
set_if_smaller(max_length,tmp);
1315
hybrid_type= DECIMAL_RESULT;
1318
case DECIMAL_RESULT:
1324
maybe_null= 1; // devision by zero
1329
/* Integer division */
1330
int64_t Item_func_int_div::val_int()
1333
int64_t value=args[0]->val_int();
1334
int64_t val2=args[1]->val_int();
1335
if ((null_value= (args[0]->null_value || args[1]->null_value)))
1339
signal_divide_by_null();
1342
return (unsigned_flag ?
1343
(uint64_t) value / (uint64_t) val2 :
1348
void Item_func_int_div::fix_length_and_dec()
1350
Item_result argtype= args[0]->result_type();
1351
/* use precision ony for the data type it is applicable for and valid */
1352
max_length=args[0]->max_length -
1353
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
1354
args[0]->decimals : 0);
1356
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
1360
int64_t Item_func_mod::int_op()
1363
int64_t value= args[0]->val_int();
1364
int64_t val2= args[1]->val_int();
1367
if ((null_value= args[0]->null_value || args[1]->null_value))
1368
return 0; /* purecov: inspected */
1371
signal_divide_by_null();
1375
if (args[0]->unsigned_flag)
1376
result= args[1]->unsigned_flag ?
1377
((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
1379
result= args[1]->unsigned_flag ?
1380
value % ((uint64_t) val2) : value % val2;
1385
double Item_func_mod::real_op()
1388
double value= args[0]->val_real();
1389
double val2= args[1]->val_real();
1390
if ((null_value= args[0]->null_value || args[1]->null_value))
1391
return 0.0; /* purecov: inspected */
1394
signal_divide_by_null();
1397
return fmod(value,val2);
1401
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
1403
my_decimal value1, *val1;
1404
my_decimal value2, *val2;
1406
val1= args[0]->val_decimal(&value1);
1407
if ((null_value= args[0]->null_value))
1409
val2= args[1]->val_decimal(&value2);
1410
if ((null_value= args[1]->null_value))
1412
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1414
case E_DEC_TRUNCATED:
1416
return decimal_value;
1417
case E_DEC_DIV_ZERO:
1418
signal_divide_by_null();
1426
void Item_func_mod::result_precision()
1428
decimals= max(args[0]->decimals, args[1]->decimals);
1429
max_length= max(args[0]->max_length, args[1]->max_length);
1433
void Item_func_mod::fix_length_and_dec()
1435
Item_num_op::fix_length_and_dec();
1437
unsigned_flag= args[0]->unsigned_flag;
1441
double Item_func_neg::real_op()
1443
double value= args[0]->val_real();
1444
null_value= args[0]->null_value;
1449
int64_t Item_func_neg::int_op()
1451
int64_t value= args[0]->val_int();
1452
null_value= args[0]->null_value;
1457
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
1459
my_decimal val, *value= args[0]->val_decimal(&val);
1460
if (!(null_value= args[0]->null_value))
1462
my_decimal2decimal(value, decimal_value);
1463
my_decimal_neg(decimal_value);
1464
return decimal_value;
1470
void Item_func_neg::fix_num_length_and_dec()
1472
decimals= args[0]->decimals;
1473
/* 1 add because sign can appear */
1474
max_length= args[0]->max_length + 1;
1478
void Item_func_neg::fix_length_and_dec()
1480
Item_func_num1::fix_length_and_dec();
1483
If this is in integer context keep the context as integer if possible
1484
(This is how multiplication and other integer functions works)
1485
Use val() to get value as arg_type doesn't mean that item is
1486
Item_int or Item_real due to existence of Item_param.
1488
if (hybrid_type == INT_RESULT && args[0]->const_item())
1490
int64_t val= args[0]->val_int();
1491
if ((uint64_t) val >= (uint64_t) INT64_MIN &&
1492
((uint64_t) val != (uint64_t) INT64_MIN ||
1493
args[0]->type() != INT_ITEM))
1496
Ensure that result is converted to DECIMAL, as int64_t can't hold
1499
hybrid_type= DECIMAL_RESULT;
1507
double Item_func_abs::real_op()
1509
double value= args[0]->val_real();
1510
null_value= args[0]->null_value;
1515
int64_t Item_func_abs::int_op()
1517
int64_t value= args[0]->val_int();
1518
if ((null_value= args[0]->null_value))
1520
return (value >= 0) || unsigned_flag ? value : -value;
1524
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
1526
my_decimal val, *value= args[0]->val_decimal(&val);
1527
if (!(null_value= args[0]->null_value))
1529
my_decimal2decimal(value, decimal_value);
1530
if (decimal_value->sign())
1531
my_decimal_neg(decimal_value);
1532
return decimal_value;
1538
void Item_func_abs::fix_length_and_dec()
1540
Item_func_num1::fix_length_and_dec();
1541
unsigned_flag= args[0]->unsigned_flag;
1545
/** Gateway to natural LOG function. */
1546
double Item_func_ln::val_real()
1549
double value= args[0]->val_real();
1550
if ((null_value= args[0]->null_value))
1554
signal_divide_by_null();
1561
Extended but so slower LOG function.
1563
We have to check if all values are > zero and first one is not one
1564
as these are the cases then result is not a number.
1566
double Item_func_log::val_real()
1569
double value= args[0]->val_real();
1570
if ((null_value= args[0]->null_value))
1574
signal_divide_by_null();
1579
double value2= args[1]->val_real();
1580
if ((null_value= args[1]->null_value))
1582
if (value2 <= 0.0 || value == 1.0)
1584
signal_divide_by_null();
1587
return log(value2) / log(value);
1592
double Item_func_log2::val_real()
1595
double value= args[0]->val_real();
1597
if ((null_value=args[0]->null_value))
1601
signal_divide_by_null();
1604
return log(value) / M_LN2;
1607
double Item_func_log10::val_real()
1610
double value= args[0]->val_real();
1611
if ((null_value= args[0]->null_value))
1615
signal_divide_by_null();
1618
return log10(value);
1621
double Item_func_exp::val_real()
1624
double value= args[0]->val_real();
1625
if ((null_value=args[0]->null_value))
1626
return 0.0; /* purecov: inspected */
1627
return fix_result(exp(value));
1630
double Item_func_sqrt::val_real()
1633
double value= args[0]->val_real();
1634
if ((null_value=(args[0]->null_value || value < 0)))
1635
return 0.0; /* purecov: inspected */
1639
double Item_func_pow::val_real()
1642
double value= args[0]->val_real();
1643
double val2= args[1]->val_real();
1644
if ((null_value=(args[0]->null_value || args[1]->null_value)))
1645
return 0.0; /* purecov: inspected */
1646
return fix_result(pow(value,val2));
1649
// Trigonometric functions
1651
double Item_func_acos::val_real()
1654
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1655
volatile double value= args[0]->val_real();
1656
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1661
double Item_func_asin::val_real()
1664
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1665
volatile double value= args[0]->val_real();
1666
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1671
double Item_func_atan::val_real()
1674
double value= args[0]->val_real();
1675
if ((null_value=args[0]->null_value))
1679
double val2= args[1]->val_real();
1680
if ((null_value=args[1]->null_value))
1682
return fix_result(atan2(value,val2));
1687
double Item_func_cos::val_real()
1690
double value= args[0]->val_real();
1691
if ((null_value=args[0]->null_value))
1696
double Item_func_sin::val_real()
1699
double value= args[0]->val_real();
1700
if ((null_value=args[0]->null_value))
1705
double Item_func_tan::val_real()
1708
double value= args[0]->val_real();
1709
if ((null_value=args[0]->null_value))
1711
return fix_result(tan(value));
1715
// Shift-functions, same as << and >> in C/C++
1718
int64_t Item_func_shift_left::val_int()
1722
uint64_t res= ((uint64_t) args[0]->val_int() <<
1723
(shift=(uint) args[1]->val_int()));
1724
if (args[0]->null_value || args[1]->null_value)
1730
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1733
int64_t Item_func_shift_right::val_int()
1737
uint64_t res= (uint64_t) args[0]->val_int() >>
1738
(shift=(uint) args[1]->val_int());
1739
if (args[0]->null_value || args[1]->null_value)
1745
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0LL);
1749
int64_t Item_func_bit_neg::val_int()
1752
uint64_t res= (uint64_t) args[0]->val_int();
1753
if ((null_value=args[0]->null_value))
1759
// Conversion functions
1761
void Item_func_integer::fix_length_and_dec()
1763
max_length=args[0]->max_length - args[0]->decimals+1;
1764
uint tmp=float_length(decimals);
1765
set_if_smaller(max_length,tmp);
1769
void Item_func_int_val::fix_num_length_and_dec()
1771
max_length= args[0]->max_length - (args[0]->decimals ?
1772
args[0]->decimals + 1 :
1774
uint tmp= float_length(decimals);
1775
set_if_smaller(max_length,tmp);
1780
void Item_func_int_val::find_num_type()
1782
switch(hybrid_type= args[0]->result_type())
1786
hybrid_type= REAL_RESULT;
1787
max_length= float_length(decimals);
1790
case DECIMAL_RESULT:
1792
-2 because in most high position can't be used any digit for int64_t
1793
and one position for increasing value during operation
1795
if ((args[0]->max_length - args[0]->decimals) >=
1796
(DECIMAL_LONGLONG_DIGITS - 2))
1798
hybrid_type= DECIMAL_RESULT;
1802
unsigned_flag= args[0]->unsigned_flag;
1803
hybrid_type= INT_RESULT;
1813
int64_t Item_func_ceiling::int_op()
1816
switch (args[0]->result_type()) {
1818
result= args[0]->val_int();
1819
null_value= args[0]->null_value;
1821
case DECIMAL_RESULT:
1823
my_decimal dec_buf, *dec;
1824
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1825
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1831
result= (int64_t)Item_func_ceiling::real_op();
1837
double Item_func_ceiling::real_op()
1840
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1843
volatile double value= args[0]->val_real();
1844
null_value= args[0]->null_value;
1849
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1851
my_decimal val, *value= args[0]->val_decimal(&val);
1852
if (!(null_value= (args[0]->null_value ||
1853
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1854
decimal_value) > 1)))
1855
return decimal_value;
1860
int64_t Item_func_floor::int_op()
1863
switch (args[0]->result_type()) {
1865
result= args[0]->val_int();
1866
null_value= args[0]->null_value;
1868
case DECIMAL_RESULT:
1870
my_decimal dec_buf, *dec;
1871
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1872
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1878
result= (int64_t)Item_func_floor::real_op();
1884
double Item_func_floor::real_op()
1887
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1890
volatile double value= args[0]->val_real();
1891
null_value= args[0]->null_value;
1892
return floor(value);
1896
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1898
my_decimal val, *value= args[0]->val_decimal(&val);
1899
if (!(null_value= (args[0]->null_value ||
1900
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1901
decimal_value) > 1)))
1902
return decimal_value;
1907
void Item_func_round::fix_length_and_dec()
1909
int decimals_to_set;
1913
unsigned_flag= args[0]->unsigned_flag;
1914
if (!args[1]->const_item())
1916
max_length= args[0]->max_length;
1917
decimals= args[0]->decimals;
1918
if (args[0]->result_type() == DECIMAL_RESULT)
1921
hybrid_type= DECIMAL_RESULT;
1924
hybrid_type= REAL_RESULT;
1928
val1= args[1]->val_int();
1929
val1_unsigned= args[1]->unsigned_flag;
1931
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1933
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1935
if (args[0]->decimals == NOT_FIXED_DEC)
1937
max_length= args[0]->max_length;
1938
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1939
hybrid_type= REAL_RESULT;
1943
switch (args[0]->result_type()) {
1946
hybrid_type= REAL_RESULT;
1947
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1948
max_length= float_length(decimals);
1951
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1953
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1954
max_length= args[0]->max_length + length_can_increase;
1955
/* Here we can keep INT_RESULT */
1956
hybrid_type= INT_RESULT;
1961
case DECIMAL_RESULT:
1963
hybrid_type= DECIMAL_RESULT;
1964
decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1965
int decimals_delta= args[0]->decimals - decimals_to_set;
1966
int precision= args[0]->decimal_precision();
1967
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1969
precision-= decimals_delta - length_increase;
1970
decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
1971
max_length= my_decimal_precision_to_length(precision, decimals,
1976
assert(0); /* This result type isn't handled */
1980
double my_double_round(double value, int64_t dec, bool dec_unsigned,
1984
bool dec_negative= (dec < 0) && !dec_unsigned;
1985
uint64_t abs_dec= dec_negative ? -dec : dec;
1987
tmp2 is here to avoid return the value with 80 bit precision
1988
This will fix that the test round(0.1,1) = round(0.1,1) is true
1990
volatile double tmp2;
1992
tmp=(abs_dec < array_elements(log_10) ?
1993
log_10[abs_dec] : pow(10.0,(double) abs_dec));
1995
if (dec_negative && my_isinf(tmp))
1997
else if (!dec_negative && my_isinf(value * tmp))
2002
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
2004
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
2007
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
2012
double Item_func_round::real_op()
2014
double value= args[0]->val_real();
2016
if (!(null_value= args[0]->null_value || args[1]->null_value))
2017
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
2024
Rounds a given value to a power of 10 specified as the 'to' argument,
2025
avoiding overflows when the value is close to the uint64_t range boundary.
2028
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
2030
uint64_t tmp= value / to * to;
2031
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
2035
int64_t Item_func_round::int_op()
2037
int64_t value= args[0]->val_int();
2038
int64_t dec= args[1]->val_int();
2041
if ((null_value= args[0]->null_value || args[1]->null_value))
2043
if ((dec >= 0) || args[1]->unsigned_flag)
2044
return value; // integer have not digits after point
2049
if(abs_dec >= array_elements(log_10_int))
2052
tmp= log_10_int[abs_dec];
2055
value= (unsigned_flag) ?
2056
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
2058
value= (unsigned_flag || value >= 0) ?
2059
my_unsigned_round((uint64_t) value, tmp) :
2060
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
2065
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
2067
my_decimal val, *value= args[0]->val_decimal(&val);
2068
int64_t dec= args[1]->val_int();
2069
if (dec >= 0 || args[1]->unsigned_flag)
2070
dec= min(dec, (int64_t) decimals);
2071
else if (dec < INT_MIN)
2074
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2075
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2076
truncate, decimal_value) > 1)))
2078
decimal_value->frac= decimals;
2079
return decimal_value;
2085
void Item_func_rand::seed_random(Item *arg)
2088
TODO: do not do reinit 'rand' for every execute of PS/SP if
2089
args[0] is a constant.
2091
uint32_t tmp= (uint32_t) arg->val_int();
2092
randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
2093
(uint32_t) (tmp*0x10000001L));
2097
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
2099
if (Item_real_func::fix_fields(thd, ref))
2101
used_tables_cache|= RAND_TABLE_BIT;
2103
{ // Only use argument once in query
2105
Allocate rand structure once: we must use thd->stmt_arena
2106
to create rand in proper mem_root if it's a prepared statement or
2109
No need to send a Rand log event if seed was given eg: RAND(seed),
2110
as it will be replicated in the query as such.
2112
if (!rand && !(rand= (struct rand_struct*)
2113
thd->stmt_arena->alloc(sizeof(*rand))))
2116
if (args[0]->const_item())
2117
seed_random (args[0]);
2122
Save the seed only the first time RAND() is used in the query
2123
Once events are forwarded rather than recreated,
2124
the following can be skipped if inside the slave thread
2126
if (!thd->rand_used)
2129
thd->rand_saved_seed1= thd->rand.seed1;
2130
thd->rand_saved_seed2= thd->rand.seed2;
2137
void Item_func_rand::update_used_tables()
2139
Item_real_func::update_used_tables();
2140
used_tables_cache|= RAND_TABLE_BIT;
2144
double Item_func_rand::val_real()
2147
if (arg_count && !args[0]->const_item())
2148
seed_random (args[0]);
2149
return my_rnd(rand);
2152
int64_t Item_func_sign::val_int()
2155
double value= args[0]->val_real();
2156
null_value=args[0]->null_value;
2157
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
2161
double Item_func_units::val_real()
2164
double value= args[0]->val_real();
2165
if ((null_value=args[0]->null_value))
2167
return value*mul+add;
2171
void Item_func_min_max::fix_length_and_dec()
2174
bool datetime_found= false;
2178
cmp_type=args[0]->result_type();
2180
for (uint i=0 ; i < arg_count ; i++)
2182
set_if_bigger(max_length, args[i]->max_length);
2183
set_if_bigger(decimals, args[i]->decimals);
2184
set_if_bigger(max_int_part, args[i]->decimal_int_part());
2185
if (args[i]->maybe_null)
2187
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
2188
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
2190
datetime_found= true;
2191
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
2192
datetime_item= args[i];
2195
if (cmp_type == STRING_RESULT)
2197
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2201
compare_as_dates= true;
2204
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
2205
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
2207
cached_field_type= agg_field_type(args, arg_count);
2212
Compare item arguments in the DATETIME context.
2216
value [out] found least/greatest DATE/DATETIME value
2219
Compare item arguments as DATETIME values and return the index of the
2220
least/greatest argument in the arguments array.
2221
The correct integer DATE/DATETIME value of the found argument is
2222
stored to the value pointer, if latter is provided.
2225
0 If one of arguments is NULL
2226
# index of the least/greatest argument
2229
uint Item_func_min_max::cmp_datetimes(uint64_t *value)
2231
uint64_t min_max= 0;
2232
uint min_max_idx= 0;
2234
for (uint i=0; i < arg_count ; i++)
2236
Item **arg= args + i;
2238
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
2239
if ((null_value= args[i]->null_value))
2241
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
2250
if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
2257
String *Item_func_min_max::val_str(String *str)
2260
if (compare_as_dates)
2263
uint min_max_idx= cmp_datetimes(NULL);
2266
str_res= args[min_max_idx]->val_str(str);
2267
str_res->set_charset(collation.collation);
2273
int64_t nr=val_int();
2276
str->set_int(nr, unsigned_flag, &my_charset_bin);
2279
case DECIMAL_RESULT:
2281
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
2284
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
2289
double nr= val_real();
2291
return 0; /* purecov: inspected */
2292
str->set_real(nr,decimals,&my_charset_bin);
2299
for (uint i=0; i < arg_count ; i++)
2302
res=args[i]->val_str(str);
2306
res2= args[i]->val_str(res == str ? &tmp_value : str);
2309
int cmp= sortcmp(res,res2,collation.collation);
2310
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
2314
if ((null_value= args[i]->null_value))
2317
res->set_charset(collation.collation);
2322
// This case should never be chosen
2326
return 0; // Keep compiler happy
2330
double Item_func_min_max::val_real()
2334
if (compare_as_dates)
2337
(void)cmp_datetimes(&result);
2338
return (double)result;
2340
for (uint i=0; i < arg_count ; i++)
2343
value= args[i]->val_real();
2346
double tmp= args[i]->val_real();
2347
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2350
if ((null_value= args[i]->null_value))
2357
int64_t Item_func_min_max::val_int()
2361
if (compare_as_dates)
2364
(void)cmp_datetimes(&result);
2365
return (int64_t)result;
2367
for (uint i=0; i < arg_count ; i++)
2370
value=args[i]->val_int();
2373
int64_t tmp=args[i]->val_int();
2374
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2377
if ((null_value= args[i]->null_value))
2384
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
2387
my_decimal tmp_buf, *tmp, *res= NULL;
2389
if (compare_as_dates)
2392
(void)cmp_datetimes(&value);
2393
uint64_t2decimal(value, dec);
2396
for (uint i=0; i < arg_count ; i++)
2399
res= args[i]->val_decimal(dec);
2402
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
2403
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
2405
if (tmp == &tmp_buf)
2407
/* Move value out of tmp_buf as this will be reused on next loop */
2408
my_decimal2decimal(tmp, dec);
2415
if ((null_value= args[i]->null_value))
2425
int64_t Item_func_length::val_int()
2428
String *res=args[0]->val_str(&value);
2432
return 0; /* purecov: inspected */
2435
return (int64_t) res->length();
2439
int64_t Item_func_char_length::val_int()
2442
String *res=args[0]->val_str(&value);
2446
return 0; /* purecov: inspected */
2449
return (int64_t) res->numchars();
2453
int64_t Item_func_coercibility::val_int()
2457
return (int64_t) args[0]->collation.derivation;
2461
void Item_func_locate::fix_length_and_dec()
2463
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
2464
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2468
int64_t Item_func_locate::val_int()
2471
String *a=args[0]->val_str(&value1);
2472
String *b=args[1]->val_str(&value2);
2476
return 0; /* purecov: inspected */
2479
/* must be int64_t to avoid truncation */
2486
start0= start= args[2]->val_int() - 1;
2488
if ((start < 0) || (start > a->length()))
2491
/* start is now sufficiently valid to pass to charpos function */
2492
start= a->charpos((int) start);
2494
if (start + b->length() > a->length())
2498
if (!b->length()) // Found empty string at start
2501
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
2503
(uint) (a->length()-start),
2504
b->ptr(), b->length(),
2507
return (int64_t) match.mb_len + start0 + 1;
2511
void Item_func_locate::print(String *str, enum_query_type query_type)
2513
str->append(STRING_WITH_LEN("locate("));
2514
args[1]->print(str, query_type);
2516
args[0]->print(str, query_type);
2520
args[2]->print(str, query_type);
2526
int64_t Item_func_field::val_int()
2530
if (cmp_type == STRING_RESULT)
2533
if (!(field= args[0]->val_str(&value)))
2535
for (uint i=1 ; i < arg_count ; i++)
2537
String *tmp_value=args[i]->val_str(&tmp);
2538
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2539
return (int64_t) (i);
2542
else if (cmp_type == INT_RESULT)
2544
int64_t val= args[0]->val_int();
2545
if (args[0]->null_value)
2547
for (uint i=1; i < arg_count ; i++)
2549
if (val == args[i]->val_int() && !args[i]->null_value)
2550
return (int64_t) (i);
2553
else if (cmp_type == DECIMAL_RESULT)
2555
my_decimal dec_arg_buf, *dec_arg,
2556
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2557
if (args[0]->null_value)
2559
for (uint i=1; i < arg_count; i++)
2561
dec_arg= args[i]->val_decimal(&dec_arg_buf);
2562
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2563
return (int64_t) (i);
2568
double val= args[0]->val_real();
2569
if (args[0]->null_value)
2571
for (uint i=1; i < arg_count ; i++)
2573
if (val == args[i]->val_real() && !args[i]->null_value)
2574
return (int64_t) (i);
2581
void Item_func_field::fix_length_and_dec()
2583
maybe_null=0; max_length=3;
2584
cmp_type= args[0]->result_type();
2585
for (uint i=1; i < arg_count ; i++)
2586
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2587
if (cmp_type == STRING_RESULT)
2588
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2592
int64_t Item_func_ascii::val_int()
2595
String *res=args[0]->val_str(&value);
2602
return (int64_t) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
2605
int64_t Item_func_ord::val_int()
2608
String *res=args[0]->val_str(&value);
2615
if (!res->length()) return 0;
2617
if (use_mb(res->charset()))
2619
register const char *str=res->ptr();
2620
register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2622
return (int64_t)((uchar) *str);
2624
n=(n<<8)|(uint32_t)((uchar) *str++);
2628
return (int64_t) ((uchar) (*res)[0]);
2631
/* Search after a string in a string of strings separated by ',' */
2632
/* Returns number of found type >= 1 or 0 if not found */
2633
/* This optimizes searching in enums to bit testing! */
2635
void Item_func_find_in_set::fix_length_and_dec()
2638
max_length=3; // 1-999
2639
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2642
static const char separator=',';
2644
int64_t Item_func_find_in_set::val_int()
2649
uint64_t tmp=(uint64_t) args[1]->val_int();
2650
if (!(null_value=args[1]->null_value || args[0]->null_value))
2658
String *find=args[0]->val_str(&value);
2659
String *buffer=args[1]->val_str(&value2);
2660
if (!find || !buffer)
2663
return 0; /* purecov: inspected */
2668
if ((diff=buffer->length() - find->length()) >= 0)
2671
const CHARSET_INFO * const cs= cmp_collation.collation;
2672
const char *str_begin= buffer->ptr();
2673
const char *str_end= buffer->ptr();
2674
const char *real_end= str_end+buffer->length();
2675
const uchar *find_str= (const uchar *) find->ptr();
2676
uint find_str_len= find->length();
2681
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end,
2682
(uchar*) real_end)) > 0)
2684
const char *substr_end= str_end + symbol_len;
2685
bool is_last_item= (substr_end == real_end);
2686
bool is_separator= (wc == (my_wc_t) separator);
2687
if (is_separator || is_last_item)
2690
if (is_last_item && !is_separator)
2691
str_end= substr_end;
2692
if (!my_strnncoll(cs, (const uchar *) str_begin,
2693
str_end - str_begin,
2694
find_str, find_str_len))
2695
return (int64_t) position;
2697
str_begin= substr_end;
2699
str_end= substr_end;
2701
else if (str_end - str_begin == 0 &&
2702
find_str_len == 0 &&
2703
wc == (my_wc_t) separator)
2704
return (int64_t) ++position;
2712
int64_t Item_func_bit_count::val_int()
2715
uint64_t value= (uint64_t) args[0]->val_int();
2716
if ((null_value= args[0]->null_value))
2717
return 0; /* purecov: inspected */
2718
return (int64_t) my_count_bits(value);
2722
/****************************************************************************
2723
** Functions to handle dynamic loadable functions
2724
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2725
** Rewritten by monty.
2726
****************************************************************************/
2728
void udf_handler::cleanup()
2734
if (u_d->func_deinit != NULL)
2736
Udf_func_deinit deinit= u_d->func_deinit;
2742
if (buffers) // Because of bug in ecc
2750
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2751
uint arg_count, Item **arguments)
2753
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2755
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2756
return(true); // Fatal error flag is set!
2758
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2762
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2768
/* Fix all arguments */
2770
used_tables_cache=0;
2773
if ((f_args.arg_count=arg_count))
2775
if (!(f_args.arg_type= (Item_result*)
2776
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2782
Item **arg,**arg_end;
2783
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2787
if (!(*arg)->fixed &&
2788
(*arg)->fix_fields(thd, arg))
2790
// we can't assign 'item' before, because fix_fields() can change arg
2792
if (item->check_cols(1))
2795
TODO: We should think about this. It is not always
2796
right way just to set an UDF result to return my_charset_bin
2797
if one argument has binary sorting order.
2798
The result collation should be calculated according to arguments
2799
derivations in some cases and should not in other cases.
2800
Moreover, some arguments can represent a numeric input
2801
which doesn't effect the result character set and collation.
2802
There is no a general rule for UDF. Everything depends on
2803
the particular user defined function.
2805
if (item->collation.collation->state & MY_CS_BINSORT)
2806
func->collation.set(&my_charset_bin);
2807
if (item->maybe_null)
2809
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2810
used_tables_cache|=item->used_tables();
2811
const_item_cache&=item->const_item();
2812
f_args.arg_type[i]=item->result_type();
2814
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2815
if (!(buffers=new String[arg_count]) ||
2816
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2817
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2818
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2819
!(num_buffer= (char*) sql_alloc(arg_count *
2820
ALIGN_SIZE(sizeof(double)))) ||
2821
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2822
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2828
func->fix_length_and_dec();
2829
initid.max_length=func->max_length;
2830
initid.maybe_null=func->maybe_null;
2831
initid.const_item=const_item_cache;
2832
initid.decimals=func->decimals;
2837
char init_msg_buff[DRIZZLE_ERRMSG_SIZE];
2838
char *to=num_buffer;
2839
for (uint i=0; i < arg_count; i++)
2842
For a constant argument i, args->args[i] points to the argument value.
2843
For non-constant, args->args[i] is NULL.
2845
f_args.args[i]= NULL; /* Non-const unless updated below. */
2847
f_args.lengths[i]= arguments[i]->max_length;
2848
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2849
f_args.attributes[i]= arguments[i]->name;
2850
f_args.attribute_lengths[i]= arguments[i]->name_length;
2852
if (arguments[i]->const_item())
2854
switch (arguments[i]->result_type())
2857
case DECIMAL_RESULT:
2859
String *res= arguments[i]->val_str(&buffers[i]);
2860
if (arguments[i]->null_value)
2862
f_args.args[i]= (char*) res->c_ptr();
2863
f_args.lengths[i]= res->length();
2867
*((int64_t*) to)= arguments[i]->val_int();
2868
if (arguments[i]->null_value)
2871
to+= ALIGN_SIZE(sizeof(int64_t));
2874
*((double*) to)= arguments[i]->val_real();
2875
if (arguments[i]->null_value)
2878
to+= ALIGN_SIZE(sizeof(double));
2882
// This case should never be chosen
2888
Udf_func_init init= u_d->func_init;
2889
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2891
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2892
u_d->name.str, init_msg_buff);
2895
func->max_length=min(initid.max_length,(unsigned long)MAX_BLOB_WIDTH);
2896
func->maybe_null=initid.maybe_null;
2897
const_item_cache=initid.const_item;
2899
Keep used_tables_cache in sync with const_item_cache.
2900
See the comment in Item_udf_func::update_used tables.
2902
if (!const_item_cache && !used_tables_cache)
2903
used_tables_cache= RAND_TABLE_BIT;
2904
func->decimals=min(initid.decimals,(unsigned int)NOT_FIXED_DEC);
2909
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2910
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2917
bool udf_handler::get_arguments()
2920
return 1; // Got an error earlier
2921
char *to= num_buffer;
2923
for (uint i=0; i < f_args.arg_count; i++)
2926
switch (f_args.arg_type[i]) {
2928
case DECIMAL_RESULT:
2930
String *res=args[i]->val_str(&buffers[str_count++]);
2931
if (!(args[i]->null_value))
2933
f_args.args[i]= (char*) res->ptr();
2934
f_args.lengths[i]= res->length();
2939
*((int64_t*) to) = args[i]->val_int();
2940
if (!args[i]->null_value)
2943
to+= ALIGN_SIZE(sizeof(int64_t));
2947
*((double*) to)= args[i]->val_real();
2948
if (!args[i]->null_value)
2951
to+= ALIGN_SIZE(sizeof(double));
2956
// This case should never be chosen
2966
(String*)NULL in case of NULL values
2968
String *udf_handler::val_str(String *str,String *save_str)
2970
uchar is_null_tmp=0;
2973
if (get_arguments())
2975
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2976
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
2979
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
2980
{ // This happens VERY seldom
2981
if (str->alloc(MAX_FIELD_WIDTH))
2987
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
2988
&is_null_tmp, &error);
2989
if (is_null_tmp || !res || error) // The !res is for safety
2993
if (res == str->ptr())
2995
str->length(res_length);
2998
save_str->set(res, res_length, str->charset());
3004
For the moment, UDF functions are returning DECIMAL values as strings
3007
my_decimal *udf_handler::val_decimal(bool *null_value, my_decimal *dec_buf)
3009
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3010
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3012
if (get_arguments())
3017
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3018
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3021
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3022
if (is_null || error)
3027
end= res+ res_length;
3028
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3033
void Item_udf_func::cleanup()
3036
Item_func::cleanup();
3040
void Item_udf_func::print(String *str, enum_query_type query_type)
3042
str->append(func_name());
3044
for (uint i=0 ; i < arg_count ; i++)
3048
args[i]->print_item_w_name(str, query_type);
3054
double Item_func_udf_float::val_real()
3057
return(udf.val(&null_value));
3061
String *Item_func_udf_float::val_str(String *str)
3064
double nr= val_real();
3066
return 0; /* purecov: inspected */
3067
str->set_real(nr,decimals,&my_charset_bin);
3072
int64_t Item_func_udf_int::val_int()
3075
return(udf.val_int(&null_value));
3079
String *Item_func_udf_int::val_str(String *str)
3082
int64_t nr=val_int();
3085
str->set_int(nr, unsigned_flag, &my_charset_bin);
3090
int64_t Item_func_udf_decimal::val_int()
3092
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3096
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3101
double Item_func_udf_decimal::val_real()
3103
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3107
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3112
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3115
return(udf.val_decimal(&null_value, dec_buf));
3119
String *Item_func_udf_decimal::val_str(String *str)
3121
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3124
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3125
str->length(DECIMAL_MAX_STR_LENGTH);
3126
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3127
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3132
void Item_func_udf_decimal::fix_length_and_dec()
3134
fix_num_length_and_dec();
3138
/* Default max_length is max argument length */
3140
void Item_func_udf_str::fix_length_and_dec()
3143
for (uint i = 0; i < arg_count; i++)
3144
set_if_bigger(max_length,args[i]->max_length);
3148
String *Item_func_udf_str::val_str(String *str)
3151
String *res=udf.val_str(str,&str_value);
3159
This has to come last in the udf_handler methods, or C for AIX
3160
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3163
udf_handler::~udf_handler()
3165
/* Everything should be properly cleaned up by this moment. */
3166
assert(not_original || !(initialized || buffers));
3173
pthread_mutex_t LOCK_user_locks;
3174
static HASH hash_user_locks;
3176
class User_level_lock
3184
pthread_cond_t cond;
3185
my_thread_id thread_id;
3186
void set_thread(THD *thd) { thread_id= thd->thread_id; }
3188
User_level_lock(const uchar *key_arg,uint length, ulong id)
3189
:key_length(length),count(1),locked(1), thread_id(id)
3191
key= (uchar*) my_memdup(key_arg,length,MYF(0));
3192
pthread_cond_init(&cond,NULL);
3195
if (my_hash_insert(&hash_user_locks,(uchar*) this))
3197
my_free(key,MYF(0));
3206
hash_delete(&hash_user_locks,(uchar*) this);
3207
my_free(key, MYF(0));
3209
pthread_cond_destroy(&cond);
3211
inline bool initialized() { return key != 0; }
3212
friend void item_user_lock_release(User_level_lock *ull);
3213
friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3217
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
3218
bool not_used __attribute__((unused)))
3220
*length= ull->key_length;
3225
static bool item_user_lock_inited= 0;
3227
void item_user_lock_init(void)
3229
pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
3230
hash_init(&hash_user_locks, system_charset_info,
3231
16,0,0,(hash_get_key) ull_get_key,NULL,0);
3232
item_user_lock_inited= 1;
3235
void item_user_lock_free(void)
3237
if (item_user_lock_inited)
3239
item_user_lock_inited= 0;
3240
hash_free(&hash_user_locks);
3241
pthread_mutex_destroy(&LOCK_user_locks);
3245
void item_user_lock_release(User_level_lock *ull)
3250
pthread_cond_signal(&ull->cond);
3256
Wait until we are at or past the given position in the master binlog
3260
int64_t Item_master_pos_wait::val_int()
3263
THD* thd = current_thd;
3264
String *log_name = args[0]->val_str(&value);
3268
if (thd->slave_thread || !log_name || !log_name->length())
3273
#ifdef HAVE_REPLICATION
3274
int64_t pos = (ulong)args[1]->val_int();
3275
int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
3276
if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
3286
void debug_sync_point(const char* lock_name, uint lock_timeout)
3293
int64_t Item_func_last_insert_id::val_int()
3295
THD *thd= current_thd;
3299
int64_t value= args[0]->val_int();
3300
null_value= args[0]->null_value;
3302
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
3303
documented in the manual. We don't want to touch
3304
first_successful_insert_id_in_cur_stmt because it would make
3305
LAST_INSERT_ID(X) take precedence over an generated auto_increment
3308
thd->arg_of_last_insert_id_function= true;
3309
thd->first_successful_insert_id_in_prev_stmt= value;
3312
return thd->read_first_successful_insert_id_in_prev_stmt();
3316
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
3318
return Item_int_func::fix_fields(thd, ref);
3322
/* This function is just used to test speed of different functions */
3324
int64_t Item_func_benchmark::val_int()
3327
char buff[MAX_FIELD_WIDTH];
3328
String tmp(buff,sizeof(buff), &my_charset_bin);
3329
my_decimal tmp_decimal;
3330
THD *thd=current_thd;
3331
uint64_t loop_count;
3333
loop_count= (uint64_t) args[0]->val_int();
3335
if (args[0]->null_value ||
3336
(!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
3338
if (!args[0]->null_value)
3341
llstr(((int64_t) loop_count), buff);
3342
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
3343
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
3344
"count", buff, "benchmark");
3352
for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
3354
switch (args[1]->result_type()) {
3356
(void) args[1]->val_real();
3359
(void) args[1]->val_int();
3362
(void) args[1]->val_str(&tmp);
3364
case DECIMAL_RESULT:
3365
(void) args[1]->val_decimal(&tmp_decimal);
3369
// This case should never be chosen
3378
void Item_func_benchmark::print(String *str, enum_query_type query_type)
3380
str->append(STRING_WITH_LEN("benchmark("));
3381
args[0]->print(str, query_type);
3383
args[1]->print(str, query_type);
3387
#define extra_size sizeof(double)
3389
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
3390
bool create_if_not_exists)
3392
user_var_entry *entry;
3394
if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
3396
create_if_not_exists)
3398
uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
3399
if (!hash_inited(hash))
3401
if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
3403
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
3405
entry->name.length=name.length;
3408
entry->update_query_id=0;
3409
entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
3410
entry->unsigned_flag= 0;
3412
If we are here, we were called from a SET or a query which sets a
3413
variable. Imagine it is this:
3414
INSERT INTO t SELECT @a:=10, @a:=@a+1.
3415
Then when we have a Item_func_get_user_var (because of the @a+1) so we
3416
think we have to write the value of @a to the binlog. But before that,
3417
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
3418
the variable as "already logged" (line below) so that it won't be logged
3419
by Item_func_get_user_var (because that's not necessary).
3421
entry->used_query_id=current_thd->query_id;
3422
entry->type=STRING_RESULT;
3423
memcpy(entry->name.str, name.str, name.length+1);
3424
if (my_hash_insert(hash,(uchar*) entry))
3426
my_free((char*) entry,MYF(0));
3434
When a user variable is updated (in a SET command or a query like
3438
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
3441
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
3442
if (Item_func::fix_fields(thd, ref) ||
3443
!(entry= get_variable(&thd->user_vars, name, 1)))
3446
Remember the last query which updated it, this way a query can later know
3447
if this variable is a constant item in the query (it is if update_query_id
3448
is different from query_id).
3450
entry->update_query_id= thd->query_id;
3452
As it is wrong and confusing to associate any
3453
character set with NULL, @a should be latin2
3454
after this query sequence:
3456
SET @a=_latin2'string';
3459
I.e. the second query should not change the charset
3460
to the current default value, but should keep the
3461
original value assigned during the first query.
3462
In order to do it, we don't copy charset
3463
from the argument if the argument is NULL
3464
and the variable has previously been initialized.
3466
null_item= (args[0]->type() == NULL_ITEM);
3467
if (!entry->collation.collation || !null_item)
3468
entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3469
collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
3470
cached_result_type= args[0]->result_type();
3476
Item_func_set_user_var::fix_length_and_dec()
3478
maybe_null=args[0]->maybe_null;
3479
max_length=args[0]->max_length;
3480
decimals=args[0]->decimals;
3481
collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3486
Mark field in read_map
3489
This is used by filesort to register used fields in a a temporary
3490
column read set or to register used fields in a view
3493
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
3497
TABLE *table= (TABLE *) arg;
3498
if (result_field->table == table || !table)
3499
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3506
Set value to user variable.
3508
@param entry pointer to structure representing variable
3509
@param set_null should we set NULL value ?
3510
@param ptr pointer to buffer with new value
3511
@param length length of new value
3512
@param type type of new value
3513
@param cs charset info for new value
3514
@param dv derivation for new value
3515
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3517
@note Sets error and fatal error if allocation fails.
3526
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
3527
Item_result type, const CHARSET_INFO * const cs, Derivation dv,
3532
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3533
if (entry->value && entry->value != pos)
3534
my_free(entry->value,MYF(0));
3540
if (type == STRING_RESULT)
3541
length++; // Store strings with end \0
3542
if (length <= extra_size)
3544
/* Save value in value struct */
3545
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3546
if (entry->value != pos)
3549
my_free(entry->value,MYF(0));
3555
/* Allocate variable */
3556
if (entry->length != length)
3558
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3559
if (entry->value == pos)
3561
entry->value= (char*) my_realloc(entry->value, length,
3562
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
3568
if (type == STRING_RESULT)
3570
length--; // Fix length change above
3571
entry->value[length]= 0; // Store end \0
3573
memcpy(entry->value,ptr,length);
3574
if (type == DECIMAL_RESULT)
3575
((my_decimal*)entry->value)->fix_buffer_pointer();
3576
entry->length= length;
3577
entry->collation.set(cs, dv);
3578
entry->unsigned_flag= unsigned_arg;
3586
Item_func_set_user_var::update_hash(void *ptr, uint length,
3587
Item_result res_type,
3588
const CHARSET_INFO * const cs, Derivation dv,
3592
If we set a variable explicitely to NULL then keep the old
3593
result type of the variable
3595
if ((null_value= args[0]->null_value) && null_item)
3596
res_type= entry->type; // Don't change type of item
3597
if (::update_hash(entry, (null_value= args[0]->null_value),
3598
ptr, length, res_type, cs, dv, unsigned_arg))
3607
/** Get the value of a variable as a double. */
3609
double user_var_entry::val_real(bool *null_value)
3611
if ((*null_value= (value == 0)))
3616
return *(double*) value;
3618
return (double) *(int64_t*) value;
3619
case DECIMAL_RESULT:
3622
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
3626
return my_atof(value); // This is null terminated
3628
assert(1); // Impossible
3631
return 0.0; // Impossible
3635
/** Get the value of a variable as an integer. */
3637
int64_t user_var_entry::val_int(bool *null_value) const
3639
if ((*null_value= (value == 0)))
3644
return (int64_t) *(double*) value;
3646
return *(int64_t*) value;
3647
case DECIMAL_RESULT:
3650
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
3656
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
3659
assert(1); // Impossible
3662
return 0LL; // Impossible
3666
/** Get the value of a variable as a string. */
3668
String *user_var_entry::val_str(bool *null_value, String *str,
3671
if ((*null_value= (value == 0)))
3676
str->set_real(*(double*) value, decimals, &my_charset_bin);
3680
str->set(*(int64_t*) value, &my_charset_bin);
3682
str->set(*(uint64_t*) value, &my_charset_bin);
3684
case DECIMAL_RESULT:
3685
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
3688
if (str->copy(value, length, collation.collation))
3689
str= 0; // EOM error
3691
assert(1); // Impossible
3697
/** Get the value of a variable as a decimal. */
3699
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
3701
if ((*null_value= (value == 0)))
3706
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
3709
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
3711
case DECIMAL_RESULT:
3712
val= (my_decimal *)value;
3715
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
3718
assert(1); // Impossible
3725
This functions is invoked on SET \@variable or
3726
\@variable:= expression.
3728
Evaluate (and check expression), store results.
3731
For now it always return OK. All problem with value evaluating
3732
will be caught by thd->is_error() check in sql_set_variables().
3739
Item_func_set_user_var::check(bool use_result_field)
3741
if (use_result_field && !result_field)
3742
use_result_field= false;
3744
switch (cached_result_type) {
3747
save_result.vreal= use_result_field ? result_field->val_real() :
3748
args[0]->val_real();
3753
save_result.vint= use_result_field ? result_field->val_int() :
3755
unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
3756
args[0]->unsigned_flag;
3761
save_result.vstr= use_result_field ? result_field->val_str(&value) :
3762
args[0]->val_str(&value);
3765
case DECIMAL_RESULT:
3767
save_result.vdec= use_result_field ?
3768
result_field->val_decimal(&decimal_buff) :
3769
args[0]->val_decimal(&decimal_buff);
3774
// This case should never be chosen
3783
This functions is invoked on
3784
SET \@variable or \@variable:= expression.
3787
We have to store the expression as such in the variable, independent of
3788
the value method used by the user
3798
Item_func_set_user_var::update()
3802
switch (cached_result_type) {
3805
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
3806
REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
3811
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
3812
INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
3818
if (!save_result.vstr) // Null value
3819
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
3820
DERIVATION_IMPLICIT, 0);
3822
res= update_hash((void*) save_result.vstr->ptr(),
3823
save_result.vstr->length(), STRING_RESULT,
3824
save_result.vstr->charset(),
3825
DERIVATION_IMPLICIT, 0);
3828
case DECIMAL_RESULT:
3830
if (!save_result.vdec) // Null value
3831
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
3832
DERIVATION_IMPLICIT, 0);
3834
res= update_hash((void*) save_result.vdec,
3835
sizeof(my_decimal), DECIMAL_RESULT,
3836
&my_charset_bin, DERIVATION_IMPLICIT, 0);
3841
// This case should never be chosen
3849
double Item_func_set_user_var::val_real()
3853
update(); // Store expression
3854
return entry->val_real(&null_value);
3857
int64_t Item_func_set_user_var::val_int()
3861
update(); // Store expression
3862
return entry->val_int(&null_value);
3865
String *Item_func_set_user_var::val_str(String *str)
3869
update(); // Store expression
3870
return entry->val_str(&null_value, str, decimals);
3874
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
3878
update(); // Store expression
3879
return entry->val_decimal(&null_value, val);
3883
double Item_func_set_user_var::val_result()
3887
update(); // Store expression
3888
return entry->val_real(&null_value);
3891
int64_t Item_func_set_user_var::val_int_result()
3895
update(); // Store expression
3896
return entry->val_int(&null_value);
3899
String *Item_func_set_user_var::str_result(String *str)
3903
update(); // Store expression
3904
return entry->val_str(&null_value, str, decimals);
3908
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
3912
update(); // Store expression
3913
return entry->val_decimal(&null_value, val);
3917
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
3919
str->append(STRING_WITH_LEN("(@"));
3920
str->append(name.str, name.length);
3921
str->append(STRING_WITH_LEN(":="));
3922
args[0]->print(str, query_type);
3927
void Item_func_set_user_var::print_as_stmt(String *str,
3928
enum_query_type query_type)
3930
str->append(STRING_WITH_LEN("set @"));
3931
str->append(name.str, name.length);
3932
str->append(STRING_WITH_LEN(":="));
3933
args[0]->print(str, query_type);
3937
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
3943
return protocol->store(result_field);
3945
return Item::send(protocol, str_arg);
3948
void Item_func_set_user_var::make_field(Send_field *tmp_field)
3952
result_field->make_field(tmp_field);
3953
assert(tmp_field->table_name != 0);
3955
tmp_field->col_name=Item::name; // Use user supplied name
3958
Item::make_field(tmp_field);
3963
Save the value of a user variable into a field
3967
field target field to save the value to
3968
no_conversion flag indicating whether conversions are allowed
3971
Save the function value into a field and update the user variable
3972
accordingly. If a result field is defined and the target field doesn't
3973
coincide with it then the value from the result field will be used as
3974
the new value of the user variable.
3976
The reason to have this method rather than simply using the result
3977
field in the val_xxx() methods is that the value from the result field
3978
not always can be used when the result field is defined.
3979
Let's consider the following cases:
3980
1) when filling a tmp table the result field is defined but the value of it
3981
is undefined because it has to be produced yet. Thus we can't use it.
3982
2) on execution of an INSERT ... SELECT statement the save_in_field()
3983
function will be called to fill the data in the new record. If the SELECT
3984
part uses a tmp table then the result field is defined and should be
3985
used in order to get the correct result.
3987
The difference between the SET_USER_VAR function and regular functions
3988
like CONCAT is that the Item_func objects for the regular functions are
3989
replaced by Item_field objects after the values of these functions have
3990
been stored in a tmp table. Yet an object of the Item_field class cannot
3991
be used to update a user variable.
3992
Due to this we have to handle the result field in a special way here and
3993
in the Item_func_set_user_var::send() function.
4000
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
4001
bool can_use_result_field)
4003
bool use_result_field= (!can_use_result_field ? 0 :
4004
(result_field && result_field != field));
4007
/* Update the value of the user variable */
4008
check(use_result_field);
4011
if (result_type() == STRING_RESULT ||
4012
(result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
4015
const CHARSET_INFO * const cs= collation.collation;
4016
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
4017
str_value.set_quick(buff, sizeof(buff), cs);
4018
result= entry->val_str(&null_value, &str_value, decimals);
4022
str_value.set_quick(0, 0, cs);
4023
return set_field_to_null_with_conversions(field, no_conversions);
4026
/* NOTE: If null_value == false, "result" must be not NULL. */
4028
field->set_notnull();
4029
error=field->store(result->ptr(),result->length(),cs);
4030
str_value.set_quick(0, 0, cs);
4032
else if (result_type() == REAL_RESULT)
4034
double nr= entry->val_real(&null_value);
4036
return set_field_to_null(field);
4037
field->set_notnull();
4038
error=field->store(nr);
4040
else if (result_type() == DECIMAL_RESULT)
4042
my_decimal decimal_value;
4043
my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
4045
return set_field_to_null(field);
4046
field->set_notnull();
4047
error=field->store_decimal(val);
4051
int64_t nr= entry->val_int(&null_value);
4053
return set_field_to_null_with_conversions(field, no_conversions);
4054
field->set_notnull();
4055
error=field->store(nr, unsigned_flag);
4062
Item_func_get_user_var::val_str(String *str)
4066
return((String*) 0); // No such variable
4067
return(var_entry->val_str(&null_value, str, decimals));
4071
double Item_func_get_user_var::val_real()
4075
return 0.0; // No such variable
4076
return (var_entry->val_real(&null_value));
4080
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
4085
return var_entry->val_decimal(&null_value, dec);
4089
int64_t Item_func_get_user_var::val_int()
4093
return 0LL; // No such variable
4094
return (var_entry->val_int(&null_value));
4099
Get variable by name and, if necessary, put the record of variable
4100
use into the binary log.
4102
When a user variable is invoked from an update query (INSERT, UPDATE etc),
4103
stores this variable and its value in thd->user_var_events, so that it can be
4104
written to the binlog (will be written just before the query is written, see
4107
@param thd Current thread
4108
@param name Variable name
4109
@param[out] out_entry variable structure or NULL. The pointer is set
4110
regardless of whether function succeeded or not.
4115
1 Failed to put appropriate record into binary log
4119
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
4120
LEX_STRING &name, user_var_entry **out_entry)
4122
BINLOG_USER_VAR_EVENT *user_var_event;
4123
user_var_entry *var_entry;
4124
var_entry= get_variable(&thd->user_vars, name, 0);
4127
Any reference to user-defined variable which is done from stored
4128
function or trigger affects their execution and the execution of the
4129
calling statement. We must log all such variables even if they are
4130
not involved in table-updating statements.
4132
if (!(opt_bin_log &&
4133
(is_update_query(sql_command) || thd->in_sub_stmt)))
4135
*out_entry= var_entry;
4142
If the variable does not exist, it's NULL, but we want to create it so
4143
that it gets into the binlog (if it didn't, the slave could be
4144
influenced by a variable of the same name previously set by another
4146
We create it like if it had been explicitly set with SET before.
4147
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
4148
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
4149
in dispatch_command()). Instead of building a one-element list to pass to
4150
sql_set_variables(), we could instead manually call check() and update();
4151
this would save memory and time; but calling sql_set_variables() makes
4152
one unique place to maintain (sql_set_variables()).
4154
Manipulation with lex is necessary since free_underlaid_joins
4155
is going to release memory belonging to the main query.
4158
List<set_var_base> tmp_var_list;
4159
LEX *sav_lex= thd->lex, lex_tmp;
4162
tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
4164
/* Create the variable */
4165
if (sql_set_variables(thd, &tmp_var_list))
4171
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
4174
else if (var_entry->used_query_id == thd->query_id ||
4175
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
4178
If this variable was already stored in user_var_events by this query
4179
(because it's used in more than one place in the query), don't store
4182
*out_entry= var_entry;
4188
First we need to store value of var_entry, when the next situation
4191
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
4192
We have to write to binlog value @a= 1.
4194
We allocate the user_var_event on user_var_events_alloc pool, not on
4195
the this-statement-execution pool because in SPs user_var_event objects
4196
may need to be valid after current [SP] statement execution pool is
4199
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
4200
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
4201
alloc_root(thd->user_var_events_alloc, size)))
4204
user_var_event->value= (char*) user_var_event +
4205
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
4206
user_var_event->user_var_event= var_entry;
4207
user_var_event->type= var_entry->type;
4208
user_var_event->charset_number= var_entry->collation.collation->number;
4209
if (!var_entry->value)
4212
user_var_event->length= 0;
4213
user_var_event->value= 0;
4217
user_var_event->length= var_entry->length;
4218
memcpy(user_var_event->value, var_entry->value,
4221
/* Mark that this variable has been used by this query */
4222
var_entry->used_query_id= thd->query_id;
4223
if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
4226
*out_entry= var_entry;
4230
*out_entry= var_entry;
4234
void Item_func_get_user_var::fix_length_and_dec()
4236
THD *thd=current_thd;
4239
decimals=NOT_FIXED_DEC;
4240
max_length=MAX_BLOB_WIDTH;
4242
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
4245
If the variable didn't exist it has been created as a STRING-type.
4246
'var_entry' is NULL only if there occured an error during the call to
4247
get_var_with_binlog.
4251
m_cached_result_type= var_entry->type;
4252
unsigned_flag= var_entry->unsigned_flag;
4253
max_length= var_entry->length;
4255
collation.set(var_entry->collation);
4256
switch(m_cached_result_type) {
4258
max_length= DBL_DIG + 8;
4261
max_length= MAX_BIGINT_WIDTH;
4265
max_length= MAX_BLOB_WIDTH;
4267
case DECIMAL_RESULT:
4268
max_length= DECIMAL_MAX_STR_LENGTH;
4269
decimals= DECIMAL_MAX_SCALE;
4271
case ROW_RESULT: // Keep compiler happy
4279
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
4281
m_cached_result_type= STRING_RESULT;
4282
max_length= MAX_BLOB_WIDTH;
4287
bool Item_func_get_user_var::const_item() const
4289
return (!var_entry || current_thd->query_id != var_entry->update_query_id);
4293
enum Item_result Item_func_get_user_var::result_type() const
4295
return m_cached_result_type;
4299
void Item_func_get_user_var::print(String *str,
4300
enum_query_type query_type __attribute__((unused)))
4302
str->append(STRING_WITH_LEN("(@"));
4303
str->append(name.str,name.length);
4308
bool Item_func_get_user_var::eq(const Item *item,
4309
bool binary_cmp __attribute__((unused))) const
4311
/* Assume we don't have rtti */
4313
return 1; // Same item is same.
4314
/* Check if other type is also a get_user_var() object */
4315
if (item->type() != FUNC_ITEM ||
4316
((Item_func*) item)->functype() != functype())
4318
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
4319
return (name.length == other->name.length &&
4320
!memcmp(name.str, other->name.str, name.length));
4324
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
4327
if (Item::fix_fields(thd, ref) ||
4328
!(entry= get_variable(&thd->user_vars, name, 1)))
4330
entry->type= STRING_RESULT;
4332
Let us set the same collation which is used for loading
4333
of fields in LOAD DATA INFILE.
4334
(Since Item_user_var_as_out_param is used only there).
4336
entry->collation.set(thd->variables.collation_database);
4337
entry->update_query_id= thd->query_id;
4342
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
4344
::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
4345
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4349
void Item_user_var_as_out_param::set_value(const char *str, uint length,
4350
const CHARSET_INFO * const cs)
4352
::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
4353
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
4357
double Item_user_var_as_out_param::val_real()
4364
int64_t Item_user_var_as_out_param::val_int()
4371
String* Item_user_var_as_out_param::val_str(String *str __attribute__((unused)))
4378
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((unused)))
4385
void Item_user_var_as_out_param::print(String *str,
4386
enum_query_type query_type __attribute__((unused)))
4389
str->append(name.str,name.length);
4393
Item_func_get_system_var::
4394
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
4395
LEX_STRING *component_arg, const char *name_arg,
4396
size_t name_len_arg)
4397
:var(var_arg), var_type(var_type_arg), component(*component_arg)
4399
/* set_name() will allocate the name */
4400
set_name(name_arg, name_len_arg, system_charset_info);
4405
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
4410
Evaluate the system variable and substitute the result (a basic constant)
4411
instead of this item. If the variable can not be evaluated,
4412
the error is reported in sys_var::item().
4414
if (!(item= var->item(thd, var_type, &component)))
4415
return(1); // Impossible
4416
item->set_name(name, 0, system_charset_info); // don't allocate a new name
4417
thd->change_item_tree(ref, item);
4423
bool Item_func_get_system_var::is_written_to_binlog()
4425
return var->is_written_to_binlog(var_type);
4428
int64_t Item_func_bit_xor::val_int()
4431
uint64_t arg1= (uint64_t) args[0]->val_int();
4432
uint64_t arg2= (uint64_t) args[1]->val_int();
4433
if ((null_value= (args[0]->null_value || args[1]->null_value)))
4435
return (int64_t) (arg1 ^ arg2);
4439
/***************************************************************************
4441
****************************************************************************/
4444
Return value of an system variable base[.name] as a constant item.
4446
@param thd Thread handler
4447
@param var_type global / session
4448
@param name Name of base or system variable
4449
@param component Component.
4452
If component.str = 0 then the variable name is in 'name'
4460
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
4461
LEX_STRING component)
4464
LEX_STRING *base_name, *component_name;
4468
base_name= &component;
4469
component_name= &name;
4474
component_name= &component; // Empty string
4477
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
4481
if (!var->is_struct())
4483
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
4488
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
4490
return new Item_func_get_system_var(var, var_type, component_name,
4496
Check a user level lock.
4498
Sets null_value=true on error.
4503
0 Already taken, or error
4506
int64_t Item_func_is_free_lock::val_int()
4509
String *res=args[0]->val_str(&value);
4510
User_level_lock *ull;
4513
if (!res || !res->length())
4519
pthread_mutex_lock(&LOCK_user_locks);
4520
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4521
(size_t) res->length());
4522
pthread_mutex_unlock(&LOCK_user_locks);
4523
if (!ull || !ull->locked)
4528
int64_t Item_func_is_used_lock::val_int()
4531
String *res=args[0]->val_str(&value);
4532
User_level_lock *ull;
4535
if (!res || !res->length())
4538
pthread_mutex_lock(&LOCK_user_locks);
4539
ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
4540
(size_t) res->length());
4541
pthread_mutex_unlock(&LOCK_user_locks);
4542
if (!ull || !ull->locked)
4546
return ull->thread_id;
4550
int64_t Item_func_row_count::val_int()
4553
THD *thd= current_thd;
4555
return thd->row_count_func;
4558
int64_t Item_func_found_rows::val_int()
4561
THD *thd= current_thd;
4563
return thd->found_rows();