58
void Item_func::set_arguments(List<Item> &list)
61
arg_count=list.elements;
62
args= tmp_arg; // If 2 arguments
63
if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
65
List_iterator_fast<Item> li(list);
67
Item **save_args= args;
72
with_sum_func|=item->with_sum_func;
75
list.empty(); // Fields are used
78
Item_func::Item_func(List<Item> &list)
84
Item_func::Item_func(THD *thd, Item_func *item)
85
:Item_result_field(thd, item),
86
allowed_arg_cols(item->allowed_arg_cols),
87
arg_count(item->arg_count),
88
used_tables_cache(item->used_tables_cache),
89
not_null_tables_cache(item->not_null_tables_cache),
90
const_item_cache(item->const_item_cache)
98
if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
101
memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
107
Resolve references to table column for a function and its argument
112
ref Pointer to where this object is used. This reference
113
is used if we want to replace this object with another
114
one (for example in the summary functions).
117
Call fix_fields() for all arguments to the function. The main intention
118
is to allow all Item_field() objects to setup pointers to the table fields.
120
Sets as a side effect the following class variables:
121
maybe_null Set if any argument may return NULL
122
with_sum_func Set if any of the arguments contains a sum function
123
used_tables_cache Set to union of the tables used by arguments
125
str_value.charset If this is a string function, set this to the
126
character set for the first argument.
127
If any argument is binary, this is set to binary
129
If for any item any of the defaults are wrong, then this can
130
be fixed in the fix_length_and_dec() function that is called
131
after this one or by writing a specialized fix_fields() for the
136
true Got error. Stored with my_error().
140
Item_func::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
143
Item **arg,**arg_end;
144
void *save_thd_marker= thd->thd_marker;
145
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
147
used_tables_cache= not_null_tables_cache= 0;
150
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
151
return true; // Fatal error if flag is set!
153
{ // Print purify happy
154
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
158
We can't yet set item to *arg as fix_fields may change *arg
159
We shouldn't call fix_fields() twice, so check 'fixed' field first
161
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
162
return true; /* purecov: inspected */
165
if (allowed_arg_cols)
167
if (item->check_cols(allowed_arg_cols))
172
/* we have to fetch allowed_arg_cols from first argument */
173
assert(arg == args); // it is first argument
174
allowed_arg_cols= item->cols();
175
assert(allowed_arg_cols); // Can't be 0 any more
178
if (item->maybe_null)
181
with_sum_func= with_sum_func || item->with_sum_func;
182
used_tables_cache|= item->used_tables();
183
not_null_tables_cache|= item->not_null_tables();
184
const_item_cache&= item->const_item();
185
with_subselect|= item->with_subselect;
188
fix_length_and_dec();
189
if (thd->is_error()) // An error inside fix_length_and_dec occured
192
thd->thd_marker= save_thd_marker;
197
void Item_func::fix_after_pullout(st_select_lex *new_parent,
198
Item **ref __attribute__((__unused__)))
200
Item **arg,**arg_end;
202
used_tables_cache= not_null_tables_cache= 0;
207
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
209
(*arg)->fix_after_pullout(new_parent, arg);
212
used_tables_cache|= item->used_tables();
213
not_null_tables_cache|= item->not_null_tables();
214
const_item_cache&= item->const_item();
220
bool Item_func::walk(Item_processor processor, bool walk_subquery,
225
Item **arg,**arg_end;
226
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
228
if ((*arg)->walk(processor, walk_subquery, argument))
232
return (this->*processor)(argument);
235
void Item_func::traverse_cond(Cond_traverser traverser,
236
void *argument, traverse_order order)
240
Item **arg,**arg_end;
244
(*traverser)(this, argument);
245
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
247
(*arg)->traverse_cond(traverser, argument, order);
251
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
253
(*arg)->traverse_cond(traverser, argument, order);
255
(*traverser)(this, argument);
259
(*traverser)(this, argument);
264
Transform an Item_func object with a transformer callback function.
266
The function recursively applies the transform method to each
267
argument of the Item_func node.
268
If the call of the method for an argument item returns a new item
269
the old item is substituted for a new one.
270
After this the transformer is applied to the root node
271
of the Item_func object.
272
@param transformer the transformer callback function to be applied to
273
the nodes of the tree of the object
274
@param argument parameter to be passed to the transformer
277
Item returned as the result of transformation of the root node
280
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
284
Item **arg,**arg_end;
285
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
287
Item *new_item= (*arg)->transform(transformer, argument);
292
THD::change_item_tree() should be called only if the tree was
293
really transformed, i.e. when a new item has been created.
294
Otherwise we'll be allocating a lot of unnecessary memory for
295
change records at each execution.
297
if (*arg != new_item)
298
current_thd->change_item_tree(arg, new_item);
301
return (this->*transformer)(argument);
306
Compile Item_func object with a processor and a transformer
309
First the function applies the analyzer to the root node of
310
the Item_func object. Then if the analizer succeeeds (returns true)
311
the function recursively applies the compile method to each argument
312
of the Item_func node.
313
If the call of the method for an argument item returns a new item
314
the old item is substituted for a new one.
315
After this the transformer is applied to the root node
316
of the Item_func object.
318
@param analyzer the analyzer callback function to be applied to the
319
nodes of the tree of the object
320
@param[in,out] arg_p parameter to be passed to the processor
321
@param transformer the transformer callback function to be applied to the
322
nodes of the tree of the object
323
@param arg_t parameter to be passed to the transformer
326
Item returned as the result of transformation of the root node
329
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
330
Item_transformer transformer, uchar *arg_t)
332
if (!(this->*analyzer)(arg_p))
336
Item **arg,**arg_end;
337
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
340
The same parameter value of arg_p must be passed
341
to analyze any argument of the condition formula.
343
uchar *arg_v= *arg_p;
344
Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
345
if (new_item && *arg != new_item)
346
current_thd->change_item_tree(arg, new_item);
349
return (this->*transformer)(arg_t);
353
See comments in Item_cmp_func::split_sum_func()
356
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
359
Item **arg, **arg_end;
360
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
361
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, true);
365
void Item_func::update_used_tables()
369
for (uint i=0 ; i < arg_count ; i++)
371
args[i]->update_used_tables();
372
used_tables_cache|=args[i]->used_tables();
373
const_item_cache&=args[i]->const_item();
378
table_map Item_func::used_tables() const
380
return used_tables_cache;
384
table_map Item_func::not_null_tables() const
386
return not_null_tables_cache;
390
void Item_func::print(String *str, enum_query_type query_type)
392
str->append(func_name());
394
print_args(str, 0, query_type);
399
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
401
for (uint i=from ; i < arg_count ; i++)
405
args[i]->print(str, query_type);
410
void Item_func::print_op(String *str, enum_query_type query_type)
413
for (uint i=0 ; i < arg_count-1 ; i++)
415
args[i]->print(str, query_type);
417
str->append(func_name());
420
args[arg_count-1]->print(str, query_type);
425
bool Item_func::eq(const Item *item, bool binary_cmp) const
427
/* Assume we don't have rtti */
430
if (item->type() != FUNC_ITEM)
432
Item_func *item_func=(Item_func*) item;
433
Item_func::Functype func_type;
434
if ((func_type= functype()) != item_func->functype() ||
435
arg_count != item_func->arg_count ||
436
(func_type != Item_func::FUNC_SP &&
437
func_name() != item_func->func_name()) ||
438
(func_type == Item_func::FUNC_SP &&
439
my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
441
for (uint i=0; i < arg_count ; i++)
442
if (!args[i]->eq(item_func->args[i], binary_cmp))
448
Field *Item_func::tmp_table_field(TABLE *table)
452
switch (result_type()) {
454
if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
455
field= new Field_int64_t(max_length, maybe_null, name, unsigned_flag);
457
field= new Field_long(max_length, maybe_null, name, unsigned_flag);
460
field= new Field_double(max_length, maybe_null, name, decimals);
463
return make_string_field(table);
466
field= new Field_new_decimal(my_decimal_precision_to_length(decimal_precision(),
469
maybe_null, name, decimals, unsigned_flag);
473
// This case should never be chosen
484
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
487
int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
488
return decimal_value;
492
String *Item_real_func::val_str(String *str)
495
double nr= val_real();
497
return 0; /* purecov: inspected */
498
str->set_real(nr,decimals, &my_charset_bin);
503
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
506
double nr= val_real();
508
return 0; /* purecov: inspected */
509
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
510
return decimal_value;
51
514
void Item_func::fix_num_length_and_dec()
53
uint32_t fl_length= 0;
55
for (uint32_t i=0 ; i < arg_count ; i++)
518
for (uint i=0 ; i < arg_count ; i++)
57
520
set_if_bigger(decimals,args[i]->decimals);
58
521
set_if_bigger(fl_length, args[i]->max_length);
150
620
return copy_or_same(thd);
623
double Item_int_func::val_real()
627
return unsigned_flag ? (double) ((uint64_t) val_int()) : (double) val_int();
631
String *Item_int_func::val_str(String *str)
634
int64_t nr=val_int();
637
str->set_int(nr, unsigned_flag, &my_charset_bin);
642
void Item_func_connection_id::fix_length_and_dec()
644
Item_int_func::fix_length_and_dec();
649
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
651
if (Item_int_func::fix_fields(thd, ref))
653
thd->thread_specific_used= true;
654
value= thd->variables.pseudo_thread_id;
660
Check arguments here to determine result's type for a numeric
661
function of two arguments.
664
void Item_num_op::find_num_type(void)
666
assert(arg_count == 2);
667
Item_result r0= args[0]->result_type();
668
Item_result r1= args[1]->result_type();
670
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
671
r0 == STRING_RESULT || r1 ==STRING_RESULT)
674
max_length= float_length(decimals);
675
hybrid_type= REAL_RESULT;
677
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
679
hybrid_type= DECIMAL_RESULT;
684
assert(r0 == INT_RESULT && r1 == INT_RESULT);
686
hybrid_type=INT_RESULT;
694
Set result type for a numeric function of one argument
695
(can be also used by a numeric function of many arguments, if the result
696
type depends only on the first argument)
699
void Item_func_num1::find_num_type()
701
switch (hybrid_type= args[0]->result_type()) {
703
unsigned_flag= args[0]->unsigned_flag;
707
hybrid_type= REAL_RESULT;
708
max_length= float_length(decimals);
719
void Item_func_num1::fix_num_length_and_dec()
721
decimals= args[0]->decimals;
722
max_length= args[0]->max_length;
726
void Item_func_numhybrid::fix_length_and_dec()
728
fix_num_length_and_dec();
733
String *Item_func_numhybrid::val_str(String *str)
736
switch (hybrid_type) {
739
my_decimal decimal_value, *val;
740
if (!(val= decimal_op(&decimal_value)))
741
return 0; // null is set
742
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
743
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
748
int64_t nr= int_op();
750
return 0; /* purecov: inspected */
751
str->set_int(nr, unsigned_flag, &my_charset_bin);
756
double nr= real_op();
758
return 0; /* purecov: inspected */
759
str->set_real(nr,decimals,&my_charset_bin);
763
return str_op(&str_value);
771
double Item_func_numhybrid::val_real()
774
switch (hybrid_type) {
777
my_decimal decimal_value, *val;
779
if (!(val= decimal_op(&decimal_value)))
780
return 0.0; // null is set
781
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
786
int64_t result= int_op();
787
return unsigned_flag ? (double) ((uint64_t) result) : (double) result;
795
String *res= str_op(&str_value);
796
return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
797
&end_not_used, &err_not_used) : 0.0);
806
int64_t Item_func_numhybrid::val_int()
809
switch (hybrid_type) {
812
my_decimal decimal_value, *val;
813
if (!(val= decimal_op(&decimal_value)))
814
return 0; // null is set
816
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
822
return (int64_t) rint(real_op());
827
if (!(res= str_op(&str_value)))
830
char *end= (char*) res->ptr() + res->length();
831
CHARSET_INFO *cs= str_value.charset();
832
return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
841
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
843
my_decimal *val= decimal_value;
845
switch (hybrid_type) {
847
val= decimal_op(decimal_value);
851
int64_t result= int_op();
852
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
857
double result= (double)real_op();
858
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
864
if (!(res= str_op(&str_value)))
867
str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
868
res->length(), res->charset(), decimal_value);
879
void Item_func_signed::print(String *str, enum_query_type query_type)
881
str->append(STRING_WITH_LEN("cast("));
882
args[0]->print(str, query_type);
883
str->append(STRING_WITH_LEN(" as signed)"));
888
int64_t Item_func_signed::val_int_from_str(int *error)
890
char buff[MAX_FIELD_WIDTH], *end, *start;
892
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
896
For a string result, we must first get the string and then convert it
900
if (!(res= args[0]->val_str(&tmp)))
907
start= (char *)res->ptr();
908
length= res->length();
911
value= my_strtoll10(start, &end, error);
912
if (*error > 0 || end != start+ length)
915
String err_tmp(err_buff,(uint32) sizeof(err_buff), system_charset_info);
916
err_tmp.copy(start, length, system_charset_info);
917
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
918
ER_TRUNCATED_WRONG_VALUE,
919
ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
926
int64_t Item_func_signed::val_int()
931
if (args[0]->cast_to_int_type() != STRING_RESULT ||
932
args[0]->result_as_int64_t())
934
value= args[0]->val_int();
935
null_value= args[0]->null_value;
939
value= val_int_from_str(&error);
940
if (value < 0 && error == 0)
942
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
943
"Cast to signed converted positive out-of-range integer to "
944
"it's negative complement");
950
void Item_func_unsigned::print(String *str, enum_query_type query_type)
952
str->append(STRING_WITH_LEN("cast("));
953
args[0]->print(str, query_type);
954
str->append(STRING_WITH_LEN(" as unsigned)"));
959
int64_t Item_func_unsigned::val_int()
964
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
966
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
967
if (!(null_value= args[0]->null_value))
968
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
973
else if (args[0]->cast_to_int_type() != STRING_RESULT ||
974
args[0]->result_as_int64_t())
976
value= args[0]->val_int();
977
null_value= args[0]->null_value;
981
value= val_int_from_str(&error);
983
push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
984
"Cast to unsigned converted negative integer to it's "
985
"positive complement");
990
String *Item_decimal_typecast::val_str(String *str)
992
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
995
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
1000
double Item_decimal_typecast::val_real()
1002
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1006
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
1011
int64_t Item_decimal_typecast::val_int()
1013
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
1017
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
1022
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
1024
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
1028
if ((null_value= args[0]->null_value))
1030
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
1036
my_decimal_set_zero(dec);
1040
precision= my_decimal_length_to_precision(max_length,
1041
decimals, unsigned_flag);
1042
if (precision - decimals < (uint) my_decimal_intg(dec))
1044
max_my_decimal(dec, precision, decimals);
1051
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1052
ER_WARN_DATA_OUT_OF_RANGE,
1053
ER(ER_WARN_DATA_OUT_OF_RANGE),
1059
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
1061
char len_buf[20*3 + 1];
1064
uint precision= my_decimal_length_to_precision(max_length, decimals,
1066
str->append(STRING_WITH_LEN("cast("));
1067
args[0]->print(str, query_type);
1068
str->append(STRING_WITH_LEN(" as decimal("));
1070
end=int10_to_str(precision, len_buf,10);
1071
str->append(len_buf, (uint32) (end - len_buf));
1075
end=int10_to_str(decimals, len_buf,10);
1076
str->append(len_buf, (uint32) (end - len_buf));
1083
double Item_func_plus::real_op()
1085
double value= args[0]->val_real() + args[1]->val_real();
1086
if ((null_value=args[0]->null_value || args[1]->null_value))
1088
return fix_result(value);
1092
int64_t Item_func_plus::int_op()
1094
int64_t value=args[0]->val_int()+args[1]->val_int();
1095
if ((null_value=args[0]->null_value || args[1]->null_value))
1102
Calculate plus of two decimals.
1104
@param decimal_value Buffer that can be used to store result
1107
0 Value was NULL; In this case null_value is set
1109
\# Value of operation as a decimal
1112
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
1114
my_decimal value1, *val1;
1115
my_decimal value2, *val2;
1116
val1= args[0]->val_decimal(&value1);
1117
if ((null_value= args[0]->null_value))
1119
val2= args[1]->val_decimal(&value2);
1120
if (!(null_value= (args[1]->null_value ||
1121
(my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
1123
return decimal_value;
1128
Set precision of results for additive operations (+ and -)
1130
void Item_func_additive_op::result_precision()
1132
decimals= max(args[0]->decimals, args[1]->decimals);
1133
int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
1134
args[1]->decimal_precision() - args[1]->decimals);
1135
int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
1137
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1138
if (result_type() == INT_RESULT)
1139
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1141
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1142
max_length= my_decimal_precision_to_length(precision, decimals,
1148
The following function is here to allow the user to force
1149
subtraction of UNSIGNED BIGINT to return negative values.
1152
void Item_func_minus::fix_length_and_dec()
1154
Item_num_op::fix_length_and_dec();
1155
if (unsigned_flag &&
1156
(current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
1161
double Item_func_minus::real_op()
1163
double value= args[0]->val_real() - args[1]->val_real();
1164
if ((null_value=args[0]->null_value || args[1]->null_value))
1166
return fix_result(value);
1170
int64_t Item_func_minus::int_op()
1172
int64_t value=args[0]->val_int() - args[1]->val_int();
1173
if ((null_value=args[0]->null_value || args[1]->null_value))
1180
See Item_func_plus::decimal_op for comments.
1183
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
1185
my_decimal value1, *val1;
1186
my_decimal value2, *val2=
1188
val1= args[0]->val_decimal(&value1);
1189
if ((null_value= args[0]->null_value))
1191
val2= args[1]->val_decimal(&value2);
1192
if (!(null_value= (args[1]->null_value ||
1193
(my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
1195
return decimal_value;
1200
double Item_func_mul::real_op()
1203
double value= args[0]->val_real() * args[1]->val_real();
1204
if ((null_value=args[0]->null_value || args[1]->null_value))
1206
return fix_result(value);
1210
int64_t Item_func_mul::int_op()
1213
int64_t value=args[0]->val_int()*args[1]->val_int();
1214
if ((null_value=args[0]->null_value || args[1]->null_value))
1220
/** See Item_func_plus::decimal_op for comments. */
1222
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
1224
my_decimal value1, *val1;
1225
my_decimal value2, *val2;
1226
val1= args[0]->val_decimal(&value1);
1227
if ((null_value= args[0]->null_value))
1229
val2= args[1]->val_decimal(&value2);
1230
if (!(null_value= (args[1]->null_value ||
1231
(my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
1233
return decimal_value;
1238
void Item_func_mul::result_precision()
1240
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1241
if (result_type() == INT_RESULT)
1242
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1244
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1245
decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
1246
int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
1247
DECIMAL_MAX_PRECISION);
1248
max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
1252
double Item_func_div::real_op()
1255
double value= args[0]->val_real();
1256
double val2= args[1]->val_real();
1257
if ((null_value= args[0]->null_value || args[1]->null_value))
1261
signal_divide_by_null();
1264
return fix_result(value/val2);
1268
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
1270
my_decimal value1, *val1;
1271
my_decimal value2, *val2;
1274
val1= args[0]->val_decimal(&value1);
1275
if ((null_value= args[0]->null_value))
1277
val2= args[1]->val_decimal(&value2);
1278
if ((null_value= args[1]->null_value))
1280
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1281
val1, val2, prec_increment)) > 3)
1283
if (err == E_DEC_DIV_ZERO)
1284
signal_divide_by_null();
1288
return decimal_value;
1292
void Item_func_div::result_precision()
1294
uint precision=min(args[0]->decimal_precision() + prec_increment,
1295
DECIMAL_MAX_PRECISION);
1296
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
1297
if (result_type() == INT_RESULT)
1298
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
1300
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
1301
decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
1302
max_length= my_decimal_precision_to_length(precision, decimals,
1307
void Item_func_div::fix_length_and_dec()
1309
prec_increment= current_thd->variables.div_precincrement;
1310
Item_num_op::fix_length_and_dec();
1311
switch(hybrid_type) {
1314
decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1315
set_if_smaller(decimals, NOT_FIXED_DEC);
1316
max_length=args[0]->max_length - args[0]->decimals + decimals;
1317
uint tmp=float_length(decimals);
1318
set_if_smaller(max_length,tmp);
1322
hybrid_type= DECIMAL_RESULT;
1325
case DECIMAL_RESULT:
1331
maybe_null= 1; // devision by zero
1336
/* Integer division */
1337
int64_t Item_func_int_div::val_int()
1340
int64_t value=args[0]->val_int();
1341
int64_t val2=args[1]->val_int();
1342
if ((null_value= (args[0]->null_value || args[1]->null_value)))
1346
signal_divide_by_null();
1349
return (unsigned_flag ?
1350
(uint64_t) value / (uint64_t) val2 :
1355
void Item_func_int_div::fix_length_and_dec()
1357
Item_result argtype= args[0]->result_type();
1358
/* use precision ony for the data type it is applicable for and valid */
1359
max_length=args[0]->max_length -
1360
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
1361
args[0]->decimals : 0);
1363
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
1367
int64_t Item_func_mod::int_op()
1370
int64_t value= args[0]->val_int();
1371
int64_t val2= args[1]->val_int();
1374
if ((null_value= args[0]->null_value || args[1]->null_value))
1375
return 0; /* purecov: inspected */
1378
signal_divide_by_null();
1382
if (args[0]->unsigned_flag)
1383
result= args[1]->unsigned_flag ?
1384
((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
1386
result= args[1]->unsigned_flag ?
1387
value % ((uint64_t) val2) : value % val2;
1392
double Item_func_mod::real_op()
1395
double value= args[0]->val_real();
1396
double val2= args[1]->val_real();
1397
if ((null_value= args[0]->null_value || args[1]->null_value))
1398
return 0.0; /* purecov: inspected */
1401
signal_divide_by_null();
1404
return fmod(value,val2);
1408
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
1410
my_decimal value1, *val1;
1411
my_decimal value2, *val2;
1413
val1= args[0]->val_decimal(&value1);
1414
if ((null_value= args[0]->null_value))
1416
val2= args[1]->val_decimal(&value2);
1417
if ((null_value= args[1]->null_value))
1419
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
1421
case E_DEC_TRUNCATED:
1423
return decimal_value;
1424
case E_DEC_DIV_ZERO:
1425
signal_divide_by_null();
1433
void Item_func_mod::result_precision()
1435
decimals= max(args[0]->decimals, args[1]->decimals);
1436
max_length= max(args[0]->max_length, args[1]->max_length);
1440
void Item_func_mod::fix_length_and_dec()
1442
Item_num_op::fix_length_and_dec();
1444
unsigned_flag= args[0]->unsigned_flag;
1448
double Item_func_neg::real_op()
1450
double value= args[0]->val_real();
1451
null_value= args[0]->null_value;
1456
int64_t Item_func_neg::int_op()
1458
int64_t value= args[0]->val_int();
1459
null_value= args[0]->null_value;
1464
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
1466
my_decimal val, *value= args[0]->val_decimal(&val);
1467
if (!(null_value= args[0]->null_value))
1469
my_decimal2decimal(value, decimal_value);
1470
my_decimal_neg(decimal_value);
1471
return decimal_value;
1477
void Item_func_neg::fix_num_length_and_dec()
1479
decimals= args[0]->decimals;
1480
/* 1 add because sign can appear */
1481
max_length= args[0]->max_length + 1;
1485
void Item_func_neg::fix_length_and_dec()
1487
Item_func_num1::fix_length_and_dec();
1490
If this is in integer context keep the context as integer if possible
1491
(This is how multiplication and other integer functions works)
1492
Use val() to get value as arg_type doesn't mean that item is
1493
Item_int or Item_real due to existence of Item_param.
1495
if (hybrid_type == INT_RESULT && args[0]->const_item())
1497
int64_t val= args[0]->val_int();
1498
if ((uint64_t) val >= (uint64_t) INT64_MIN &&
1499
((uint64_t) val != (uint64_t) INT64_MIN ||
1500
args[0]->type() != INT_ITEM))
1503
Ensure that result is converted to DECIMAL, as int64_t can't hold
1506
hybrid_type= DECIMAL_RESULT;
1514
double Item_func_abs::real_op()
1516
double value= args[0]->val_real();
1517
null_value= args[0]->null_value;
1522
int64_t Item_func_abs::int_op()
1524
int64_t value= args[0]->val_int();
1525
if ((null_value= args[0]->null_value))
1527
return (value >= 0) || unsigned_flag ? value : -value;
1531
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
1533
my_decimal val, *value= args[0]->val_decimal(&val);
1534
if (!(null_value= args[0]->null_value))
1536
my_decimal2decimal(value, decimal_value);
1537
if (decimal_value->sign())
1538
my_decimal_neg(decimal_value);
1539
return decimal_value;
1545
void Item_func_abs::fix_length_and_dec()
1547
Item_func_num1::fix_length_and_dec();
1548
unsigned_flag= args[0]->unsigned_flag;
1552
/** Gateway to natural LOG function. */
1553
double Item_func_ln::val_real()
1556
double value= args[0]->val_real();
1557
if ((null_value= args[0]->null_value))
1561
signal_divide_by_null();
1568
Extended but so slower LOG function.
1570
We have to check if all values are > zero and first one is not one
1571
as these are the cases then result is not a number.
1573
double Item_func_log::val_real()
1576
double value= args[0]->val_real();
1577
if ((null_value= args[0]->null_value))
1581
signal_divide_by_null();
1586
double value2= args[1]->val_real();
1587
if ((null_value= args[1]->null_value))
1589
if (value2 <= 0.0 || value == 1.0)
1591
signal_divide_by_null();
1594
return log(value2) / log(value);
1599
double Item_func_log2::val_real()
1602
double value= args[0]->val_real();
1604
if ((null_value=args[0]->null_value))
1608
signal_divide_by_null();
1611
return log(value) / M_LN2;
1614
double Item_func_log10::val_real()
1617
double value= args[0]->val_real();
1618
if ((null_value= args[0]->null_value))
1622
signal_divide_by_null();
1625
return log10(value);
1628
double Item_func_exp::val_real()
1631
double value= args[0]->val_real();
1632
if ((null_value=args[0]->null_value))
1633
return 0.0; /* purecov: inspected */
1634
return fix_result(exp(value));
1637
double Item_func_sqrt::val_real()
1640
double value= args[0]->val_real();
1641
if ((null_value=(args[0]->null_value || value < 0)))
1642
return 0.0; /* purecov: inspected */
1646
double Item_func_pow::val_real()
1649
double value= args[0]->val_real();
1650
double val2= args[1]->val_real();
1651
if ((null_value=(args[0]->null_value || args[1]->null_value)))
1652
return 0.0; /* purecov: inspected */
1653
return fix_result(pow(value,val2));
1656
// Trigonometric functions
1658
double Item_func_acos::val_real()
1661
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1662
volatile double value= args[0]->val_real();
1663
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1668
double Item_func_asin::val_real()
1671
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
1672
volatile double value= args[0]->val_real();
1673
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
1678
double Item_func_atan::val_real()
1681
double value= args[0]->val_real();
1682
if ((null_value=args[0]->null_value))
1686
double val2= args[1]->val_real();
1687
if ((null_value=args[1]->null_value))
1689
return fix_result(atan2(value,val2));
1694
double Item_func_cos::val_real()
1697
double value= args[0]->val_real();
1698
if ((null_value=args[0]->null_value))
1703
double Item_func_sin::val_real()
1706
double value= args[0]->val_real();
1707
if ((null_value=args[0]->null_value))
1712
double Item_func_tan::val_real()
1715
double value= args[0]->val_real();
1716
if ((null_value=args[0]->null_value))
1718
return fix_result(tan(value));
153
1722
// Shift-functions, same as << and >> in C/C++
155
1725
int64_t Item_func_shift_left::val_int()
157
1727
assert(fixed == 1);
159
1729
uint64_t res= ((uint64_t) args[0]->val_int() <<
160
1730
(shift=(uint) args[1]->val_int()));
161
1731
if (args[0]->null_value || args[1]->null_value)
198
1768
void Item_func_integer::fix_length_and_dec()
200
1770
max_length=args[0]->max_length - args[0]->decimals+1;
201
uint32_t tmp=float_length(decimals);
1771
uint tmp=float_length(decimals);
202
1772
set_if_smaller(max_length,tmp);
1776
void Item_func_int_val::fix_num_length_and_dec()
1778
max_length= args[0]->max_length - (args[0]->decimals ?
1779
args[0]->decimals + 1 :
1781
uint tmp= float_length(decimals);
1782
set_if_smaller(max_length,tmp);
1787
void Item_func_int_val::find_num_type()
1789
switch(hybrid_type= args[0]->result_type())
1793
hybrid_type= REAL_RESULT;
1794
max_length= float_length(decimals);
1797
case DECIMAL_RESULT:
1799
-2 because in most high position can't be used any digit for int64_t
1800
and one position for increasing value during operation
1802
if ((args[0]->max_length - args[0]->decimals) >=
1803
(DECIMAL_LONGLONG_DIGITS - 2))
1805
hybrid_type= DECIMAL_RESULT;
1809
unsigned_flag= args[0]->unsigned_flag;
1810
hybrid_type= INT_RESULT;
1820
int64_t Item_func_ceiling::int_op()
1823
switch (args[0]->result_type()) {
1825
result= args[0]->val_int();
1826
null_value= args[0]->null_value;
1828
case DECIMAL_RESULT:
1830
my_decimal dec_buf, *dec;
1831
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
1832
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1838
result= (int64_t)Item_func_ceiling::real_op();
1844
double Item_func_ceiling::real_op()
1847
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1850
volatile double value= args[0]->val_real();
1851
null_value= args[0]->null_value;
1856
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
1858
my_decimal val, *value= args[0]->val_decimal(&val);
1859
if (!(null_value= (args[0]->null_value ||
1860
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
1861
decimal_value) > 1)))
1862
return decimal_value;
1867
int64_t Item_func_floor::int_op()
1870
switch (args[0]->result_type()) {
1872
result= args[0]->val_int();
1873
null_value= args[0]->null_value;
1875
case DECIMAL_RESULT:
1877
my_decimal dec_buf, *dec;
1878
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
1879
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
1885
result= (int64_t)Item_func_floor::real_op();
1891
double Item_func_floor::real_op()
1894
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
1897
volatile double value= args[0]->val_real();
1898
null_value= args[0]->null_value;
1899
return floor(value);
1903
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
1905
my_decimal val, *value= args[0]->val_decimal(&val);
1906
if (!(null_value= (args[0]->null_value ||
1907
my_decimal_floor(E_DEC_FATAL_ERROR, value,
1908
decimal_value) > 1)))
1909
return decimal_value;
1914
void Item_func_round::fix_length_and_dec()
1916
int decimals_to_set;
1920
unsigned_flag= args[0]->unsigned_flag;
1921
if (!args[1]->const_item())
1923
max_length= args[0]->max_length;
1924
decimals= args[0]->decimals;
1925
if (args[0]->result_type() == DECIMAL_RESULT)
1928
hybrid_type= DECIMAL_RESULT;
1931
hybrid_type= REAL_RESULT;
1935
val1= args[1]->val_int();
1936
val1_unsigned= args[1]->unsigned_flag;
1938
decimals_to_set= val1_unsigned ? INT_MAX : 0;
1940
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
1942
if (args[0]->decimals == NOT_FIXED_DEC)
1944
max_length= args[0]->max_length;
1945
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1946
hybrid_type= REAL_RESULT;
1950
switch (args[0]->result_type()) {
1953
hybrid_type= REAL_RESULT;
1954
decimals= min(decimals_to_set, NOT_FIXED_DEC);
1955
max_length= float_length(decimals);
1958
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
1960
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
1961
max_length= args[0]->max_length + length_can_increase;
1962
/* Here we can keep INT_RESULT */
1963
hybrid_type= INT_RESULT;
1968
case DECIMAL_RESULT:
1970
hybrid_type= DECIMAL_RESULT;
1971
decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
1972
int decimals_delta= args[0]->decimals - decimals_to_set;
1973
int precision= args[0]->decimal_precision();
1974
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
1976
precision-= decimals_delta - length_increase;
1977
decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
1978
max_length= my_decimal_precision_to_length(precision, decimals,
1983
assert(0); /* This result type isn't handled */
1987
double my_double_round(double value, int64_t dec, bool dec_unsigned,
1991
bool dec_negative= (dec < 0) && !dec_unsigned;
1992
uint64_t abs_dec= dec_negative ? -dec : dec;
1994
tmp2 is here to avoid return the value with 80 bit precision
1995
This will fix that the test round(0.1,1) = round(0.1,1) is true
1997
volatile double tmp2;
1999
tmp=(abs_dec < array_elements(log_10) ?
2000
log_10[abs_dec] : pow(10.0,(double) abs_dec));
2002
if (dec_negative && my_isinf(tmp))
2004
else if (!dec_negative && my_isinf(value * tmp))
2009
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
2011
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
2014
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
2019
double Item_func_round::real_op()
2021
double value= args[0]->val_real();
2023
if (!(null_value= args[0]->null_value || args[1]->null_value))
2024
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
2031
Rounds a given value to a power of 10 specified as the 'to' argument,
2032
avoiding overflows when the value is close to the uint64_t range boundary.
2035
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
2037
uint64_t tmp= value / to * to;
2038
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
2042
int64_t Item_func_round::int_op()
2044
int64_t value= args[0]->val_int();
2045
int64_t dec= args[1]->val_int();
2048
if ((null_value= args[0]->null_value || args[1]->null_value))
2050
if ((dec >= 0) || args[1]->unsigned_flag)
2051
return value; // integer have not digits after point
2056
if(abs_dec >= array_elements(log_10_int))
2059
tmp= log_10_int[abs_dec];
2062
value= (unsigned_flag) ?
2063
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
2065
value= (unsigned_flag || value >= 0) ?
2066
my_unsigned_round((uint64_t) value, tmp) :
2067
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
2072
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
2074
my_decimal val, *value= args[0]->val_decimal(&val);
2075
int64_t dec= args[1]->val_int();
2076
if (dec >= 0 || args[1]->unsigned_flag)
2077
dec= min((uint64_t) dec, decimals);
2078
else if (dec < INT_MIN)
2081
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2082
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2083
truncate, decimal_value) > 1)))
2085
decimal_value->frac= decimals;
2086
return decimal_value;
2092
void Item_func_rand::seed_random(Item *arg)
2095
TODO: do not do reinit 'rand' for every execute of PS/SP if
2096
args[0] is a constant.
2098
uint32 tmp= (uint32) arg->val_int();
2099
randominit(rand, (uint32) (tmp*0x10001L+55555555L),
2100
(uint32) (tmp*0x10000001L));
2104
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
2106
if (Item_real_func::fix_fields(thd, ref))
2108
used_tables_cache|= RAND_TABLE_BIT;
2110
{ // Only use argument once in query
2112
Allocate rand structure once: we must use thd->stmt_arena
2113
to create rand in proper mem_root if it's a prepared statement or
2116
No need to send a Rand log event if seed was given eg: RAND(seed),
2117
as it will be replicated in the query as such.
2119
if (!rand && !(rand= (struct rand_struct*)
2120
thd->stmt_arena->alloc(sizeof(*rand))))
2123
if (args[0]->const_item())
2124
seed_random (args[0]);
2129
Save the seed only the first time RAND() is used in the query
2130
Once events are forwarded rather than recreated,
2131
the following can be skipped if inside the slave thread
2133
if (!thd->rand_used)
2136
thd->rand_saved_seed1= thd->rand.seed1;
2137
thd->rand_saved_seed2= thd->rand.seed2;
2144
void Item_func_rand::update_used_tables()
2146
Item_real_func::update_used_tables();
2147
used_tables_cache|= RAND_TABLE_BIT;
2151
double Item_func_rand::val_real()
2154
if (arg_count && !args[0]->const_item())
2155
seed_random (args[0]);
2156
return my_rnd(rand);
2159
int64_t Item_func_sign::val_int()
2162
double value= args[0]->val_real();
2163
null_value=args[0]->null_value;
2164
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
206
2168
double Item_func_units::val_real()
208
2170
assert(fixed == 1);
495
2741
return (int64_t) my_count_bits(value);
2745
/****************************************************************************
2746
** Functions to handle dynamic loadable functions
2747
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2748
** Rewritten by monty.
2749
****************************************************************************/
2751
void udf_handler::cleanup()
2757
if (u_d->func_deinit != NULL)
2759
Udf_func_deinit deinit= u_d->func_deinit;
2765
if (buffers) // Because of bug in ecc
2773
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2774
uint arg_count, Item **arguments)
2776
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2778
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2779
return(true); // Fatal error flag is set!
2781
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2785
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2791
/* Fix all arguments */
2793
used_tables_cache=0;
2796
if ((f_args.arg_count=arg_count))
2798
if (!(f_args.arg_type= (Item_result*)
2799
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2805
Item **arg,**arg_end;
2806
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2810
if (!(*arg)->fixed &&
2811
(*arg)->fix_fields(thd, arg))
2813
// we can't assign 'item' before, because fix_fields() can change arg
2815
if (item->check_cols(1))
2818
TODO: We should think about this. It is not always
2819
right way just to set an UDF result to return my_charset_bin
2820
if one argument has binary sorting order.
2821
The result collation should be calculated according to arguments
2822
derivations in some cases and should not in other cases.
2823
Moreover, some arguments can represent a numeric input
2824
which doesn't effect the result character set and collation.
2825
There is no a general rule for UDF. Everything depends on
2826
the particular user defined function.
2828
if (item->collation.collation->state & MY_CS_BINSORT)
2829
func->collation.set(&my_charset_bin);
2830
if (item->maybe_null)
2832
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2833
used_tables_cache|=item->used_tables();
2834
const_item_cache&=item->const_item();
2835
f_args.arg_type[i]=item->result_type();
2837
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2838
if (!(buffers=new String[arg_count]) ||
2839
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2840
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2841
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2842
!(num_buffer= (char*) sql_alloc(arg_count *
2843
ALIGN_SIZE(sizeof(double)))) ||
2844
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2845
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2851
func->fix_length_and_dec();
2852
initid.max_length=func->max_length;
2853
initid.maybe_null=func->maybe_null;
2854
initid.const_item=const_item_cache;
2855
initid.decimals=func->decimals;
2860
char init_msg_buff[MYSQL_ERRMSG_SIZE];
2861
char *to=num_buffer;
2862
for (uint i=0; i < arg_count; i++)
2865
For a constant argument i, args->args[i] points to the argument value.
2866
For non-constant, args->args[i] is NULL.
2868
f_args.args[i]= NULL; /* Non-const unless updated below. */
2870
f_args.lengths[i]= arguments[i]->max_length;
2871
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2872
f_args.attributes[i]= arguments[i]->name;
2873
f_args.attribute_lengths[i]= arguments[i]->name_length;
2875
if (arguments[i]->const_item())
2877
switch (arguments[i]->result_type())
2880
case DECIMAL_RESULT:
2882
String *res= arguments[i]->val_str(&buffers[i]);
2883
if (arguments[i]->null_value)
2885
f_args.args[i]= (char*) res->c_ptr();
2886
f_args.lengths[i]= res->length();
2890
*((int64_t*) to)= arguments[i]->val_int();
2891
if (arguments[i]->null_value)
2894
to+= ALIGN_SIZE(sizeof(int64_t));
2897
*((double*) to)= arguments[i]->val_real();
2898
if (arguments[i]->null_value)
2901
to+= ALIGN_SIZE(sizeof(double));
2905
// This case should never be chosen
2911
Udf_func_init init= u_d->func_init;
2912
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2914
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2915
u_d->name.str, init_msg_buff);
2918
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2919
func->maybe_null=initid.maybe_null;
2920
const_item_cache=initid.const_item;
2922
Keep used_tables_cache in sync with const_item_cache.
2923
See the comment in Item_udf_func::update_used tables.
2925
if (!const_item_cache && !used_tables_cache)
2926
used_tables_cache= RAND_TABLE_BIT;
2927
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2932
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2933
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2940
bool udf_handler::get_arguments()
2943
return 1; // Got an error earlier
2944
char *to= num_buffer;
2946
for (uint i=0; i < f_args.arg_count; i++)
2949
switch (f_args.arg_type[i]) {
2951
case DECIMAL_RESULT:
2953
String *res=args[i]->val_str(&buffers[str_count++]);
2954
if (!(args[i]->null_value))
2956
f_args.args[i]= (char*) res->ptr();
2957
f_args.lengths[i]= res->length();
2962
*((int64_t*) to) = args[i]->val_int();
2963
if (!args[i]->null_value)
2966
to+= ALIGN_SIZE(sizeof(int64_t));
2970
*((double*) to)= args[i]->val_real();
2971
if (!args[i]->null_value)
2974
to+= ALIGN_SIZE(sizeof(double));
2979
// This case should never be chosen
2989
(String*)NULL in case of NULL values
2991
String *udf_handler::val_str(String *str,String *save_str)
2993
uchar is_null_tmp=0;
2996
if (get_arguments())
2998
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2999
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3002
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
3003
{ // This happens VERY seldom
3004
if (str->alloc(MAX_FIELD_WIDTH))
3010
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3011
&is_null_tmp, &error);
3012
if (is_null_tmp || !res || error) // The !res is for safety
3016
if (res == str->ptr())
3018
str->length(res_length);
3021
save_str->set(res, res_length, str->charset());
3027
For the moment, UDF functions are returning DECIMAL values as strings
3030
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3032
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3033
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3035
if (get_arguments())
3040
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3041
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3044
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3045
if (is_null || error)
3050
end= res+ res_length;
3051
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3056
void Item_udf_func::cleanup()
3059
Item_func::cleanup();
3063
void Item_udf_func::print(String *str, enum_query_type query_type)
3065
str->append(func_name());
3067
for (uint i=0 ; i < arg_count ; i++)
3071
args[i]->print_item_w_name(str, query_type);
3077
double Item_func_udf_float::val_real()
3080
return(udf.val(&null_value));
3084
String *Item_func_udf_float::val_str(String *str)
3087
double nr= val_real();
3089
return 0; /* purecov: inspected */
3090
str->set_real(nr,decimals,&my_charset_bin);
3095
int64_t Item_func_udf_int::val_int()
3098
return(udf.val_int(&null_value));
3102
String *Item_func_udf_int::val_str(String *str)
3105
int64_t nr=val_int();
3108
str->set_int(nr, unsigned_flag, &my_charset_bin);
3113
int64_t Item_func_udf_decimal::val_int()
3115
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3119
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3124
double Item_func_udf_decimal::val_real()
3126
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3130
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3135
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3138
return(udf.val_decimal(&null_value, dec_buf));
3142
String *Item_func_udf_decimal::val_str(String *str)
3144
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3147
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3148
str->length(DECIMAL_MAX_STR_LENGTH);
3149
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3150
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3155
void Item_func_udf_decimal::fix_length_and_dec()
3157
fix_num_length_and_dec();
3161
/* Default max_length is max argument length */
3163
void Item_func_udf_str::fix_length_and_dec()
3166
for (uint i = 0; i < arg_count; i++)
3167
set_if_bigger(max_length,args[i]->max_length);
3171
String *Item_func_udf_str::val_str(String *str)
3174
String *res=udf.val_str(str,&str_value);
3182
This has to come last in the udf_handler methods, or C for AIX
3183
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3186
udf_handler::~udf_handler()
3188
/* Everything should be properly cleaned up by this moment. */
3189
assert(not_original || !(initialized || buffers));
499
3193
** User level locks