~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

  • Committer: Monty Taylor
  • Date: 2008-10-13 09:29:43 UTC
  • mfrom: (509 drizzle)
  • mto: (509.1.4 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081013092943-rwvx4a6d85b5l2dh
MergedĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
150
150
  return copy_or_same(thd);
151
151
}
152
152
 
 
153
 
 
154
 
 
155
void Item_func_connection_id::fix_length_and_dec()
 
156
{
 
157
  Item_int_func::fix_length_and_dec();
 
158
  max_length= 10;
 
159
}
 
160
 
 
161
 
 
162
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
 
163
{
 
164
  if (Item_int_func::fix_fields(thd, ref))
 
165
    return true;
 
166
  thd->thread_specific_used= true;
 
167
  value= thd->variables.pseudo_thread_id;
 
168
  return false;
 
169
}
 
170
 
 
171
 
 
172
/**
 
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)
 
176
*/
 
177
 
 
178
void Item_func_num1::find_num_type()
 
179
{
 
180
  switch (hybrid_type= args[0]->result_type()) {
 
181
  case INT_RESULT:
 
182
    unsigned_flag= args[0]->unsigned_flag;
 
183
    break;
 
184
  case STRING_RESULT:
 
185
  case REAL_RESULT:
 
186
    hybrid_type= REAL_RESULT;
 
187
    max_length= float_length(decimals);
 
188
    break;
 
189
  case DECIMAL_RESULT:
 
190
    break;
 
191
  default:
 
192
    assert(0);
 
193
  }
 
194
  return;
 
195
}
 
196
 
 
197
 
 
198
void Item_func_num1::fix_num_length_and_dec()
 
199
{
 
200
  decimals= args[0]->decimals;
 
201
  max_length= args[0]->max_length;
 
202
}
 
203
 
 
204
 
 
205
void Item_func_signed::print(String *str, enum_query_type query_type)
 
206
{
 
207
  str->append(STRING_WITH_LEN("cast("));
 
208
  args[0]->print(str, query_type);
 
209
  str->append(STRING_WITH_LEN(" as signed)"));
 
210
 
 
211
}
 
212
 
 
213
int64_t Item_func_signed::val_int_from_str(int *error)
 
214
{
 
215
  char buff[MAX_FIELD_WIDTH], *end, *start;
 
216
  uint32_t length;
 
217
  String tmp(buff,sizeof(buff), &my_charset_bin), *res;
 
218
  int64_t value;
 
219
 
 
220
  /*
 
221
    For a string result, we must first get the string and then convert it
 
222
    to a int64_t
 
223
  */
 
224
 
 
225
  if (!(res= args[0]->val_str(&tmp)))
 
226
  {
 
227
    null_value= 1;
 
228
    *error= 0;
 
229
    return 0;
 
230
  }
 
231
  null_value= 0;
 
232
  start= (char *)res->ptr();
 
233
  length= res->length();
 
234
 
 
235
  end= start + length;
 
236
  value= my_strtoll10(start, &end, error);
 
237
  if (*error > 0 || end != start+ length)
 
238
  {
 
239
    char err_buff[128];
 
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",
 
245
                        err_tmp.c_ptr());
 
246
  }
 
247
  return value;
 
248
}
 
249
 
 
250
 
 
251
int64_t Item_func_signed::val_int()
 
252
{
 
253
  int64_t value;
 
254
  int error;
 
255
 
 
256
  if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
257
      args[0]->result_as_int64_t())
 
258
  {
 
259
    value= args[0]->val_int();
 
260
    null_value= args[0]->null_value; 
 
261
    return value;
 
262
  }
 
263
 
 
264
  value= val_int_from_str(&error);
 
265
  if (value < 0 && error == 0)
 
266
  {
 
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");
 
270
  }
 
271
  return value;
 
272
}
 
273
 
 
274
 
 
275
void Item_func_unsigned::print(String *str, enum_query_type query_type)
 
276
{
 
277
  str->append(STRING_WITH_LEN("cast("));
 
278
  args[0]->print(str, query_type);
 
279
  str->append(STRING_WITH_LEN(" as unsigned)"));
 
280
 
 
281
}
 
282
 
 
283
 
 
284
int64_t Item_func_unsigned::val_int()
 
285
{
 
286
  int64_t value;
 
287
  int error;
 
288
 
 
289
  if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
 
290
  {
 
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);
 
294
    else
 
295
      value= 0;
 
296
    return value;
 
297
  }
 
298
  else if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
299
           args[0]->result_as_int64_t())
 
300
  {
 
301
    value= args[0]->val_int();
 
302
    null_value= args[0]->null_value; 
 
303
    return value;
 
304
  }
 
305
 
 
306
  value= val_int_from_str(&error);
 
307
  if (error < 0)
 
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");
 
311
  return value;
 
312
}
 
313
 
 
314
 
 
315
String *Item_decimal_typecast::val_str(String *str)
 
316
{
 
317
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
318
  if (null_value)
 
319
    return NULL;
 
320
  my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
 
321
  return str;
 
322
}
 
323
 
 
324
 
 
325
double Item_decimal_typecast::val_real()
 
326
{
 
327
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
328
  double res;
 
329
  if (null_value)
 
330
    return 0.0;
 
331
  my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
 
332
  return res;
 
333
}
 
334
 
 
335
 
 
336
int64_t Item_decimal_typecast::val_int()
 
337
{
 
338
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
339
  int64_t res;
 
340
  if (null_value)
 
341
    return 0;
 
342
  my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
 
343
  return res;
 
344
}
 
345
 
 
346
 
 
347
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
 
348
{
 
349
  my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
 
350
  bool sign;
 
351
  uint32_t precision;
 
352
 
 
353
  if ((null_value= args[0]->null_value))
 
354
    return NULL;
 
355
  my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
 
356
  sign= dec->sign();
 
357
  precision= my_decimal_length_to_precision(max_length,
 
358
                                            decimals, unsigned_flag);
 
359
  if (precision - decimals < (uint) my_decimal_intg(dec))
 
360
  {
 
361
    max_my_decimal(dec, precision, decimals);
 
362
    dec->sign(sign);
 
363
    goto err;
 
364
  }
 
365
  return dec;
 
366
 
 
367
err:
 
368
  push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
369
                      ER_WARN_DATA_OUT_OF_RANGE,
 
370
                      ER(ER_WARN_DATA_OUT_OF_RANGE),
 
371
                      name, 1);
 
372
  return dec;
 
373
}
 
374
 
 
375
 
 
376
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
 
377
{
 
378
  char len_buf[20*3 + 1];
 
379
  char *end;
 
380
 
 
381
  uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
 
382
                                                 unsigned_flag);
 
383
  str->append(STRING_WITH_LEN("cast("));
 
384
  args[0]->print(str, query_type);
 
385
  str->append(STRING_WITH_LEN(" as decimal("));
 
386
 
 
387
  end=int10_to_str(precision, len_buf,10);
 
388
  str->append(len_buf, (uint32_t) (end - len_buf));
 
389
 
 
390
  str->append(',');
 
391
 
 
392
  end=int10_to_str(decimals, len_buf,10);
 
393
  str->append(len_buf, (uint32_t) (end - len_buf));
 
394
 
 
395
  str->append(')');
 
396
  str->append(')');
 
397
}
 
398
 
 
399
 
 
400
double Item_func_plus::real_op()
 
401
{
 
402
  double value= args[0]->val_real() + args[1]->val_real();
 
403
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
404
    return 0.0;
 
405
  return fix_result(value);
 
406
}
 
407
 
 
408
 
 
409
int64_t Item_func_plus::int_op()
 
410
{
 
411
  int64_t value=args[0]->val_int()+args[1]->val_int();
 
412
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
413
    return 0;
 
414
  return value;
 
415
}
 
416
 
 
417
 
 
418
/**
 
419
  Calculate plus of two decimals.
 
420
 
 
421
  @param decimal_value  Buffer that can be used to store result
 
422
 
 
423
  @retval
 
424
    0  Value was NULL;  In this case null_value is set
 
425
  @retval
 
426
    \# Value of operation as a decimal
 
427
*/
 
428
 
 
429
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
 
430
{
 
431
  my_decimal value1, *val1;
 
432
  my_decimal value2, *val2;
 
433
  val1= args[0]->val_decimal(&value1);
 
434
  if ((null_value= args[0]->null_value))
 
435
    return 0;
 
436
  val2= args[1]->val_decimal(&value2);
 
437
  if (!(null_value= (args[1]->null_value ||
 
438
                     (my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
 
439
                                     val2) > 3))))
 
440
    return decimal_value;
 
441
  return 0;
 
442
}
 
443
 
 
444
/**
 
445
  Set precision of results for additive operations (+ and -)
 
446
*/
 
447
void Item_func_additive_op::result_precision()
 
448
{
 
449
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
450
  int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
 
451
                        args[1]->decimal_precision() - args[1]->decimals);
 
452
  int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
 
453
 
 
454
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
455
  if (result_type() == INT_RESULT)
 
456
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
457
  else
 
458
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
459
  max_length= my_decimal_precision_to_length(precision, decimals,
 
460
                                             unsigned_flag);
 
461
}
 
462
 
 
463
 
 
464
/**
 
465
  The following function is here to allow the user to force
 
466
  subtraction of UNSIGNED BIGINT to return negative values.
 
467
*/
 
468
 
 
469
void Item_func_minus::fix_length_and_dec()
 
470
{
 
471
  Item_num_op::fix_length_and_dec();
 
472
}
 
473
 
 
474
 
 
475
double Item_func_minus::real_op()
 
476
{
 
477
  double value= args[0]->val_real() - args[1]->val_real();
 
478
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
479
    return 0.0;
 
480
  return fix_result(value);
 
481
}
 
482
 
 
483
 
 
484
int64_t Item_func_minus::int_op()
 
485
{
 
486
  int64_t value=args[0]->val_int() - args[1]->val_int();
 
487
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
488
    return 0;
 
489
  return value;
 
490
}
 
491
 
 
492
 
 
493
/**
 
494
  See Item_func_plus::decimal_op for comments.
 
495
*/
 
496
 
 
497
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
 
498
{
 
499
  my_decimal value1, *val1;
 
500
  my_decimal value2, *val2= 
 
501
 
 
502
  val1= args[0]->val_decimal(&value1);
 
503
  if ((null_value= args[0]->null_value))
 
504
    return 0;
 
505
  val2= args[1]->val_decimal(&value2);
 
506
  if (!(null_value= (args[1]->null_value ||
 
507
                     (my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
 
508
                                     val2) > 3))))
 
509
    return decimal_value;
 
510
  return 0;
 
511
}
 
512
 
 
513
 
 
514
double Item_func_mul::real_op()
 
515
{
 
516
  assert(fixed == 1);
 
517
  double value= args[0]->val_real() * args[1]->val_real();
 
518
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
519
    return 0.0;
 
520
  return fix_result(value);
 
521
}
 
522
 
 
523
 
 
524
int64_t Item_func_mul::int_op()
 
525
{
 
526
  assert(fixed == 1);
 
527
  int64_t value=args[0]->val_int()*args[1]->val_int();
 
528
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
529
    return 0;
 
530
  return value;
 
531
}
 
532
 
 
533
 
 
534
/** See Item_func_plus::decimal_op for comments. */
 
535
 
 
536
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
 
537
{
 
538
  my_decimal value1, *val1;
 
539
  my_decimal value2, *val2;
 
540
  val1= args[0]->val_decimal(&value1);
 
541
  if ((null_value= args[0]->null_value))
 
542
    return 0;
 
543
  val2= args[1]->val_decimal(&value2);
 
544
  if (!(null_value= (args[1]->null_value ||
 
545
                     (my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
 
546
                                    val2) > 3))))
 
547
    return decimal_value;
 
548
  return 0;
 
549
}
 
550
 
 
551
 
 
552
void Item_func_mul::result_precision()
 
553
{
 
554
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
555
  if (result_type() == INT_RESULT)
 
556
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
557
  else
 
558
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
559
  decimals= cmin(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
560
  int precision= cmin(args[0]->decimal_precision() + args[1]->decimal_precision(),
 
561
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
562
  max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
 
563
}
 
564
 
 
565
 
 
566
double Item_func_div::real_op()
 
567
{
 
568
  assert(fixed == 1);
 
569
  double value= args[0]->val_real();
 
570
  double val2= args[1]->val_real();
 
571
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
572
    return 0.0;
 
573
  if (val2 == 0.0)
 
574
  {
 
575
    signal_divide_by_null();
 
576
    return 0.0;
 
577
  }
 
578
  return fix_result(value/val2);
 
579
}
 
580
 
 
581
 
 
582
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
 
583
{
 
584
  my_decimal value1, *val1;
 
585
  my_decimal value2, *val2;
 
586
  int err;
 
587
 
 
588
  val1= args[0]->val_decimal(&value1);
 
589
  if ((null_value= args[0]->null_value))
 
590
    return 0;
 
591
  val2= args[1]->val_decimal(&value2);
 
592
  if ((null_value= args[1]->null_value))
 
593
    return 0;
 
594
  if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
595
                           val1, val2, prec_increment)) > 3)
 
596
  {
 
597
    if (err == E_DEC_DIV_ZERO)
 
598
      signal_divide_by_null();
 
599
    null_value= 1;
 
600
    return 0;
 
601
  }
 
602
  return decimal_value;
 
603
}
 
604
 
 
605
 
 
606
void Item_func_div::result_precision()
 
607
{
 
608
  uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
 
609
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
610
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
611
  if (result_type() == INT_RESULT)
 
612
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
613
  else
 
614
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
615
  decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
 
616
  max_length= my_decimal_precision_to_length(precision, decimals,
 
617
                                             unsigned_flag);
 
618
}
 
619
 
 
620
 
 
621
void Item_func_div::fix_length_and_dec()
 
622
{
 
623
  prec_increment= current_thd->variables.div_precincrement;
 
624
  Item_num_op::fix_length_and_dec();
 
625
  switch(hybrid_type) {
 
626
  case REAL_RESULT:
 
627
  {
 
628
    decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
 
629
    set_if_smaller(decimals, NOT_FIXED_DEC);
 
630
    max_length=args[0]->max_length - args[0]->decimals + decimals;
 
631
    uint32_t tmp=float_length(decimals);
 
632
    set_if_smaller(max_length,tmp);
 
633
    break;
 
634
  }
 
635
  case INT_RESULT:
 
636
    hybrid_type= DECIMAL_RESULT;
 
637
    result_precision();
 
638
    break;
 
639
  case DECIMAL_RESULT:
 
640
    result_precision();
 
641
    break;
 
642
  default:
 
643
    assert(0);
 
644
  }
 
645
  maybe_null= 1; // devision by zero
 
646
  return;
 
647
}
 
648
 
 
649
 
 
650
/* Integer division */
 
651
int64_t Item_func_int_div::val_int()
 
652
{
 
653
  assert(fixed == 1);
 
654
  int64_t value=args[0]->val_int();
 
655
  int64_t val2=args[1]->val_int();
 
656
  if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
657
    return 0;
 
658
  if (val2 == 0)
 
659
  {
 
660
    signal_divide_by_null();
 
661
    return 0;
 
662
  }
 
663
  return (unsigned_flag ?
 
664
          (uint64_t) value / (uint64_t) val2 :
 
665
          value / val2);
 
666
}
 
667
 
 
668
 
 
669
void Item_func_int_div::fix_length_and_dec()
 
670
{
 
671
  Item_result argtype= args[0]->result_type();
 
672
  /* use precision ony for the data type it is applicable for and valid */
 
673
  max_length=args[0]->max_length -
 
674
    (argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
 
675
     args[0]->decimals : 0);
 
676
  maybe_null=1;
 
677
  unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
 
678
}
 
679
 
 
680
 
 
681
int64_t Item_func_mod::int_op()
 
682
{
 
683
  assert(fixed == 1);
 
684
  int64_t value=  args[0]->val_int();
 
685
  int64_t val2= args[1]->val_int();
 
686
  int64_t result;
 
687
 
 
688
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
689
    return 0; /* purecov: inspected */
 
690
  if (val2 == 0)
 
691
  {
 
692
    signal_divide_by_null();
 
693
    return 0;
 
694
  }
 
695
 
 
696
  if (args[0]->unsigned_flag)
 
697
    result= args[1]->unsigned_flag ? 
 
698
      ((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
 
699
  else
 
700
    result= args[1]->unsigned_flag ?
 
701
      value % ((uint64_t) val2) : value % val2;
 
702
 
 
703
  return result;
 
704
}
 
705
 
 
706
double Item_func_mod::real_op()
 
707
{
 
708
  assert(fixed == 1);
 
709
  double value= args[0]->val_real();
 
710
  double val2=  args[1]->val_real();
 
711
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
712
    return 0.0; /* purecov: inspected */
 
713
  if (val2 == 0.0)
 
714
  {
 
715
    signal_divide_by_null();
 
716
    return 0.0;
 
717
  }
 
718
  return fmod(value,val2);
 
719
}
 
720
 
 
721
 
 
722
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
 
723
{
 
724
  my_decimal value1, *val1;
 
725
  my_decimal value2, *val2;
 
726
 
 
727
  val1= args[0]->val_decimal(&value1);
 
728
  if ((null_value= args[0]->null_value))
 
729
    return 0;
 
730
  val2= args[1]->val_decimal(&value2);
 
731
  if ((null_value= args[1]->null_value))
 
732
    return 0;
 
733
  switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
734
                         val1, val2)) {
 
735
  case E_DEC_TRUNCATED:
 
736
  case E_DEC_OK:
 
737
    return decimal_value;
 
738
  case E_DEC_DIV_ZERO:
 
739
    signal_divide_by_null();
 
740
  default:
 
741
    null_value= 1;
 
742
    return 0;
 
743
  }
 
744
}
 
745
 
 
746
 
 
747
void Item_func_mod::result_precision()
 
748
{
 
749
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
750
  max_length= cmax(args[0]->max_length, args[1]->max_length);
 
751
}
 
752
 
 
753
 
 
754
void Item_func_mod::fix_length_and_dec()
 
755
{
 
756
  Item_num_op::fix_length_and_dec();
 
757
  maybe_null= 1;
 
758
  unsigned_flag= args[0]->unsigned_flag;
 
759
}
 
760
 
 
761
 
 
762
double Item_func_neg::real_op()
 
763
{
 
764
  double value= args[0]->val_real();
 
765
  null_value= args[0]->null_value;
 
766
  return -value;
 
767
}
 
768
 
 
769
 
 
770
int64_t Item_func_neg::int_op()
 
771
{
 
772
  int64_t value= args[0]->val_int();
 
773
  null_value= args[0]->null_value;
 
774
  return -value;
 
775
}
 
776
 
 
777
 
 
778
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
 
779
{
 
780
  my_decimal val, *value= args[0]->val_decimal(&val);
 
781
  if (!(null_value= args[0]->null_value))
 
782
  {
 
783
    my_decimal2decimal(value, decimal_value);
 
784
    my_decimal_neg(decimal_value);
 
785
    return decimal_value;
 
786
  }
 
787
  return 0;
 
788
}
 
789
 
 
790
 
 
791
void Item_func_neg::fix_num_length_and_dec()
 
792
{
 
793
  decimals= args[0]->decimals;
 
794
  /* 1 add because sign can appear */
 
795
  max_length= args[0]->max_length + 1;
 
796
}
 
797
 
 
798
 
 
799
void Item_func_neg::fix_length_and_dec()
 
800
{
 
801
  Item_func_num1::fix_length_and_dec();
 
802
 
 
803
  /*
 
804
    If this is in integer context keep the context as integer if possible
 
805
    (This is how multiplication and other integer functions works)
 
806
    Use val() to get value as arg_type doesn't mean that item is
 
807
    Item_int or Item_real due to existence of Item_param.
 
808
  */
 
809
  if (hybrid_type == INT_RESULT && args[0]->const_item())
 
810
  {
 
811
    int64_t val= args[0]->val_int();
 
812
    if ((uint64_t) val >= (uint64_t) INT64_MIN &&
 
813
        ((uint64_t) val != (uint64_t) INT64_MIN ||
 
814
          args[0]->type() != INT_ITEM))        
 
815
    {
 
816
      /*
 
817
        Ensure that result is converted to DECIMAL, as int64_t can't hold
 
818
        the negated number
 
819
      */
 
820
      hybrid_type= DECIMAL_RESULT;
 
821
    }
 
822
  }
 
823
  unsigned_flag= 0;
 
824
  return;
 
825
}
 
826
 
 
827
 
 
828
double Item_func_abs::real_op()
 
829
{
 
830
  double value= args[0]->val_real();
 
831
  null_value= args[0]->null_value;
 
832
  return fabs(value);
 
833
}
 
834
 
 
835
 
 
836
int64_t Item_func_abs::int_op()
 
837
{
 
838
  int64_t value= args[0]->val_int();
 
839
  if ((null_value= args[0]->null_value))
 
840
    return 0;
 
841
  return (value >= 0) || unsigned_flag ? value : -value;
 
842
}
 
843
 
 
844
 
 
845
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
 
846
{
 
847
  my_decimal val, *value= args[0]->val_decimal(&val);
 
848
  if (!(null_value= args[0]->null_value))
 
849
  {
 
850
    my_decimal2decimal(value, decimal_value);
 
851
    if (decimal_value->sign())
 
852
      my_decimal_neg(decimal_value);
 
853
    return decimal_value;
 
854
  }
 
855
  return 0;
 
856
}
 
857
 
 
858
 
 
859
void Item_func_abs::fix_length_and_dec()
 
860
{
 
861
  Item_func_num1::fix_length_and_dec();
 
862
  unsigned_flag= args[0]->unsigned_flag;
 
863
}
 
864
 
 
865
 
 
866
/** Gateway to natural LOG function. */
 
867
double Item_func_ln::val_real()
 
868
{
 
869
  assert(fixed == 1);
 
870
  double value= args[0]->val_real();
 
871
  if ((null_value= args[0]->null_value))
 
872
    return 0.0;
 
873
  if (value <= 0.0)
 
874
  {
 
875
    signal_divide_by_null();
 
876
    return 0.0;
 
877
  }
 
878
  return log(value);
 
879
}
 
880
 
 
881
/** 
 
882
  Extended but so slower LOG function.
 
883
 
 
884
  We have to check if all values are > zero and first one is not one
 
885
  as these are the cases then result is not a number.
 
886
*/ 
 
887
double Item_func_log::val_real()
 
888
{
 
889
  assert(fixed == 1);
 
890
  double value= args[0]->val_real();
 
891
  if ((null_value= args[0]->null_value))
 
892
    return 0.0;
 
893
  if (value <= 0.0)
 
894
  {
 
895
    signal_divide_by_null();
 
896
    return 0.0;
 
897
  }
 
898
  if (arg_count == 2)
 
899
  {
 
900
    double value2= args[1]->val_real();
 
901
    if ((null_value= args[1]->null_value))
 
902
      return 0.0;
 
903
    if (value2 <= 0.0 || value == 1.0)
 
904
    {
 
905
      signal_divide_by_null();
 
906
      return 0.0;
 
907
    }
 
908
    return log(value2) / log(value);
 
909
  }
 
910
  return log(value);
 
911
}
 
912
 
 
913
double Item_func_log2::val_real()
 
914
{
 
915
  assert(fixed == 1);
 
916
  double value= args[0]->val_real();
 
917
 
 
918
  if ((null_value=args[0]->null_value))
 
919
    return 0.0;
 
920
  if (value <= 0.0)
 
921
  {
 
922
    signal_divide_by_null();
 
923
    return 0.0;
 
924
  }
 
925
  return log(value) / M_LN2;
 
926
}
 
927
 
 
928
double Item_func_log10::val_real()
 
929
{
 
930
  assert(fixed == 1);
 
931
  double value= args[0]->val_real();
 
932
  if ((null_value= args[0]->null_value))
 
933
    return 0.0;
 
934
  if (value <= 0.0)
 
935
  {
 
936
    signal_divide_by_null();
 
937
    return 0.0;
 
938
  }
 
939
  return log10(value);
 
940
}
 
941
 
 
942
double Item_func_exp::val_real()
 
943
{
 
944
  assert(fixed == 1);
 
945
  double value= args[0]->val_real();
 
946
  if ((null_value=args[0]->null_value))
 
947
    return 0.0; /* purecov: inspected */
 
948
  return fix_result(exp(value));
 
949
}
 
950
 
 
951
double Item_func_sqrt::val_real()
 
952
{
 
953
  assert(fixed == 1);
 
954
  double value= args[0]->val_real();
 
955
  if ((null_value=(args[0]->null_value || value < 0)))
 
956
    return 0.0; /* purecov: inspected */
 
957
  return sqrt(value);
 
958
}
 
959
 
 
960
double Item_func_pow::val_real()
 
961
{
 
962
  assert(fixed == 1);
 
963
  double value= args[0]->val_real();
 
964
  double val2= args[1]->val_real();
 
965
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
 
966
    return 0.0; /* purecov: inspected */
 
967
  return fix_result(pow(value,val2));
 
968
}
 
969
 
 
970
// Trigonometric functions
 
971
 
 
972
double Item_func_acos::val_real()
 
973
{
 
974
  assert(fixed == 1);
 
975
  // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
 
976
  volatile double value= args[0]->val_real();
 
977
  if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
978
    return 0.0;
 
979
  return acos(value);
 
980
}
 
981
 
 
982
double Item_func_asin::val_real()
 
983
{
 
984
  assert(fixed == 1);
 
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))))
 
988
    return 0.0;
 
989
  return asin(value);
 
990
}
 
991
 
 
992
double Item_func_atan::val_real()
 
993
{
 
994
  assert(fixed == 1);
 
995
  double value= args[0]->val_real();
 
996
  if ((null_value=args[0]->null_value))
 
997
    return 0.0;
 
998
  if (arg_count == 2)
 
999
  {
 
1000
    double val2= args[1]->val_real();
 
1001
    if ((null_value=args[1]->null_value))
 
1002
      return 0.0;
 
1003
    return fix_result(atan2(value,val2));
 
1004
  }
 
1005
  return atan(value);
 
1006
}
 
1007
 
 
1008
double Item_func_cos::val_real()
 
1009
{
 
1010
  assert(fixed == 1);
 
1011
  double value= args[0]->val_real();
 
1012
  if ((null_value=args[0]->null_value))
 
1013
    return 0.0;
 
1014
  return cos(value);
 
1015
}
 
1016
 
 
1017
double Item_func_sin::val_real()
 
1018
{
 
1019
  assert(fixed == 1);
 
1020
  double value= args[0]->val_real();
 
1021
  if ((null_value=args[0]->null_value))
 
1022
    return 0.0;
 
1023
  return sin(value);
 
1024
}
 
1025
 
 
1026
double Item_func_tan::val_real()
 
1027
{
 
1028
  assert(fixed == 1);
 
1029
  double value= args[0]->val_real();
 
1030
  if ((null_value=args[0]->null_value))
 
1031
    return 0.0;
 
1032
  return fix_result(tan(value));
 
1033
}
 
1034
 
 
1035
 
153
1036
// Shift-functions, same as << and >> in C/C++
154
1037
 
 
1038
 
155
1039
int64_t Item_func_shift_left::val_int()
156
1040
{
157
1041
  assert(fixed == 1);
203
1087
  decimals=0;
204
1088
}
205
1089
 
 
1090
void Item_func_int_val::fix_num_length_and_dec()
 
1091
{
 
1092
  max_length= args[0]->max_length - (args[0]->decimals ?
 
1093
                                     args[0]->decimals + 1 :
 
1094
                                     0) + 2;
 
1095
  uint32_t tmp= float_length(decimals);
 
1096
  set_if_smaller(max_length,tmp);
 
1097
  decimals= 0;
 
1098
}
 
1099
 
 
1100
 
 
1101
void Item_func_int_val::find_num_type()
 
1102
{
 
1103
  switch(hybrid_type= args[0]->result_type())
 
1104
  {
 
1105
  case STRING_RESULT:
 
1106
  case REAL_RESULT:
 
1107
    hybrid_type= REAL_RESULT;
 
1108
    max_length= float_length(decimals);
 
1109
    break;
 
1110
  case INT_RESULT:
 
1111
  case DECIMAL_RESULT:
 
1112
    /*
 
1113
      -2 because in most high position can't be used any digit for int64_t
 
1114
      and one position for increasing value during operation
 
1115
    */
 
1116
    if ((args[0]->max_length - args[0]->decimals) >=
 
1117
        (DECIMAL_LONGLONG_DIGITS - 2))
 
1118
    {
 
1119
      hybrid_type= DECIMAL_RESULT;
 
1120
    }
 
1121
    else
 
1122
    {
 
1123
      unsigned_flag= args[0]->unsigned_flag;
 
1124
      hybrid_type= INT_RESULT;
 
1125
    }
 
1126
    break;
 
1127
  default:
 
1128
    assert(0);
 
1129
  }
 
1130
  return;
 
1131
}
 
1132
 
 
1133
 
 
1134
int64_t Item_func_ceiling::int_op()
 
1135
{
 
1136
  int64_t result;
 
1137
  switch (args[0]->result_type()) {
 
1138
  case INT_RESULT:
 
1139
    result= args[0]->val_int();
 
1140
    null_value= args[0]->null_value;
 
1141
    break;
 
1142
  case DECIMAL_RESULT:
 
1143
  {
 
1144
    my_decimal dec_buf, *dec;
 
1145
    if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
 
1146
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1147
    else
 
1148
      result= 0;
 
1149
    break;
 
1150
  }
 
1151
  default:
 
1152
    result= (int64_t)Item_func_ceiling::real_op();
 
1153
  };
 
1154
  return result;
 
1155
}
 
1156
 
 
1157
 
 
1158
double Item_func_ceiling::real_op()
 
1159
{
 
1160
  /*
 
1161
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1162
    bug)
 
1163
  */
 
1164
  volatile double value= args[0]->val_real();
 
1165
  null_value= args[0]->null_value;
 
1166
  return ceil(value);
 
1167
}
 
1168
 
 
1169
 
 
1170
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
 
1171
{
 
1172
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1173
  if (!(null_value= (args[0]->null_value ||
 
1174
                     my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
 
1175
                                        decimal_value) > 1)))
 
1176
    return decimal_value;
 
1177
  return 0;
 
1178
}
 
1179
 
 
1180
 
 
1181
int64_t Item_func_floor::int_op()
 
1182
{
 
1183
  int64_t result;
 
1184
  switch (args[0]->result_type()) {
 
1185
  case INT_RESULT:
 
1186
    result= args[0]->val_int();
 
1187
    null_value= args[0]->null_value;
 
1188
    break;
 
1189
  case DECIMAL_RESULT:
 
1190
  {
 
1191
    my_decimal dec_buf, *dec;
 
1192
    if ((dec= Item_func_floor::decimal_op(&dec_buf)))
 
1193
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1194
    else
 
1195
      result= 0;
 
1196
    break;
 
1197
  }
 
1198
  default:
 
1199
    result= (int64_t)Item_func_floor::real_op();
 
1200
  };
 
1201
  return result;
 
1202
}
 
1203
 
 
1204
 
 
1205
double Item_func_floor::real_op()
 
1206
{
 
1207
  /*
 
1208
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1209
    bug)
 
1210
  */
 
1211
  volatile double value= args[0]->val_real();
 
1212
  null_value= args[0]->null_value;
 
1213
  return floor(value);
 
1214
}
 
1215
 
 
1216
 
 
1217
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
 
1218
{
 
1219
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1220
  if (!(null_value= (args[0]->null_value ||
 
1221
                     my_decimal_floor(E_DEC_FATAL_ERROR, value,
 
1222
                                      decimal_value) > 1)))
 
1223
    return decimal_value;
 
1224
  return 0;
 
1225
}
 
1226
 
 
1227
 
 
1228
void Item_func_round::fix_length_and_dec()
 
1229
{
 
1230
  int      decimals_to_set;
 
1231
  int64_t val1;
 
1232
  bool     val1_unsigned;
 
1233
  
 
1234
  unsigned_flag= args[0]->unsigned_flag;
 
1235
  if (!args[1]->const_item())
 
1236
  {
 
1237
    max_length= args[0]->max_length;
 
1238
    decimals= args[0]->decimals;
 
1239
    if (args[0]->result_type() == DECIMAL_RESULT)
 
1240
    {
 
1241
      max_length++;
 
1242
      hybrid_type= DECIMAL_RESULT;
 
1243
    }
 
1244
    else
 
1245
      hybrid_type= REAL_RESULT;
 
1246
    return;
 
1247
  }
 
1248
 
 
1249
  val1= args[1]->val_int();
 
1250
  val1_unsigned= args[1]->unsigned_flag;
 
1251
  if (val1 < 0)
 
1252
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
 
1253
  else
 
1254
    decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
 
1255
 
 
1256
  if (args[0]->decimals == NOT_FIXED_DEC)
 
1257
  {
 
1258
    max_length= args[0]->max_length;
 
1259
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1260
    hybrid_type= REAL_RESULT;
 
1261
    return;
 
1262
  }
 
1263
  
 
1264
  switch (args[0]->result_type()) {
 
1265
  case REAL_RESULT:
 
1266
  case STRING_RESULT:
 
1267
    hybrid_type= REAL_RESULT;
 
1268
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1269
    max_length= float_length(decimals);
 
1270
    break;
 
1271
  case INT_RESULT:
 
1272
    if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
 
1273
    {
 
1274
      int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
 
1275
      max_length= args[0]->max_length + length_can_increase;
 
1276
      /* Here we can keep INT_RESULT */
 
1277
      hybrid_type= INT_RESULT;
 
1278
      decimals= 0;
 
1279
      break;
 
1280
    }
 
1281
    /* fall through */
 
1282
  case DECIMAL_RESULT:
 
1283
  {
 
1284
    hybrid_type= DECIMAL_RESULT;
 
1285
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
 
1286
    int decimals_delta= args[0]->decimals - decimals_to_set;
 
1287
    int precision= args[0]->decimal_precision();
 
1288
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
1289
 
 
1290
    precision-= decimals_delta - length_increase;
 
1291
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
 
1292
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1293
                                               unsigned_flag);
 
1294
    break;
 
1295
  }
 
1296
  default:
 
1297
    assert(0); /* This result type isn't handled */
 
1298
  }
 
1299
}
 
1300
 
 
1301
double my_double_round(double value, int64_t dec, bool dec_unsigned,
 
1302
                       bool truncate)
 
1303
{
 
1304
  double tmp;
 
1305
  bool dec_negative= (dec < 0) && !dec_unsigned;
 
1306
  uint64_t abs_dec= dec_negative ? -dec : dec;
 
1307
  /*
 
1308
    tmp2 is here to avoid return the value with 80 bit precision
 
1309
    This will fix that the test round(0.1,1) = round(0.1,1) is true
 
1310
  */
 
1311
  volatile double tmp2;
 
1312
 
 
1313
  tmp=(abs_dec < array_elements(log_10) ?
 
1314
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
 
1315
 
 
1316
  if (dec_negative && std::isinf(tmp))
 
1317
    tmp2= 0;
 
1318
  else if (!dec_negative && std::isinf(value * tmp))
 
1319
    tmp2= value;
 
1320
  else if (truncate)
 
1321
  {
 
1322
    if (value >= 0)
 
1323
      tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
 
1324
    else
 
1325
      tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
 
1326
  }
 
1327
  else
 
1328
    tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
 
1329
  return tmp2;
 
1330
}
 
1331
 
 
1332
 
 
1333
double Item_func_round::real_op()
 
1334
{
 
1335
  double value= args[0]->val_real();
 
1336
 
 
1337
  if (!(null_value= args[0]->null_value || args[1]->null_value))
 
1338
    return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
 
1339
                           truncate);
 
1340
 
 
1341
  return 0.0;
 
1342
}
 
1343
 
 
1344
/*
 
1345
  Rounds a given value to a power of 10 specified as the 'to' argument,
 
1346
  avoiding overflows when the value is close to the uint64_t range boundary.
 
1347
*/
 
1348
 
 
1349
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
 
1350
{
 
1351
  uint64_t tmp= value / to * to;
 
1352
  return (value - tmp < (to >> 1)) ? tmp : tmp + to;
 
1353
}
 
1354
 
 
1355
 
 
1356
int64_t Item_func_round::int_op()
 
1357
{
 
1358
  int64_t value= args[0]->val_int();
 
1359
  int64_t dec= args[1]->val_int();
 
1360
  decimals= 0;
 
1361
  uint64_t abs_dec;
 
1362
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
1363
    return 0;
 
1364
  if ((dec >= 0) || args[1]->unsigned_flag)
 
1365
    return value; // integer have not digits after point
 
1366
 
 
1367
  abs_dec= -dec;
 
1368
  int64_t tmp;
 
1369
  
 
1370
  if(abs_dec >= array_elements(log_10_int))
 
1371
    return 0;
 
1372
  
 
1373
  tmp= log_10_int[abs_dec];
 
1374
  
 
1375
  if (truncate)
 
1376
    value= (unsigned_flag) ?
 
1377
      ((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
 
1378
  else
 
1379
    value= (unsigned_flag || value >= 0) ?
 
1380
      my_unsigned_round((uint64_t) value, tmp) :
 
1381
      -(int64_t) my_unsigned_round((uint64_t) -value, tmp);
 
1382
  return value;
 
1383
}
 
1384
 
 
1385
 
 
1386
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
 
1387
{
 
1388
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1389
  int64_t dec= args[1]->val_int();
 
1390
  if (dec >= 0 || args[1]->unsigned_flag)
 
1391
    dec= cmin(dec, (int64_t) decimals);
 
1392
  else if (dec < INT_MIN)
 
1393
    dec= INT_MIN;
 
1394
    
 
1395
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
 
1396
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
 
1397
                                      truncate, decimal_value) > 1))) 
 
1398
  {
 
1399
    decimal_value->frac= decimals;
 
1400
    return decimal_value;
 
1401
  }
 
1402
  return 0;
 
1403
}
 
1404
 
 
1405
 
 
1406
void Item_func_rand::seed_random(Item *arg)
 
1407
{
 
1408
  /*
 
1409
    TODO: do not do reinit 'rand' for every execute of PS/SP if
 
1410
    args[0] is a constant.
 
1411
  */
 
1412
  uint32_t tmp= (uint32_t) arg->val_int();
 
1413
  randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
 
1414
             (uint32_t) (tmp*0x10000001L));
 
1415
}
 
1416
 
 
1417
 
 
1418
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
 
1419
{
 
1420
  if (Item_real_func::fix_fields(thd, ref))
 
1421
    return true;
 
1422
  used_tables_cache|= RAND_TABLE_BIT;
 
1423
  if (arg_count)
 
1424
  {                                     // Only use argument once in query
 
1425
    /*
 
1426
      No need to send a Rand log event if seed was given eg: RAND(seed),
 
1427
      as it will be replicated in the query as such.
 
1428
    */
 
1429
    if (!rand && !(rand= (struct rand_struct*)
 
1430
                   thd->alloc(sizeof(*rand))))
 
1431
      return true;
 
1432
 
 
1433
    if (args[0]->const_item())
 
1434
      seed_random (args[0]);
 
1435
  }
 
1436
  else
 
1437
  {
 
1438
    /*
 
1439
      Save the seed only the first time RAND() is used in the query
 
1440
      Once events are forwarded rather than recreated,
 
1441
      the following can be skipped if inside the slave thread
 
1442
    */
 
1443
    if (!thd->rand_used)
 
1444
    {
 
1445
      thd->rand_used= 1;
 
1446
      thd->rand_saved_seed1= thd->rand.seed1;
 
1447
      thd->rand_saved_seed2= thd->rand.seed2;
 
1448
    }
 
1449
    rand= &thd->rand;
 
1450
  }
 
1451
  return false;
 
1452
}
 
1453
 
 
1454
void Item_func_rand::update_used_tables()
 
1455
{
 
1456
  Item_real_func::update_used_tables();
 
1457
  used_tables_cache|= RAND_TABLE_BIT;
 
1458
}
 
1459
 
 
1460
 
 
1461
double Item_func_rand::val_real()
 
1462
{
 
1463
  assert(fixed == 1);
 
1464
  if (arg_count && !args[0]->const_item())
 
1465
    seed_random (args[0]);
 
1466
  return my_rnd(rand);
 
1467
}
 
1468
 
 
1469
int64_t Item_func_sign::val_int()
 
1470
{
 
1471
  assert(fixed == 1);
 
1472
  double value= args[0]->val_real();
 
1473
  null_value=args[0]->null_value;
 
1474
  return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
 
1475
}
 
1476
 
 
1477
 
206
1478
double Item_func_units::val_real()
207
1479
{
208
1480
  assert(fixed == 1);
213
1485
}
214
1486
 
215
1487
 
 
1488
void Item_func_min_max::fix_length_and_dec()
 
1489
{
 
1490
  int max_int_part=0;
 
1491
  bool datetime_found= false;
 
1492
  decimals=0;
 
1493
  max_length=0;
 
1494
  maybe_null=0;
 
1495
  cmp_type=args[0]->result_type();
 
1496
 
 
1497
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1498
  {
 
1499
    set_if_bigger(max_length, args[i]->max_length);
 
1500
    set_if_bigger(decimals, args[i]->decimals);
 
1501
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
 
1502
    if (args[i]->maybe_null)
 
1503
      maybe_null=1;
 
1504
    cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
 
1505
    if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
 
1506
    {
 
1507
      datetime_found= true;
 
1508
      if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
 
1509
        datetime_item= args[i];
 
1510
    }
 
1511
  }
 
1512
  if (cmp_type == STRING_RESULT)
 
1513
  {
 
1514
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
 
1515
    if (datetime_found)
 
1516
    {
 
1517
      thd= current_thd;
 
1518
      compare_as_dates= true;
 
1519
    }
 
1520
  }
 
1521
  else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
 
1522
    max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
 
1523
                                            unsigned_flag);
 
1524
  cached_field_type= agg_field_type(args, arg_count);
 
1525
}
 
1526
 
 
1527
 
 
1528
/*
 
1529
  Compare item arguments in the DATETIME context.
 
1530
 
 
1531
  SYNOPSIS
 
1532
    cmp_datetimes()
 
1533
    value [out]   found least/greatest DATE/DATETIME value
 
1534
 
 
1535
  DESCRIPTION
 
1536
    Compare item arguments as DATETIME values and return the index of the
 
1537
    least/greatest argument in the arguments array.
 
1538
    The correct integer DATE/DATETIME value of the found argument is
 
1539
    stored to the value pointer, if latter is provided.
 
1540
 
 
1541
  RETURN
 
1542
   0    If one of arguments is NULL
 
1543
   #    index of the least/greatest argument
 
1544
*/
 
1545
 
 
1546
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
 
1547
{
 
1548
  uint64_t min_max= 0;
 
1549
  uint32_t min_max_idx= 0;
 
1550
 
 
1551
  for (uint32_t i=0; i < arg_count ; i++)
 
1552
  {
 
1553
    Item **arg= args + i;
 
1554
    bool is_null;
 
1555
    uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
 
1556
    if ((null_value= args[i]->null_value))
 
1557
      return 0;
 
1558
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
1559
    {
 
1560
      min_max= res;
 
1561
      min_max_idx= i;
 
1562
    }
 
1563
  }
 
1564
  if (value)
 
1565
  {
 
1566
    *value= min_max;
 
1567
    if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
 
1568
      *value/= 1000000L;
 
1569
  }
 
1570
  return min_max_idx;
 
1571
}
 
1572
 
 
1573
 
 
1574
String *Item_func_min_max::val_str(String *str)
 
1575
{
 
1576
  assert(fixed == 1);
 
1577
  if (compare_as_dates)
 
1578
  {
 
1579
    String *str_res;
 
1580
    uint32_t min_max_idx= cmp_datetimes(NULL);
 
1581
    if (null_value)
 
1582
      return 0;
 
1583
    str_res= args[min_max_idx]->val_str(str);
 
1584
    str_res->set_charset(collation.collation);
 
1585
    return str_res;
 
1586
  }
 
1587
  switch (cmp_type) {
 
1588
  case INT_RESULT:
 
1589
  {
 
1590
    int64_t nr=val_int();
 
1591
    if (null_value)
 
1592
      return 0;
 
1593
    str->set_int(nr, unsigned_flag, &my_charset_bin);
 
1594
    return str;
 
1595
  }
 
1596
  case DECIMAL_RESULT:
 
1597
  {
 
1598
    my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
 
1599
    if (null_value)
 
1600
      return 0;
 
1601
    my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
 
1602
    return str;
 
1603
  }
 
1604
  case REAL_RESULT:
 
1605
  {
 
1606
    double nr= val_real();
 
1607
    if (null_value)
 
1608
      return 0; /* purecov: inspected */
 
1609
    str->set_real(nr,decimals,&my_charset_bin);
 
1610
    return str;
 
1611
  }
 
1612
  case STRING_RESULT:
 
1613
  {
 
1614
    String *res= NULL;
 
1615
 
 
1616
    for (uint32_t i=0; i < arg_count ; i++)
 
1617
    {
 
1618
      if (i == 0)
 
1619
        res=args[i]->val_str(str);
 
1620
      else
 
1621
      {
 
1622
        String *res2;
 
1623
        res2= args[i]->val_str(res == str ? &tmp_value : str);
 
1624
        if (res2)
 
1625
        {
 
1626
          int cmp= sortcmp(res,res2,collation.collation);
 
1627
          if ((cmp_sign < 0 ? cmp : -cmp) < 0)
 
1628
            res=res2;
 
1629
        }
 
1630
      }
 
1631
      if ((null_value= args[i]->null_value))
 
1632
        return 0;
 
1633
    }
 
1634
    res->set_charset(collation.collation);
 
1635
    return res;
 
1636
  }
 
1637
  case ROW_RESULT:
 
1638
  default:
 
1639
    // This case should never be chosen
 
1640
    assert(0);
 
1641
    return 0;
 
1642
  }
 
1643
  return 0;                                     // Keep compiler happy
 
1644
}
 
1645
 
 
1646
 
 
1647
double Item_func_min_max::val_real()
 
1648
{
 
1649
  assert(fixed == 1);
 
1650
  double value=0.0;
 
1651
  if (compare_as_dates)
 
1652
  {
 
1653
    uint64_t result= 0;
 
1654
    (void)cmp_datetimes(&result);
 
1655
    return (double)result;
 
1656
  }
 
1657
  for (uint32_t i=0; i < arg_count ; i++)
 
1658
  {
 
1659
    if (i == 0)
 
1660
      value= args[i]->val_real();
 
1661
    else
 
1662
    {
 
1663
      double tmp= args[i]->val_real();
 
1664
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
1665
        value=tmp;
 
1666
    }
 
1667
    if ((null_value= args[i]->null_value))
 
1668
      break;
 
1669
  }
 
1670
  return value;
 
1671
}
 
1672
 
 
1673
 
 
1674
int64_t Item_func_min_max::val_int()
 
1675
{
 
1676
  assert(fixed == 1);
 
1677
  int64_t value=0;
 
1678
  if (compare_as_dates)
 
1679
  {
 
1680
    uint64_t result= 0;
 
1681
    (void)cmp_datetimes(&result);
 
1682
    return (int64_t)result;
 
1683
  }
 
1684
  for (uint32_t i=0; i < arg_count ; i++)
 
1685
  {
 
1686
    if (i == 0)
 
1687
      value=args[i]->val_int();
 
1688
    else
 
1689
    {
 
1690
      int64_t tmp=args[i]->val_int();
 
1691
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
1692
        value=tmp;
 
1693
    }
 
1694
    if ((null_value= args[i]->null_value))
 
1695
      break;
 
1696
  }
 
1697
  return value;
 
1698
}
 
1699
 
 
1700
 
 
1701
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
 
1702
{
 
1703
  assert(fixed == 1);
 
1704
  my_decimal tmp_buf, *tmp, *res= NULL;
 
1705
 
 
1706
  if (compare_as_dates)
 
1707
  {
 
1708
    uint64_t value= 0;
 
1709
    (void)cmp_datetimes(&value);
 
1710
    uint64_t2decimal(value, dec);
 
1711
    return dec;
 
1712
  }
 
1713
  for (uint32_t i=0; i < arg_count ; i++)
 
1714
  {
 
1715
    if (i == 0)
 
1716
      res= args[i]->val_decimal(dec);
 
1717
    else
 
1718
    {
 
1719
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
 
1720
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
 
1721
      {
 
1722
        if (tmp == &tmp_buf)
 
1723
        {
 
1724
          /* Move value out of tmp_buf as this will be reused on next loop */
 
1725
          my_decimal2decimal(tmp, dec);
 
1726
          res= dec;
 
1727
        }
 
1728
        else
 
1729
          res= tmp;
 
1730
      }
 
1731
    }
 
1732
    if ((null_value= args[i]->null_value))
 
1733
    {
 
1734
      res= 0;
 
1735
      break;
 
1736
    }
 
1737
  }
 
1738
  return res;
 
1739
}
 
1740
 
 
1741
 
 
1742
int64_t Item_func_length::val_int()
 
1743
{
 
1744
  assert(fixed == 1);
 
1745
  String *res=args[0]->val_str(&value);
 
1746
  if (!res)
 
1747
  {
 
1748
    null_value=1;
 
1749
    return 0; /* purecov: inspected */
 
1750
  }
 
1751
  null_value=0;
 
1752
  return (int64_t) res->length();
 
1753
}
 
1754
 
 
1755
 
216
1756
int64_t Item_func_char_length::val_int()
217
1757
{
218
1758
  assert(fixed == 1);