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::fix_num_length_and_dec()
53
uint32_t fl_length= 0;
55
for (uint32_t i=0 ; i < arg_count ; i++)
57
set_if_bigger(decimals,args[i]->decimals);
58
set_if_bigger(fl_length, args[i]->max_length);
60
max_length=float_length(decimals);
61
if (fl_length > max_length)
63
decimals= NOT_FIXED_DEC;
64
max_length= float_length(NOT_FIXED_DEC);
69
Set max_length/decimals of function if function is fixed point and
70
result length/precision depends on argument ones.
73
void Item_func::count_decimal_length()
78
for (uint32_t i=0 ; i < arg_count ; i++)
80
set_if_bigger(decimals, args[i]->decimals);
81
set_if_bigger(max_int_part, args[i]->decimal_int_part());
82
set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
84
int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
85
max_length= my_decimal_precision_to_length(precision, decimals,
91
Set max_length of if it is maximum length of its arguments.
94
void Item_func::count_only_length()
98
for (uint32_t i=0 ; i < arg_count ; i++)
100
set_if_bigger(max_length, args[i]->max_length);
101
set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
107
Set max_length/decimals of function if function is floating point and
108
result length/precision depends on argument ones.
111
void Item_func::count_real_length()
116
for (uint32_t i=0 ; i < arg_count ; i++)
118
if (decimals != NOT_FIXED_DEC)
120
set_if_bigger(decimals, args[i]->decimals);
121
set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
123
set_if_bigger(max_length, args[i]->max_length);
125
if (decimals != NOT_FIXED_DEC)
129
if (length < max_length) // If previous operation gave overflow
130
max_length= UINT32_MAX;
138
void Item_func::signal_divide_by_null()
140
THD *thd= current_thd;
141
push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
146
Item *Item_func::get_tmp_table_item(THD *thd)
148
if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
149
return new Item_field(result_field);
150
return copy_or_same(thd);
155
void Item_func_connection_id::fix_length_and_dec()
157
Item_int_func::fix_length_and_dec();
162
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
164
if (Item_int_func::fix_fields(thd, ref))
166
thd->thread_specific_used= true;
167
value= thd->variables.pseudo_thread_id;
173
Set result type for a numeric function of one argument
174
(can be also used by a numeric function of many arguments, if the result
175
type depends only on the first argument)
178
void Item_func_num1::find_num_type()
180
switch (hybrid_type= args[0]->result_type()) {
182
unsigned_flag= args[0]->unsigned_flag;
186
hybrid_type= REAL_RESULT;
187
max_length= float_length(decimals);
198
void Item_func_num1::fix_num_length_and_dec()
200
decimals= args[0]->decimals;
201
max_length= args[0]->max_length;
205
void Item_func_signed::print(String *str, enum_query_type query_type)
207
str->append(STRING_WITH_LEN("cast("));
208
args[0]->print(str, query_type);
209
str->append(STRING_WITH_LEN(" as signed)"));
213
int64_t Item_func_signed::val_int_from_str(int *error)
215
char buff[MAX_FIELD_WIDTH], *end, *start;
217
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
221
For a string result, we must first get the string and then convert it
225
if (!(res= args[0]->val_str(&tmp)))
232
start= (char *)res->ptr();
233
length= res->length();
236
value= my_strtoll10(start, &end, error);
237
if (*error > 0 || end != start+ length)
240
String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
241
err_tmp.copy(start, length, system_charset_info);
242
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
243
ER_TRUNCATED_WRONG_VALUE,
244
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
251
int64_t Item_func_signed::val_int()
256
if (args[0]->cast_to_int_type() != STRING_RESULT ||
257
args[0]->result_as_int64_t())
259
value= args[0]->val_int();
260
null_value= args[0]->null_value;
264
value= val_int_from_str(&error);
265
if (value < 0 && error == 0)
267
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
268
"Cast to signed converted positive out-of-range integer to "
269
"it's negative complement");
275
void Item_func_unsigned::print(String *str, enum_query_type query_type)
277
str->append(STRING_WITH_LEN("cast("));
278
args[0]->print(str, query_type);
279
str->append(STRING_WITH_LEN(" as unsigned)"));
284
int64_t Item_func_unsigned::val_int()
289
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
291
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
292
if (!(null_value= args[0]->null_value))
293
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
298
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
299
args[0]->result_as_int64_t())
301
value= args[0]->val_int();
302
null_value= args[0]->null_value;
306
value= val_int_from_str(&error);
308
push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
309
"Cast to unsigned converted negative integer to it's "
310
"positive complement");
315
String *Item_decimal_typecast::val_str(String *str)
317
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
320
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
325
double Item_decimal_typecast::val_real()
327
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
331
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
336
int64_t Item_decimal_typecast::val_int()
338
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
342
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
347
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
349
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
353
if ((null_value= args[0]->null_value))
355
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
361
my_decimal_set_zero(dec);
365
precision= my_decimal_length_to_precision(max_length,
366
decimals, unsigned_flag);
367
if (precision - decimals < (uint) my_decimal_intg(dec))
369
max_my_decimal(dec, precision, decimals);
376
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
377
ER_WARN_DATA_OUT_OF_RANGE,
378
ER(ER_WARN_DATA_OUT_OF_RANGE),
384
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
386
char len_buf[20*3 + 1];
389
uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
391
str->append(STRING_WITH_LEN("cast("));
392
args[0]->print(str, query_type);
393
str->append(STRING_WITH_LEN(" as decimal("));
395
end=int10_to_str(precision, len_buf,10);
396
str->append(len_buf, (uint32_t) (end - len_buf));
400
end=int10_to_str(decimals, len_buf,10);
401
str->append(len_buf, (uint32_t) (end - len_buf));
408
double Item_func_plus::real_op()
410
double value= args[0]->val_real() + args[1]->val_real();
411
if ((null_value=args[0]->null_value || args[1]->null_value))
413
return fix_result(value);
417
int64_t Item_func_plus::int_op()
419
int64_t value=args[0]->val_int()+args[1]->val_int();
420
if ((null_value=args[0]->null_value || args[1]->null_value))
427
Calculate plus of two decimals.
429
@param decimal_value Buffer that can be used to store result
432
0 Value was NULL; In this case null_value is set
434
\# Value of operation as a decimal
437
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
439
my_decimal value1, *val1;
440
my_decimal value2, *val2;
441
val1= args[0]->val_decimal(&value1);
442
if ((null_value= args[0]->null_value))
444
val2= args[1]->val_decimal(&value2);
445
if (!(null_value= (args[1]->null_value ||
446
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
448
return decimal_value;
453
Set precision of results for additive operations (+ and -)
455
void Item_func_additive_op::result_precision()
457
decimals= cmax(args[0]->decimals, args[1]->decimals);
458
int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
459
args[1]->decimal_precision() - args[1]->decimals);
460
int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
462
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
463
if (result_type() == INT_RESULT)
464
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
466
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
467
max_length= my_decimal_precision_to_length(precision, decimals,
473
The following function is here to allow the user to force
474
subtraction of UNSIGNED BIGINT to return negative values.
477
void Item_func_minus::fix_length_and_dec()
479
Item_num_op::fix_length_and_dec();
485
double Item_func_minus::real_op()
487
double value= args[0]->val_real() - args[1]->val_real();
488
if ((null_value=args[0]->null_value || args[1]->null_value))
490
return fix_result(value);
494
int64_t Item_func_minus::int_op()
496
int64_t value=args[0]->val_int() - args[1]->val_int();
497
if ((null_value=args[0]->null_value || args[1]->null_value))
504
See Item_func_plus::decimal_op for comments.
507
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
509
my_decimal value1, *val1;
510
my_decimal value2, *val2=
512
val1= args[0]->val_decimal(&value1);
513
if ((null_value= args[0]->null_value))
515
val2= args[1]->val_decimal(&value2);
516
if (!(null_value= (args[1]->null_value ||
517
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
519
return decimal_value;
524
double Item_func_mul::real_op()
527
double value= args[0]->val_real() * args[1]->val_real();
528
if ((null_value=args[0]->null_value || args[1]->null_value))
530
return fix_result(value);
534
int64_t Item_func_mul::int_op()
537
int64_t value=args[0]->val_int()*args[1]->val_int();
538
if ((null_value=args[0]->null_value || args[1]->null_value))
544
/** See Item_func_plus::decimal_op for comments. */
546
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
548
my_decimal value1, *val1;
549
my_decimal value2, *val2;
550
val1= args[0]->val_decimal(&value1);
551
if ((null_value= args[0]->null_value))
553
val2= args[1]->val_decimal(&value2);
554
if (!(null_value= (args[1]->null_value ||
555
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
557
return decimal_value;
562
void Item_func_mul::result_precision()
564
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
565
if (result_type() == INT_RESULT)
566
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
568
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
569
decimals= cmin(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
570
int precision= cmin(args[0]->decimal_precision() + args[1]->decimal_precision(),
571
(unsigned int)DECIMAL_MAX_PRECISION);
572
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
576
double Item_func_div::real_op()
579
double value= args[0]->val_real();
580
double val2= args[1]->val_real();
581
if ((null_value= args[0]->null_value || args[1]->null_value))
585
signal_divide_by_null();
588
return fix_result(value/val2);
592
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
594
my_decimal value1, *val1;
595
my_decimal value2, *val2;
598
val1= args[0]->val_decimal(&value1);
599
if ((null_value= args[0]->null_value))
601
val2= args[1]->val_decimal(&value2);
602
if ((null_value= args[1]->null_value))
604
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
605
val1, val2, prec_increment)) > 3)
607
if (err == E_DEC_DIV_ZERO)
608
signal_divide_by_null();
612
return decimal_value;
616
void Item_func_div::result_precision()
618
uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
619
(unsigned int)DECIMAL_MAX_PRECISION);
620
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
621
if (result_type() == INT_RESULT)
622
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
624
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
625
decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
626
max_length= my_decimal_precision_to_length(precision, decimals,
631
void Item_func_div::fix_length_and_dec()
633
prec_increment= current_thd->variables.div_precincrement;
634
Item_num_op::fix_length_and_dec();
635
switch(hybrid_type) {
638
decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
639
set_if_smaller(decimals, NOT_FIXED_DEC);
640
max_length=args[0]->max_length - args[0]->decimals + decimals;
641
uint32_t tmp=float_length(decimals);
642
set_if_smaller(max_length,tmp);
646
hybrid_type= DECIMAL_RESULT;
655
maybe_null= 1; // devision by zero
660
/* Integer division */
661
int64_t Item_func_int_div::val_int()
664
int64_t value=args[0]->val_int();
665
int64_t val2=args[1]->val_int();
666
if ((null_value= (args[0]->null_value || args[1]->null_value)))
670
signal_divide_by_null();
673
return (unsigned_flag ?
674
(uint64_t) value / (uint64_t) val2 :
679
void Item_func_int_div::fix_length_and_dec()
681
Item_result argtype= args[0]->result_type();
682
/* use precision ony for the data type it is applicable for and valid */
683
max_length=args[0]->max_length -
684
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
685
args[0]->decimals : 0);
687
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
691
int64_t Item_func_mod::int_op()
694
int64_t value= args[0]->val_int();
695
int64_t val2= args[1]->val_int();
698
if ((null_value= args[0]->null_value || args[1]->null_value))
699
return 0; /* purecov: inspected */
702
signal_divide_by_null();
706
if (args[0]->unsigned_flag)
707
result= args[1]->unsigned_flag ?
708
((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
710
result= args[1]->unsigned_flag ?
711
value % ((uint64_t) val2) : value % val2;
716
double Item_func_mod::real_op()
719
double value= args[0]->val_real();
720
double val2= args[1]->val_real();
721
if ((null_value= args[0]->null_value || args[1]->null_value))
722
return 0.0; /* purecov: inspected */
725
signal_divide_by_null();
728
return fmod(value,val2);
732
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
734
my_decimal value1, *val1;
735
my_decimal value2, *val2;
737
val1= args[0]->val_decimal(&value1);
738
if ((null_value= args[0]->null_value))
740
val2= args[1]->val_decimal(&value2);
741
if ((null_value= args[1]->null_value))
743
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
745
case E_DEC_TRUNCATED:
747
return decimal_value;
749
signal_divide_by_null();
757
void Item_func_mod::result_precision()
759
decimals= cmax(args[0]->decimals, args[1]->decimals);
760
max_length= cmax(args[0]->max_length, args[1]->max_length);
764
void Item_func_mod::fix_length_and_dec()
766
Item_num_op::fix_length_and_dec();
768
unsigned_flag= args[0]->unsigned_flag;
772
double Item_func_neg::real_op()
774
double value= args[0]->val_real();
775
null_value= args[0]->null_value;
780
int64_t Item_func_neg::int_op()
782
int64_t value= args[0]->val_int();
783
null_value= args[0]->null_value;
788
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
790
my_decimal val, *value= args[0]->val_decimal(&val);
791
if (!(null_value= args[0]->null_value))
793
my_decimal2decimal(value, decimal_value);
794
my_decimal_neg(decimal_value);
795
return decimal_value;
801
void Item_func_neg::fix_num_length_and_dec()
803
decimals= args[0]->decimals;
804
/* 1 add because sign can appear */
805
max_length= args[0]->max_length + 1;
809
void Item_func_neg::fix_length_and_dec()
811
Item_func_num1::fix_length_and_dec();
814
If this is in integer context keep the context as integer if possible
815
(This is how multiplication and other integer functions works)
816
Use val() to get value as arg_type doesn't mean that item is
817
Item_int or Item_real due to existence of Item_param.
819
if (hybrid_type == INT_RESULT && args[0]->const_item())
821
int64_t val= args[0]->val_int();
822
if ((uint64_t) val >= (uint64_t) INT64_MIN &&
823
((uint64_t) val != (uint64_t) INT64_MIN ||
824
args[0]->type() != INT_ITEM))
827
Ensure that result is converted to DECIMAL, as int64_t can't hold
830
hybrid_type= DECIMAL_RESULT;
838
double Item_func_abs::real_op()
840
double value= args[0]->val_real();
841
null_value= args[0]->null_value;
846
int64_t Item_func_abs::int_op()
848
int64_t value= args[0]->val_int();
849
if ((null_value= args[0]->null_value))
851
return (value >= 0) || unsigned_flag ? value : -value;
855
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
857
my_decimal val, *value= args[0]->val_decimal(&val);
858
if (!(null_value= args[0]->null_value))
860
my_decimal2decimal(value, decimal_value);
861
if (decimal_value->sign())
862
my_decimal_neg(decimal_value);
863
return decimal_value;
869
void Item_func_abs::fix_length_and_dec()
871
Item_func_num1::fix_length_and_dec();
872
unsigned_flag= args[0]->unsigned_flag;
876
/** Gateway to natural LOG function. */
877
double Item_func_ln::val_real()
880
double value= args[0]->val_real();
881
if ((null_value= args[0]->null_value))
885
signal_divide_by_null();
892
Extended but so slower LOG function.
894
We have to check if all values are > zero and first one is not one
895
as these are the cases then result is not a number.
897
double Item_func_log::val_real()
900
double value= args[0]->val_real();
901
if ((null_value= args[0]->null_value))
905
signal_divide_by_null();
910
double value2= args[1]->val_real();
911
if ((null_value= args[1]->null_value))
913
if (value2 <= 0.0 || value == 1.0)
915
signal_divide_by_null();
918
return log(value2) / log(value);
923
double Item_func_log2::val_real()
926
double value= args[0]->val_real();
928
if ((null_value=args[0]->null_value))
932
signal_divide_by_null();
935
return log(value) / M_LN2;
938
double Item_func_log10::val_real()
941
double value= args[0]->val_real();
942
if ((null_value= args[0]->null_value))
946
signal_divide_by_null();
952
double Item_func_exp::val_real()
955
double value= args[0]->val_real();
956
if ((null_value=args[0]->null_value))
957
return 0.0; /* purecov: inspected */
958
return fix_result(exp(value));
961
double Item_func_sqrt::val_real()
964
double value= args[0]->val_real();
965
if ((null_value=(args[0]->null_value || value < 0)))
966
return 0.0; /* purecov: inspected */
970
double Item_func_pow::val_real()
973
double value= args[0]->val_real();
974
double val2= args[1]->val_real();
975
if ((null_value=(args[0]->null_value || args[1]->null_value)))
976
return 0.0; /* purecov: inspected */
977
return fix_result(pow(value,val2));
980
// Trigonometric functions
982
double Item_func_acos::val_real()
985
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
986
volatile double value= args[0]->val_real();
987
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
992
double Item_func_asin::val_real()
995
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
996
volatile double value= args[0]->val_real();
997
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1002
double Item_func_atan::val_real()
1005
double value= args[0]->val_real();
1006
if ((null_value=args[0]->null_value))
1010
double val2= args[1]->val_real();
1011
if ((null_value=args[1]->null_value))
1013
return fix_result(atan2(value,val2));
1018
double Item_func_cos::val_real()
1021
double value= args[0]->val_real();
1022
if ((null_value=args[0]->null_value))
1027
double Item_func_sin::val_real()
1030
double value= args[0]->val_real();
1031
if ((null_value=args[0]->null_value))
1036
double Item_func_tan::val_real()
1039
double value= args[0]->val_real();
1040
if ((null_value=args[0]->null_value))
1042
return fix_result(tan(value));
1046
// Shift-functions, same as << and >> in C/C++
1049
int64_t Item_func_shift_left::val_int()
1053
uint64_t res= ((uint64_t) args[0]->val_int() <<
1054
(shift=(uint) args[1]->val_int()));
1055
if (args[0]->null_value || args[1]->null_value)
1061
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
1064
int64_t Item_func_shift_right::val_int()
1068
uint64_t res= (uint64_t) args[0]->val_int() >>
1069
(shift=(uint) args[1]->val_int());
1070
if (args[0]->null_value || args[1]->null_value)
1076
return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
1080
int64_t Item_func_bit_neg::val_int()
1083
uint64_t res= (uint64_t) args[0]->val_int();
1084
if ((null_value=args[0]->null_value))
1090
// Conversion functions
1092
void Item_func_integer::fix_length_and_dec()
1094
max_length=args[0]->max_length - args[0]->decimals+1;
1095
uint32_t tmp=float_length(decimals);
1096
set_if_smaller(max_length,tmp);
1100
void Item_func_int_val::fix_num_length_and_dec()
1102
max_length= args[0]->max_length - (args[0]->decimals ?
1103
args[0]->decimals + 1 :
1105
uint32_t tmp= float_length(decimals);
1106
set_if_smaller(max_length,tmp);
1111
void Item_func_int_val::find_num_type()
1113
switch(hybrid_type= args[0]->result_type())
1117
hybrid_type= REAL_RESULT;
1118
max_length= float_length(decimals);
1121
case DECIMAL_RESULT:
1123
-2 because in most high position can't be used any digit for int64_t
1124
and one position for increasing value during operation
1126
if ((args[0]->max_length - args[0]->decimals) >=
1127
(DECIMAL_LONGLONG_DIGITS - 2))
1129
hybrid_type= DECIMAL_RESULT;
1133
unsigned_flag= args[0]->unsigned_flag;
1134
hybrid_type= INT_RESULT;
1144
int64_t Item_func_ceiling::int_op()
1147
switch (args[0]->result_type()) {
1149
result= args[0]->val_int();
1150
null_value= args[0]->null_value;
1152
case DECIMAL_RESULT:
1154
my_decimal dec_buf, *dec;
1155
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1156
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1162
result= (int64_t)Item_func_ceiling::real_op();
1168
double Item_func_ceiling::real_op()
1171
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1174
volatile double value= args[0]->val_real();
1175
null_value= args[0]->null_value;
1180
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1182
my_decimal val, *value= args[0]->val_decimal(&val);
1183
if (!(null_value= (args[0]->null_value ||
1184
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1185
decimal_value) > 1)))
1186
return decimal_value;
1191
int64_t Item_func_floor::int_op()
1194
switch (args[0]->result_type()) {
1196
result= args[0]->val_int();
1197
null_value= args[0]->null_value;
1199
case DECIMAL_RESULT:
1201
my_decimal dec_buf, *dec;
1202
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1203
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1209
result= (int64_t)Item_func_floor::real_op();
1215
double Item_func_floor::real_op()
1218
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1221
volatile double value= args[0]->val_real();
1222
null_value= args[0]->null_value;
1223
return floor(value);
1227
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1229
my_decimal val, *value= args[0]->val_decimal(&val);
1230
if (!(null_value= (args[0]->null_value ||
1231
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1232
decimal_value) > 1)))
1233
return decimal_value;
1238
void Item_func_round::fix_length_and_dec()
1240
int decimals_to_set;
1244
unsigned_flag= args[0]->unsigned_flag;
1245
if (!args[1]->const_item())
1247
max_length= args[0]->max_length;
1248
decimals= args[0]->decimals;
1249
if (args[0]->result_type() == DECIMAL_RESULT)
1252
hybrid_type= DECIMAL_RESULT;
1255
hybrid_type= REAL_RESULT;
1259
val1= args[1]->val_int();
1260
val1_unsigned= args[1]->unsigned_flag;
1262
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1264
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1266
if (args[0]->decimals == NOT_FIXED_DEC)
1268
max_length= args[0]->max_length;
1269
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
1270
hybrid_type= REAL_RESULT;
1274
switch (args[0]->result_type()) {
1277
hybrid_type= REAL_RESULT;
1278
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
1279
max_length= float_length(decimals);
1282
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1284
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1285
max_length= args[0]->max_length + length_can_increase;
1286
/* Here we can keep INT_RESULT */
1287
hybrid_type= INT_RESULT;
1292
case DECIMAL_RESULT:
1294
hybrid_type= DECIMAL_RESULT;
1295
decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
1296
int decimals_delta= args[0]->decimals - decimals_to_set;
1297
int precision= args[0]->decimal_precision();
1298
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1300
precision-= decimals_delta - length_increase;
1301
decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
1302
max_length= my_decimal_precision_to_length(precision, decimals,
1307
assert(0); /* This result type isn't handled */
1311
double my_double_round(double value, int64_t dec, bool dec_unsigned,
1315
bool dec_negative= (dec < 0) && !dec_unsigned;
1316
uint64_t abs_dec= dec_negative ? -dec : dec;
1318
tmp2 is here to avoid return the value with 80 bit precision
1319
This will fix that the test round(0.1,1) = round(0.1,1) is true
1321
volatile double tmp2;
1323
tmp=(abs_dec < array_elements(log_10) ?
1324
log_10[abs_dec] : pow(10.0,(double) abs_dec));
1326
if (dec_negative && std::isinf(tmp))
1328
else if (!dec_negative && std::isinf(value * tmp))
1333
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
1335
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
1338
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
1343
double Item_func_round::real_op()
1345
double value= args[0]->val_real();
1347
if (!(null_value= args[0]->null_value || args[1]->null_value))
1348
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
1355
Rounds a given value to a power of 10 specified as the 'to' argument,
1356
avoiding overflows when the value is close to the uint64_t range boundary.
1359
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
1361
uint64_t tmp= value / to * to;
1362
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
1366
int64_t Item_func_round::int_op()
1368
int64_t value= args[0]->val_int();
1369
int64_t dec= args[1]->val_int();
1372
if ((null_value= args[0]->null_value || args[1]->null_value))
1374
if ((dec >= 0) || args[1]->unsigned_flag)
1375
return value; // integer have not digits after point
1380
if(abs_dec >= array_elements(log_10_int))
1383
tmp= log_10_int[abs_dec];
1386
value= (unsigned_flag) ?
1387
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
1389
value= (unsigned_flag || value >= 0) ?
1390
my_unsigned_round((uint64_t) value, tmp) :
1391
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
1396
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
1398
my_decimal val, *value= args[0]->val_decimal(&val);
1399
int64_t dec= args[1]->val_int();
1400
if (dec >= 0 || args[1]->unsigned_flag)
1401
dec= cmin(dec, (int64_t) decimals);
1402
else if (dec < INT_MIN)
1405
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
1406
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
1407
truncate, decimal_value) > 1)))
1409
decimal_value->frac= decimals;
1410
return decimal_value;
1416
void Item_func_rand::seed_random(Item *arg)
1419
TODO: do not do reinit 'rand' for every execute of PS/SP if
1420
args[0] is a constant.
1422
uint32_t tmp= (uint32_t) arg->val_int();
1423
randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
1424
(uint32_t) (tmp*0x10000001L));
1428
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
1430
if (Item_real_func::fix_fields(thd, ref))
1432
used_tables_cache|= RAND_TABLE_BIT;
1434
{ // Only use argument once in query
1436
No need to send a Rand log event if seed was given eg: RAND(seed),
1437
as it will be replicated in the query as such.
1439
if (!rand && !(rand= (struct rand_struct*)
1440
thd->alloc(sizeof(*rand))))
1443
if (args[0]->const_item())
1444
seed_random (args[0]);
1449
Save the seed only the first time RAND() is used in the query
1450
Once events are forwarded rather than recreated,
1451
the following can be skipped if inside the slave thread
1453
if (!thd->rand_used)
1456
thd->rand_saved_seed1= thd->rand.seed1;
1457
thd->rand_saved_seed2= thd->rand.seed2;
1464
void Item_func_rand::update_used_tables()
1466
Item_real_func::update_used_tables();
1467
used_tables_cache|= RAND_TABLE_BIT;
1471
double Item_func_rand::val_real()
1474
if (arg_count && !args[0]->const_item())
1475
seed_random (args[0]);
1476
return my_rnd(rand);
1479
int64_t Item_func_sign::val_int()
1482
double value= args[0]->val_real();
1483
null_value=args[0]->null_value;
1484
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
1488
double Item_func_units::val_real()
1491
double value= args[0]->val_real();
1492
if ((null_value=args[0]->null_value))
1494
return value*mul+add;
1498
void Item_func_min_max::fix_length_and_dec()
1501
bool datetime_found= false;
1505
cmp_type=args[0]->result_type();
1507
for (uint32_t i=0 ; i < arg_count ; i++)
1509
set_if_bigger(max_length, args[i]->max_length);
1510
set_if_bigger(decimals, args[i]->decimals);
1511
set_if_bigger(max_int_part, args[i]->decimal_int_part());
1512
if (args[i]->maybe_null)
1514
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
1515
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
1517
datetime_found= true;
1518
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
1519
datetime_item= args[i];
1522
if (cmp_type == STRING_RESULT)
1524
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
1528
compare_as_dates= true;
1531
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
1532
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
1534
cached_field_type= agg_field_type(args, arg_count);
1539
Compare item arguments in the DATETIME context.
1543
value [out] found least/greatest DATE/DATETIME value
1546
Compare item arguments as DATETIME values and return the index of the
1547
least/greatest argument in the arguments array.
1548
The correct integer DATE/DATETIME value of the found argument is
1549
stored to the value pointer, if latter is provided.
1552
0 If one of arguments is NULL
1553
# index of the least/greatest argument
1556
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
1558
uint64_t min_max= 0;
1559
uint32_t min_max_idx= 0;
1561
for (uint32_t i=0; i < arg_count ; i++)
1563
Item **arg= args + i;
1565
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
1566
if ((null_value= args[i]->null_value))
1568
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
1577
if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
1584
String *Item_func_min_max::val_str(String *str)
1587
if (compare_as_dates)
1590
uint32_t min_max_idx= cmp_datetimes(NULL);
1593
str_res= args[min_max_idx]->val_str(str);
1594
str_res->set_charset(collation.collation);
1600
int64_t nr=val_int();
1603
str->set_int(nr, unsigned_flag, &my_charset_bin);
1606
case DECIMAL_RESULT:
1608
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
1611
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
1616
double nr= val_real();
1618
return 0; /* purecov: inspected */
1619
str->set_real(nr,decimals,&my_charset_bin);
1626
for (uint32_t i=0; i < arg_count ; i++)
1629
res=args[i]->val_str(str);
1633
res2= args[i]->val_str(res == str ? &tmp_value : str);
1636
int cmp= sortcmp(res,res2,collation.collation);
1637
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
1641
if ((null_value= args[i]->null_value))
1644
res->set_charset(collation.collation);
1649
// This case should never be chosen
1653
return 0; // Keep compiler happy
1657
double Item_func_min_max::val_real()
1661
if (compare_as_dates)
1664
(void)cmp_datetimes(&result);
1665
return (double)result;
1667
for (uint32_t i=0; i < arg_count ; i++)
1670
value= args[i]->val_real();
1673
double tmp= args[i]->val_real();
1674
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
1677
if ((null_value= args[i]->null_value))
1684
int64_t Item_func_min_max::val_int()
1688
if (compare_as_dates)
1691
(void)cmp_datetimes(&result);
1692
return (int64_t)result;
1694
for (uint32_t i=0; i < arg_count ; i++)
1697
value=args[i]->val_int();
1700
int64_t tmp=args[i]->val_int();
1701
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
1704
if ((null_value= args[i]->null_value))
1711
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
1714
my_decimal tmp_buf, *tmp, *res= NULL;
1716
if (compare_as_dates)
1719
(void)cmp_datetimes(&value);
1720
uint64_t2decimal(value, dec);
1723
for (uint32_t i=0; i < arg_count ; i++)
1726
res= args[i]->val_decimal(dec);
1729
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
1730
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
1732
if (tmp == &tmp_buf)
1734
/* Move value out of tmp_buf as this will be reused on next loop */
1735
my_decimal2decimal(tmp, dec);
1742
if ((null_value= args[i]->null_value))
1752
int64_t Item_func_length::val_int()
1755
String *res=args[0]->val_str(&value);
1759
return 0; /* purecov: inspected */
1762
return (int64_t) res->length();
1766
int64_t Item_func_char_length::val_int()
1769
String *res=args[0]->val_str(&value);
1773
return 0; /* purecov: inspected */
1776
return (int64_t) res->numchars();
1780
int64_t Item_func_coercibility::val_int()
1784
return (int64_t) args[0]->collation.derivation;
1788
void Item_func_locate::fix_length_and_dec()
1790
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
1791
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
1795
int64_t Item_func_locate::val_int()
1798
String *a=args[0]->val_str(&value1);
1799
String *b=args[1]->val_str(&value2);
1803
return 0; /* purecov: inspected */
1806
/* must be int64_t to avoid truncation */
1813
start0= start= args[2]->val_int() - 1;
1815
if ((start < 0) || (start > a->length()))
1818
/* start is now sufficiently valid to pass to charpos function */
1819
start= a->charpos((int) start);
1821
if (start + b->length() > a->length())
1825
if (!b->length()) // Found empty string at start
1828
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
1830
(uint) (a->length()-start),
1831
b->ptr(), b->length(),
1834
return (int64_t) match.mb_len + start0 + 1;
1838
void Item_func_locate::print(String *str, enum_query_type query_type)
1840
str->append(STRING_WITH_LEN("locate("));
1841
args[1]->print(str, query_type);
1843
args[0]->print(str, query_type);
1847
args[2]->print(str, query_type);
1853
int64_t Item_func_field::val_int()
1857
if (cmp_type == STRING_RESULT)
1860
if (!(field= args[0]->val_str(&value)))
1862
for (uint32_t i=1 ; i < arg_count ; i++)
1864
String *tmp_value=args[i]->val_str(&tmp);
1865
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
1866
return (int64_t) (i);
1869
else if (cmp_type == INT_RESULT)
1871
int64_t val= args[0]->val_int();
1872
if (args[0]->null_value)
1874
for (uint32_t i=1; i < arg_count ; i++)
1876
if (val == args[i]->val_int() && !args[i]->null_value)
1877
return (int64_t) (i);
1880
else if (cmp_type == DECIMAL_RESULT)
1882
my_decimal dec_arg_buf, *dec_arg,
1883
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
1884
if (args[0]->null_value)
1886
for (uint32_t i=1; i < arg_count; i++)
1888
dec_arg= args[i]->val_decimal(&dec_arg_buf);
1889
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
1890
return (int64_t) (i);
1895
double val= args[0]->val_real();
1896
if (args[0]->null_value)
1898
for (uint32_t i=1; i < arg_count ; i++)
1900
if (val == args[i]->val_real() && !args[i]->null_value)
1901
return (int64_t) (i);
1908
void Item_func_field::fix_length_and_dec()
1910
maybe_null=0; max_length=3;
1911
cmp_type= args[0]->result_type();
1912
for (uint32_t i=1; i < arg_count ; i++)
1913
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
1914
if (cmp_type == STRING_RESULT)
1915
agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
1919
int64_t Item_func_ascii::val_int()
1922
String *res=args[0]->val_str(&value);
1929
return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
1932
int64_t Item_func_ord::val_int()
1935
String *res=args[0]->val_str(&value);
1942
if (!res->length()) return 0;
1944
if (use_mb(res->charset()))
1946
register const char *str=res->ptr();
1947
register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
1949
return (int64_t)((unsigned char) *str);
1951
n=(n<<8)|(uint32_t)((unsigned char) *str++);
1955
return (int64_t) ((unsigned char) (*res)[0]);
1958
/* Search after a string in a string of strings separated by ',' */
1959
/* Returns number of found type >= 1 or 0 if not found */
1960
/* This optimizes searching in enums to bit testing! */
1962
void Item_func_find_in_set::fix_length_and_dec()
1965
max_length=3; // 1-999
1966
agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
1969
static const char separator=',';
1971
int64_t Item_func_find_in_set::val_int()
1976
uint64_t tmp=(uint64_t) args[1]->val_int();
1977
if (!(null_value=args[1]->null_value || args[0]->null_value))
1985
String *find=args[0]->val_str(&value);
1986
String *buffer=args[1]->val_str(&value2);
1987
if (!find || !buffer)
1990
return 0; /* purecov: inspected */
1995
if ((diff=buffer->length() - find->length()) >= 0)
1998
const CHARSET_INFO * const cs= cmp_collation.collation;
1999
const char *str_begin= buffer->ptr();
2000
const char *str_end= buffer->ptr();
2001
const char *real_end= str_end+buffer->length();
2002
const unsigned char *find_str= (const unsigned char *) find->ptr();
2003
uint32_t find_str_len= find->length();
2008
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end,
2009
(unsigned char*) real_end)) > 0)
2011
const char *substr_end= str_end + symbol_len;
2012
bool is_last_item= (substr_end == real_end);
2013
bool is_separator= (wc == (my_wc_t) separator);
2014
if (is_separator || is_last_item)
2017
if (is_last_item && !is_separator)
2018
str_end= substr_end;
2019
if (!my_strnncoll(cs, (const unsigned char *) str_begin,
2020
str_end - str_begin,
2021
find_str, find_str_len))
2022
return (int64_t) position;
2024
str_begin= substr_end;
2026
str_end= substr_end;
2028
else if (str_end - str_begin == 0 &&
2029
find_str_len == 0 &&
2030
wc == (my_wc_t) separator)
2031
return (int64_t) ++position;
2039
int64_t Item_func_bit_count::val_int()
2042
uint64_t value= (uint64_t) args[0]->val_int();
2043
if ((null_value= args[0]->null_value))
2044
return 0; /* purecov: inspected */
2045
return (int64_t) my_count_bits(value);
2052
pthread_mutex_t LOCK_user_locks;
2053
static HASH hash_user_locks;
2055
class User_level_lock
2063
pthread_cond_t cond;
2064
my_thread_id thread_id;
2065
void set_thread(THD *thd) { thread_id= thd->thread_id; }
2067
User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id)
2068
:key_length(length),count(1),locked(1), thread_id(id)
2070
key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
2071
pthread_cond_init(&cond,NULL);
2074
if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
2085
hash_delete(&hash_user_locks,(unsigned char*) this);
2088
pthread_cond_destroy(&cond);
2090
inline bool initialized() { return key != 0; }
2091
friend void item_user_lock_release(User_level_lock *ull);
2092
friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2096
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
2097
bool not_used __attribute__((unused)))
2099
*length= ull->key_length;
2104
static bool item_user_lock_inited= 0;
2106
void item_user_lock_init(void)
2108
pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
2109
hash_init(&hash_user_locks, system_charset_info,
2110
16,0,0,(hash_get_key) ull_get_key,NULL,0);
2111
item_user_lock_inited= 1;
2114
void item_user_lock_free(void)
2116
if (item_user_lock_inited)
2118
item_user_lock_inited= 0;
2119
hash_free(&hash_user_locks);
2120
pthread_mutex_destroy(&LOCK_user_locks);
2124
void item_user_lock_release(User_level_lock *ull)
2129
pthread_cond_signal(&ull->cond);
2135
Wait until we are at or past the given position in the master binlog
2139
int64_t Item_master_pos_wait::val_int()
2142
THD* thd = current_thd;
2143
String *log_name = args[0]->val_str(&value);
2147
if (thd->slave_thread || !log_name || !log_name->length())
2152
int64_t pos = (ulong)args[1]->val_int();
2153
int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
2154
if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
2163
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
2170
int64_t Item_func_last_insert_id::val_int()
2172
THD *thd= current_thd;
2176
int64_t value= args[0]->val_int();
2177
null_value= args[0]->null_value;
2179
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
2180
documented in the manual. We don't want to touch
2181
first_successful_insert_id_in_cur_stmt because it would make
2182
LAST_INSERT_ID(X) take precedence over an generated auto_increment
2185
thd->arg_of_last_insert_id_function= true;
2186
thd->first_successful_insert_id_in_prev_stmt= value;
2189
return thd->read_first_successful_insert_id_in_prev_stmt();
2193
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
2195
return Item_int_func::fix_fields(thd, ref);
2199
/* This function is just used to test speed of different functions */
2201
int64_t Item_func_benchmark::val_int()
2204
char buff[MAX_FIELD_WIDTH];
2205
String tmp(buff,sizeof(buff), &my_charset_bin);
2206
my_decimal tmp_decimal;
2207
THD *thd=current_thd;
2208
uint64_t loop_count;
2210
loop_count= (uint64_t) args[0]->val_int();
2212
if (args[0]->null_value ||
2213
(!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
2215
if (!args[0]->null_value)
2218
llstr(((int64_t) loop_count), buff);
2219
push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
2220
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
2221
"count", buff, "benchmark");
2229
for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
2231
switch (args[1]->result_type()) {
2233
(void) args[1]->val_real();
2236
(void) args[1]->val_int();
2239
(void) args[1]->val_str(&tmp);
2241
case DECIMAL_RESULT:
2242
(void) args[1]->val_decimal(&tmp_decimal);
2246
// This case should never be chosen
2255
void Item_func_benchmark::print(String *str, enum_query_type query_type)
2257
str->append(STRING_WITH_LEN("benchmark("));
2258
args[0]->print(str, query_type);
2260
args[1]->print(str, query_type);
2264
#define extra_size sizeof(double)
2266
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
2267
bool create_if_not_exists)
2269
user_var_entry *entry;
2271
if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
2273
create_if_not_exists)
2275
uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
2276
if (!hash_inited(hash))
2278
if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
2280
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
2282
entry->name.length=name.length;
2285
entry->update_query_id=0;
2286
entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
2287
entry->unsigned_flag= 0;
2289
If we are here, we were called from a SET or a query which sets a
2290
variable. Imagine it is this:
2291
INSERT INTO t SELECT @a:=10, @a:=@a+1.
2292
Then when we have a Item_func_get_user_var (because of the @a+1) so we
2293
think we have to write the value of @a to the binlog. But before that,
2294
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
2295
the variable as "already logged" (line below) so that it won't be logged
2296
by Item_func_get_user_var (because that's not necessary).
2298
entry->used_query_id=current_thd->query_id;
2299
entry->type=STRING_RESULT;
2300
memcpy(entry->name.str, name.str, name.length+1);
2301
if (my_hash_insert(hash,(unsigned char*) entry))
2303
free((char*) entry);
2311
When a user variable is updated (in a SET command or a query like
2315
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
2318
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
2319
if (Item_func::fix_fields(thd, ref) ||
2320
!(entry= get_variable(&thd->user_vars, name, 1)))
2323
Remember the last query which updated it, this way a query can later know
2324
if this variable is a constant item in the query (it is if update_query_id
2325
is different from query_id).
2327
entry->update_query_id= thd->query_id;
2329
As it is wrong and confusing to associate any
2330
character set with NULL, @a should be latin2
2331
after this query sequence:
2333
SET @a=_latin2'string';
2336
I.e. the second query should not change the charset
2337
to the current default value, but should keep the
2338
original value assigned during the first query.
2339
In order to do it, we don't copy charset
2340
from the argument if the argument is NULL
2341
and the variable has previously been initialized.
2343
null_item= (args[0]->type() == NULL_ITEM);
2344
if (!entry->collation.collation || !null_item)
2345
entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
2346
collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
2347
cached_result_type= args[0]->result_type();
2353
Item_func_set_user_var::fix_length_and_dec()
2355
maybe_null=args[0]->maybe_null;
2356
max_length=args[0]->max_length;
2357
decimals=args[0]->decimals;
2358
collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
2363
Mark field in read_map
2366
This is used by filesort to register used fields in a a temporary
2367
column read set or to register used fields in a view
2370
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
2374
Table *table= (Table *) arg;
2375
if (result_field->table == table || !table)
2376
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
2383
Set value to user variable.
2385
@param entry pointer to structure representing variable
2386
@param set_null should we set NULL value ?
2387
@param ptr pointer to buffer with new value
2388
@param length length of new value
2389
@param type type of new value
2390
@param cs charset info for new value
2391
@param dv derivation for new value
2392
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
2394
@note Sets error and fatal error if allocation fails.
2403
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
2404
Item_result type, const CHARSET_INFO * const cs, Derivation dv,
2409
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
2410
if (entry->value && entry->value != pos)
2417
if (type == STRING_RESULT)
2418
length++; // Store strings with end \0
2419
if (length <= extra_size)
2421
/* Save value in value struct */
2422
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
2423
if (entry->value != pos)
2432
/* Allocate variable */
2433
if (entry->length != length)
2435
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
2436
if (entry->value == pos)
2438
entry->value= (char*) my_realloc(entry->value, length,
2439
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
2445
if (type == STRING_RESULT)
2447
length--; // Fix length change above
2448
entry->value[length]= 0; // Store end \0
2450
memcpy(entry->value,ptr,length);
2451
if (type == DECIMAL_RESULT)
2452
((my_decimal*)entry->value)->fix_buffer_pointer();
2453
entry->length= length;
2454
entry->collation.set(cs, dv);
2455
entry->unsigned_flag= unsigned_arg;
2463
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
2464
Item_result res_type,
2465
const CHARSET_INFO * const cs, Derivation dv,
2469
If we set a variable explicitely to NULL then keep the old
2470
result type of the variable
2472
if ((null_value= args[0]->null_value) && null_item)
2473
res_type= entry->type; // Don't change type of item
2474
if (::update_hash(entry, (null_value= args[0]->null_value),
2475
ptr, length, res_type, cs, dv, unsigned_arg))
2484
/** Get the value of a variable as a double. */
2486
double user_var_entry::val_real(bool *null_value)
2488
if ((*null_value= (value == 0)))
2493
return *(double*) value;
2495
return (double) *(int64_t*) value;
2496
case DECIMAL_RESULT:
2499
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
2503
return my_atof(value); // This is null terminated
2505
assert(1); // Impossible
2508
return 0.0; // Impossible
2512
/** Get the value of a variable as an integer. */
2514
int64_t user_var_entry::val_int(bool *null_value) const
2516
if ((*null_value= (value == 0)))
2521
return (int64_t) *(double*) value;
2523
return *(int64_t*) value;
2524
case DECIMAL_RESULT:
2527
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
2533
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
2536
assert(1); // Impossible
2539
return 0L; // Impossible
2543
/** Get the value of a variable as a string. */
2545
String *user_var_entry::val_str(bool *null_value, String *str,
2548
if ((*null_value= (value == 0)))
2553
str->set_real(*(double*) value, decimals, &my_charset_bin);
2557
str->set(*(int64_t*) value, &my_charset_bin);
2559
str->set(*(uint64_t*) value, &my_charset_bin);
2561
case DECIMAL_RESULT:
2562
my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
2565
if (str->copy(value, length, collation.collation))
2566
str= 0; // EOM error
2568
assert(1); // Impossible
2574
/** Get the value of a variable as a decimal. */
2576
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
2578
if ((*null_value= (value == 0)))
2583
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
2586
int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
2588
case DECIMAL_RESULT:
2589
val= (my_decimal *)value;
2592
str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
2595
assert(1); // Impossible
2602
This functions is invoked on SET \@variable or
2603
\@variable:= expression.
2605
Evaluate (and check expression), store results.
2608
For now it always return OK. All problem with value evaluating
2609
will be caught by thd->is_error() check in sql_set_variables().
2616
Item_func_set_user_var::check(bool use_result_field)
2618
if (use_result_field && !result_field)
2619
use_result_field= false;
2621
switch (cached_result_type) {
2624
save_result.vreal= use_result_field ? result_field->val_real() :
2625
args[0]->val_real();
2630
save_result.vint= use_result_field ? result_field->val_int() :
2632
unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
2633
args[0]->unsigned_flag;
2638
save_result.vstr= use_result_field ? result_field->val_str(&value) :
2639
args[0]->val_str(&value);
2642
case DECIMAL_RESULT:
2644
save_result.vdec= use_result_field ?
2645
result_field->val_decimal(&decimal_buff) :
2646
args[0]->val_decimal(&decimal_buff);
2651
// This case should never be chosen
2660
This functions is invoked on
2661
SET \@variable or \@variable:= expression.
2664
We have to store the expression as such in the variable, independent of
2665
the value method used by the user
2675
Item_func_set_user_var::update()
2679
switch (cached_result_type) {
2682
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
2683
REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
2688
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
2689
INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
2695
if (!save_result.vstr) // Null value
2696
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
2697
DERIVATION_IMPLICIT, 0);
2699
res= update_hash((void*) save_result.vstr->ptr(),
2700
save_result.vstr->length(), STRING_RESULT,
2701
save_result.vstr->charset(),
2702
DERIVATION_IMPLICIT, 0);
2705
case DECIMAL_RESULT:
2707
if (!save_result.vdec) // Null value
2708
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
2709
DERIVATION_IMPLICIT, 0);
2711
res= update_hash((void*) save_result.vdec,
2712
sizeof(my_decimal), DECIMAL_RESULT,
2713
&my_charset_bin, DERIVATION_IMPLICIT, 0);
2718
// This case should never be chosen
2726
double Item_func_set_user_var::val_real()
2730
update(); // Store expression
2731
return entry->val_real(&null_value);
2734
int64_t Item_func_set_user_var::val_int()
2738
update(); // Store expression
2739
return entry->val_int(&null_value);
2742
String *Item_func_set_user_var::val_str(String *str)
2746
update(); // Store expression
2747
return entry->val_str(&null_value, str, decimals);
2751
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
2755
update(); // Store expression
2756
return entry->val_decimal(&null_value, val);
2760
double Item_func_set_user_var::val_result()
2764
update(); // Store expression
2765
return entry->val_real(&null_value);
2768
int64_t Item_func_set_user_var::val_int_result()
2772
update(); // Store expression
2773
return entry->val_int(&null_value);
2776
String *Item_func_set_user_var::str_result(String *str)
2780
update(); // Store expression
2781
return entry->val_str(&null_value, str, decimals);
2785
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
2789
update(); // Store expression
2790
return entry->val_decimal(&null_value, val);
2794
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
2796
str->append(STRING_WITH_LEN("(@"));
2797
str->append(name.str, name.length);
2798
str->append(STRING_WITH_LEN(":="));
2799
args[0]->print(str, query_type);
2804
void Item_func_set_user_var::print_as_stmt(String *str,
2805
enum_query_type query_type)
2807
str->append(STRING_WITH_LEN("set @"));
2808
str->append(name.str, name.length);
2809
str->append(STRING_WITH_LEN(":="));
2810
args[0]->print(str, query_type);
2814
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
2820
return protocol->store(result_field);
2822
return Item::send(protocol, str_arg);
2825
void Item_func_set_user_var::make_field(Send_field *tmp_field)
2829
result_field->make_field(tmp_field);
2830
assert(tmp_field->table_name != 0);
2832
tmp_field->col_name=Item::name; // Use user supplied name
2835
Item::make_field(tmp_field);
2840
Save the value of a user variable into a field
2844
field target field to save the value to
2845
no_conversion flag indicating whether conversions are allowed
2848
Save the function value into a field and update the user variable
2849
accordingly. If a result field is defined and the target field doesn't
2850
coincide with it then the value from the result field will be used as
2851
the new value of the user variable.
2853
The reason to have this method rather than simply using the result
2854
field in the val_xxx() methods is that the value from the result field
2855
not always can be used when the result field is defined.
2856
Let's consider the following cases:
2857
1) when filling a tmp table the result field is defined but the value of it
2858
is undefined because it has to be produced yet. Thus we can't use it.
2859
2) on execution of an INSERT ... SELECT statement the save_in_field()
2860
function will be called to fill the data in the new record. If the SELECT
2861
part uses a tmp table then the result field is defined and should be
2862
used in order to get the correct result.
2864
The difference between the SET_USER_VAR function and regular functions
2865
like CONCAT is that the Item_func objects for the regular functions are
2866
replaced by Item_field objects after the values of these functions have
2867
been stored in a tmp table. Yet an object of the Item_field class cannot
2868
be used to update a user variable.
2869
Due to this we have to handle the result field in a special way here and
2870
in the Item_func_set_user_var::send() function.
2877
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
2878
bool can_use_result_field)
2880
bool use_result_field= (!can_use_result_field ? 0 :
2881
(result_field && result_field != field));
2884
/* Update the value of the user variable */
2885
check(use_result_field);
2888
if (result_type() == STRING_RESULT ||
2889
(result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
2892
const CHARSET_INFO * const cs= collation.collation;
2893
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
2894
str_value.set_quick(buff, sizeof(buff), cs);
2895
result= entry->val_str(&null_value, &str_value, decimals);
2899
str_value.set_quick(0, 0, cs);
2900
return set_field_to_null_with_conversions(field, no_conversions);
2903
/* NOTE: If null_value == false, "result" must be not NULL. */
2905
field->set_notnull();
2906
error=field->store(result->ptr(),result->length(),cs);
2907
str_value.set_quick(0, 0, cs);
2909
else if (result_type() == REAL_RESULT)
2911
double nr= entry->val_real(&null_value);
2913
return set_field_to_null(field);
2914
field->set_notnull();
2915
error=field->store(nr);
2917
else if (result_type() == DECIMAL_RESULT)
2919
my_decimal decimal_value;
2920
my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
2922
return set_field_to_null(field);
2923
field->set_notnull();
2924
error=field->store_decimal(val);
2928
int64_t nr= entry->val_int(&null_value);
2930
return set_field_to_null_with_conversions(field, no_conversions);
2931
field->set_notnull();
2932
error=field->store(nr, unsigned_flag);
2939
Item_func_get_user_var::val_str(String *str)
2943
return((String*) 0); // No such variable
2944
return(var_entry->val_str(&null_value, str, decimals));
2948
double Item_func_get_user_var::val_real()
2952
return 0.0; // No such variable
2953
return (var_entry->val_real(&null_value));
2957
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
2962
return var_entry->val_decimal(&null_value, dec);
2966
int64_t Item_func_get_user_var::val_int()
2970
return 0L; // No such variable
2971
return (var_entry->val_int(&null_value));
2976
Get variable by name and, if necessary, put the record of variable
2977
use into the binary log.
2979
When a user variable is invoked from an update query (INSERT, UPDATE etc),
2980
stores this variable and its value in thd->user_var_events, so that it can be
2981
written to the binlog (will be written just before the query is written, see
2984
@param thd Current thread
2985
@param name Variable name
2986
@param[out] out_entry variable structure or NULL. The pointer is set
2987
regardless of whether function succeeded or not.
2992
1 Failed to put appropriate record into binary log
2996
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
2997
LEX_STRING &name, user_var_entry **out_entry)
2999
BINLOG_USER_VAR_EVENT *user_var_event;
3000
user_var_entry *var_entry;
3001
var_entry= get_variable(&thd->user_vars, name, 0);
3004
Any reference to user-defined variable which is done from stored
3005
function or trigger affects their execution and the execution of the
3006
calling statement. We must log all such variables even if they are
3007
not involved in table-updating statements.
3009
if (!(opt_bin_log && is_update_query(sql_command)))
3011
*out_entry= var_entry;
3018
If the variable does not exist, it's NULL, but we want to create it so
3019
that it gets into the binlog (if it didn't, the slave could be
3020
influenced by a variable of the same name previously set by another
3022
We create it like if it had been explicitly set with SET before.
3023
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
3024
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
3025
in dispatch_command()). Instead of building a one-element list to pass to
3026
sql_set_variables(), we could instead manually call check() and update();
3027
this would save memory and time; but calling sql_set_variables() makes
3028
one unique place to maintain (sql_set_variables()).
3030
Manipulation with lex is necessary since free_underlaid_joins
3031
is going to release memory belonging to the main query.
3034
List<set_var_base> tmp_var_list;
3035
LEX *sav_lex= thd->lex, lex_tmp;
3038
tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
3040
/* Create the variable */
3041
if (sql_set_variables(thd, &tmp_var_list))
3047
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
3050
else if (var_entry->used_query_id == thd->query_id ||
3051
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
3054
If this variable was already stored in user_var_events by this query
3055
(because it's used in more than one place in the query), don't store
3058
*out_entry= var_entry;
3064
First we need to store value of var_entry, when the next situation
3067
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
3068
We have to write to binlog value @a= 1.
3070
We allocate the user_var_event on user_var_events_alloc pool, not on
3071
the this-statement-execution pool because in SPs user_var_event objects
3072
may need to be valid after current [SP] statement execution pool is
3075
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
3076
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
3077
alloc_root(thd->user_var_events_alloc, size)))
3080
user_var_event->value= (char*) user_var_event +
3081
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
3082
user_var_event->user_var_event= var_entry;
3083
user_var_event->type= var_entry->type;
3084
user_var_event->charset_number= var_entry->collation.collation->number;
3085
if (!var_entry->value)
3088
user_var_event->length= 0;
3089
user_var_event->value= 0;
3093
user_var_event->length= var_entry->length;
3094
memcpy(user_var_event->value, var_entry->value,
3097
/* Mark that this variable has been used by this query */
3098
var_entry->used_query_id= thd->query_id;
3099
if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
3102
*out_entry= var_entry;
3106
*out_entry= var_entry;
3110
void Item_func_get_user_var::fix_length_and_dec()
3112
THD *thd=current_thd;
3115
decimals=NOT_FIXED_DEC;
3116
max_length=MAX_BLOB_WIDTH;
3118
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
3121
If the variable didn't exist it has been created as a STRING-type.
3122
'var_entry' is NULL only if there occured an error during the call to
3123
get_var_with_binlog.
3127
m_cached_result_type= var_entry->type;
3128
unsigned_flag= var_entry->unsigned_flag;
3129
max_length= var_entry->length;
3131
collation.set(var_entry->collation);
3132
switch(m_cached_result_type) {
3134
max_length= DBL_DIG + 8;
3137
max_length= MAX_BIGINT_WIDTH;
3141
max_length= MAX_BLOB_WIDTH;
3143
case DECIMAL_RESULT:
3144
max_length= DECIMAL_MAX_STR_LENGTH;
3145
decimals= DECIMAL_MAX_SCALE;
3147
case ROW_RESULT: // Keep compiler happy
3155
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
3157
m_cached_result_type= STRING_RESULT;
3158
max_length= MAX_BLOB_WIDTH;
3163
bool Item_func_get_user_var::const_item() const
3165
return (!var_entry || current_thd->query_id != var_entry->update_query_id);
3169
enum Item_result Item_func_get_user_var::result_type() const
3171
return m_cached_result_type;
3175
void Item_func_get_user_var::print(String *str,
3176
enum_query_type query_type __attribute__((unused)))
3178
str->append(STRING_WITH_LEN("(@"));
3179
str->append(name.str,name.length);
3184
bool Item_func_get_user_var::eq(const Item *item,
3185
bool binary_cmp __attribute__((unused))) const
3187
/* Assume we don't have rtti */
3189
return 1; // Same item is same.
3190
/* Check if other type is also a get_user_var() object */
3191
if (item->type() != FUNC_ITEM ||
3192
((Item_func*) item)->functype() != functype())
3194
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
3195
return (name.length == other->name.length &&
3196
!memcmp(name.str, other->name.str, name.length));
3200
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
3203
if (Item::fix_fields(thd, ref) ||
3204
!(entry= get_variable(&thd->user_vars, name, 1)))
3206
entry->type= STRING_RESULT;
3208
Let us set the same collation which is used for loading
3209
of fields in LOAD DATA INFILE.
3210
(Since Item_user_var_as_out_param is used only there).
3212
entry->collation.set(thd->variables.collation_database);
3213
entry->update_query_id= thd->query_id;
3218
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
3220
::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
3221
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
3225
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
3226
const CHARSET_INFO * const cs)
3228
::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
3229
DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
3233
double Item_user_var_as_out_param::val_real()
3240
int64_t Item_user_var_as_out_param::val_int()
3247
String* Item_user_var_as_out_param::val_str(String *str __attribute__((unused)))
3254
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((unused)))
3261
void Item_user_var_as_out_param::print(String *str,
3262
enum_query_type query_type __attribute__((unused)))
3265
str->append(name.str,name.length);
3269
Item_func_get_system_var::
3270
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
3271
LEX_STRING *component_arg, const char *name_arg,
3272
size_t name_len_arg)
3273
:var(var_arg), var_type(var_type_arg), component(*component_arg)
3275
/* set_name() will allocate the name */
3276
set_name(name_arg, name_len_arg, system_charset_info);
3281
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
3286
Evaluate the system variable and substitute the result (a basic constant)
3287
instead of this item. If the variable can not be evaluated,
3288
the error is reported in sys_var::item().
3290
if (!(item= var->item(thd, var_type, &component)))
3291
return(1); // Impossible
3292
item->set_name(name, 0, system_charset_info); // don't allocate a new name
3293
thd->change_item_tree(ref, item);
3299
bool Item_func_get_system_var::is_written_to_binlog()
3301
return var->is_written_to_binlog(var_type);
3304
int64_t Item_func_bit_xor::val_int()
3307
uint64_t arg1= (uint64_t) args[0]->val_int();
3308
uint64_t arg2= (uint64_t) args[1]->val_int();
3309
if ((null_value= (args[0]->null_value || args[1]->null_value)))
3311
return (int64_t) (arg1 ^ arg2);
3315
/***************************************************************************
3317
****************************************************************************/
3320
Return value of an system variable base[.name] as a constant item.
3322
@param thd Thread handler
3323
@param var_type global / session
3324
@param name Name of base or system variable
3325
@param component Component.
3328
If component.str = 0 then the variable name is in 'name'
3336
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
3337
LEX_STRING component)
3340
LEX_STRING *base_name, *component_name;
3344
base_name= &component;
3345
component_name= &name;
3350
component_name= &component; // Empty string
3353
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
3357
if (!var->is_struct())
3359
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
3364
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
3366
return new Item_func_get_system_var(var, var_type, component_name,
3372
Check a user level lock.
3374
Sets null_value=true on error.
3379
0 Already taken, or error
3382
int64_t Item_func_is_free_lock::val_int()
3385
String *res=args[0]->val_str(&value);
3386
User_level_lock *ull;
3389
if (!res || !res->length())
3395
pthread_mutex_lock(&LOCK_user_locks);
3396
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
3397
(size_t) res->length());
3398
pthread_mutex_unlock(&LOCK_user_locks);
3399
if (!ull || !ull->locked)
3404
int64_t Item_func_is_used_lock::val_int()
3407
String *res=args[0]->val_str(&value);
3408
User_level_lock *ull;
3411
if (!res || !res->length())
3414
pthread_mutex_lock(&LOCK_user_locks);
3415
ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
3416
(size_t) res->length());
3417
pthread_mutex_unlock(&LOCK_user_locks);
3418
if (!ull || !ull->locked)
3422
return ull->thread_id;
3426
int64_t Item_func_row_count::val_int()
3429
THD *thd= current_thd;
3431
return thd->row_count_func;
3434
int64_t Item_func_found_rows::val_int()
3437
THD *thd= current_thd;
3439
return thd->found_rows();