206
int64_t Item_func_floor::int_op()
209
switch (args[0]->result_type()) {
211
result= args[0]->val_int();
212
null_value= args[0]->null_value;
216
my_decimal dec_buf, *dec;
217
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
218
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
224
result= (int64_t)Item_func_floor::real_op();
230
double Item_func_floor::real_op()
233
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
236
volatile double value= args[0]->val_real();
237
null_value= args[0]->null_value;
242
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
244
my_decimal val, *value= args[0]->val_decimal(&val);
245
if (!(null_value= (args[0]->null_value ||
246
my_decimal_floor(E_DEC_FATAL_ERROR, value,
247
decimal_value) > 1)))
248
return decimal_value;
253
void Item_func_round::fix_length_and_dec()
259
unsigned_flag= args[0]->unsigned_flag;
260
if (!args[1]->const_item())
262
max_length= args[0]->max_length;
263
decimals= args[0]->decimals;
264
if (args[0]->result_type() == DECIMAL_RESULT)
267
hybrid_type= DECIMAL_RESULT;
270
hybrid_type= REAL_RESULT;
274
val1= args[1]->val_int();
275
val1_unsigned= args[1]->unsigned_flag;
277
decimals_to_set= val1_unsigned ? INT_MAX : 0;
279
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
281
if (args[0]->decimals == NOT_FIXED_DEC)
283
max_length= args[0]->max_length;
284
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
285
hybrid_type= REAL_RESULT;
289
switch (args[0]->result_type()) {
292
hybrid_type= REAL_RESULT;
293
decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
294
max_length= float_length(decimals);
297
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
299
int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
300
max_length= args[0]->max_length + length_can_increase;
301
/* Here we can keep INT_RESULT */
302
hybrid_type= INT_RESULT;
309
hybrid_type= DECIMAL_RESULT;
310
decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
311
int decimals_delta= args[0]->decimals - decimals_to_set;
312
int precision= args[0]->decimal_precision();
313
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
315
precision-= decimals_delta - length_increase;
316
decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
317
max_length= my_decimal_precision_to_length(precision, decimals,
322
assert(0); /* This result type isn't handled */
326
double my_double_round(double value, int64_t dec, bool dec_unsigned,
330
bool dec_negative= (dec < 0) && !dec_unsigned;
331
uint64_t abs_dec= dec_negative ? -dec : dec;
333
tmp2 is here to avoid return the value with 80 bit precision
334
This will fix that the test round(0.1,1) = round(0.1,1) is true
336
volatile double tmp2;
338
tmp=(abs_dec < array_elements(log_10) ?
339
log_10[abs_dec] : pow(10.0,(double) abs_dec));
341
if (dec_negative && std::isinf(tmp))
343
else if (!dec_negative && std::isinf(value * tmp))
348
tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
350
tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
353
tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
358
double Item_func_round::real_op()
360
double value= args[0]->val_real();
362
if (!(null_value= args[0]->null_value || args[1]->null_value))
363
return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
370
Rounds a given value to a power of 10 specified as the 'to' argument,
371
avoiding overflows when the value is close to the uint64_t range boundary.
374
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
376
uint64_t tmp= value / to * to;
377
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
381
int64_t Item_func_round::int_op()
383
int64_t value= args[0]->val_int();
384
int64_t dec= args[1]->val_int();
387
if ((null_value= args[0]->null_value || args[1]->null_value))
389
if ((dec >= 0) || args[1]->unsigned_flag)
390
return value; // integer have not digits after point
395
if(abs_dec >= array_elements(log_10_int))
398
tmp= log_10_int[abs_dec];
401
value= (unsigned_flag) ?
402
((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
404
value= (unsigned_flag || value >= 0) ?
405
my_unsigned_round((uint64_t) value, tmp) :
406
-(int64_t) my_unsigned_round((uint64_t) -value, tmp);
411
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
413
my_decimal val, *value= args[0]->val_decimal(&val);
414
int64_t dec= args[1]->val_int();
415
if (dec >= 0 || args[1]->unsigned_flag)
416
dec= cmin(dec, (int64_t) decimals);
417
else if (dec < INT_MIN)
420
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
421
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
422
truncate, decimal_value) > 1)))
424
decimal_value->frac= decimals;
425
return decimal_value;
431
void Item_func_rand::seed_random(Item *arg)
434
TODO: do not do reinit 'rand' for every execute of PS/SP if
435
args[0] is a constant.
437
uint32_t tmp= (uint32_t) arg->val_int();
438
randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
439
(uint32_t) (tmp*0x10000001L));
443
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
445
if (Item_real_func::fix_fields(thd, ref))
447
used_tables_cache|= RAND_TABLE_BIT;
449
{ // Only use argument once in query
451
No need to send a Rand log event if seed was given eg: RAND(seed),
452
as it will be replicated in the query as such.
454
if (!rand && !(rand= (struct rand_struct*)
455
thd->alloc(sizeof(*rand))))
458
if (args[0]->const_item())
459
seed_random (args[0]);
464
Save the seed only the first time RAND() is used in the query
465
Once events are forwarded rather than recreated,
466
the following can be skipped if inside the slave thread
471
thd->rand_saved_seed1= thd->rand.seed1;
472
thd->rand_saved_seed2= thd->rand.seed2;
479
void Item_func_rand::update_used_tables()
481
Item_real_func::update_used_tables();
482
used_tables_cache|= RAND_TABLE_BIT;
486
double Item_func_rand::val_real()
489
if (arg_count && !args[0]->const_item())
490
seed_random (args[0]);
494
int64_t Item_func_sign::val_int()
497
double value= args[0]->val_real();
498
null_value=args[0]->null_value;
499
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
503
206
double Item_func_units::val_real()
505
208
assert(fixed == 1);
513
void Item_func_min_max::fix_length_and_dec()
516
bool datetime_found= false;
520
cmp_type=args[0]->result_type();
522
for (uint32_t i=0 ; i < arg_count ; i++)
524
set_if_bigger(max_length, args[i]->max_length);
525
set_if_bigger(decimals, args[i]->decimals);
526
set_if_bigger(max_int_part, args[i]->decimal_int_part());
527
if (args[i]->maybe_null)
529
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
530
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
532
datetime_found= true;
533
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
534
datetime_item= args[i];
537
if (cmp_type == STRING_RESULT)
539
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
543
compare_as_dates= true;
546
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
547
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
549
cached_field_type= agg_field_type(args, arg_count);
554
Compare item arguments in the DATETIME context.
558
value [out] found least/greatest DATE/DATETIME value
561
Compare item arguments as DATETIME values and return the index of the
562
least/greatest argument in the arguments array.
563
The correct integer DATE/DATETIME value of the found argument is
564
stored to the value pointer, if latter is provided.
567
0 If one of arguments is NULL
568
# index of the least/greatest argument
571
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
574
uint32_t min_max_idx= 0;
576
for (uint32_t i=0; i < arg_count ; i++)
578
Item **arg= args + i;
580
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
581
if ((null_value= args[i]->null_value))
583
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
592
if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
599
String *Item_func_min_max::val_str(String *str)
602
if (compare_as_dates)
605
uint32_t min_max_idx= cmp_datetimes(NULL);
608
str_res= args[min_max_idx]->val_str(str);
609
str_res->set_charset(collation.collation);
615
int64_t nr=val_int();
618
str->set_int(nr, unsigned_flag, &my_charset_bin);
623
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
626
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
631
double nr= val_real();
633
return 0; /* purecov: inspected */
634
str->set_real(nr,decimals,&my_charset_bin);
641
for (uint32_t i=0; i < arg_count ; i++)
644
res=args[i]->val_str(str);
648
res2= args[i]->val_str(res == str ? &tmp_value : str);
651
int cmp= sortcmp(res,res2,collation.collation);
652
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
656
if ((null_value= args[i]->null_value))
659
res->set_charset(collation.collation);
664
// This case should never be chosen
668
return 0; // Keep compiler happy
672
double Item_func_min_max::val_real()
676
if (compare_as_dates)
679
(void)cmp_datetimes(&result);
680
return (double)result;
682
for (uint32_t i=0; i < arg_count ; i++)
685
value= args[i]->val_real();
688
double tmp= args[i]->val_real();
689
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
692
if ((null_value= args[i]->null_value))
699
int64_t Item_func_min_max::val_int()
703
if (compare_as_dates)
706
(void)cmp_datetimes(&result);
707
return (int64_t)result;
709
for (uint32_t i=0; i < arg_count ; i++)
712
value=args[i]->val_int();
715
int64_t tmp=args[i]->val_int();
716
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
719
if ((null_value= args[i]->null_value))
726
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
729
my_decimal tmp_buf, *tmp, *res= NULL;
731
if (compare_as_dates)
734
(void)cmp_datetimes(&value);
735
uint64_t2decimal(value, dec);
738
for (uint32_t i=0; i < arg_count ; i++)
741
res= args[i]->val_decimal(dec);
744
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
745
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
749
/* Move value out of tmp_buf as this will be reused on next loop */
750
my_decimal2decimal(tmp, dec);
757
if ((null_value= args[i]->null_value))
767
int64_t Item_func_length::val_int()
770
String *res=args[0]->val_str(&value);
774
return 0; /* purecov: inspected */
777
return (int64_t) res->length();
781
216
int64_t Item_func_char_length::val_int()
783
218
assert(fixed == 1);