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
unsigned char 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,
214
unsigned char *argument)
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, unsigned char *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, unsigned char **arg_p,
323
Item_transformer transformer, unsigned char *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
unsigned char *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 (uint32_t 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, uint32_t from, enum_query_type query_type)
394
for (uint32_t 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 (uint32_t 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 (uint32_t 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()
509
uint32_t fl_length= 0;
511
for (uint32_t 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 (uint32_t 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= cmin(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 (uint32_t 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 (uint32_t 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
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
607
Item *Item_func::get_tmp_table_item(THD *thd)
609
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
610
return new Item_field(result_field);
611
return copy_or_same(thd);
614
double Item_int_func::val_real()
618
return unsigned_flag ? (double) ((uint64_t) val_int()) : (double) val_int();
622
String *Item_int_func::val_str(String *str)
625
int64_t nr=val_int();
628
str->set_int(nr, unsigned_flag, &my_charset_bin);
633
void Item_func_connection_id::fix_length_and_dec()
635
Item_int_func::fix_length_and_dec();
640
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
642
if (Item_int_func::fix_fields(thd, ref))
644
thd->thread_specific_used= true;
645
value= thd->variables.pseudo_thread_id;
651
Check arguments here to determine result's type for a numeric
652
function of two arguments.
655
void Item_num_op::find_num_type(void)
657
assert(arg_count == 2);
658
Item_result r0= args[0]->result_type();
659
Item_result r1= args[1]->result_type();
661
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
662
r0 == STRING_RESULT || r1 ==STRING_RESULT)
665
max_length= float_length(decimals);
666
hybrid_type= REAL_RESULT;
668
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
670
hybrid_type= DECIMAL_RESULT;
675
assert(r0 == INT_RESULT && r1 == INT_RESULT);
677
hybrid_type=INT_RESULT;
685
Set result type for a numeric function of one argument
686
(can be also used by a numeric function of many arguments, if the result
687
type depends only on the first argument)
690
void Item_func_num1::find_num_type()
692
switch (hybrid_type= args[0]->result_type()) {
694
unsigned_flag= args[0]->unsigned_flag;
698
hybrid_type= REAL_RESULT;
699
max_length= float_length(decimals);
710
void Item_func_num1::fix_num_length_and_dec()
712
decimals= args[0]->decimals;
713
max_length= args[0]->max_length;
717
void Item_func_numhybrid::fix_length_and_dec()
719
fix_num_length_and_dec();
724
String *Item_func_numhybrid::val_str(String *str)
727
switch (hybrid_type) {
730
my_decimal decimal_value, *val;
731
if (!(val= decimal_op(&decimal_value)))
732
return 0; // null is set
733
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
734
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
739
int64_t nr= int_op();
741
return 0; /* purecov: inspected */
742
str->set_int(nr, unsigned_flag, &my_charset_bin);
747
double nr= real_op();
749
return 0; /* purecov: inspected */
750
str->set_real(nr,decimals,&my_charset_bin);
754
return str_op(&str_value);
762
double Item_func_numhybrid::val_real()
765
switch (hybrid_type) {
768
my_decimal decimal_value, *val;
770
if (!(val= decimal_op(&decimal_value)))
771
return 0.0; // null is set
772
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
777
int64_t result= int_op();
778
return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
786
String *res= str_op(&str_value);
787
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
788
&end_not_used, &err_not_used) : 0.0);
797
int64_t Item_func_numhybrid::val_int()
800
switch (hybrid_type) {
803
my_decimal decimal_value, *val;
804
if (!(val= decimal_op(&decimal_value)))
805
return 0; // null is set
807
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
813
return (int64_t) rint(real_op());
818
if (!(res= str_op(&str_value)))
821
char *end= (char*) res->ptr() + res->length();
822
const CHARSET_INFO * const cs= str_value.charset();
823
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
832
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
834
my_decimal *val= decimal_value;
836
switch (hybrid_type) {
838
val= decimal_op(decimal_value);
842
int64_t result= int_op();
843
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
848
double result= (double)real_op();
849
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
855
if (!(res= str_op(&str_value)))
858
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
859
res->length(), res->charset(), decimal_value);
870
void Item_func_signed::print(String *str, enum_query_type query_type)
872
str->append(STRING_WITH_LEN("cast("));
873
args[0]->print(str, query_type);
874
str->append(STRING_WITH_LEN(" as signed)"));
879
int64_t Item_func_signed::val_int_from_str(int *error)
881
char buff[MAX_FIELD_WIDTH], *end, *start;
883
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
887
For a string result, we must first get the string and then convert it
891
if (!(res= args[0]->val_str(&tmp)))
898
start= (char *)res->ptr();
899
length= res->length();
902
value= my_strtoll10(start, &end, error);
903
if (*error > 0 || end != start+ length)
906
String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
907
err_tmp.copy(start, length, system_charset_info);
908
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
909
ER_TRUNCATED_WRONG_VALUE,
910
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
917
int64_t Item_func_signed::val_int()
922
if (args[0]->cast_to_int_type() != STRING_RESULT ||
923
args[0]->result_as_int64_t())
925
value= args[0]->val_int();
926
null_value= args[0]->null_value;
930
value= val_int_from_str(&error);
931
if (value < 0 && error == 0)
933
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
934
"Cast to signed converted positive out-of-range integer to "
935
"it's negative complement");
941
void Item_func_unsigned::print(String *str, enum_query_type query_type)
943
str->append(STRING_WITH_LEN("cast("));
944
args[0]->print(str, query_type);
945
str->append(STRING_WITH_LEN(" as unsigned)"));
950
int64_t Item_func_unsigned::val_int()
955
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
957
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
958
if (!(null_value= args[0]->null_value))
959
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
964
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
965
args[0]->result_as_int64_t())
967
value= args[0]->val_int();
968
null_value= args[0]->null_value;
972
value= val_int_from_str(&error);
974
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
975
"Cast to unsigned converted negative integer to it's "
976
"positive complement");
981
String *Item_decimal_typecast::val_str(String *str)
983
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
986
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
991
double Item_decimal_typecast::val_real()
993
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
997
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
1002
int64_t Item_decimal_typecast::val_int()
1004
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1008
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
1013
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
1015
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1019
if ((null_value= args[0]->null_value))
1021
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
1027
my_decimal_set_zero(dec);
1031
precision= my_decimal_length_to_precision(max_length,
1032
decimals, unsigned_flag);
1033
if (precision - decimals < (uint) my_decimal_intg(dec))
1035
max_my_decimal(dec, precision, decimals);
1042
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1043
ER_WARN_DATA_OUT_OF_RANGE,
1044
ER(ER_WARN_DATA_OUT_OF_RANGE),
1050
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
1052
char len_buf[20*3 + 1];
1055
uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
1057
str->append(STRING_WITH_LEN("cast("));
1058
args[0]->print(str, query_type);
1059
str->append(STRING_WITH_LEN(" as decimal("));
1061
end=int10_to_str(precision, len_buf,10);
1062
str->append(len_buf, (uint32_t) (end - len_buf));
1066
end=int10_to_str(decimals, len_buf,10);
1067
str->append(len_buf, (uint32_t) (end - len_buf));
1074
double Item_func_plus::real_op()
1076
double value= args[0]->val_real() + args[1]->val_real();
1077
if ((null_value=args[0]->null_value || args[1]->null_value))
1079
return fix_result(value);
1083
int64_t Item_func_plus::int_op()
1085
int64_t value=args[0]->val_int()+args[1]->val_int();
1086
if ((null_value=args[0]->null_value || args[1]->null_value))
1093
Calculate plus of two decimals.
1095
@param decimal_value Buffer that can be used to store result
1098
0 Value was NULL; In this case null_value is set
1100
\# Value of operation as a decimal
1103
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
1105
my_decimal value1, *val1;
1106
my_decimal value2, *val2;
1107
val1= args[0]->val_decimal(&value1);
1108
if ((null_value= args[0]->null_value))
1110
val2= args[1]->val_decimal(&value2);
1111
if (!(null_value= (args[1]->null_value ||
1112
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
1114
return decimal_value;
1119
Set precision of results for additive operations (+ and -)
1121
void Item_func_additive_op::result_precision()
1123
decimals= cmax(args[0]->decimals, args[1]->decimals);
1124
int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
1125
args[1]->decimal_precision() - args[1]->decimals);
1126
int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1128
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1129
if (result_type() == INT_RESULT)
1130
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1132
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1133
max_length= my_decimal_precision_to_length(precision, decimals,
1139
The following function is here to allow the user to force
1140
subtraction of UNSIGNED BIGINT to return negative values.
1143
void Item_func_minus::fix_length_and_dec()
1145
Item_num_op::fix_length_and_dec();
1151
double Item_func_minus::real_op()
1153
double value= args[0]->val_real() - args[1]->val_real();
1154
if ((null_value=args[0]->null_value || args[1]->null_value))
1156
return fix_result(value);
1160
int64_t Item_func_minus::int_op()
1162
int64_t value=args[0]->val_int() - args[1]->val_int();
1163
if ((null_value=args[0]->null_value || args[1]->null_value))
1170
See Item_func_plus::decimal_op for comments.
1173
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
1175
my_decimal value1, *val1;
1176
my_decimal value2, *val2=
1178
val1= args[0]->val_decimal(&value1);
1179
if ((null_value= args[0]->null_value))
1181
val2= args[1]->val_decimal(&value2);
1182
if (!(null_value= (args[1]->null_value ||
1183
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
1185
return decimal_value;
1190
double Item_func_mul::real_op()
1193
double value= args[0]->val_real() * args[1]->val_real();
1194
if ((null_value=args[0]->null_value || args[1]->null_value))
1196
return fix_result(value);
1200
int64_t Item_func_mul::int_op()
1203
int64_t value=args[0]->val_int()*args[1]->val_int();
1204
if ((null_value=args[0]->null_value || args[1]->null_value))
1210
/** See Item_func_plus::decimal_op for comments. */
1212
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
1214
my_decimal value1, *val1;
1215
my_decimal value2, *val2;
1216
val1= args[0]->val_decimal(&value1);
1217
if ((null_value= args[0]->null_value))
1219
val2= args[1]->val_decimal(&value2);
1220
if (!(null_value= (args[1]->null_value ||
1221
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
1223
return decimal_value;
1228
void Item_func_mul::result_precision()
1230
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1231
if (result_type() == INT_RESULT)
1232
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1234
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1235
decimals= cmin(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1236
int precision= cmin(args[0]->decimal_precision() + args[1]->decimal_precision(),
1237
(unsigned int)DECIMAL_MAX_PRECISION);
1238
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1242
double Item_func_div::real_op()
1245
double value= args[0]->val_real();
1246
double val2= args[1]->val_real();
1247
if ((null_value= args[0]->null_value || args[1]->null_value))
1251
signal_divide_by_null();
1254
return fix_result(value/val2);
1258
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
1260
my_decimal value1, *val1;
1261
my_decimal value2, *val2;
1264
val1= args[0]->val_decimal(&value1);
1265
if ((null_value= args[0]->null_value))
1267
val2= args[1]->val_decimal(&value2);
1268
if ((null_value= args[1]->null_value))
1270
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1271
val1, val2, prec_increment)) > 3)
1273
if (err == E_DEC_DIV_ZERO)
1274
signal_divide_by_null();
1278
return decimal_value;
1282
void Item_func_div::result_precision()
1284
uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
1285
(unsigned int)DECIMAL_MAX_PRECISION);
1286
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1287
if (result_type() == INT_RESULT)
1288
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1290
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1291
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
1292
max_length= my_decimal_precision_to_length(precision, decimals,
1297
void Item_func_div::fix_length_and_dec()
1299
prec_increment= current_thd->variables.div_precincrement;
1300
Item_num_op::fix_length_and_dec();
1301
switch(hybrid_type) {
1304
decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
1305
set_if_smaller(decimals, NOT_FIXED_DEC);
1306
max_length=args[0]->max_length - args[0]->decimals + decimals;
1307
uint32_t tmp=float_length(decimals);
1308
set_if_smaller(max_length,tmp);
1312
hybrid_type= DECIMAL_RESULT;
1315
case DECIMAL_RESULT:
1321
maybe_null= 1; // devision by zero
1326
/* Integer division */
1327
int64_t Item_func_int_div::val_int()
1330
int64_t value=args[0]->val_int();
1331
int64_t val2=args[1]->val_int();
1332
if ((null_value= (args[0]->null_value || args[1]->null_value)))
1336
signal_divide_by_null();
1339
return (unsigned_flag ?
1340
(uint64_t) value / (uint64_t) val2 :
1345
void Item_func_int_div::fix_length_and_dec()
1347
Item_result argtype= args[0]->result_type();
1348
/* use precision ony for the data type it is applicable for and valid */
1349
max_length=args[0]->max_length -
1350
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
1351
args[0]->decimals : 0);
1353
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
1357
int64_t Item_func_mod::int_op()
1360
int64_t value= args[0]->val_int();
1361
int64_t val2= args[1]->val_int();
1364
if ((null_value= args[0]->null_value || args[1]->null_value))
1365
return 0; /* purecov: inspected */
1368
signal_divide_by_null();
1372
if (args[0]->unsigned_flag)
1373
result= args[1]->unsigned_flag ?
1374
((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
1376
result= args[1]->unsigned_flag ?
1377
value % ((uint64_t) val2) : value % val2;
1382
double Item_func_mod::real_op()
1385
double value= args[0]->val_real();
1386
double val2= args[1]->val_real();
1387
if ((null_value= args[0]->null_value || args[1]->null_value))
1388
return 0.0; /* purecov: inspected */
1391
signal_divide_by_null();
1394
return fmod(value,val2);
1398
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
1400
my_decimal value1, *val1;
1401
my_decimal value2, *val2;
1403
val1= args[0]->val_decimal(&value1);
1404
if ((null_value= args[0]->null_value))
1406
val2= args[1]->val_decimal(&value2);
1407
if ((null_value= args[1]->null_value))
1409
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1411
case E_DEC_TRUNCATED:
1413
return decimal_value;
1414
case E_DEC_DIV_ZERO:
1415
signal_divide_by_null();
1423
void Item_func_mod::result_precision()
1425
decimals= cmax(args[0]->decimals, args[1]->decimals);
1426
max_length= cmax(args[0]->max_length, args[1]->max_length);
1430
void Item_func_mod::fix_length_and_dec()
1432
Item_num_op::fix_length_and_dec();
1434
unsigned_flag= args[0]->unsigned_flag;
1438
double Item_func_neg::real_op()
1440
double value= args[0]->val_real();
1441
null_value= args[0]->null_value;
1446
int64_t Item_func_neg::int_op()
1448
int64_t value= args[0]->val_int();
1449
null_value= args[0]->null_value;
1454
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
1456
my_decimal val, *value= args[0]->val_decimal(&val);
1457
if (!(null_value= args[0]->null_value))
1459
my_decimal2decimal(value, decimal_value);
1460
my_decimal_neg(decimal_value);
1461
return decimal_value;
1467
void Item_func_neg::fix_num_length_and_dec()
1469
decimals= args[0]->decimals;
1470
/* 1 add because sign can appear */
1471
max_length= args[0]->max_length + 1;
1475
void Item_func_neg::fix_length_and_dec()
1477
Item_func_num1::fix_length_and_dec();
1480
If this is in integer context keep the context as integer if possible
1481
(This is how multiplication and other integer functions works)
1482
Use val() to get value as arg_type doesn't mean that item is
1483
Item_int or Item_real due to existence of Item_param.
1485
if (hybrid_type == INT_RESULT && args[0]->const_item())
1487
int64_t val= args[0]->val_int();
1488
if ((uint64_t) val >= (uint64_t) INT64_MIN &&
1489
((uint64_t) val != (uint64_t) INT64_MIN ||
1490
args[0]->type() != INT_ITEM))
1493
Ensure that result is converted to DECIMAL, as int64_t can't hold
1496
hybrid_type= DECIMAL_RESULT;
1504
double Item_func_abs::real_op()
1506
double value= args[0]->val_real();
1507
null_value= args[0]->null_value;
1512
int64_t Item_func_abs::int_op()
1514
int64_t value= args[0]->val_int();
1515
if ((null_value= args[0]->null_value))
1517
return (value >= 0) || unsigned_flag ? value : -value;
1521
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
1523
my_decimal val, *value= args[0]->val_decimal(&val);
1524
if (!(null_value= args[0]->null_value))
1526
my_decimal2decimal(value, decimal_value);
1527
if (decimal_value->sign())
1528
my_decimal_neg(decimal_value);
1529
return decimal_value;
1535
void Item_func_abs::fix_length_and_dec()
1537
Item_func_num1::fix_length_and_dec();
1538
unsigned_flag= args[0]->unsigned_flag;
1542
/** Gateway to natural LOG function. */
1543
double Item_func_ln::val_real()
1546
double value= args[0]->val_real();
1547
if ((null_value= args[0]->null_value))
1551
signal_divide_by_null();
1558
Extended but so slower LOG function.
1560
We have to check if all values are > zero and first one is not one
1561
as these are the cases then result is not a number.
1563
double Item_func_log::val_real()
1566
double value= args[0]->val_real();
1567
if ((null_value= args[0]->null_value))
1571
signal_divide_by_null();
1576
double value2= args[1]->val_real();
1577
if ((null_value= args[1]->null_value))
1579
if (value2 <= 0.0 || value == 1.0)
1581
signal_divide_by_null();
1584
return log(value2) / log(value);
1589
double Item_func_log2::val_real()
1592
double value= args[0]->val_real();
1594
if ((null_value=args[0]->null_value))
1598
signal_divide_by_null();
1601
return log(value) / M_LN2;
1604
double Item_func_log10::val_real()
1607
double value= args[0]->val_real();
1608
if ((null_value= args[0]->null_value))
1612
signal_divide_by_null();
1615
return log10(value);
1618
double Item_func_exp::val_real()
1621
double value= args[0]->val_real();
1622
if ((null_value=args[0]->null_value))
1623
return 0.0; /* purecov: inspected */
1624
return fix_result(exp(value));
1627
double Item_func_sqrt::val_real()
1630
double value= args[0]->val_real();
1631
if ((null_value=(args[0]->null_value || value < 0)))
1632
return 0.0; /* purecov: inspected */
1636
double Item_func_pow::val_real()
1639
double value= args[0]->val_real();
1640
double val2= args[1]->val_real();
1641
if ((null_value=(args[0]->null_value || args[1]->null_value)))
1642
return 0.0; /* purecov: inspected */
1643
return fix_result(pow(value,val2));
1646
// Trigonometric functions
1648
double Item_func_acos::val_real()
1651
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1652
volatile double value= args[0]->val_real();
1653
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1658
double Item_func_asin::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_atan::val_real()
1671
double value= args[0]->val_real();
1672
if ((null_value=args[0]->null_value))
1676
double val2= args[1]->val_real();
1677
if ((null_value=args[1]->null_value))
1679
return fix_result(atan2(value,val2));
1684
double Item_func_cos::val_real()
1687
double value= args[0]->val_real();
1688
if ((null_value=args[0]->null_value))
1693
double Item_func_sin::val_real()
1696
double value= args[0]->val_real();
1697
if ((null_value=args[0]->null_value))
1702
double Item_func_tan::val_real()
1705
double value= args[0]->val_real();
1706
if ((null_value=args[0]->null_value))
1708
return fix_result(tan(value));
1712
// Shift-functions, same as << and >> in C/C++
1715
int64_t Item_func_shift_left::val_int()
1719
uint64_t res= ((uint64_t) args[0]->val_int() <<
1720
(shift=(uint) args[1]->val_int()));
1721
if (args[0]->null_value || args[1]->null_value)
1727
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
1730
int64_t Item_func_shift_right::val_int()
1734
uint64_t res= (uint64_t) args[0]->val_int() >>
1735
(shift=(uint) args[1]->val_int());
1736
if (args[0]->null_value || args[1]->null_value)
1742
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
1746
int64_t Item_func_bit_neg::val_int()
1749
uint64_t res= (uint64_t) args[0]->val_int();
1750
if ((null_value=args[0]->null_value))
1756
// Conversion functions
1758
void Item_func_integer::fix_length_and_dec()
1760
max_length=args[0]->max_length - args[0]->decimals+1;
1761
uint32_t tmp=float_length(decimals);
1762
set_if_smaller(max_length,tmp);
1766
void Item_func_int_val::fix_num_length_and_dec()
1768
max_length= args[0]->max_length - (args[0]->decimals ?
1769
args[0]->decimals + 1 :
1771
uint32_t tmp= float_length(decimals);
1772
set_if_smaller(max_length,tmp);
1777
void Item_func_int_val::find_num_type()
1779
switch(hybrid_type= args[0]->result_type())
1783
hybrid_type= REAL_RESULT;
1784
max_length= float_length(decimals);
1787
case DECIMAL_RESULT:
1789
-2 because in most high position can't be used any digit for int64_t
1790
and one position for increasing value during operation
1792
if ((args[0]->max_length - args[0]->decimals) >=
1793
(DECIMAL_LONGLONG_DIGITS - 2))
1795
hybrid_type= DECIMAL_RESULT;
1799
unsigned_flag= args[0]->unsigned_flag;
1800
hybrid_type= INT_RESULT;
1810
int64_t Item_func_ceiling::int_op()
1813
switch (args[0]->result_type()) {
1815
result= args[0]->val_int();
1816
null_value= args[0]->null_value;
1818
case DECIMAL_RESULT:
1820
my_decimal dec_buf, *dec;
1821
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1822
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1828
result= (int64_t)Item_func_ceiling::real_op();
1834
double Item_func_ceiling::real_op()
1837
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1840
volatile double value= args[0]->val_real();
1841
null_value= args[0]->null_value;
1846
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1848
my_decimal val, *value= args[0]->val_decimal(&val);
1849
if (!(null_value= (args[0]->null_value ||
1850
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1851
decimal_value) > 1)))
1852
return decimal_value;
1857
int64_t Item_func_floor::int_op()
1860
switch (args[0]->result_type()) {
1862
result= args[0]->val_int();
1863
null_value= args[0]->null_value;
1865
case DECIMAL_RESULT:
1867
my_decimal dec_buf, *dec;
1868
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1869
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1875
result= (int64_t)Item_func_floor::real_op();
1881
double Item_func_floor::real_op()
1884
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1887
volatile double value= args[0]->val_real();
1888
null_value= args[0]->null_value;
1889
return floor(value);
1893
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1895
my_decimal val, *value= args[0]->val_decimal(&val);
1896
if (!(null_value= (args[0]->null_value ||
1897
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1898
decimal_value) > 1)))
1899
return decimal_value;
1904
void Item_func_round::fix_length_and_dec()
1906
int decimals_to_set;
1910
unsigned_flag= args[0]->unsigned_flag;
1911
if (!args[1]->const_item())
1913
max_length= args[0]->max_length;
1914
decimals= args[0]->decimals;
1915
if (args[0]->result_type() == DECIMAL_RESULT)
1918
hybrid_type= DECIMAL_RESULT;
1921
hybrid_type= REAL_RESULT;
1925
val1= args[1]->val_int();
1926
val1_unsigned= args[1]->unsigned_flag;
1928
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1930
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1932
if (args[0]->decimals == NOT_FIXED_DEC)
1934
max_length= args[0]->max_length;
1935
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
1936
hybrid_type= REAL_RESULT;
1940
switch (args[0]->result_type()) {
1943
hybrid_type= REAL_RESULT;
1944
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
1945
max_length= float_length(decimals);
1948
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1950
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1951
max_length= args[0]->max_length + length_can_increase;
1952
/* Here we can keep INT_RESULT */
1953
hybrid_type= INT_RESULT;
1958
case DECIMAL_RESULT:
1960
hybrid_type= DECIMAL_RESULT;
1961
decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
1962
int decimals_delta= args[0]->decimals - decimals_to_set;
1963
int precision= args[0]->decimal_precision();
1964
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1966
precision-= decimals_delta - length_increase;
1967
decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
1968
max_length= my_decimal_precision_to_length(precision, decimals,
1973
assert(0); /* This result type isn't handled */
1977
double my_double_round(double value, int64_t dec, bool dec_unsigned,
1981
bool dec_negative= (dec < 0) && !dec_unsigned;
1982
uint64_t abs_dec= dec_negative ? -dec : dec;
1984
tmp2 is here to avoid return the value with 80 bit precision
1985
This will fix that the test round(0.1,1) = round(0.1,1) is true
1987
volatile double tmp2;
1989
tmp=(abs_dec < array_elements(log_10) ?
1990
log_10[abs_dec] : pow(10.0,(double) abs_dec));
1992
if (dec_negative && isinf(tmp))
1994
else if (!dec_negative && isinf(value * tmp))
1999
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
2001
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
2004
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
2009
double Item_func_round::real_op()
2011
double value= args[0]->val_real();
2013
if (!(null_value= args[0]->null_value || args[1]->null_value))
2014
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
2021
Rounds a given value to a power of 10 specified as the 'to' argument,
2022
avoiding overflows when the value is close to the uint64_t range boundary.
2025
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
2027
uint64_t tmp= value / to * to;
2028
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
2032
int64_t Item_func_round::int_op()
2034
int64_t value= args[0]->val_int();
2035
int64_t dec= args[1]->val_int();
2038
if ((null_value= args[0]->null_value || args[1]->null_value))
2040
if ((dec >= 0) || args[1]->unsigned_flag)
2041
return value; // integer have not digits after point
2046
if(abs_dec >= array_elements(log_10_int))
2049
tmp= log_10_int[abs_dec];
2052
value= (unsigned_flag) ?
2053
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
2055
value= (unsigned_flag || value >= 0) ?
2056
my_unsigned_round((uint64_t) value, tmp) :
2057
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
2062
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
2064
my_decimal val, *value= args[0]->val_decimal(&val);
2065
int64_t dec= args[1]->val_int();
2066
if (dec >= 0 || args[1]->unsigned_flag)
2067
dec= cmin(dec, (int64_t) decimals);
2068
else if (dec < INT_MIN)
2071
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2072
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2073
truncate, decimal_value) > 1)))
2075
decimal_value->frac= decimals;
2076
return decimal_value;
2082
void Item_func_rand::seed_random(Item *arg)
2085
TODO: do not do reinit 'rand' for every execute of PS/SP if
2086
args[0] is a constant.
2088
uint32_t tmp= (uint32_t) arg->val_int();
2089
randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
2090
(uint32_t) (tmp*0x10000001L));
2094
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
2096
if (Item_real_func::fix_fields(thd, ref))
2098
used_tables_cache|= RAND_TABLE_BIT;
2100
{ // Only use argument once in query
2102
No need to send a Rand log event if seed was given eg: RAND(seed),
2103
as it will be replicated in the query as such.
2105
if (!rand && !(rand= (struct rand_struct*)
2106
thd->alloc(sizeof(*rand))))
2109
if (args[0]->const_item())
2110
seed_random (args[0]);
2115
Save the seed only the first time RAND() is used in the query
2116
Once events are forwarded rather than recreated,
2117
the following can be skipped if inside the slave thread
2119
if (!thd->rand_used)
2122
thd->rand_saved_seed1= thd->rand.seed1;
2123
thd->rand_saved_seed2= thd->rand.seed2;
2130
void Item_func_rand::update_used_tables()
2132
Item_real_func::update_used_tables();
2133
used_tables_cache|= RAND_TABLE_BIT;
2137
double Item_func_rand::val_real()
2140
if (arg_count && !args[0]->const_item())
2141
seed_random (args[0]);
2142
return my_rnd(rand);
2145
int64_t Item_func_sign::val_int()
2148
double value= args[0]->val_real();
2149
null_value=args[0]->null_value;
2150
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
2154
double Item_func_units::val_real()
2157
double value= args[0]->val_real();
2158
if ((null_value=args[0]->null_value))
2160
return value*mul+add;
2164
void Item_func_min_max::fix_length_and_dec()
2167
bool datetime_found= false;
2171
cmp_type=args[0]->result_type();
2173
for (uint32_t i=0 ; i < arg_count ; i++)
2175
set_if_bigger(max_length, args[i]->max_length);
2176
set_if_bigger(decimals, args[i]->decimals);
2177
set_if_bigger(max_int_part, args[i]->decimal_int_part());
2178
if (args[i]->maybe_null)
2180
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
2181
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
2183
datetime_found= true;
2184
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
2185
datetime_item= args[i];
2188
if (cmp_type == STRING_RESULT)
2190
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2194
compare_as_dates= true;
2197
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
2198
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
2200
cached_field_type= agg_field_type(args, arg_count);
2205
Compare item arguments in the DATETIME context.
2209
value [out] found least/greatest DATE/DATETIME value
2212
Compare item arguments as DATETIME values and return the index of the
2213
least/greatest argument in the arguments array.
2214
The correct integer DATE/DATETIME value of the found argument is
2215
stored to the value pointer, if latter is provided.
2218
0 If one of arguments is NULL
2219
# index of the least/greatest argument
2222
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
2224
uint64_t min_max= 0;
2225
uint32_t min_max_idx= 0;
2227
for (uint32_t i=0; i < arg_count ; i++)
2229
Item **arg= args + i;
2231
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
2232
if ((null_value= args[i]->null_value))
2234
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
2243
if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
2250
String *Item_func_min_max::val_str(String *str)
2253
if (compare_as_dates)
2256
uint32_t min_max_idx= cmp_datetimes(NULL);
2259
str_res= args[min_max_idx]->val_str(str);
2260
str_res->set_charset(collation.collation);
2266
int64_t nr=val_int();
2269
str->set_int(nr, unsigned_flag, &my_charset_bin);
2272
case DECIMAL_RESULT:
2274
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
2277
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
2282
double nr= val_real();
2284
return 0; /* purecov: inspected */
2285
str->set_real(nr,decimals,&my_charset_bin);
2292
for (uint32_t i=0; i < arg_count ; i++)
2295
res=args[i]->val_str(str);
2299
res2= args[i]->val_str(res == str ? &tmp_value : str);
2302
int cmp= sortcmp(res,res2,collation.collation);
2303
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
2307
if ((null_value= args[i]->null_value))
2310
res->set_charset(collation.collation);
2315
// This case should never be chosen
2319
return 0; // Keep compiler happy
2323
double Item_func_min_max::val_real()
2327
if (compare_as_dates)
2330
(void)cmp_datetimes(&result);
2331
return (double)result;
2333
for (uint32_t i=0; i < arg_count ; i++)
2336
value= args[i]->val_real();
2339
double tmp= args[i]->val_real();
2340
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2343
if ((null_value= args[i]->null_value))
2350
int64_t Item_func_min_max::val_int()
2354
if (compare_as_dates)
2357
(void)cmp_datetimes(&result);
2358
return (int64_t)result;
2360
for (uint32_t i=0; i < arg_count ; i++)
2363
value=args[i]->val_int();
2366
int64_t tmp=args[i]->val_int();
2367
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
2370
if ((null_value= args[i]->null_value))
2377
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
2380
my_decimal tmp_buf, *tmp, *res= NULL;
2382
if (compare_as_dates)
2385
(void)cmp_datetimes(&value);
2386
uint64_t2decimal(value, dec);
2389
for (uint32_t i=0; i < arg_count ; i++)
2392
res= args[i]->val_decimal(dec);
2395
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
2396
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
2398
if (tmp == &tmp_buf)
2400
/* Move value out of tmp_buf as this will be reused on next loop */
2401
my_decimal2decimal(tmp, dec);
2408
if ((null_value= args[i]->null_value))
2418
int64_t Item_func_length::val_int()
2421
String *res=args[0]->val_str(&value);
2425
return 0; /* purecov: inspected */
2428
return (int64_t) res->length();
2432
int64_t Item_func_char_length::val_int()
2435
String *res=args[0]->val_str(&value);
2439
return 0; /* purecov: inspected */
2442
return (int64_t) res->numchars();
2446
int64_t Item_func_coercibility::val_int()
2450
return (int64_t) args[0]->collation.derivation;
2454
void Item_func_locate::fix_length_and_dec()
2456
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
2457
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2461
int64_t Item_func_locate::val_int()
2464
String *a=args[0]->val_str(&value1);
2465
String *b=args[1]->val_str(&value2);
2469
return 0; /* purecov: inspected */
2472
/* must be int64_t to avoid truncation */
2479
start0= start= args[2]->val_int() - 1;
2481
if ((start < 0) || (start > a->length()))
2484
/* start is now sufficiently valid to pass to charpos function */
2485
start= a->charpos((int) start);
2487
if (start + b->length() > a->length())
2491
if (!b->length()) // Found empty string at start
2494
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
2496
(uint) (a->length()-start),
2497
b->ptr(), b->length(),
2500
return (int64_t) match.mb_len + start0 + 1;
2504
void Item_func_locate::print(String *str, enum_query_type query_type)
2506
str->append(STRING_WITH_LEN("locate("));
2507
args[1]->print(str, query_type);
2509
args[0]->print(str, query_type);
2513
args[2]->print(str, query_type);
2519
int64_t Item_func_field::val_int()
2523
if (cmp_type == STRING_RESULT)
2526
if (!(field= args[0]->val_str(&value)))
2528
for (uint32_t i=1 ; i < arg_count ; i++)
2530
String *tmp_value=args[i]->val_str(&tmp);
2531
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
2532
return (int64_t) (i);
2535
else if (cmp_type == INT_RESULT)
2537
int64_t val= args[0]->val_int();
2538
if (args[0]->null_value)
2540
for (uint32_t i=1; i < arg_count ; i++)
2542
if (val == args[i]->val_int() && !args[i]->null_value)
2543
return (int64_t) (i);
2546
else if (cmp_type == DECIMAL_RESULT)
2548
my_decimal dec_arg_buf, *dec_arg,
2549
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
2550
if (args[0]->null_value)
2552
for (uint32_t i=1; i < arg_count; i++)
2554
dec_arg= args[i]->val_decimal(&dec_arg_buf);
2555
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
2556
return (int64_t) (i);
2561
double val= args[0]->val_real();
2562
if (args[0]->null_value)
2564
for (uint32_t i=1; i < arg_count ; i++)
2566
if (val == args[i]->val_real() && !args[i]->null_value)
2567
return (int64_t) (i);
2574
void Item_func_field::fix_length_and_dec()
2576
maybe_null=0; max_length=3;
2577
cmp_type= args[0]->result_type();
2578
for (uint32_t i=1; i < arg_count ; i++)
2579
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
2580
if (cmp_type == STRING_RESULT)
2581
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2585
int64_t Item_func_ascii::val_int()
2588
String *res=args[0]->val_str(&value);
2595
return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
2598
int64_t Item_func_ord::val_int()
2601
String *res=args[0]->val_str(&value);
2608
if (!res->length()) return 0;
2610
if (use_mb(res->charset()))
2612
register const char *str=res->ptr();
2613
register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
2615
return (int64_t)((unsigned char) *str);
2617
n=(n<<8)|(uint32_t)((unsigned char) *str++);
2621
return (int64_t) ((unsigned char) (*res)[0]);
2624
/* Search after a string in a string of strings separated by ',' */
2625
/* Returns number of found type >= 1 or 0 if not found */
2626
/* This optimizes searching in enums to bit testing! */
2628
void Item_func_find_in_set::fix_length_and_dec()
2631
max_length=3; // 1-999
2632
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
2635
static const char separator=',';
2637
int64_t Item_func_find_in_set::val_int()
2642
uint64_t tmp=(uint64_t) args[1]->val_int();
2643
if (!(null_value=args[1]->null_value || args[0]->null_value))
2651
String *find=args[0]->val_str(&value);
2652
String *buffer=args[1]->val_str(&value2);
2653
if (!find || !buffer)
2656
return 0; /* purecov: inspected */
2661
if ((diff=buffer->length() - find->length()) >= 0)
2664
const CHARSET_INFO * const cs= cmp_collation.collation;
2665
const char *str_begin= buffer->ptr();
2666
const char *str_end= buffer->ptr();
2667
const char *real_end= str_end+buffer->length();
2668
const unsigned char *find_str= (const unsigned char *) find->ptr();
2669
uint32_t find_str_len= find->length();
2674
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end,
2675
(unsigned char*) real_end)) > 0)
2677
const char *substr_end= str_end + symbol_len;
2678
bool is_last_item= (substr_end == real_end);
2679
bool is_separator= (wc == (my_wc_t) separator);
2680
if (is_separator || is_last_item)
2683
if (is_last_item && !is_separator)
2684
str_end= substr_end;
2685
if (!my_strnncoll(cs, (const unsigned char *) str_begin,
2686
str_end - str_begin,
2687
find_str, find_str_len))
2688
return (int64_t) position;
2690
str_begin= substr_end;
2692
str_end= substr_end;
2694
else if (str_end - str_begin == 0 &&
2695
find_str_len == 0 &&
2696
wc == (my_wc_t) separator)
2697
return (int64_t) ++position;
2705
int64_t Item_func_bit_count::val_int()
2708
uint64_t value= (uint64_t) args[0]->val_int();
2709
if ((null_value= args[0]->null_value))
2710
return 0; /* purecov: inspected */
2711
return (int64_t) my_count_bits(value);
2718
pthread_mutex_t LOCK_user_locks;
2719
static HASH hash_user_locks;
2721
class User_level_lock
2729
pthread_cond_t cond;
2730
my_thread_id thread_id;
2731
void set_thread(THD *thd) { thread_id= thd->thread_id; }
2733
User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id)
2734
:key_length(length),count(1),locked(1), thread_id(id)
2736
key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
2737
pthread_cond_init(&cond,NULL);
2740
if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
2751
hash_delete(&hash_user_locks,(unsigned char*) this);
2754
pthread_cond_destroy(&cond);
2756
inline bool initialized() { return key != 0; }
2757
friend void item_user_lock_release(User_level_lock *ull);
2758
friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2762
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2763
bool not_used __attribute__((unused)))
2765
*length= ull->key_length;
2770
static bool item_user_lock_inited= 0;
2772
void item_user_lock_init(void)
2774
pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
2775
hash_init(&hash_user_locks, system_charset_info,
2776
16,0,0,(hash_get_key) ull_get_key,NULL,0);
2777
item_user_lock_inited= 1;
2780
void item_user_lock_free(void)
2782
if (item_user_lock_inited)
2784
item_user_lock_inited= 0;
2785
hash_free(&hash_user_locks);
2786
pthread_mutex_destroy(&LOCK_user_locks);
2790
void item_user_lock_release(User_level_lock *ull)
2795
pthread_cond_signal(&ull->cond);
2801
Wait until we are at or past the given position in the master binlog
2805
int64_t Item_master_pos_wait::val_int()
2808
THD* thd = current_thd;
2809
String *log_name = args[0]->val_str(&value);
2813
if (thd->slave_thread || !log_name || !log_name->length())
2818
int64_t pos = (ulong)args[1]->val_int();
2819
int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
2820
if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
2829
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
2836
int64_t Item_func_last_insert_id::val_int()
2838
THD *thd= current_thd;
2842
int64_t value= args[0]->val_int();
2843
null_value= args[0]->null_value;
2845
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
2846
documented in the manual. We don't want to touch
2847
first_successful_insert_id_in_cur_stmt because it would make
2848
LAST_INSERT_ID(X) take precedence over an generated auto_increment
2851
thd->arg_of_last_insert_id_function= true;
2852
thd->first_successful_insert_id_in_prev_stmt= value;
2855
return thd->read_first_successful_insert_id_in_prev_stmt();
2859
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
2861
return Item_int_func::fix_fields(thd, ref);
2865
/* This function is just used to test speed of different functions */
2867
int64_t Item_func_benchmark::val_int()
2870
char buff[MAX_FIELD_WIDTH];
2871
String tmp(buff,sizeof(buff), &my_charset_bin);
2872
my_decimal tmp_decimal;
2873
THD *thd=current_thd;
2874
uint64_t loop_count;
2876
loop_count= (uint64_t) args[0]->val_int();
2878
if (args[0]->null_value ||
2879
(!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
2881
if (!args[0]->null_value)
2884
llstr(((int64_t) loop_count), buff);
2885
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2886
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
2887
"count", buff, "benchmark");
2895
for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
2897
switch (args[1]->result_type()) {
2899
(void) args[1]->val_real();
2902
(void) args[1]->val_int();
2905
(void) args[1]->val_str(&tmp);
2907
case DECIMAL_RESULT:
2908
(void) args[1]->val_decimal(&tmp_decimal);
2912
// This case should never be chosen
2921
void Item_func_benchmark::print(String *str, enum_query_type query_type)
2923
str->append(STRING_WITH_LEN("benchmark("));
2924
args[0]->print(str, query_type);
2926
args[1]->print(str, query_type);
2930
#define extra_size sizeof(double)
2932
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
2933
bool create_if_not_exists)
2935
user_var_entry *entry;
2937
if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
2939
create_if_not_exists)
2941
uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
2942
if (!hash_inited(hash))
2944
if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
2946
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
2948
entry->name.length=name.length;
2951
entry->update_query_id=0;
2952
entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
2953
entry->unsigned_flag= 0;
2955
If we are here, we were called from a SET or a query which sets a
2956
variable. Imagine it is this:
2957
INSERT INTO t SELECT @a:=10, @a:=@a+1.
2958
Then when we have a Item_func_get_user_var (because of the @a+1) so we
2959
think we have to write the value of @a to the binlog. But before that,
2960
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
2961
the variable as "already logged" (line below) so that it won't be logged
2962
by Item_func_get_user_var (because that's not necessary).
2964
entry->used_query_id=current_thd->query_id;
2965
entry->type=STRING_RESULT;
2966
memcpy(entry->name.str, name.str, name.length+1);
2967
if (my_hash_insert(hash,(unsigned char*) entry))
2969
free((char*) entry);
2977
When a user variable is updated (in a SET command or a query like
2981
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
2984
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
2985
if (Item_func::fix_fields(thd, ref) ||
2986
!(entry= get_variable(&thd->user_vars, name, 1)))
2989
Remember the last query which updated it, this way a query can later know
2990
if this variable is a constant item in the query (it is if update_query_id
2991
is different from query_id).
2993
entry->update_query_id= thd->query_id;
2995
As it is wrong and confusing to associate any
2996
character set with NULL, @a should be latin2
2997
after this query sequence:
2999
SET @a=_latin2'string';
3002
I.e. the second query should not change the charset
3003
to the current default value, but should keep the
3004
original value assigned during the first query.
3005
In order to do it, we don't copy charset
3006
from the argument if the argument is NULL
3007
and the variable has previously been initialized.
3009
null_item= (args[0]->type() == NULL_ITEM);
3010
if (!entry->collation.collation || !null_item)
3011
entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3012
collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
3013
cached_result_type= args[0]->result_type();
3019
Item_func_set_user_var::fix_length_and_dec()
3021
maybe_null=args[0]->maybe_null;
3022
max_length=args[0]->max_length;
3023
decimals=args[0]->decimals;
3024
collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3029
Mark field in read_map
3032
This is used by filesort to register used fields in a a temporary
3033
column read set or to register used fields in a view
3036
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
3040
Table *table= (Table *) arg;
3041
if (result_field->table == table || !table)
3042
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
3043
if (result_field->vcol_info && result_field->vcol_info->expr_item)
3044
return result_field->vcol_info->
3045
expr_item->walk(&Item::register_field_in_read_map, 1, arg);
3052
Mark field in bitmap supplied as *arg
3056
bool Item_func_set_user_var::register_field_in_bitmap(unsigned char *arg)
3058
MY_BITMAP *bitmap = (MY_BITMAP *) arg;
3062
bitmap_set_bit(bitmap, result_field->field_index);
3069
Set value to user variable.
3071
@param entry pointer to structure representing variable
3072
@param set_null should we set NULL value ?
3073
@param ptr pointer to buffer with new value
3074
@param length length of new value
3075
@param type type of new value
3076
@param cs charset info for new value
3077
@param dv derivation for new value
3078
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3080
@note Sets error and fatal error if allocation fails.
3089
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
3090
Item_result type, const CHARSET_INFO * const cs, Derivation dv,
3095
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3096
if (entry->value && entry->value != pos)
3103
if (type == STRING_RESULT)
3104
length++; // Store strings with end \0
3105
if (length <= extra_size)
3107
/* Save value in value struct */
3108
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3109
if (entry->value != pos)
3118
/* Allocate variable */
3119
if (entry->length != length)
3121
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
3122
if (entry->value == pos)
3124
entry->value= (char*) my_realloc(entry->value, length,
3125
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
3131
if (type == STRING_RESULT)
3133
length--; // Fix length change above
3134
entry->value[length]= 0; // Store end \0
3136
memcpy(entry->value,ptr,length);
3137
if (type == DECIMAL_RESULT)
3138
((my_decimal*)entry->value)->fix_buffer_pointer();
3139
entry->length= length;
3140
entry->collation.set(cs, dv);
3141
entry->unsigned_flag= unsigned_arg;
3149
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
3150
Item_result res_type,
3151
const CHARSET_INFO * const cs, Derivation dv,
3155
If we set a variable explicitely to NULL then keep the old
3156
result type of the variable
3158
if ((null_value= args[0]->null_value) && null_item)
3159
res_type= entry->type; // Don't change type of item
3160
if (::update_hash(entry, (null_value= args[0]->null_value),
3161
ptr, length, res_type, cs, dv, unsigned_arg))
3170
/** Get the value of a variable as a double. */
3172
double user_var_entry::val_real(bool *null_value)
3174
if ((*null_value= (value == 0)))
3179
return *(double*) value;
3181
return (double) *(int64_t*) value;
3182
case DECIMAL_RESULT:
3185
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
3189
return my_atof(value); // This is null terminated
3191
assert(1); // Impossible
3194
return 0.0; // Impossible
3198
/** Get the value of a variable as an integer. */
3200
int64_t user_var_entry::val_int(bool *null_value) const
3202
if ((*null_value= (value == 0)))
3207
return (int64_t) *(double*) value;
3209
return *(int64_t*) value;
3210
case DECIMAL_RESULT:
3213
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
3219
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
3222
assert(1); // Impossible
3225
return 0L; // Impossible
3229
/** Get the value of a variable as a string. */
3231
String *user_var_entry::val_str(bool *null_value, String *str,
3234
if ((*null_value= (value == 0)))
3239
str->set_real(*(double*) value, decimals, &my_charset_bin);
3243
str->set(*(int64_t*) value, &my_charset_bin);
3245
str->set(*(uint64_t*) value, &my_charset_bin);
3247
case DECIMAL_RESULT:
3248
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
3251
if (str->copy(value, length, collation.collation))
3252
str= 0; // EOM error
3254
assert(1); // Impossible
3260
/** Get the value of a variable as a decimal. */
3262
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
3264
if ((*null_value= (value == 0)))
3269
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
3272
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
3274
case DECIMAL_RESULT:
3275
val= (my_decimal *)value;
3278
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
3281
assert(1); // Impossible
3288
This functions is invoked on SET \@variable or
3289
\@variable:= expression.
3291
Evaluate (and check expression), store results.
3294
For now it always return OK. All problem with value evaluating
3295
will be caught by thd->is_error() check in sql_set_variables().
3302
Item_func_set_user_var::check(bool use_result_field)
3304
if (use_result_field && !result_field)
3305
use_result_field= false;
3307
switch (cached_result_type) {
3310
save_result.vreal= use_result_field ? result_field->val_real() :
3311
args[0]->val_real();
3316
save_result.vint= use_result_field ? result_field->val_int() :
3318
unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
3319
args[0]->unsigned_flag;
3324
save_result.vstr= use_result_field ? result_field->val_str(&value) :
3325
args[0]->val_str(&value);
3328
case DECIMAL_RESULT:
3330
save_result.vdec= use_result_field ?
3331
result_field->val_decimal(&decimal_buff) :
3332
args[0]->val_decimal(&decimal_buff);
3337
// This case should never be chosen
3346
This functions is invoked on
3347
SET \@variable or \@variable:= expression.
3350
We have to store the expression as such in the variable, independent of
3351
the value method used by the user
3361
Item_func_set_user_var::update()
3365
switch (cached_result_type) {
3368
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
3369
REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
3374
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
3375
INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
3381
if (!save_result.vstr) // Null value
3382
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
3383
DERIVATION_IMPLICIT, 0);
3385
res= update_hash((void*) save_result.vstr->ptr(),
3386
save_result.vstr->length(), STRING_RESULT,
3387
save_result.vstr->charset(),
3388
DERIVATION_IMPLICIT, 0);
3391
case DECIMAL_RESULT:
3393
if (!save_result.vdec) // Null value
3394
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
3395
DERIVATION_IMPLICIT, 0);
3397
res= update_hash((void*) save_result.vdec,
3398
sizeof(my_decimal), DECIMAL_RESULT,
3399
&my_charset_bin, DERIVATION_IMPLICIT, 0);
3404
// This case should never be chosen
3412
double Item_func_set_user_var::val_real()
3416
update(); // Store expression
3417
return entry->val_real(&null_value);
3420
int64_t Item_func_set_user_var::val_int()
3424
update(); // Store expression
3425
return entry->val_int(&null_value);
3428
String *Item_func_set_user_var::val_str(String *str)
3432
update(); // Store expression
3433
return entry->val_str(&null_value, str, decimals);
3437
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
3441
update(); // Store expression
3442
return entry->val_decimal(&null_value, val);
3446
double Item_func_set_user_var::val_result()
3450
update(); // Store expression
3451
return entry->val_real(&null_value);
3454
int64_t Item_func_set_user_var::val_int_result()
3458
update(); // Store expression
3459
return entry->val_int(&null_value);
3462
String *Item_func_set_user_var::str_result(String *str)
3466
update(); // Store expression
3467
return entry->val_str(&null_value, str, decimals);
3471
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
3475
update(); // Store expression
3476
return entry->val_decimal(&null_value, val);
3480
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
3482
str->append(STRING_WITH_LEN("(@"));
3483
str->append(name.str, name.length);
3484
str->append(STRING_WITH_LEN(":="));
3485
args[0]->print(str, query_type);
3490
void Item_func_set_user_var::print_as_stmt(String *str,
3491
enum_query_type query_type)
3493
str->append(STRING_WITH_LEN("set @"));
3494
str->append(name.str, name.length);
3495
str->append(STRING_WITH_LEN(":="));
3496
args[0]->print(str, query_type);
3500
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
3506
return protocol->store(result_field);
3508
return Item::send(protocol, str_arg);
3511
void Item_func_set_user_var::make_field(Send_field *tmp_field)
3515
result_field->make_field(tmp_field);
3516
assert(tmp_field->table_name != 0);
3518
tmp_field->col_name=Item::name; // Use user supplied name
3521
Item::make_field(tmp_field);
3526
Save the value of a user variable into a field
3530
field target field to save the value to
3531
no_conversion flag indicating whether conversions are allowed
3534
Save the function value into a field and update the user variable
3535
accordingly. If a result field is defined and the target field doesn't
3536
coincide with it then the value from the result field will be used as
3537
the new value of the user variable.
3539
The reason to have this method rather than simply using the result
3540
field in the val_xxx() methods is that the value from the result field
3541
not always can be used when the result field is defined.
3542
Let's consider the following cases:
3543
1) when filling a tmp table the result field is defined but the value of it
3544
is undefined because it has to be produced yet. Thus we can't use it.
3545
2) on execution of an INSERT ... SELECT statement the save_in_field()
3546
function will be called to fill the data in the new record. If the SELECT
3547
part uses a tmp table then the result field is defined and should be
3548
used in order to get the correct result.
3550
The difference between the SET_USER_VAR function and regular functions
3551
like CONCAT is that the Item_func objects for the regular functions are
3552
replaced by Item_field objects after the values of these functions have
3553
been stored in a tmp table. Yet an object of the Item_field class cannot
3554
be used to update a user variable.
3555
Due to this we have to handle the result field in a special way here and
3556
in the Item_func_set_user_var::send() function.
3563
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
3564
bool can_use_result_field)
3566
bool use_result_field= (!can_use_result_field ? 0 :
3567
(result_field && result_field != field));
3570
/* Update the value of the user variable */
3571
check(use_result_field);
3574
if (result_type() == STRING_RESULT ||
3575
(result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
3578
const CHARSET_INFO * const cs= collation.collation;
3579
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
3580
str_value.set_quick(buff, sizeof(buff), cs);
3581
result= entry->val_str(&null_value, &str_value, decimals);
3585
str_value.set_quick(0, 0, cs);
3586
return set_field_to_null_with_conversions(field, no_conversions);
3589
/* NOTE: If null_value == false, "result" must be not NULL. */
3591
field->set_notnull();
3592
error=field->store(result->ptr(),result->length(),cs);
3593
str_value.set_quick(0, 0, cs);
3595
else if (result_type() == REAL_RESULT)
3597
double nr= entry->val_real(&null_value);
3599
return set_field_to_null(field);
3600
field->set_notnull();
3601
error=field->store(nr);
3603
else if (result_type() == DECIMAL_RESULT)
3605
my_decimal decimal_value;
3606
my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
3608
return set_field_to_null(field);
3609
field->set_notnull();
3610
error=field->store_decimal(val);
3614
int64_t nr= entry->val_int(&null_value);
3616
return set_field_to_null_with_conversions(field, no_conversions);
3617
field->set_notnull();
3618
error=field->store(nr, unsigned_flag);
3625
Item_func_get_user_var::val_str(String *str)
3629
return((String*) 0); // No such variable
3630
return(var_entry->val_str(&null_value, str, decimals));
3634
double Item_func_get_user_var::val_real()
3638
return 0.0; // No such variable
3639
return (var_entry->val_real(&null_value));
3643
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
3648
return var_entry->val_decimal(&null_value, dec);
3652
int64_t Item_func_get_user_var::val_int()
3656
return 0L; // No such variable
3657
return (var_entry->val_int(&null_value));
3662
Get variable by name and, if necessary, put the record of variable
3663
use into the binary log.
3665
When a user variable is invoked from an update query (INSERT, UPDATE etc),
3666
stores this variable and its value in thd->user_var_events, so that it can be
3667
written to the binlog (will be written just before the query is written, see
3670
@param thd Current thread
3671
@param name Variable name
3672
@param[out] out_entry variable structure or NULL. The pointer is set
3673
regardless of whether function succeeded or not.
3678
1 Failed to put appropriate record into binary log
3682
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
3683
LEX_STRING &name, user_var_entry **out_entry)
3685
BINLOG_USER_VAR_EVENT *user_var_event;
3686
user_var_entry *var_entry;
3687
var_entry= get_variable(&thd->user_vars, name, 0);
3690
Any reference to user-defined variable which is done from stored
3691
function or trigger affects their execution and the execution of the
3692
calling statement. We must log all such variables even if they are
3693
not involved in table-updating statements.
3695
if (!(opt_bin_log && is_update_query(sql_command)))
3697
*out_entry= var_entry;
3704
If the variable does not exist, it's NULL, but we want to create it so
3705
that it gets into the binlog (if it didn't, the slave could be
3706
influenced by a variable of the same name previously set by another
3708
We create it like if it had been explicitly set with SET before.
3709
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
3710
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
3711
in dispatch_command()). Instead of building a one-element list to pass to
3712
sql_set_variables(), we could instead manually call check() and update();
3713
this would save memory and time; but calling sql_set_variables() makes
3714
one unique place to maintain (sql_set_variables()).
3716
Manipulation with lex is necessary since free_underlaid_joins
3717
is going to release memory belonging to the main query.
3720
List<set_var_base> tmp_var_list;
3721
LEX *sav_lex= thd->lex, lex_tmp;
3724
tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
3726
/* Create the variable */
3727
if (sql_set_variables(thd, &tmp_var_list))
3733
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
3736
else if (var_entry->used_query_id == thd->query_id ||
3737
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
3740
If this variable was already stored in user_var_events by this query
3741
(because it's used in more than one place in the query), don't store
3744
*out_entry= var_entry;
3750
First we need to store value of var_entry, when the next situation
3753
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
3754
We have to write to binlog value @a= 1.
3756
We allocate the user_var_event on user_var_events_alloc pool, not on
3757
the this-statement-execution pool because in SPs user_var_event objects
3758
may need to be valid after current [SP] statement execution pool is
3761
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
3762
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
3763
alloc_root(thd->user_var_events_alloc, size)))
3766
user_var_event->value= (char*) user_var_event +
3767
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
3768
user_var_event->user_var_event= var_entry;
3769
user_var_event->type= var_entry->type;
3770
user_var_event->charset_number= var_entry->collation.collation->number;
3771
if (!var_entry->value)
3774
user_var_event->length= 0;
3775
user_var_event->value= 0;
3779
user_var_event->length= var_entry->length;
3780
memcpy(user_var_event->value, var_entry->value,
3783
/* Mark that this variable has been used by this query */
3784
var_entry->used_query_id= thd->query_id;
3785
if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
3788
*out_entry= var_entry;
3792
*out_entry= var_entry;
3796
void Item_func_get_user_var::fix_length_and_dec()
3798
THD *thd=current_thd;
3801
decimals=NOT_FIXED_DEC;
3802
max_length=MAX_BLOB_WIDTH;
3804
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
3807
If the variable didn't exist it has been created as a STRING-type.
3808
'var_entry' is NULL only if there occured an error during the call to
3809
get_var_with_binlog.
3813
m_cached_result_type= var_entry->type;
3814
unsigned_flag= var_entry->unsigned_flag;
3815
max_length= var_entry->length;
3817
collation.set(var_entry->collation);
3818
switch(m_cached_result_type) {
3820
max_length= DBL_DIG + 8;
3823
max_length= MAX_BIGINT_WIDTH;
3827
max_length= MAX_BLOB_WIDTH;
3829
case DECIMAL_RESULT:
3830
max_length= DECIMAL_MAX_STR_LENGTH;
3831
decimals= DECIMAL_MAX_SCALE;
3833
case ROW_RESULT: // Keep compiler happy
3841
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
3843
m_cached_result_type= STRING_RESULT;
3844
max_length= MAX_BLOB_WIDTH;
3849
bool Item_func_get_user_var::const_item() const
3851
return (!var_entry || current_thd->query_id != var_entry->update_query_id);
3855
enum Item_result Item_func_get_user_var::result_type() const
3857
return m_cached_result_type;
3861
void Item_func_get_user_var::print(String *str,
3862
enum_query_type query_type __attribute__((unused)))
3864
str->append(STRING_WITH_LEN("(@"));
3865
str->append(name.str,name.length);
3870
bool Item_func_get_user_var::eq(const Item *item,
3871
bool binary_cmp __attribute__((unused))) const
3873
/* Assume we don't have rtti */
3875
return 1; // Same item is same.
3876
/* Check if other type is also a get_user_var() object */
3877
if (item->type() != FUNC_ITEM ||
3878
((Item_func*) item)->functype() != functype())
3880
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
3881
return (name.length == other->name.length &&
3882
!memcmp(name.str, other->name.str, name.length));
3886
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
3889
if (Item::fix_fields(thd, ref) ||
3890
!(entry= get_variable(&thd->user_vars, name, 1)))
3892
entry->type= STRING_RESULT;
3894
Let us set the same collation which is used for loading
3895
of fields in LOAD DATA INFILE.
3896
(Since Item_user_var_as_out_param is used only there).
3898
entry->collation.set(thd->variables.collation_database);
3899
entry->update_query_id= thd->query_id;
3904
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
3906
::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
3907
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
3911
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
3912
const CHARSET_INFO * const cs)
3914
::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
3915
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
3919
double Item_user_var_as_out_param::val_real()
3926
int64_t Item_user_var_as_out_param::val_int()
3933
String* Item_user_var_as_out_param::val_str(String *str __attribute__((unused)))
3940
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((unused)))
3947
void Item_user_var_as_out_param::print(String *str,
3948
enum_query_type query_type __attribute__((unused)))
3951
str->append(name.str,name.length);
3955
Item_func_get_system_var::
3956
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
3957
LEX_STRING *component_arg, const char *name_arg,
3958
size_t name_len_arg)
3959
:var(var_arg), var_type(var_type_arg), component(*component_arg)
3961
/* set_name() will allocate the name */
3962
set_name(name_arg, name_len_arg, system_charset_info);
3967
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
3972
Evaluate the system variable and substitute the result (a basic constant)
3973
instead of this item. If the variable can not be evaluated,
3974
the error is reported in sys_var::item().
3976
if (!(item= var->item(thd, var_type, &component)))
3977
return(1); // Impossible
3978
item->set_name(name, 0, system_charset_info); // don't allocate a new name
3979
thd->change_item_tree(ref, item);
3985
bool Item_func_get_system_var::is_written_to_binlog()
3987
return var->is_written_to_binlog(var_type);
3990
int64_t Item_func_bit_xor::val_int()
3993
uint64_t arg1= (uint64_t) args[0]->val_int();
3994
uint64_t arg2= (uint64_t) args[1]->val_int();
3995
if ((null_value= (args[0]->null_value || args[1]->null_value)))
3997
return (int64_t) (arg1 ^ arg2);
4001
/***************************************************************************
4003
****************************************************************************/
4006
Return value of an system variable base[.name] as a constant item.
4008
@param thd Thread handler
4009
@param var_type global / session
4010
@param name Name of base or system variable
4011
@param component Component.
4014
If component.str = 0 then the variable name is in 'name'
4022
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
4023
LEX_STRING component)
4026
LEX_STRING *base_name, *component_name;
4030
base_name= &component;
4031
component_name= &name;
4036
component_name= &component; // Empty string
4039
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
4043
if (!var->is_struct())
4045
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
4050
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
4052
return new Item_func_get_system_var(var, var_type, component_name,
4058
Check a user level lock.
4060
Sets null_value=true on error.
4065
0 Already taken, or error
4068
int64_t Item_func_is_free_lock::val_int()
4071
String *res=args[0]->val_str(&value);
4072
User_level_lock *ull;
4075
if (!res || !res->length())
4081
pthread_mutex_lock(&LOCK_user_locks);
4082
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
4083
(size_t) res->length());
4084
pthread_mutex_unlock(&LOCK_user_locks);
4085
if (!ull || !ull->locked)
4090
int64_t Item_func_is_used_lock::val_int()
4093
String *res=args[0]->val_str(&value);
4094
User_level_lock *ull;
4097
if (!res || !res->length())
4100
pthread_mutex_lock(&LOCK_user_locks);
4101
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
4102
(size_t) res->length());
4103
pthread_mutex_unlock(&LOCK_user_locks);
4104
if (!ull || !ull->locked)
4108
return ull->thread_id;
4112
int64_t Item_func_row_count::val_int()
4115
THD *thd= current_thd;
4117
return thd->row_count_func;
4120
int64_t Item_func_found_rows::val_int()
4123
THD *thd= current_thd;
4125
return thd->found_rows();