~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/item_func.cc

  • Committer: Brian Aker
  • Date: 2008-07-13 21:20:24 UTC
  • Revision ID: brian@tangent.org-20080713212024-o6263c1vha7yxdeu
More bool removal. More cow bell!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** Copyright (C) 2000-2003 MySQL AB
 
1
/* Copyright (C) 2000-2003 MySQL AB
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
21
21
  This file defines all numerical functions
22
22
*/
23
23
 
24
 
#include <drizzled/server_includes.h>
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
 
29
#include "slave.h"                              // for wait_for_master_pos
25
30
#include "rpl_mi.h"
26
 
#include <mysys/my_bit.h>
27
 
#include <drizzled/drizzled_error_messages.h>
 
31
#include <m_ctype.h>
 
32
#include <hash.h>
 
33
#include <time.h>
 
34
#include <ft_global.h>
 
35
#include <my_bit.h>
28
36
 
29
37
bool check_reserved_words(LEX_STRING *name)
30
38
{
48
56
}
49
57
 
50
58
 
 
59
void Item_func::set_arguments(List<Item> &list)
 
60
{
 
61
  allowed_arg_cols= 1;
 
62
  arg_count=list.elements;
 
63
  args= tmp_arg;                                // If 2 arguments
 
64
  if (arg_count <= 2 || (args=(Item**) sql_alloc(sizeof(Item*)*arg_count)))
 
65
  {
 
66
    List_iterator_fast<Item> li(list);
 
67
    Item *item;
 
68
    Item **save_args= args;
 
69
 
 
70
    while ((item=li++))
 
71
    {
 
72
      *(save_args++)= item;
 
73
      with_sum_func|=item->with_sum_func;
 
74
    }
 
75
  }
 
76
  list.empty();                                 // Fields are used
 
77
}
 
78
 
 
79
Item_func::Item_func(List<Item> &list)
 
80
  :allowed_arg_cols(1)
 
81
{
 
82
  set_arguments(list);
 
83
}
 
84
 
 
85
Item_func::Item_func(THD *thd, Item_func *item)
 
86
  :Item_result_field(thd, item),
 
87
   allowed_arg_cols(item->allowed_arg_cols),
 
88
   arg_count(item->arg_count),
 
89
   used_tables_cache(item->used_tables_cache),
 
90
   not_null_tables_cache(item->not_null_tables_cache),
 
91
   const_item_cache(item->const_item_cache)
 
92
{
 
93
  if (arg_count)
 
94
  {
 
95
    if (arg_count <=2)
 
96
      args= tmp_arg;
 
97
    else
 
98
    {
 
99
      if (!(args=(Item**) thd->alloc(sizeof(Item*)*arg_count)))
 
100
        return;
 
101
    }
 
102
    memcpy((char*) args, (char*) item->args, sizeof(Item*)*arg_count);
 
103
  }
 
104
}
 
105
 
 
106
 
 
107
/*
 
108
  Resolve references to table column for a function and its argument
 
109
 
 
110
  SYNOPSIS:
 
111
  fix_fields()
 
112
  thd           Thread object
 
113
  ref           Pointer to where this object is used.  This reference
 
114
                is used if we want to replace this object with another
 
115
                one (for example in the summary functions).
 
116
 
 
117
  DESCRIPTION
 
118
    Call fix_fields() for all arguments to the function.  The main intention
 
119
    is to allow all Item_field() objects to setup pointers to the table fields.
 
120
 
 
121
    Sets as a side effect the following class variables:
 
122
      maybe_null        Set if any argument may return NULL
 
123
      with_sum_func     Set if any of the arguments contains a sum function
 
124
      used_tables_cache Set to union of the tables used by arguments
 
125
 
 
126
      str_value.charset If this is a string function, set this to the
 
127
                        character set for the first argument.
 
128
                        If any argument is binary, this is set to binary
 
129
 
 
130
   If for any item any of the defaults are wrong, then this can
 
131
   be fixed in the fix_length_and_dec() function that is called
 
132
   after this one or by writing a specialized fix_fields() for the
 
133
   item.
 
134
 
 
135
  RETURN VALUES
 
136
  false ok
 
137
  true  Got error.  Stored with my_error().
 
138
*/
 
139
 
 
140
bool
 
141
Item_func::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
 
142
{
 
143
  assert(fixed == 0);
 
144
  Item **arg,**arg_end;
 
145
  void *save_thd_marker= thd->thd_marker;
 
146
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
147
  thd->thd_marker= 0;
 
148
  used_tables_cache= not_null_tables_cache= 0;
 
149
  const_item_cache=1;
 
150
 
 
151
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
 
152
    return true;                                // Fatal error if flag is set!
 
153
  if (arg_count)
 
154
  {                                             // Print purify happy
 
155
    for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
 
156
    {
 
157
      Item *item;
 
158
      /*
 
159
        We can't yet set item to *arg as fix_fields may change *arg
 
160
        We shouldn't call fix_fields() twice, so check 'fixed' field first
 
161
      */
 
162
      if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
 
163
        return true;                            /* purecov: inspected */
 
164
      item= *arg;
 
165
 
 
166
      if (allowed_arg_cols)
 
167
      {
 
168
        if (item->check_cols(allowed_arg_cols))
 
169
          return 1;
 
170
      }
 
171
      else
 
172
      {
 
173
        /*  we have to fetch allowed_arg_cols from first argument */
 
174
        assert(arg == args); // it is first argument
 
175
        allowed_arg_cols= item->cols();
 
176
        assert(allowed_arg_cols); // Can't be 0 any more
 
177
      }
 
178
 
 
179
      if (item->maybe_null)
 
180
        maybe_null=1;
 
181
 
 
182
      with_sum_func= with_sum_func || item->with_sum_func;
 
183
      used_tables_cache|=     item->used_tables();
 
184
      not_null_tables_cache|= item->not_null_tables();
 
185
      const_item_cache&=      item->const_item();
 
186
      with_subselect|=        item->with_subselect;
 
187
    }
 
188
  }
 
189
  fix_length_and_dec();
 
190
  if (thd->is_error()) // An error inside fix_length_and_dec occured
 
191
    return true;
 
192
  fixed= 1;
 
193
  thd->thd_marker= save_thd_marker;
 
194
  return false;
 
195
}
 
196
 
 
197
 
 
198
void Item_func::fix_after_pullout(st_select_lex *new_parent,
 
199
                                  Item **ref __attribute__((__unused__)))
 
200
{
 
201
  Item **arg,**arg_end;
 
202
 
 
203
  used_tables_cache= not_null_tables_cache= 0;
 
204
  const_item_cache=1;
 
205
 
 
206
  if (arg_count)
 
207
  {
 
208
    for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
 
209
    {
 
210
      (*arg)->fix_after_pullout(new_parent, arg);
 
211
      Item *item= *arg;
 
212
 
 
213
      used_tables_cache|=     item->used_tables();
 
214
      not_null_tables_cache|= item->not_null_tables();
 
215
      const_item_cache&=      item->const_item();
 
216
    }
 
217
  }
 
218
}
 
219
 
 
220
 
 
221
bool Item_func::walk(Item_processor processor, bool walk_subquery,
 
222
                     uchar *argument)
 
223
{
 
224
  if (arg_count)
 
225
  {
 
226
    Item **arg,**arg_end;
 
227
    for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
228
    {
 
229
      if ((*arg)->walk(processor, walk_subquery, argument))
 
230
        return 1;
 
231
    }
 
232
  }
 
233
  return (this->*processor)(argument);
 
234
}
 
235
 
 
236
void Item_func::traverse_cond(Cond_traverser traverser,
 
237
                              void *argument, traverse_order order)
 
238
{
 
239
  if (arg_count)
 
240
  {
 
241
    Item **arg,**arg_end;
 
242
 
 
243
    switch (order) {
 
244
    case(PREFIX):
 
245
      (*traverser)(this, argument);
 
246
      for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
247
      {
 
248
        (*arg)->traverse_cond(traverser, argument, order);
 
249
      }
 
250
      break;
 
251
    case (POSTFIX):
 
252
      for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
253
      {
 
254
        (*arg)->traverse_cond(traverser, argument, order);
 
255
      }
 
256
      (*traverser)(this, argument);
 
257
    }
 
258
  }
 
259
  else
 
260
    (*traverser)(this, argument);
 
261
}
 
262
 
 
263
 
 
264
/**
 
265
  Transform an Item_func object with a transformer callback function.
 
266
 
 
267
    The function recursively applies the transform method to each
 
268
    argument of the Item_func node.
 
269
    If the call of the method for an argument item returns a new item
 
270
    the old item is substituted for a new one.
 
271
    After this the transformer is applied to the root node
 
272
    of the Item_func object. 
 
273
  @param transformer   the transformer callback function to be applied to
 
274
                       the nodes of the tree of the object
 
275
  @param argument      parameter to be passed to the transformer
 
276
 
 
277
  @return
 
278
    Item returned as the result of transformation of the root node
 
279
*/
 
280
 
 
281
Item *Item_func::transform(Item_transformer transformer, uchar *argument)
 
282
{
 
283
  if (arg_count)
 
284
  {
 
285
    Item **arg,**arg_end;
 
286
    for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
287
    {
 
288
      Item *new_item= (*arg)->transform(transformer, argument);
 
289
      if (!new_item)
 
290
        return 0;
 
291
 
 
292
      /*
 
293
        THD::change_item_tree() should be called only if the tree was
 
294
        really transformed, i.e. when a new item has been created.
 
295
        Otherwise we'll be allocating a lot of unnecessary memory for
 
296
        change records at each execution.
 
297
      */
 
298
      if (*arg != new_item)
 
299
        current_thd->change_item_tree(arg, new_item);
 
300
    }
 
301
  }
 
302
  return (this->*transformer)(argument);
 
303
}
 
304
 
 
305
 
 
306
/**
 
307
  Compile Item_func object with a processor and a transformer
 
308
  callback functions.
 
309
 
 
310
    First the function applies the analyzer to the root node of
 
311
    the Item_func object. Then if the analizer succeeeds (returns true)
 
312
    the function recursively applies the compile method to each argument
 
313
    of the Item_func node.
 
314
    If the call of the method for an argument item returns a new item
 
315
    the old item is substituted for a new one.
 
316
    After this the transformer is applied to the root node
 
317
    of the Item_func object. 
 
318
 
 
319
  @param analyzer      the analyzer callback function to be applied to the
 
320
                       nodes of the tree of the object
 
321
  @param[in,out] arg_p parameter to be passed to the processor
 
322
  @param transformer   the transformer callback function to be applied to the
 
323
                       nodes of the tree of the object
 
324
  @param arg_t         parameter to be passed to the transformer
 
325
 
 
326
  @return
 
327
    Item returned as the result of transformation of the root node
 
328
*/
 
329
 
 
330
Item *Item_func::compile(Item_analyzer analyzer, uchar **arg_p,
 
331
                         Item_transformer transformer, uchar *arg_t)
 
332
{
 
333
  if (!(this->*analyzer)(arg_p))
 
334
    return 0;
 
335
  if (arg_count)
 
336
  {
 
337
    Item **arg,**arg_end;
 
338
    for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
 
339
    {
 
340
      /* 
 
341
        The same parameter value of arg_p must be passed
 
342
        to analyze any argument of the condition formula.
 
343
      */   
 
344
      uchar *arg_v= *arg_p;
 
345
      Item *new_item= (*arg)->compile(analyzer, &arg_v, transformer, arg_t);
 
346
      if (new_item && *arg != new_item)
 
347
        current_thd->change_item_tree(arg, new_item);
 
348
    }
 
349
  }
 
350
  return (this->*transformer)(arg_t);
 
351
}
 
352
 
 
353
/**
 
354
  See comments in Item_cmp_func::split_sum_func()
 
355
*/
 
356
 
 
357
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
 
358
                               List<Item> &fields)
 
359
{
 
360
  Item **arg, **arg_end;
 
361
  for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
 
362
    (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, true);
 
363
}
 
364
 
 
365
 
 
366
void Item_func::update_used_tables()
 
367
{
 
368
  used_tables_cache=0;
 
369
  const_item_cache=1;
 
370
  for (uint i=0 ; i < arg_count ; i++)
 
371
  {
 
372
    args[i]->update_used_tables();
 
373
    used_tables_cache|=args[i]->used_tables();
 
374
    const_item_cache&=args[i]->const_item();
 
375
  }
 
376
}
 
377
 
 
378
 
 
379
table_map Item_func::used_tables() const
 
380
{
 
381
  return used_tables_cache;
 
382
}
 
383
 
 
384
 
 
385
table_map Item_func::not_null_tables() const
 
386
{
 
387
  return not_null_tables_cache;
 
388
}
 
389
 
 
390
 
 
391
void Item_func::print(String *str, enum_query_type query_type)
 
392
{
 
393
  str->append(func_name());
 
394
  str->append('(');
 
395
  print_args(str, 0, query_type);
 
396
  str->append(')');
 
397
}
 
398
 
 
399
 
 
400
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
 
401
{
 
402
  for (uint i=from ; i < arg_count ; i++)
 
403
  {
 
404
    if (i != from)
 
405
      str->append(',');
 
406
    args[i]->print(str, query_type);
 
407
  }
 
408
}
 
409
 
 
410
 
 
411
void Item_func::print_op(String *str, enum_query_type query_type)
 
412
{
 
413
  str->append('(');
 
414
  for (uint i=0 ; i < arg_count-1 ; i++)
 
415
  {
 
416
    args[i]->print(str, query_type);
 
417
    str->append(' ');
 
418
    str->append(func_name());
 
419
    str->append(' ');
 
420
  }
 
421
  args[arg_count-1]->print(str, query_type);
 
422
  str->append(')');
 
423
}
 
424
 
 
425
 
 
426
bool Item_func::eq(const Item *item, bool binary_cmp) const
 
427
{
 
428
  /* Assume we don't have rtti */
 
429
  if (this == item)
 
430
    return 1;
 
431
  if (item->type() != FUNC_ITEM)
 
432
    return 0;
 
433
  Item_func *item_func=(Item_func*) item;
 
434
  Item_func::Functype func_type;
 
435
  if ((func_type= functype()) != item_func->functype() ||
 
436
      arg_count != item_func->arg_count ||
 
437
      (func_type != Item_func::FUNC_SP &&
 
438
       func_name() != item_func->func_name()) ||
 
439
      (func_type == Item_func::FUNC_SP &&
 
440
       my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
 
441
    return 0;
 
442
  for (uint i=0; i < arg_count ; i++)
 
443
    if (!args[i]->eq(item_func->args[i], binary_cmp))
 
444
      return 0;
 
445
  return 1;
 
446
}
 
447
 
 
448
 
 
449
Field *Item_func::tmp_table_field(TABLE *table)
 
450
{
 
451
  Field *field;
 
452
 
 
453
  switch (result_type()) {
 
454
  case INT_RESULT:
 
455
    if (max_length > MY_INT32_NUM_DECIMAL_DIGITS)
 
456
      field= new Field_longlong(max_length, maybe_null, name, unsigned_flag);
 
457
    else
 
458
      field= new Field_long(max_length, maybe_null, name, unsigned_flag);
 
459
    break;
 
460
  case REAL_RESULT:
 
461
    field= new Field_double(max_length, maybe_null, name, decimals);
 
462
    break;
 
463
  case STRING_RESULT:
 
464
    return make_string_field(table);
 
465
    break;
 
466
  case DECIMAL_RESULT:
 
467
    field= new Field_new_decimal(my_decimal_precision_to_length(decimal_precision(),
 
468
                                                                decimals,
 
469
                                                                unsigned_flag),
 
470
                                 maybe_null, name, decimals, unsigned_flag);
 
471
    break;
 
472
  case ROW_RESULT:
 
473
  default:
 
474
    // This case should never be chosen
 
475
    assert(0);
 
476
    field= 0;
 
477
    break;
 
478
  }
 
479
  if (field)
 
480
    field->init(table);
 
481
  return field;
 
482
}
 
483
 
 
484
 
 
485
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
 
486
{
 
487
  assert(fixed);
 
488
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
489
  return decimal_value;
 
490
}
 
491
 
 
492
 
 
493
String *Item_real_func::val_str(String *str)
 
494
{
 
495
  assert(fixed == 1);
 
496
  double nr= val_real();
 
497
  if (null_value)
 
498
    return 0; /* purecov: inspected */
 
499
  str->set_real(nr,decimals, &my_charset_bin);
 
500
  return str;
 
501
}
 
502
 
 
503
 
 
504
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
 
505
{
 
506
  assert(fixed);
 
507
  double nr= val_real();
 
508
  if (null_value)
 
509
    return 0; /* purecov: inspected */
 
510
  double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
 
511
  return decimal_value;
 
512
}
 
513
 
 
514
 
51
515
void Item_func::fix_num_length_and_dec()
52
516
{
53
 
  uint32_t fl_length= 0;
 
517
  uint fl_length= 0;
54
518
  decimals=0;
55
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
519
  for (uint i=0 ; i < arg_count ; i++)
56
520
  {
57
521
    set_if_bigger(decimals,args[i]->decimals);
58
522
    set_if_bigger(fl_length, args[i]->max_length);
65
529
  }
66
530
}
67
531
 
 
532
 
 
533
void Item_func_numhybrid::fix_num_length_and_dec()
 
534
{}
 
535
 
 
536
 
68
537
/**
69
538
  Set max_length/decimals of function if function is fixed point and
70
539
  result length/precision depends on argument ones.
75
544
  int max_int_part= 0;
76
545
  decimals= 0;
77
546
  unsigned_flag= 1;
78
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
547
  for (uint i=0 ; i < arg_count ; i++)
79
548
  {
80
549
    set_if_bigger(decimals, args[i]->decimals);
81
550
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
82
551
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
83
552
  }
84
 
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
553
  int precision= min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
85
554
  max_length= my_decimal_precision_to_length(precision, decimals,
86
555
                                             unsigned_flag);
87
556
}
95
564
{
96
565
  max_length= 0;
97
566
  unsigned_flag= 0;
98
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
567
  for (uint i=0 ; i < arg_count ; i++)
99
568
  {
100
569
    set_if_bigger(max_length, args[i]->max_length);
101
570
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
110
579
 
111
580
void Item_func::count_real_length()
112
581
{
113
 
  uint32_t length= 0;
 
582
  uint32 length= 0;
114
583
  decimals= 0;
115
584
  max_length= 0;
116
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
585
  for (uint i=0 ; i < arg_count ; i++)
117
586
  {
118
587
    if (decimals != NOT_FIXED_DEC)
119
588
    {
127
596
    max_length= length;
128
597
    length+= decimals;
129
598
    if (length < max_length)  // If previous operation gave overflow
130
 
      max_length= UINT32_MAX;
 
599
      max_length= UINT_MAX32;
131
600
    else
132
601
      max_length= length;
133
602
  }
138
607
void Item_func::signal_divide_by_null()
139
608
{
140
609
  THD *thd= current_thd;
141
 
  push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
610
  if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
 
611
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO,
 
612
                 ER(ER_DIVISION_BY_ZERO));
142
613
  null_value= 1;
143
614
}
144
615
 
150
621
  return copy_or_same(thd);
151
622
}
152
623
 
 
624
double Item_int_func::val_real()
 
625
{
 
626
  assert(fixed == 1);
 
627
 
 
628
  return unsigned_flag ? (double) ((ulonglong) val_int()) : (double) val_int();
 
629
}
 
630
 
 
631
 
 
632
String *Item_int_func::val_str(String *str)
 
633
{
 
634
  assert(fixed == 1);
 
635
  longlong nr=val_int();
 
636
  if (null_value)
 
637
    return 0;
 
638
  str->set_int(nr, unsigned_flag, &my_charset_bin);
 
639
  return str;
 
640
}
 
641
 
 
642
 
 
643
void Item_func_connection_id::fix_length_and_dec()
 
644
{
 
645
  Item_int_func::fix_length_and_dec();
 
646
  max_length= 10;
 
647
}
 
648
 
 
649
 
 
650
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
 
651
{
 
652
  if (Item_int_func::fix_fields(thd, ref))
 
653
    return true;
 
654
  thd->thread_specific_used= true;
 
655
  value= thd->variables.pseudo_thread_id;
 
656
  return false;
 
657
}
 
658
 
 
659
 
 
660
/**
 
661
  Check arguments here to determine result's type for a numeric
 
662
  function of two arguments.
 
663
*/
 
664
 
 
665
void Item_num_op::find_num_type(void)
 
666
{
 
667
  assert(arg_count == 2);
 
668
  Item_result r0= args[0]->result_type();
 
669
  Item_result r1= args[1]->result_type();
 
670
 
 
671
  if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
 
672
      r0 == STRING_RESULT || r1 ==STRING_RESULT)
 
673
  {
 
674
    count_real_length();
 
675
    max_length= float_length(decimals);
 
676
    hybrid_type= REAL_RESULT;
 
677
  }
 
678
  else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT)
 
679
  {
 
680
    hybrid_type= DECIMAL_RESULT;
 
681
    result_precision();
 
682
  }
 
683
  else
 
684
  {
 
685
    assert(r0 == INT_RESULT && r1 == INT_RESULT);
 
686
    decimals= 0;
 
687
    hybrid_type=INT_RESULT;
 
688
    result_precision();
 
689
  }
 
690
  return;
 
691
}
 
692
 
 
693
 
 
694
/**
 
695
  Set result type for a numeric function of one argument
 
696
  (can be also used by a numeric function of many arguments, if the result
 
697
  type depends only on the first argument)
 
698
*/
 
699
 
 
700
void Item_func_num1::find_num_type()
 
701
{
 
702
  switch (hybrid_type= args[0]->result_type()) {
 
703
  case INT_RESULT:
 
704
    unsigned_flag= args[0]->unsigned_flag;
 
705
    break;
 
706
  case STRING_RESULT:
 
707
  case REAL_RESULT:
 
708
    hybrid_type= REAL_RESULT;
 
709
    max_length= float_length(decimals);
 
710
    break;
 
711
  case DECIMAL_RESULT:
 
712
    break;
 
713
  default:
 
714
    assert(0);
 
715
  }
 
716
  return;
 
717
}
 
718
 
 
719
 
 
720
void Item_func_num1::fix_num_length_and_dec()
 
721
{
 
722
  decimals= args[0]->decimals;
 
723
  max_length= args[0]->max_length;
 
724
}
 
725
 
 
726
 
 
727
void Item_func_numhybrid::fix_length_and_dec()
 
728
{
 
729
  fix_num_length_and_dec();
 
730
  find_num_type();
 
731
}
 
732
 
 
733
 
 
734
String *Item_func_numhybrid::val_str(String *str)
 
735
{
 
736
  assert(fixed == 1);
 
737
  switch (hybrid_type) {
 
738
  case DECIMAL_RESULT:
 
739
  {
 
740
    my_decimal decimal_value, *val;
 
741
    if (!(val= decimal_op(&decimal_value)))
 
742
      return 0;                                 // null is set
 
743
    my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, false, val);
 
744
    my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
 
745
    break;
 
746
  }
 
747
  case INT_RESULT:
 
748
  {
 
749
    longlong nr= int_op();
 
750
    if (null_value)
 
751
      return 0; /* purecov: inspected */
 
752
    str->set_int(nr, unsigned_flag, &my_charset_bin);
 
753
    break;
 
754
  }
 
755
  case REAL_RESULT:
 
756
  {
 
757
    double nr= real_op();
 
758
    if (null_value)
 
759
      return 0; /* purecov: inspected */
 
760
    str->set_real(nr,decimals,&my_charset_bin);
 
761
    break;
 
762
  }
 
763
  case STRING_RESULT:
 
764
    return str_op(&str_value);
 
765
  default:
 
766
    assert(0);
 
767
  }
 
768
  return str;
 
769
}
 
770
 
 
771
 
 
772
double Item_func_numhybrid::val_real()
 
773
{
 
774
  assert(fixed == 1);
 
775
  switch (hybrid_type) {
 
776
  case DECIMAL_RESULT:
 
777
  {
 
778
    my_decimal decimal_value, *val;
 
779
    double result;
 
780
    if (!(val= decimal_op(&decimal_value)))
 
781
      return 0.0;                               // null is set
 
782
    my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
 
783
    return result;
 
784
  }
 
785
  case INT_RESULT:
 
786
  {
 
787
    longlong result= int_op();
 
788
    return unsigned_flag ? (double) ((ulonglong) result) : (double) result;
 
789
  }
 
790
  case REAL_RESULT:
 
791
    return real_op();
 
792
  case STRING_RESULT:
 
793
  {
 
794
    char *end_not_used;
 
795
    int err_not_used;
 
796
    String *res= str_op(&str_value);
 
797
    return (res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
 
798
                             &end_not_used, &err_not_used) : 0.0);
 
799
  }
 
800
  default:
 
801
    assert(0);
 
802
  }
 
803
  return 0.0;
 
804
}
 
805
 
 
806
 
 
807
longlong Item_func_numhybrid::val_int()
 
808
{
 
809
  assert(fixed == 1);
 
810
  switch (hybrid_type) {
 
811
  case DECIMAL_RESULT:
 
812
  {
 
813
    my_decimal decimal_value, *val;
 
814
    if (!(val= decimal_op(&decimal_value)))
 
815
      return 0;                                 // null is set
 
816
    longlong result;
 
817
    my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
 
818
    return result;
 
819
  }
 
820
  case INT_RESULT:
 
821
    return int_op();
 
822
  case REAL_RESULT:
 
823
    return (longlong) rint(real_op());
 
824
  case STRING_RESULT:
 
825
  {
 
826
    int err_not_used;
 
827
    String *res;
 
828
    if (!(res= str_op(&str_value)))
 
829
      return 0;
 
830
 
 
831
    char *end= (char*) res->ptr() + res->length();
 
832
    CHARSET_INFO *cs= str_value.charset();
 
833
    return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
 
834
  }
 
835
  default:
 
836
    assert(0);
 
837
  }
 
838
  return 0;
 
839
}
 
840
 
 
841
 
 
842
my_decimal *Item_func_numhybrid::val_decimal(my_decimal *decimal_value)
 
843
{
 
844
  my_decimal *val= decimal_value;
 
845
  assert(fixed == 1);
 
846
  switch (hybrid_type) {
 
847
  case DECIMAL_RESULT:
 
848
    val= decimal_op(decimal_value);
 
849
    break;
 
850
  case INT_RESULT:
 
851
  {
 
852
    longlong result= int_op();
 
853
    int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
 
854
    break;
 
855
  }
 
856
  case REAL_RESULT:
 
857
  {
 
858
    double result= (double)real_op();
 
859
    double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
 
860
    break;
 
861
  }
 
862
  case STRING_RESULT:
 
863
  {
 
864
    String *res;
 
865
    if (!(res= str_op(&str_value)))
 
866
      return NULL;
 
867
 
 
868
    str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
 
869
                   res->length(), res->charset(), decimal_value);
 
870
    break;
 
871
  }  
 
872
  case ROW_RESULT:
 
873
  default:
 
874
    assert(0);
 
875
  }
 
876
  return val;
 
877
}
 
878
 
 
879
 
 
880
void Item_func_signed::print(String *str, enum_query_type query_type)
 
881
{
 
882
  str->append(STRING_WITH_LEN("cast("));
 
883
  args[0]->print(str, query_type);
 
884
  str->append(STRING_WITH_LEN(" as signed)"));
 
885
 
 
886
}
 
887
 
 
888
 
 
889
longlong Item_func_signed::val_int_from_str(int *error)
 
890
{
 
891
  char buff[MAX_FIELD_WIDTH], *end, *start;
 
892
  uint32 length;
 
893
  String tmp(buff,sizeof(buff), &my_charset_bin), *res;
 
894
  longlong value;
 
895
 
 
896
  /*
 
897
    For a string result, we must first get the string and then convert it
 
898
    to a longlong
 
899
  */
 
900
 
 
901
  if (!(res= args[0]->val_str(&tmp)))
 
902
  {
 
903
    null_value= 1;
 
904
    *error= 0;
 
905
    return 0;
 
906
  }
 
907
  null_value= 0;
 
908
  start= (char *)res->ptr();
 
909
  length= res->length();
 
910
 
 
911
  end= start + length;
 
912
  value= my_strtoll10(start, &end, error);
 
913
  if (*error > 0 || end != start+ length)
 
914
  {
 
915
    char err_buff[128];
 
916
    String err_tmp(err_buff,(uint32) sizeof(err_buff), system_charset_info);
 
917
    err_tmp.copy(start, length, system_charset_info);
 
918
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
 
919
                        ER_TRUNCATED_WRONG_VALUE,
 
920
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
 
921
                        err_tmp.c_ptr());
 
922
  }
 
923
  return value;
 
924
}
 
925
 
 
926
 
 
927
longlong Item_func_signed::val_int()
 
928
{
 
929
  longlong value;
 
930
  int error;
 
931
 
 
932
  if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
933
      args[0]->result_as_longlong())
 
934
  {
 
935
    value= args[0]->val_int();
 
936
    null_value= args[0]->null_value; 
 
937
    return value;
 
938
  }
 
939
 
 
940
  value= val_int_from_str(&error);
 
941
  if (value < 0 && error == 0)
 
942
  {
 
943
    push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
944
                 "Cast to signed converted positive out-of-range integer to "
 
945
                 "it's negative complement");
 
946
  }
 
947
  return value;
 
948
}
 
949
 
 
950
 
 
951
void Item_func_unsigned::print(String *str, enum_query_type query_type)
 
952
{
 
953
  str->append(STRING_WITH_LEN("cast("));
 
954
  args[0]->print(str, query_type);
 
955
  str->append(STRING_WITH_LEN(" as unsigned)"));
 
956
 
 
957
}
 
958
 
 
959
 
 
960
longlong Item_func_unsigned::val_int()
 
961
{
 
962
  longlong value;
 
963
  int error;
 
964
 
 
965
  if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
 
966
  {
 
967
    my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
 
968
    if (!(null_value= args[0]->null_value))
 
969
      my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
 
970
    else
 
971
      value= 0;
 
972
    return value;
 
973
  }
 
974
  else if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
975
           args[0]->result_as_longlong())
 
976
  {
 
977
    value= args[0]->val_int();
 
978
    null_value= args[0]->null_value; 
 
979
    return value;
 
980
  }
 
981
 
 
982
  value= val_int_from_str(&error);
 
983
  if (error < 0)
 
984
    push_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
985
                 "Cast to unsigned converted negative integer to it's "
 
986
                 "positive complement");
 
987
  return value;
 
988
}
 
989
 
 
990
 
 
991
String *Item_decimal_typecast::val_str(String *str)
 
992
{
 
993
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
994
  if (null_value)
 
995
    return NULL;
 
996
  my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
 
997
  return str;
 
998
}
 
999
 
 
1000
 
 
1001
double Item_decimal_typecast::val_real()
 
1002
{
 
1003
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
1004
  double res;
 
1005
  if (null_value)
 
1006
    return 0.0;
 
1007
  my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
 
1008
  return res;
 
1009
}
 
1010
 
 
1011
 
 
1012
longlong Item_decimal_typecast::val_int()
 
1013
{
 
1014
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
1015
  longlong res;
 
1016
  if (null_value)
 
1017
    return 0;
 
1018
  my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
 
1019
  return res;
 
1020
}
 
1021
 
 
1022
 
 
1023
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
 
1024
{
 
1025
  my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
 
1026
  bool sign;
 
1027
  uint precision;
 
1028
 
 
1029
  if ((null_value= args[0]->null_value))
 
1030
    return NULL;
 
1031
  my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
 
1032
  sign= dec->sign();
 
1033
  if (unsigned_flag)
 
1034
  {
 
1035
    if (sign)
 
1036
    {
 
1037
      my_decimal_set_zero(dec);
 
1038
      goto err;
 
1039
    }
 
1040
  }
 
1041
  precision= my_decimal_length_to_precision(max_length,
 
1042
                                            decimals, unsigned_flag);
 
1043
  if (precision - decimals < (uint) my_decimal_intg(dec))
 
1044
  {
 
1045
    max_my_decimal(dec, precision, decimals);
 
1046
    dec->sign(sign);
 
1047
    goto err;
 
1048
  }
 
1049
  return dec;
 
1050
 
 
1051
err:
 
1052
  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
 
1053
                      ER_WARN_DATA_OUT_OF_RANGE,
 
1054
                      ER(ER_WARN_DATA_OUT_OF_RANGE),
 
1055
                      name, 1);
 
1056
  return dec;
 
1057
}
 
1058
 
 
1059
 
 
1060
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
 
1061
{
 
1062
  char len_buf[20*3 + 1];
 
1063
  char *end;
 
1064
 
 
1065
  uint precision= my_decimal_length_to_precision(max_length, decimals,
 
1066
                                                 unsigned_flag);
 
1067
  str->append(STRING_WITH_LEN("cast("));
 
1068
  args[0]->print(str, query_type);
 
1069
  str->append(STRING_WITH_LEN(" as decimal("));
 
1070
 
 
1071
  end=int10_to_str(precision, len_buf,10);
 
1072
  str->append(len_buf, (uint32) (end - len_buf));
 
1073
 
 
1074
  str->append(',');
 
1075
 
 
1076
  end=int10_to_str(decimals, len_buf,10);
 
1077
  str->append(len_buf, (uint32) (end - len_buf));
 
1078
 
 
1079
  str->append(')');
 
1080
  str->append(')');
 
1081
}
 
1082
 
 
1083
 
 
1084
double Item_func_plus::real_op()
 
1085
{
 
1086
  double value= args[0]->val_real() + args[1]->val_real();
 
1087
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1088
    return 0.0;
 
1089
  return fix_result(value);
 
1090
}
 
1091
 
 
1092
 
 
1093
longlong Item_func_plus::int_op()
 
1094
{
 
1095
  longlong value=args[0]->val_int()+args[1]->val_int();
 
1096
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1097
    return 0;
 
1098
  return value;
 
1099
}
 
1100
 
 
1101
 
 
1102
/**
 
1103
  Calculate plus of two decimals.
 
1104
 
 
1105
  @param decimal_value  Buffer that can be used to store result
 
1106
 
 
1107
  @retval
 
1108
    0  Value was NULL;  In this case null_value is set
 
1109
  @retval
 
1110
    \# Value of operation as a decimal
 
1111
*/
 
1112
 
 
1113
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
 
1114
{
 
1115
  my_decimal value1, *val1;
 
1116
  my_decimal value2, *val2;
 
1117
  val1= args[0]->val_decimal(&value1);
 
1118
  if ((null_value= args[0]->null_value))
 
1119
    return 0;
 
1120
  val2= args[1]->val_decimal(&value2);
 
1121
  if (!(null_value= (args[1]->null_value ||
 
1122
                     (my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
 
1123
                                     val2) > 3))))
 
1124
    return decimal_value;
 
1125
  return 0;
 
1126
}
 
1127
 
 
1128
/**
 
1129
  Set precision of results for additive operations (+ and -)
 
1130
*/
 
1131
void Item_func_additive_op::result_precision()
 
1132
{
 
1133
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1134
  int max_int_part= max(args[0]->decimal_precision() - args[0]->decimals,
 
1135
                        args[1]->decimal_precision() - args[1]->decimals);
 
1136
  int precision= min(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
 
1137
 
 
1138
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
1139
  if (result_type() == INT_RESULT)
 
1140
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
1141
  else
 
1142
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
1143
  max_length= my_decimal_precision_to_length(precision, decimals,
 
1144
                                             unsigned_flag);
 
1145
}
 
1146
 
 
1147
 
 
1148
/**
 
1149
  The following function is here to allow the user to force
 
1150
  subtraction of UNSIGNED BIGINT to return negative values.
 
1151
*/
 
1152
 
 
1153
void Item_func_minus::fix_length_and_dec()
 
1154
{
 
1155
  Item_num_op::fix_length_and_dec();
 
1156
  if (unsigned_flag &&
 
1157
      (current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
 
1158
    unsigned_flag=0;
 
1159
}
 
1160
 
 
1161
 
 
1162
double Item_func_minus::real_op()
 
1163
{
 
1164
  double value= args[0]->val_real() - args[1]->val_real();
 
1165
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1166
    return 0.0;
 
1167
  return fix_result(value);
 
1168
}
 
1169
 
 
1170
 
 
1171
longlong Item_func_minus::int_op()
 
1172
{
 
1173
  longlong value=args[0]->val_int() - args[1]->val_int();
 
1174
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1175
    return 0;
 
1176
  return value;
 
1177
}
 
1178
 
 
1179
 
 
1180
/**
 
1181
  See Item_func_plus::decimal_op for comments.
 
1182
*/
 
1183
 
 
1184
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
 
1185
{
 
1186
  my_decimal value1, *val1;
 
1187
  my_decimal value2, *val2= 
 
1188
 
 
1189
  val1= args[0]->val_decimal(&value1);
 
1190
  if ((null_value= args[0]->null_value))
 
1191
    return 0;
 
1192
  val2= args[1]->val_decimal(&value2);
 
1193
  if (!(null_value= (args[1]->null_value ||
 
1194
                     (my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
 
1195
                                     val2) > 3))))
 
1196
    return decimal_value;
 
1197
  return 0;
 
1198
}
 
1199
 
 
1200
 
 
1201
double Item_func_mul::real_op()
 
1202
{
 
1203
  assert(fixed == 1);
 
1204
  double value= args[0]->val_real() * args[1]->val_real();
 
1205
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1206
    return 0.0;
 
1207
  return fix_result(value);
 
1208
}
 
1209
 
 
1210
 
 
1211
longlong Item_func_mul::int_op()
 
1212
{
 
1213
  assert(fixed == 1);
 
1214
  longlong value=args[0]->val_int()*args[1]->val_int();
 
1215
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
1216
    return 0;
 
1217
  return value;
 
1218
}
 
1219
 
 
1220
 
 
1221
/** See Item_func_plus::decimal_op for comments. */
 
1222
 
 
1223
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
 
1224
{
 
1225
  my_decimal value1, *val1;
 
1226
  my_decimal value2, *val2;
 
1227
  val1= args[0]->val_decimal(&value1);
 
1228
  if ((null_value= args[0]->null_value))
 
1229
    return 0;
 
1230
  val2= args[1]->val_decimal(&value2);
 
1231
  if (!(null_value= (args[1]->null_value ||
 
1232
                     (my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
 
1233
                                    val2) > 3))))
 
1234
    return decimal_value;
 
1235
  return 0;
 
1236
}
 
1237
 
 
1238
 
 
1239
void Item_func_mul::result_precision()
 
1240
{
 
1241
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
1242
  if (result_type() == INT_RESULT)
 
1243
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
1244
  else
 
1245
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
1246
  decimals= min(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
1247
  int precision= min(args[0]->decimal_precision() + args[1]->decimal_precision(),
 
1248
                     DECIMAL_MAX_PRECISION);
 
1249
  max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
 
1250
}
 
1251
 
 
1252
 
 
1253
double Item_func_div::real_op()
 
1254
{
 
1255
  assert(fixed == 1);
 
1256
  double value= args[0]->val_real();
 
1257
  double val2= args[1]->val_real();
 
1258
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
1259
    return 0.0;
 
1260
  if (val2 == 0.0)
 
1261
  {
 
1262
    signal_divide_by_null();
 
1263
    return 0.0;
 
1264
  }
 
1265
  return fix_result(value/val2);
 
1266
}
 
1267
 
 
1268
 
 
1269
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
 
1270
{
 
1271
  my_decimal value1, *val1;
 
1272
  my_decimal value2, *val2;
 
1273
  int err;
 
1274
 
 
1275
  val1= args[0]->val_decimal(&value1);
 
1276
  if ((null_value= args[0]->null_value))
 
1277
    return 0;
 
1278
  val2= args[1]->val_decimal(&value2);
 
1279
  if ((null_value= args[1]->null_value))
 
1280
    return 0;
 
1281
  if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
1282
                           val1, val2, prec_increment)) > 3)
 
1283
  {
 
1284
    if (err == E_DEC_DIV_ZERO)
 
1285
      signal_divide_by_null();
 
1286
    null_value= 1;
 
1287
    return 0;
 
1288
  }
 
1289
  return decimal_value;
 
1290
}
 
1291
 
 
1292
 
 
1293
void Item_func_div::result_precision()
 
1294
{
 
1295
  uint precision=min(args[0]->decimal_precision() + prec_increment,
 
1296
                     DECIMAL_MAX_PRECISION);
 
1297
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
1298
  if (result_type() == INT_RESULT)
 
1299
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
1300
  else
 
1301
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
1302
  decimals= min(args[0]->decimals + prec_increment, DECIMAL_MAX_SCALE);
 
1303
  max_length= my_decimal_precision_to_length(precision, decimals,
 
1304
                                             unsigned_flag);
 
1305
}
 
1306
 
 
1307
 
 
1308
void Item_func_div::fix_length_and_dec()
 
1309
{
 
1310
  prec_increment= current_thd->variables.div_precincrement;
 
1311
  Item_num_op::fix_length_and_dec();
 
1312
  switch(hybrid_type) {
 
1313
  case REAL_RESULT:
 
1314
  {
 
1315
    decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
 
1316
    set_if_smaller(decimals, NOT_FIXED_DEC);
 
1317
    max_length=args[0]->max_length - args[0]->decimals + decimals;
 
1318
    uint tmp=float_length(decimals);
 
1319
    set_if_smaller(max_length,tmp);
 
1320
    break;
 
1321
  }
 
1322
  case INT_RESULT:
 
1323
    hybrid_type= DECIMAL_RESULT;
 
1324
    result_precision();
 
1325
    break;
 
1326
  case DECIMAL_RESULT:
 
1327
    result_precision();
 
1328
    break;
 
1329
  default:
 
1330
    assert(0);
 
1331
  }
 
1332
  maybe_null= 1; // devision by zero
 
1333
  return;
 
1334
}
 
1335
 
 
1336
 
 
1337
/* Integer division */
 
1338
longlong Item_func_int_div::val_int()
 
1339
{
 
1340
  assert(fixed == 1);
 
1341
  longlong value=args[0]->val_int();
 
1342
  longlong val2=args[1]->val_int();
 
1343
  if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
1344
    return 0;
 
1345
  if (val2 == 0)
 
1346
  {
 
1347
    signal_divide_by_null();
 
1348
    return 0;
 
1349
  }
 
1350
  return (unsigned_flag ?
 
1351
          (ulonglong) value / (ulonglong) val2 :
 
1352
          value / val2);
 
1353
}
 
1354
 
 
1355
 
 
1356
void Item_func_int_div::fix_length_and_dec()
 
1357
{
 
1358
  Item_result argtype= args[0]->result_type();
 
1359
  /* use precision ony for the data type it is applicable for and valid */
 
1360
  max_length=args[0]->max_length -
 
1361
    (argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
 
1362
     args[0]->decimals : 0);
 
1363
  maybe_null=1;
 
1364
  unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
 
1365
}
 
1366
 
 
1367
 
 
1368
longlong Item_func_mod::int_op()
 
1369
{
 
1370
  assert(fixed == 1);
 
1371
  longlong value=  args[0]->val_int();
 
1372
  longlong val2= args[1]->val_int();
 
1373
  longlong result;
 
1374
 
 
1375
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
1376
    return 0; /* purecov: inspected */
 
1377
  if (val2 == 0)
 
1378
  {
 
1379
    signal_divide_by_null();
 
1380
    return 0;
 
1381
  }
 
1382
 
 
1383
  if (args[0]->unsigned_flag)
 
1384
    result= args[1]->unsigned_flag ? 
 
1385
      ((ulonglong) value) % ((ulonglong) val2) : ((ulonglong) value) % val2;
 
1386
  else
 
1387
    result= args[1]->unsigned_flag ?
 
1388
      value % ((ulonglong) val2) : value % val2;
 
1389
 
 
1390
  return result;
 
1391
}
 
1392
 
 
1393
double Item_func_mod::real_op()
 
1394
{
 
1395
  assert(fixed == 1);
 
1396
  double value= args[0]->val_real();
 
1397
  double val2=  args[1]->val_real();
 
1398
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
1399
    return 0.0; /* purecov: inspected */
 
1400
  if (val2 == 0.0)
 
1401
  {
 
1402
    signal_divide_by_null();
 
1403
    return 0.0;
 
1404
  }
 
1405
  return fmod(value,val2);
 
1406
}
 
1407
 
 
1408
 
 
1409
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
 
1410
{
 
1411
  my_decimal value1, *val1;
 
1412
  my_decimal value2, *val2;
 
1413
 
 
1414
  val1= args[0]->val_decimal(&value1);
 
1415
  if ((null_value= args[0]->null_value))
 
1416
    return 0;
 
1417
  val2= args[1]->val_decimal(&value2);
 
1418
  if ((null_value= args[1]->null_value))
 
1419
    return 0;
 
1420
  switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
1421
                         val1, val2)) {
 
1422
  case E_DEC_TRUNCATED:
 
1423
  case E_DEC_OK:
 
1424
    return decimal_value;
 
1425
  case E_DEC_DIV_ZERO:
 
1426
    signal_divide_by_null();
 
1427
  default:
 
1428
    null_value= 1;
 
1429
    return 0;
 
1430
  }
 
1431
}
 
1432
 
 
1433
 
 
1434
void Item_func_mod::result_precision()
 
1435
{
 
1436
  decimals= max(args[0]->decimals, args[1]->decimals);
 
1437
  max_length= max(args[0]->max_length, args[1]->max_length);
 
1438
}
 
1439
 
 
1440
 
 
1441
void Item_func_mod::fix_length_and_dec()
 
1442
{
 
1443
  Item_num_op::fix_length_and_dec();
 
1444
  maybe_null= 1;
 
1445
  unsigned_flag= args[0]->unsigned_flag;
 
1446
}
 
1447
 
 
1448
 
 
1449
double Item_func_neg::real_op()
 
1450
{
 
1451
  double value= args[0]->val_real();
 
1452
  null_value= args[0]->null_value;
 
1453
  return -value;
 
1454
}
 
1455
 
 
1456
 
 
1457
longlong Item_func_neg::int_op()
 
1458
{
 
1459
  longlong value= args[0]->val_int();
 
1460
  null_value= args[0]->null_value;
 
1461
  return -value;
 
1462
}
 
1463
 
 
1464
 
 
1465
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
 
1466
{
 
1467
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1468
  if (!(null_value= args[0]->null_value))
 
1469
  {
 
1470
    my_decimal2decimal(value, decimal_value);
 
1471
    my_decimal_neg(decimal_value);
 
1472
    return decimal_value;
 
1473
  }
 
1474
  return 0;
 
1475
}
 
1476
 
 
1477
 
 
1478
void Item_func_neg::fix_num_length_and_dec()
 
1479
{
 
1480
  decimals= args[0]->decimals;
 
1481
  /* 1 add because sign can appear */
 
1482
  max_length= args[0]->max_length + 1;
 
1483
}
 
1484
 
 
1485
 
 
1486
void Item_func_neg::fix_length_and_dec()
 
1487
{
 
1488
  Item_func_num1::fix_length_and_dec();
 
1489
 
 
1490
  /*
 
1491
    If this is in integer context keep the context as integer if possible
 
1492
    (This is how multiplication and other integer functions works)
 
1493
    Use val() to get value as arg_type doesn't mean that item is
 
1494
    Item_int or Item_real due to existence of Item_param.
 
1495
  */
 
1496
  if (hybrid_type == INT_RESULT && args[0]->const_item())
 
1497
  {
 
1498
    longlong val= args[0]->val_int();
 
1499
    if ((ulonglong) val >= (ulonglong) LONGLONG_MIN &&
 
1500
        ((ulonglong) val != (ulonglong) LONGLONG_MIN ||
 
1501
          args[0]->type() != INT_ITEM))        
 
1502
    {
 
1503
      /*
 
1504
        Ensure that result is converted to DECIMAL, as longlong can't hold
 
1505
        the negated number
 
1506
      */
 
1507
      hybrid_type= DECIMAL_RESULT;
 
1508
    }
 
1509
  }
 
1510
  unsigned_flag= 0;
 
1511
  return;
 
1512
}
 
1513
 
 
1514
 
 
1515
double Item_func_abs::real_op()
 
1516
{
 
1517
  double value= args[0]->val_real();
 
1518
  null_value= args[0]->null_value;
 
1519
  return fabs(value);
 
1520
}
 
1521
 
 
1522
 
 
1523
longlong Item_func_abs::int_op()
 
1524
{
 
1525
  longlong value= args[0]->val_int();
 
1526
  if ((null_value= args[0]->null_value))
 
1527
    return 0;
 
1528
  return (value >= 0) || unsigned_flag ? value : -value;
 
1529
}
 
1530
 
 
1531
 
 
1532
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
 
1533
{
 
1534
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1535
  if (!(null_value= args[0]->null_value))
 
1536
  {
 
1537
    my_decimal2decimal(value, decimal_value);
 
1538
    if (decimal_value->sign())
 
1539
      my_decimal_neg(decimal_value);
 
1540
    return decimal_value;
 
1541
  }
 
1542
  return 0;
 
1543
}
 
1544
 
 
1545
 
 
1546
void Item_func_abs::fix_length_and_dec()
 
1547
{
 
1548
  Item_func_num1::fix_length_and_dec();
 
1549
  unsigned_flag= args[0]->unsigned_flag;
 
1550
}
 
1551
 
 
1552
 
 
1553
/** Gateway to natural LOG function. */
 
1554
double Item_func_ln::val_real()
 
1555
{
 
1556
  assert(fixed == 1);
 
1557
  double value= args[0]->val_real();
 
1558
  if ((null_value= args[0]->null_value))
 
1559
    return 0.0;
 
1560
  if (value <= 0.0)
 
1561
  {
 
1562
    signal_divide_by_null();
 
1563
    return 0.0;
 
1564
  }
 
1565
  return log(value);
 
1566
}
 
1567
 
 
1568
/** 
 
1569
  Extended but so slower LOG function.
 
1570
 
 
1571
  We have to check if all values are > zero and first one is not one
 
1572
  as these are the cases then result is not a number.
 
1573
*/ 
 
1574
double Item_func_log::val_real()
 
1575
{
 
1576
  assert(fixed == 1);
 
1577
  double value= args[0]->val_real();
 
1578
  if ((null_value= args[0]->null_value))
 
1579
    return 0.0;
 
1580
  if (value <= 0.0)
 
1581
  {
 
1582
    signal_divide_by_null();
 
1583
    return 0.0;
 
1584
  }
 
1585
  if (arg_count == 2)
 
1586
  {
 
1587
    double value2= args[1]->val_real();
 
1588
    if ((null_value= args[1]->null_value))
 
1589
      return 0.0;
 
1590
    if (value2 <= 0.0 || value == 1.0)
 
1591
    {
 
1592
      signal_divide_by_null();
 
1593
      return 0.0;
 
1594
    }
 
1595
    return log(value2) / log(value);
 
1596
  }
 
1597
  return log(value);
 
1598
}
 
1599
 
 
1600
double Item_func_log2::val_real()
 
1601
{
 
1602
  assert(fixed == 1);
 
1603
  double value= args[0]->val_real();
 
1604
 
 
1605
  if ((null_value=args[0]->null_value))
 
1606
    return 0.0;
 
1607
  if (value <= 0.0)
 
1608
  {
 
1609
    signal_divide_by_null();
 
1610
    return 0.0;
 
1611
  }
 
1612
  return log(value) / M_LN2;
 
1613
}
 
1614
 
 
1615
double Item_func_log10::val_real()
 
1616
{
 
1617
  assert(fixed == 1);
 
1618
  double value= args[0]->val_real();
 
1619
  if ((null_value= args[0]->null_value))
 
1620
    return 0.0;
 
1621
  if (value <= 0.0)
 
1622
  {
 
1623
    signal_divide_by_null();
 
1624
    return 0.0;
 
1625
  }
 
1626
  return log10(value);
 
1627
}
 
1628
 
 
1629
double Item_func_exp::val_real()
 
1630
{
 
1631
  assert(fixed == 1);
 
1632
  double value= args[0]->val_real();
 
1633
  if ((null_value=args[0]->null_value))
 
1634
    return 0.0; /* purecov: inspected */
 
1635
  return fix_result(exp(value));
 
1636
}
 
1637
 
 
1638
double Item_func_sqrt::val_real()
 
1639
{
 
1640
  assert(fixed == 1);
 
1641
  double value= args[0]->val_real();
 
1642
  if ((null_value=(args[0]->null_value || value < 0)))
 
1643
    return 0.0; /* purecov: inspected */
 
1644
  return sqrt(value);
 
1645
}
 
1646
 
 
1647
double Item_func_pow::val_real()
 
1648
{
 
1649
  assert(fixed == 1);
 
1650
  double value= args[0]->val_real();
 
1651
  double val2= args[1]->val_real();
 
1652
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
 
1653
    return 0.0; /* purecov: inspected */
 
1654
  return fix_result(pow(value,val2));
 
1655
}
 
1656
 
 
1657
// Trigonometric functions
 
1658
 
 
1659
double Item_func_acos::val_real()
 
1660
{
 
1661
  assert(fixed == 1);
 
1662
  // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
 
1663
  volatile double value= args[0]->val_real();
 
1664
  if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
1665
    return 0.0;
 
1666
  return acos(value);
 
1667
}
 
1668
 
 
1669
double Item_func_asin::val_real()
 
1670
{
 
1671
  assert(fixed == 1);
 
1672
  // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
 
1673
  volatile double value= args[0]->val_real();
 
1674
  if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
1675
    return 0.0;
 
1676
  return asin(value);
 
1677
}
 
1678
 
 
1679
double Item_func_atan::val_real()
 
1680
{
 
1681
  assert(fixed == 1);
 
1682
  double value= args[0]->val_real();
 
1683
  if ((null_value=args[0]->null_value))
 
1684
    return 0.0;
 
1685
  if (arg_count == 2)
 
1686
  {
 
1687
    double val2= args[1]->val_real();
 
1688
    if ((null_value=args[1]->null_value))
 
1689
      return 0.0;
 
1690
    return fix_result(atan2(value,val2));
 
1691
  }
 
1692
  return atan(value);
 
1693
}
 
1694
 
 
1695
double Item_func_cos::val_real()
 
1696
{
 
1697
  assert(fixed == 1);
 
1698
  double value= args[0]->val_real();
 
1699
  if ((null_value=args[0]->null_value))
 
1700
    return 0.0;
 
1701
  return cos(value);
 
1702
}
 
1703
 
 
1704
double Item_func_sin::val_real()
 
1705
{
 
1706
  assert(fixed == 1);
 
1707
  double value= args[0]->val_real();
 
1708
  if ((null_value=args[0]->null_value))
 
1709
    return 0.0;
 
1710
  return sin(value);
 
1711
}
 
1712
 
 
1713
double Item_func_tan::val_real()
 
1714
{
 
1715
  assert(fixed == 1);
 
1716
  double value= args[0]->val_real();
 
1717
  if ((null_value=args[0]->null_value))
 
1718
    return 0.0;
 
1719
  return fix_result(tan(value));
 
1720
}
 
1721
 
 
1722
 
153
1723
// Shift-functions, same as << and >> in C/C++
154
1724
 
155
 
int64_t Item_func_shift_left::val_int()
 
1725
 
 
1726
longlong Item_func_shift_left::val_int()
156
1727
{
157
1728
  assert(fixed == 1);
158
 
  uint32_t shift;
159
 
  uint64_t res= ((uint64_t) args[0]->val_int() <<
 
1729
  uint shift;
 
1730
  ulonglong res= ((ulonglong) args[0]->val_int() <<
160
1731
                  (shift=(uint) args[1]->val_int()));
161
1732
  if (args[0]->null_value || args[1]->null_value)
162
1733
  {
164
1735
    return 0;
165
1736
  }
166
1737
  null_value=0;
167
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
 
1738
  return (shift < sizeof(longlong)*8 ? (longlong) res : 0LL);
168
1739
}
169
1740
 
170
 
int64_t Item_func_shift_right::val_int()
 
1741
longlong Item_func_shift_right::val_int()
171
1742
{
172
1743
  assert(fixed == 1);
173
 
  uint32_t shift;
174
 
  uint64_t res= (uint64_t) args[0]->val_int() >>
 
1744
  uint shift;
 
1745
  ulonglong res= (ulonglong) args[0]->val_int() >>
175
1746
    (shift=(uint) args[1]->val_int());
176
1747
  if (args[0]->null_value || args[1]->null_value)
177
1748
  {
179
1750
    return 0;
180
1751
  }
181
1752
  null_value=0;
182
 
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
 
1753
  return (shift < sizeof(longlong)*8 ? (longlong) res : 0LL);
183
1754
}
184
1755
 
185
1756
 
186
 
int64_t Item_func_bit_neg::val_int()
 
1757
longlong Item_func_bit_neg::val_int()
187
1758
{
188
1759
  assert(fixed == 1);
189
 
  uint64_t res= (uint64_t) args[0]->val_int();
 
1760
  ulonglong res= (ulonglong) args[0]->val_int();
190
1761
  if ((null_value=args[0]->null_value))
191
1762
    return 0;
192
1763
  return ~res;
198
1769
void Item_func_integer::fix_length_and_dec()
199
1770
{
200
1771
  max_length=args[0]->max_length - args[0]->decimals+1;
201
 
  uint32_t tmp=float_length(decimals);
 
1772
  uint tmp=float_length(decimals);
202
1773
  set_if_smaller(max_length,tmp);
203
1774
  decimals=0;
204
1775
}
205
1776
 
 
1777
void Item_func_int_val::fix_num_length_and_dec()
 
1778
{
 
1779
  max_length= args[0]->max_length - (args[0]->decimals ?
 
1780
                                     args[0]->decimals + 1 :
 
1781
                                     0) + 2;
 
1782
  uint tmp= float_length(decimals);
 
1783
  set_if_smaller(max_length,tmp);
 
1784
  decimals= 0;
 
1785
}
 
1786
 
 
1787
 
 
1788
void Item_func_int_val::find_num_type()
 
1789
{
 
1790
  switch(hybrid_type= args[0]->result_type())
 
1791
  {
 
1792
  case STRING_RESULT:
 
1793
  case REAL_RESULT:
 
1794
    hybrid_type= REAL_RESULT;
 
1795
    max_length= float_length(decimals);
 
1796
    break;
 
1797
  case INT_RESULT:
 
1798
  case DECIMAL_RESULT:
 
1799
    /*
 
1800
      -2 because in most high position can't be used any digit for longlong
 
1801
      and one position for increasing value during operation
 
1802
    */
 
1803
    if ((args[0]->max_length - args[0]->decimals) >=
 
1804
        (DECIMAL_LONGLONG_DIGITS - 2))
 
1805
    {
 
1806
      hybrid_type= DECIMAL_RESULT;
 
1807
    }
 
1808
    else
 
1809
    {
 
1810
      unsigned_flag= args[0]->unsigned_flag;
 
1811
      hybrid_type= INT_RESULT;
 
1812
    }
 
1813
    break;
 
1814
  default:
 
1815
    assert(0);
 
1816
  }
 
1817
  return;
 
1818
}
 
1819
 
 
1820
 
 
1821
longlong Item_func_ceiling::int_op()
 
1822
{
 
1823
  longlong result;
 
1824
  switch (args[0]->result_type()) {
 
1825
  case INT_RESULT:
 
1826
    result= args[0]->val_int();
 
1827
    null_value= args[0]->null_value;
 
1828
    break;
 
1829
  case DECIMAL_RESULT:
 
1830
  {
 
1831
    my_decimal dec_buf, *dec;
 
1832
    if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
 
1833
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1834
    else
 
1835
      result= 0;
 
1836
    break;
 
1837
  }
 
1838
  default:
 
1839
    result= (longlong)Item_func_ceiling::real_op();
 
1840
  };
 
1841
  return result;
 
1842
}
 
1843
 
 
1844
 
 
1845
double Item_func_ceiling::real_op()
 
1846
{
 
1847
  /*
 
1848
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1849
    bug)
 
1850
  */
 
1851
  volatile double value= args[0]->val_real();
 
1852
  null_value= args[0]->null_value;
 
1853
  return ceil(value);
 
1854
}
 
1855
 
 
1856
 
 
1857
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
 
1858
{
 
1859
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1860
  if (!(null_value= (args[0]->null_value ||
 
1861
                     my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
 
1862
                                        decimal_value) > 1)))
 
1863
    return decimal_value;
 
1864
  return 0;
 
1865
}
 
1866
 
 
1867
 
 
1868
longlong Item_func_floor::int_op()
 
1869
{
 
1870
  longlong result;
 
1871
  switch (args[0]->result_type()) {
 
1872
  case INT_RESULT:
 
1873
    result= args[0]->val_int();
 
1874
    null_value= args[0]->null_value;
 
1875
    break;
 
1876
  case DECIMAL_RESULT:
 
1877
  {
 
1878
    my_decimal dec_buf, *dec;
 
1879
    if ((dec= Item_func_floor::decimal_op(&dec_buf)))
 
1880
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1881
    else
 
1882
      result= 0;
 
1883
    break;
 
1884
  }
 
1885
  default:
 
1886
    result= (longlong)Item_func_floor::real_op();
 
1887
  };
 
1888
  return result;
 
1889
}
 
1890
 
 
1891
 
 
1892
double Item_func_floor::real_op()
 
1893
{
 
1894
  /*
 
1895
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1896
    bug)
 
1897
  */
 
1898
  volatile double value= args[0]->val_real();
 
1899
  null_value= args[0]->null_value;
 
1900
  return floor(value);
 
1901
}
 
1902
 
 
1903
 
 
1904
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
 
1905
{
 
1906
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1907
  if (!(null_value= (args[0]->null_value ||
 
1908
                     my_decimal_floor(E_DEC_FATAL_ERROR, value,
 
1909
                                      decimal_value) > 1)))
 
1910
    return decimal_value;
 
1911
  return 0;
 
1912
}
 
1913
 
 
1914
 
 
1915
void Item_func_round::fix_length_and_dec()
 
1916
{
 
1917
  int      decimals_to_set;
 
1918
  longlong val1;
 
1919
  bool     val1_unsigned;
 
1920
  
 
1921
  unsigned_flag= args[0]->unsigned_flag;
 
1922
  if (!args[1]->const_item())
 
1923
  {
 
1924
    max_length= args[0]->max_length;
 
1925
    decimals= args[0]->decimals;
 
1926
    if (args[0]->result_type() == DECIMAL_RESULT)
 
1927
    {
 
1928
      max_length++;
 
1929
      hybrid_type= DECIMAL_RESULT;
 
1930
    }
 
1931
    else
 
1932
      hybrid_type= REAL_RESULT;
 
1933
    return;
 
1934
  }
 
1935
 
 
1936
  val1= args[1]->val_int();
 
1937
  val1_unsigned= args[1]->unsigned_flag;
 
1938
  if (val1 < 0)
 
1939
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
 
1940
  else
 
1941
    decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
 
1942
 
 
1943
  if (args[0]->decimals == NOT_FIXED_DEC)
 
1944
  {
 
1945
    max_length= args[0]->max_length;
 
1946
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
 
1947
    hybrid_type= REAL_RESULT;
 
1948
    return;
 
1949
  }
 
1950
  
 
1951
  switch (args[0]->result_type()) {
 
1952
  case REAL_RESULT:
 
1953
  case STRING_RESULT:
 
1954
    hybrid_type= REAL_RESULT;
 
1955
    decimals= min(decimals_to_set, NOT_FIXED_DEC);
 
1956
    max_length= float_length(decimals);
 
1957
    break;
 
1958
  case INT_RESULT:
 
1959
    if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
 
1960
    {
 
1961
      int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
 
1962
      max_length= args[0]->max_length + length_can_increase;
 
1963
      /* Here we can keep INT_RESULT */
 
1964
      hybrid_type= INT_RESULT;
 
1965
      decimals= 0;
 
1966
      break;
 
1967
    }
 
1968
    /* fall through */
 
1969
  case DECIMAL_RESULT:
 
1970
  {
 
1971
    hybrid_type= DECIMAL_RESULT;
 
1972
    decimals_to_set= min(DECIMAL_MAX_SCALE, decimals_to_set);
 
1973
    int decimals_delta= args[0]->decimals - decimals_to_set;
 
1974
    int precision= args[0]->decimal_precision();
 
1975
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
1976
 
 
1977
    precision-= decimals_delta - length_increase;
 
1978
    decimals= min(decimals_to_set, DECIMAL_MAX_SCALE);
 
1979
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1980
                                               unsigned_flag);
 
1981
    break;
 
1982
  }
 
1983
  default:
 
1984
    assert(0); /* This result type isn't handled */
 
1985
  }
 
1986
}
 
1987
 
 
1988
double my_double_round(double value, longlong dec, bool dec_unsigned,
 
1989
                       bool truncate)
 
1990
{
 
1991
  double tmp;
 
1992
  bool dec_negative= (dec < 0) && !dec_unsigned;
 
1993
  ulonglong abs_dec= dec_negative ? -dec : dec;
 
1994
  /*
 
1995
    tmp2 is here to avoid return the value with 80 bit precision
 
1996
    This will fix that the test round(0.1,1) = round(0.1,1) is true
 
1997
  */
 
1998
  volatile double tmp2;
 
1999
 
 
2000
  tmp=(abs_dec < array_elements(log_10) ?
 
2001
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
 
2002
 
 
2003
  if (dec_negative && my_isinf(tmp))
 
2004
    tmp2= 0;
 
2005
  else if (!dec_negative && my_isinf(value * tmp))
 
2006
    tmp2= value;
 
2007
  else if (truncate)
 
2008
  {
 
2009
    if (value >= 0)
 
2010
      tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
 
2011
    else
 
2012
      tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
 
2013
  }
 
2014
  else
 
2015
    tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
 
2016
  return tmp2;
 
2017
}
 
2018
 
 
2019
 
 
2020
double Item_func_round::real_op()
 
2021
{
 
2022
  double value= args[0]->val_real();
 
2023
 
 
2024
  if (!(null_value= args[0]->null_value || args[1]->null_value))
 
2025
    return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
 
2026
                           truncate);
 
2027
 
 
2028
  return 0.0;
 
2029
}
 
2030
 
 
2031
/*
 
2032
  Rounds a given value to a power of 10 specified as the 'to' argument,
 
2033
  avoiding overflows when the value is close to the ulonglong range boundary.
 
2034
*/
 
2035
 
 
2036
static inline ulonglong my_unsigned_round(ulonglong value, ulonglong to)
 
2037
{
 
2038
  ulonglong tmp= value / to * to;
 
2039
  return (value - tmp < (to >> 1)) ? tmp : tmp + to;
 
2040
}
 
2041
 
 
2042
 
 
2043
longlong Item_func_round::int_op()
 
2044
{
 
2045
  longlong value= args[0]->val_int();
 
2046
  longlong dec= args[1]->val_int();
 
2047
  decimals= 0;
 
2048
  ulonglong abs_dec;
 
2049
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
2050
    return 0;
 
2051
  if ((dec >= 0) || args[1]->unsigned_flag)
 
2052
    return value; // integer have not digits after point
 
2053
 
 
2054
  abs_dec= -dec;
 
2055
  longlong tmp;
 
2056
  
 
2057
  if(abs_dec >= array_elements(log_10_int))
 
2058
    return 0;
 
2059
  
 
2060
  tmp= log_10_int[abs_dec];
 
2061
  
 
2062
  if (truncate)
 
2063
    value= (unsigned_flag) ?
 
2064
      ((ulonglong) value / tmp) * tmp : (value / tmp) * tmp;
 
2065
  else
 
2066
    value= (unsigned_flag || value >= 0) ?
 
2067
      my_unsigned_round((ulonglong) value, tmp) :
 
2068
      -(longlong) my_unsigned_round((ulonglong) -value, tmp);
 
2069
  return value;
 
2070
}
 
2071
 
 
2072
 
 
2073
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
 
2074
{
 
2075
  my_decimal val, *value= args[0]->val_decimal(&val);
 
2076
  longlong dec= args[1]->val_int();
 
2077
  if (dec >= 0 || args[1]->unsigned_flag)
 
2078
    dec= min((ulonglong) dec, decimals);
 
2079
  else if (dec < INT_MIN)
 
2080
    dec= INT_MIN;
 
2081
    
 
2082
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
 
2083
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
 
2084
                                      truncate, decimal_value) > 1))) 
 
2085
  {
 
2086
    decimal_value->frac= decimals;
 
2087
    return decimal_value;
 
2088
  }
 
2089
  return 0;
 
2090
}
 
2091
 
 
2092
 
 
2093
void Item_func_rand::seed_random(Item *arg)
 
2094
{
 
2095
  /*
 
2096
    TODO: do not do reinit 'rand' for every execute of PS/SP if
 
2097
    args[0] is a constant.
 
2098
  */
 
2099
  uint32 tmp= (uint32) arg->val_int();
 
2100
  randominit(rand, (uint32) (tmp*0x10001L+55555555L),
 
2101
             (uint32) (tmp*0x10000001L));
 
2102
}
 
2103
 
 
2104
 
 
2105
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
 
2106
{
 
2107
  if (Item_real_func::fix_fields(thd, ref))
 
2108
    return true;
 
2109
  used_tables_cache|= RAND_TABLE_BIT;
 
2110
  if (arg_count)
 
2111
  {                                     // Only use argument once in query
 
2112
    /*
 
2113
      Allocate rand structure once: we must use thd->stmt_arena
 
2114
      to create rand in proper mem_root if it's a prepared statement or
 
2115
      stored procedure.
 
2116
 
 
2117
      No need to send a Rand log event if seed was given eg: RAND(seed),
 
2118
      as it will be replicated in the query as such.
 
2119
    */
 
2120
    if (!rand && !(rand= (struct rand_struct*)
 
2121
                   thd->stmt_arena->alloc(sizeof(*rand))))
 
2122
      return true;
 
2123
 
 
2124
    if (args[0]->const_item())
 
2125
      seed_random (args[0]);
 
2126
  }
 
2127
  else
 
2128
  {
 
2129
    /*
 
2130
      Save the seed only the first time RAND() is used in the query
 
2131
      Once events are forwarded rather than recreated,
 
2132
      the following can be skipped if inside the slave thread
 
2133
    */
 
2134
    if (!thd->rand_used)
 
2135
    {
 
2136
      thd->rand_used= 1;
 
2137
      thd->rand_saved_seed1= thd->rand.seed1;
 
2138
      thd->rand_saved_seed2= thd->rand.seed2;
 
2139
    }
 
2140
    rand= &thd->rand;
 
2141
  }
 
2142
  return false;
 
2143
}
 
2144
 
 
2145
void Item_func_rand::update_used_tables()
 
2146
{
 
2147
  Item_real_func::update_used_tables();
 
2148
  used_tables_cache|= RAND_TABLE_BIT;
 
2149
}
 
2150
 
 
2151
 
 
2152
double Item_func_rand::val_real()
 
2153
{
 
2154
  assert(fixed == 1);
 
2155
  if (arg_count && !args[0]->const_item())
 
2156
    seed_random (args[0]);
 
2157
  return my_rnd(rand);
 
2158
}
 
2159
 
 
2160
longlong Item_func_sign::val_int()
 
2161
{
 
2162
  assert(fixed == 1);
 
2163
  double value= args[0]->val_real();
 
2164
  null_value=args[0]->null_value;
 
2165
  return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
 
2166
}
 
2167
 
 
2168
 
206
2169
double Item_func_units::val_real()
207
2170
{
208
2171
  assert(fixed == 1);
213
2176
}
214
2177
 
215
2178
 
216
 
int64_t Item_func_char_length::val_int()
217
 
{
218
 
  assert(fixed == 1);
219
 
  String *res=args[0]->val_str(&value);
220
 
  if (!res)
221
 
  {
222
 
    null_value=1;
223
 
    return 0; /* purecov: inspected */
224
 
  }
225
 
  null_value=0;
226
 
  return (int64_t) res->numchars();
227
 
}
228
 
 
229
 
 
230
 
int64_t Item_func_coercibility::val_int()
 
2179
void Item_func_min_max::fix_length_and_dec()
 
2180
{
 
2181
  int max_int_part=0;
 
2182
  bool datetime_found= false;
 
2183
  decimals=0;
 
2184
  max_length=0;
 
2185
  maybe_null=0;
 
2186
  cmp_type=args[0]->result_type();
 
2187
 
 
2188
  for (uint i=0 ; i < arg_count ; i++)
 
2189
  {
 
2190
    set_if_bigger(max_length, args[i]->max_length);
 
2191
    set_if_bigger(decimals, args[i]->decimals);
 
2192
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
 
2193
    if (args[i]->maybe_null)
 
2194
      maybe_null=1;
 
2195
    cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
 
2196
    if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
 
2197
    {
 
2198
      datetime_found= true;
 
2199
      if (!datetime_item || args[i]->field_type() == MYSQL_TYPE_DATETIME)
 
2200
        datetime_item= args[i];
 
2201
    }
 
2202
  }
 
2203
  if (cmp_type == STRING_RESULT)
 
2204
  {
 
2205
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
 
2206
    if (datetime_found)
 
2207
    {
 
2208
      thd= current_thd;
 
2209
      compare_as_dates= true;
 
2210
    }
 
2211
  }
 
2212
  else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
 
2213
    max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
 
2214
                                            unsigned_flag);
 
2215
  cached_field_type= agg_field_type(args, arg_count);
 
2216
}
 
2217
 
 
2218
 
 
2219
/*
 
2220
  Compare item arguments in the DATETIME context.
 
2221
 
 
2222
  SYNOPSIS
 
2223
    cmp_datetimes()
 
2224
    value [out]   found least/greatest DATE/DATETIME value
 
2225
 
 
2226
  DESCRIPTION
 
2227
    Compare item arguments as DATETIME values and return the index of the
 
2228
    least/greatest argument in the arguments array.
 
2229
    The correct integer DATE/DATETIME value of the found argument is
 
2230
    stored to the value pointer, if latter is provided.
 
2231
 
 
2232
  RETURN
 
2233
   0    If one of arguments is NULL
 
2234
   #    index of the least/greatest argument
 
2235
*/
 
2236
 
 
2237
uint Item_func_min_max::cmp_datetimes(ulonglong *value)
 
2238
{
 
2239
  ulonglong min_max= 0;
 
2240
  uint min_max_idx= 0;
 
2241
 
 
2242
  for (uint i=0; i < arg_count ; i++)
 
2243
  {
 
2244
    Item **arg= args + i;
 
2245
    bool is_null;
 
2246
    ulonglong res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
 
2247
    if ((null_value= args[i]->null_value))
 
2248
      return 0;
 
2249
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
2250
    {
 
2251
      min_max= res;
 
2252
      min_max_idx= i;
 
2253
    }
 
2254
  }
 
2255
  if (value)
 
2256
  {
 
2257
    *value= min_max;
 
2258
    if (datetime_item->field_type() == MYSQL_TYPE_NEWDATE)
 
2259
      *value/= 1000000L;
 
2260
  }
 
2261
  return min_max_idx;
 
2262
}
 
2263
 
 
2264
 
 
2265
String *Item_func_min_max::val_str(String *str)
 
2266
{
 
2267
  assert(fixed == 1);
 
2268
  if (compare_as_dates)
 
2269
  {
 
2270
    String *str_res;
 
2271
    uint min_max_idx= cmp_datetimes(NULL);
 
2272
    if (null_value)
 
2273
      return 0;
 
2274
    str_res= args[min_max_idx]->val_str(str);
 
2275
    str_res->set_charset(collation.collation);
 
2276
    return str_res;
 
2277
  }
 
2278
  switch (cmp_type) {
 
2279
  case INT_RESULT:
 
2280
  {
 
2281
    longlong nr=val_int();
 
2282
    if (null_value)
 
2283
      return 0;
 
2284
    str->set_int(nr, unsigned_flag, &my_charset_bin);
 
2285
    return str;
 
2286
  }
 
2287
  case DECIMAL_RESULT:
 
2288
  {
 
2289
    my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
 
2290
    if (null_value)
 
2291
      return 0;
 
2292
    my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
 
2293
    return str;
 
2294
  }
 
2295
  case REAL_RESULT:
 
2296
  {
 
2297
    double nr= val_real();
 
2298
    if (null_value)
 
2299
      return 0; /* purecov: inspected */
 
2300
    str->set_real(nr,decimals,&my_charset_bin);
 
2301
    return str;
 
2302
  }
 
2303
  case STRING_RESULT:
 
2304
  {
 
2305
    String *res= NULL;
 
2306
 
 
2307
    for (uint i=0; i < arg_count ; i++)
 
2308
    {
 
2309
      if (i == 0)
 
2310
        res=args[i]->val_str(str);
 
2311
      else
 
2312
      {
 
2313
        String *res2;
 
2314
        res2= args[i]->val_str(res == str ? &tmp_value : str);
 
2315
        if (res2)
 
2316
        {
 
2317
          int cmp= sortcmp(res,res2,collation.collation);
 
2318
          if ((cmp_sign < 0 ? cmp : -cmp) < 0)
 
2319
            res=res2;
 
2320
        }
 
2321
      }
 
2322
      if ((null_value= args[i]->null_value))
 
2323
        return 0;
 
2324
    }
 
2325
    res->set_charset(collation.collation);
 
2326
    return res;
 
2327
  }
 
2328
  case ROW_RESULT:
 
2329
  default:
 
2330
    // This case should never be chosen
 
2331
    assert(0);
 
2332
    return 0;
 
2333
  }
 
2334
  return 0;                                     // Keep compiler happy
 
2335
}
 
2336
 
 
2337
 
 
2338
double Item_func_min_max::val_real()
 
2339
{
 
2340
  assert(fixed == 1);
 
2341
  double value=0.0;
 
2342
  if (compare_as_dates)
 
2343
  {
 
2344
    ulonglong result= 0;
 
2345
    (void)cmp_datetimes(&result);
 
2346
    return (double)result;
 
2347
  }
 
2348
  for (uint i=0; i < arg_count ; i++)
 
2349
  {
 
2350
    if (i == 0)
 
2351
      value= args[i]->val_real();
 
2352
    else
 
2353
    {
 
2354
      double tmp= args[i]->val_real();
 
2355
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
2356
        value=tmp;
 
2357
    }
 
2358
    if ((null_value= args[i]->null_value))
 
2359
      break;
 
2360
  }
 
2361
  return value;
 
2362
}
 
2363
 
 
2364
 
 
2365
longlong Item_func_min_max::val_int()
 
2366
{
 
2367
  assert(fixed == 1);
 
2368
  longlong value=0;
 
2369
  if (compare_as_dates)
 
2370
  {
 
2371
    ulonglong result= 0;
 
2372
    (void)cmp_datetimes(&result);
 
2373
    return (longlong)result;
 
2374
  }
 
2375
  for (uint i=0; i < arg_count ; i++)
 
2376
  {
 
2377
    if (i == 0)
 
2378
      value=args[i]->val_int();
 
2379
    else
 
2380
    {
 
2381
      longlong tmp=args[i]->val_int();
 
2382
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
2383
        value=tmp;
 
2384
    }
 
2385
    if ((null_value= args[i]->null_value))
 
2386
      break;
 
2387
  }
 
2388
  return value;
 
2389
}
 
2390
 
 
2391
 
 
2392
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
 
2393
{
 
2394
  assert(fixed == 1);
 
2395
  my_decimal tmp_buf, *tmp, *res= NULL;
 
2396
 
 
2397
  if (compare_as_dates)
 
2398
  {
 
2399
    ulonglong value= 0;
 
2400
    (void)cmp_datetimes(&value);
 
2401
    ulonglong2decimal(value, dec);
 
2402
    return dec;
 
2403
  }
 
2404
  for (uint i=0; i < arg_count ; i++)
 
2405
  {
 
2406
    if (i == 0)
 
2407
      res= args[i]->val_decimal(dec);
 
2408
    else
 
2409
    {
 
2410
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
 
2411
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
 
2412
      {
 
2413
        if (tmp == &tmp_buf)
 
2414
        {
 
2415
          /* Move value out of tmp_buf as this will be reused on next loop */
 
2416
          my_decimal2decimal(tmp, dec);
 
2417
          res= dec;
 
2418
        }
 
2419
        else
 
2420
          res= tmp;
 
2421
      }
 
2422
    }
 
2423
    if ((null_value= args[i]->null_value))
 
2424
    {
 
2425
      res= 0;
 
2426
      break;
 
2427
    }
 
2428
  }
 
2429
  return res;
 
2430
}
 
2431
 
 
2432
 
 
2433
longlong Item_func_length::val_int()
 
2434
{
 
2435
  assert(fixed == 1);
 
2436
  String *res=args[0]->val_str(&value);
 
2437
  if (!res)
 
2438
  {
 
2439
    null_value=1;
 
2440
    return 0; /* purecov: inspected */
 
2441
  }
 
2442
  null_value=0;
 
2443
  return (longlong) res->length();
 
2444
}
 
2445
 
 
2446
 
 
2447
longlong Item_func_char_length::val_int()
 
2448
{
 
2449
  assert(fixed == 1);
 
2450
  String *res=args[0]->val_str(&value);
 
2451
  if (!res)
 
2452
  {
 
2453
    null_value=1;
 
2454
    return 0; /* purecov: inspected */
 
2455
  }
 
2456
  null_value=0;
 
2457
  return (longlong) res->numchars();
 
2458
}
 
2459
 
 
2460
 
 
2461
longlong Item_func_coercibility::val_int()
231
2462
{
232
2463
  assert(fixed == 1);
233
2464
  null_value= 0;
234
 
  return (int64_t) args[0]->collation.derivation;
 
2465
  return (longlong) args[0]->collation.derivation;
235
2466
}
236
2467
 
237
2468
 
242
2473
}
243
2474
 
244
2475
 
245
 
int64_t Item_func_locate::val_int()
 
2476
longlong Item_func_locate::val_int()
246
2477
{
247
2478
  assert(fixed == 1);
248
2479
  String *a=args[0]->val_str(&value1);
253
2484
    return 0; /* purecov: inspected */
254
2485
  }
255
2486
  null_value=0;
256
 
  /* must be int64_t to avoid truncation */
257
 
  int64_t start=  0; 
258
 
  int64_t start0= 0;
 
2487
  /* must be longlong to avoid truncation */
 
2488
  longlong start=  0; 
 
2489
  longlong start0= 0;
259
2490
  my_match_t match;
260
2491
 
261
2492
  if (arg_count == 3)
281
2512
                                            b->ptr(), b->length(),
282
2513
                                            &match, 1))
283
2514
    return 0;
284
 
  return (int64_t) match.mb_len + start0 + 1;
 
2515
  return (longlong) match.mb_len + start0 + 1;
285
2516
}
286
2517
 
287
2518
 
300
2531
}
301
2532
 
302
2533
 
303
 
int64_t Item_func_field::val_int()
 
2534
longlong Item_func_field::val_int()
304
2535
{
305
2536
  assert(fixed == 1);
306
2537
 
309
2540
    String *field;
310
2541
    if (!(field= args[0]->val_str(&value)))
311
2542
      return 0;
312
 
    for (uint32_t i=1 ; i < arg_count ; i++)
 
2543
    for (uint i=1 ; i < arg_count ; i++)
313
2544
    {
314
2545
      String *tmp_value=args[i]->val_str(&tmp);
315
2546
      if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
316
 
        return (int64_t) (i);
 
2547
        return (longlong) (i);
317
2548
    }
318
2549
  }
319
2550
  else if (cmp_type == INT_RESULT)
320
2551
  {
321
 
    int64_t val= args[0]->val_int();
 
2552
    longlong val= args[0]->val_int();
322
2553
    if (args[0]->null_value)
323
2554
      return 0;
324
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2555
    for (uint i=1; i < arg_count ; i++)
325
2556
    {
326
2557
      if (val == args[i]->val_int() && !args[i]->null_value)
327
 
        return (int64_t) (i);
 
2558
        return (longlong) (i);
328
2559
    }
329
2560
  }
330
2561
  else if (cmp_type == DECIMAL_RESULT)
333
2564
               dec_buf, *dec= args[0]->val_decimal(&dec_buf);
334
2565
    if (args[0]->null_value)
335
2566
      return 0;
336
 
    for (uint32_t i=1; i < arg_count; i++)
 
2567
    for (uint i=1; i < arg_count; i++)
337
2568
    {
338
2569
      dec_arg= args[i]->val_decimal(&dec_arg_buf);
339
2570
      if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
340
 
        return (int64_t) (i);
 
2571
        return (longlong) (i);
341
2572
    }
342
2573
  }
343
2574
  else
345
2576
    double val= args[0]->val_real();
346
2577
    if (args[0]->null_value)
347
2578
      return 0;
348
 
    for (uint32_t i=1; i < arg_count ; i++)
 
2579
    for (uint i=1; i < arg_count ; i++)
349
2580
    {
350
2581
      if (val == args[i]->val_real() && !args[i]->null_value)
351
 
        return (int64_t) (i);
 
2582
        return (longlong) (i);
352
2583
    }
353
2584
  }
354
2585
  return 0;
359
2590
{
360
2591
  maybe_null=0; max_length=3;
361
2592
  cmp_type= args[0]->result_type();
362
 
  for (uint32_t i=1; i < arg_count ; i++)
 
2593
  for (uint i=1; i < arg_count ; i++)
363
2594
    cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
364
2595
  if (cmp_type == STRING_RESULT)
365
2596
    agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
366
2597
}
367
2598
 
368
2599
 
369
 
int64_t Item_func_ascii::val_int()
 
2600
longlong Item_func_ascii::val_int()
370
2601
{
371
2602
  assert(fixed == 1);
372
2603
  String *res=args[0]->val_str(&value);
376
2607
    return 0;
377
2608
  }
378
2609
  null_value=0;
379
 
  return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
 
2610
  return (longlong) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
380
2611
}
381
2612
 
382
 
int64_t Item_func_ord::val_int()
 
2613
longlong Item_func_ord::val_int()
383
2614
{
384
2615
  assert(fixed == 1);
385
2616
  String *res=args[0]->val_str(&value);
394
2625
  if (use_mb(res->charset()))
395
2626
  {
396
2627
    register const char *str=res->ptr();
397
 
    register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
 
2628
    register uint32 n=0, l=my_ismbchar(res->charset(),str,str+res->length());
398
2629
    if (!l)
399
 
      return (int64_t)((unsigned char) *str);
 
2630
      return (longlong)((uchar) *str);
400
2631
    while (l--)
401
 
      n=(n<<8)|(uint32_t)((unsigned char) *str++);
402
 
    return (int64_t) n;
 
2632
      n=(n<<8)|(uint32)((uchar) *str++);
 
2633
    return (longlong) n;
403
2634
  }
404
2635
#endif
405
 
  return (int64_t) ((unsigned char) (*res)[0]);
 
2636
  return (longlong) ((uchar) (*res)[0]);
406
2637
}
407
2638
 
408
2639
        /* Search after a string in a string of strings separated by ',' */
413
2644
{
414
2645
  decimals=0;
415
2646
  max_length=3;                                 // 1-999
 
2647
  if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
 
2648
  {
 
2649
    Field *field= ((Item_field*) args[1])->field;
 
2650
    if (field->real_type() == MYSQL_TYPE_SET)
 
2651
    {
 
2652
      String *find=args[0]->val_str(&value);
 
2653
      if (find)
 
2654
      {
 
2655
        enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
 
2656
                              find->length(), 0);
 
2657
        enum_bit=0;
 
2658
        if (enum_value)
 
2659
          enum_bit=1LL << (enum_value-1);
 
2660
      }
 
2661
    }
 
2662
  }
416
2663
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
417
2664
}
418
2665
 
419
2666
static const char separator=',';
420
2667
 
421
 
int64_t Item_func_find_in_set::val_int()
 
2668
longlong Item_func_find_in_set::val_int()
422
2669
{
423
2670
  assert(fixed == 1);
424
2671
  if (enum_value)
425
2672
  {
426
 
    uint64_t tmp=(uint64_t) args[1]->val_int();
 
2673
    ulonglong tmp=(ulonglong) args[1]->val_int();
427
2674
    if (!(null_value=args[1]->null_value || args[0]->null_value))
428
2675
    {
429
2676
      if (tmp & enum_bit)
445
2692
  if ((diff=buffer->length() - find->length()) >= 0)
446
2693
  {
447
2694
    my_wc_t wc;
448
 
    const CHARSET_INFO * const cs= cmp_collation.collation;
 
2695
    CHARSET_INFO *cs= cmp_collation.collation;
449
2696
    const char *str_begin= buffer->ptr();
450
2697
    const char *str_end= buffer->ptr();
451
2698
    const char *real_end= str_end+buffer->length();
452
 
    const unsigned char *find_str= (const unsigned char *) find->ptr();
453
 
    uint32_t find_str_len= find->length();
 
2699
    const uchar *find_str= (const uchar *) find->ptr();
 
2700
    uint find_str_len= find->length();
454
2701
    int position= 0;
455
2702
    while (1)
456
2703
    {
457
2704
      int symbol_len;
458
 
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end, 
459
 
                                       (unsigned char*) real_end)) > 0)
 
2705
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end, 
 
2706
                                       (uchar*) real_end)) > 0)
460
2707
      {
461
2708
        const char *substr_end= str_end + symbol_len;
462
2709
        bool is_last_item= (substr_end == real_end);
466
2713
          position++;
467
2714
          if (is_last_item && !is_separator)
468
2715
            str_end= substr_end;
469
 
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
 
2716
          if (!my_strnncoll(cs, (const uchar *) str_begin,
470
2717
                            str_end - str_begin,
471
2718
                            find_str, find_str_len))
472
 
            return (int64_t) position;
 
2719
            return (longlong) position;
473
2720
          else
474
2721
            str_begin= substr_end;
475
2722
        }
478
2725
      else if (str_end - str_begin == 0 &&
479
2726
               find_str_len == 0 &&
480
2727
               wc == (my_wc_t) separator)
481
 
        return (int64_t) ++position;
 
2728
        return (longlong) ++position;
482
2729
      else
483
 
        return 0L;
 
2730
        return 0LL;
484
2731
    }
485
2732
  }
486
2733
  return 0;
487
2734
}
488
2735
 
489
 
int64_t Item_func_bit_count::val_int()
 
2736
longlong Item_func_bit_count::val_int()
490
2737
{
491
2738
  assert(fixed == 1);
492
 
  uint64_t value= (uint64_t) args[0]->val_int();
 
2739
  ulonglong value= (ulonglong) args[0]->val_int();
493
2740
  if ((null_value= args[0]->null_value))
494
2741
    return 0; /* purecov: inspected */
495
 
  return (int64_t) my_count_bits(value);
 
2742
  return (longlong) my_count_bits(value);
 
2743
}
 
2744
 
 
2745
 
 
2746
/****************************************************************************
 
2747
** Functions to handle dynamic loadable functions
 
2748
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
 
2749
** Rewritten by monty.
 
2750
****************************************************************************/
 
2751
 
 
2752
void udf_handler::cleanup()
 
2753
{
 
2754
  if (!not_original)
 
2755
  {
 
2756
    if (initialized)
 
2757
    {
 
2758
      if (u_d->func_deinit != NULL)
 
2759
      {
 
2760
        Udf_func_deinit deinit= u_d->func_deinit;
 
2761
        (*deinit)(&initid);
 
2762
      }
 
2763
 
 
2764
      initialized= false;
 
2765
    }
 
2766
    if (buffers)                                // Because of bug in ecc
 
2767
      delete [] buffers;
 
2768
    buffers= 0;
 
2769
  }
 
2770
}
 
2771
 
 
2772
 
 
2773
bool
 
2774
udf_handler::fix_fields(THD *thd, Item_result_field *func,
 
2775
                        uint arg_count, Item **arguments)
 
2776
{
 
2777
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
 
2778
 
 
2779
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
 
2780
    return(true);                               // Fatal error flag is set!
 
2781
 
 
2782
  udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
 
2783
 
 
2784
  if (!tmp_udf)
 
2785
  {
 
2786
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
 
2787
    return(true);
 
2788
  }
 
2789
  u_d=tmp_udf;
 
2790
  args=arguments;
 
2791
 
 
2792
  /* Fix all arguments */
 
2793
  func->maybe_null=0;
 
2794
  used_tables_cache=0;
 
2795
  const_item_cache=1;
 
2796
 
 
2797
  if ((f_args.arg_count=arg_count))
 
2798
  {
 
2799
    if (!(f_args.arg_type= (Item_result*)
 
2800
          sql_alloc(f_args.arg_count*sizeof(Item_result))))
 
2801
 
 
2802
    {
 
2803
      return(true);
 
2804
    }
 
2805
    uint i;
 
2806
    Item **arg,**arg_end;
 
2807
    for (i=0, arg=arguments, arg_end=arguments+arg_count;
 
2808
         arg != arg_end ;
 
2809
         arg++,i++)
 
2810
    {
 
2811
      if (!(*arg)->fixed &&
 
2812
          (*arg)->fix_fields(thd, arg))
 
2813
        return(1);
 
2814
      // we can't assign 'item' before, because fix_fields() can change arg
 
2815
      Item *item= *arg;
 
2816
      if (item->check_cols(1))
 
2817
        return(true);
 
2818
      /*
 
2819
        TODO: We should think about this. It is not always
 
2820
        right way just to set an UDF result to return my_charset_bin
 
2821
        if one argument has binary sorting order.
 
2822
        The result collation should be calculated according to arguments
 
2823
        derivations in some cases and should not in other cases.
 
2824
        Moreover, some arguments can represent a numeric input
 
2825
        which doesn't effect the result character set and collation.
 
2826
        There is no a general rule for UDF. Everything depends on
 
2827
        the particular user defined function.
 
2828
      */
 
2829
      if (item->collation.collation->state & MY_CS_BINSORT)
 
2830
        func->collation.set(&my_charset_bin);
 
2831
      if (item->maybe_null)
 
2832
        func->maybe_null=1;
 
2833
      func->with_sum_func= func->with_sum_func || item->with_sum_func;
 
2834
      used_tables_cache|=item->used_tables();
 
2835
      const_item_cache&=item->const_item();
 
2836
      f_args.arg_type[i]=item->result_type();
 
2837
    }
 
2838
    //TODO: why all following memory is not allocated with 1 call of sql_alloc?
 
2839
    if (!(buffers=new String[arg_count]) ||
 
2840
        !(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2841
        !(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
 
2842
        !(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
 
2843
        !(num_buffer= (char*) sql_alloc(arg_count *
 
2844
                                        ALIGN_SIZE(sizeof(double)))) ||
 
2845
        !(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
 
2846
        !(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
 
2847
                                                       sizeof(long))))
 
2848
    {
 
2849
      return(true);
 
2850
    }
 
2851
  }
 
2852
  func->fix_length_and_dec();
 
2853
  initid.max_length=func->max_length;
 
2854
  initid.maybe_null=func->maybe_null;
 
2855
  initid.const_item=const_item_cache;
 
2856
  initid.decimals=func->decimals;
 
2857
  initid.ptr=0;
 
2858
 
 
2859
  if (u_d->func_init)
 
2860
  {
 
2861
    char init_msg_buff[MYSQL_ERRMSG_SIZE];
 
2862
    char *to=num_buffer;
 
2863
    for (uint i=0; i < arg_count; i++)
 
2864
    {
 
2865
      /*
 
2866
       For a constant argument i, args->args[i] points to the argument value. 
 
2867
       For non-constant, args->args[i] is NULL.
 
2868
      */
 
2869
      f_args.args[i]= NULL;         /* Non-const unless updated below. */
 
2870
 
 
2871
      f_args.lengths[i]= arguments[i]->max_length;
 
2872
      f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
 
2873
      f_args.attributes[i]= arguments[i]->name;
 
2874
      f_args.attribute_lengths[i]= arguments[i]->name_length;
 
2875
 
 
2876
      if (arguments[i]->const_item())
 
2877
      {
 
2878
        switch (arguments[i]->result_type()) 
 
2879
        {
 
2880
        case STRING_RESULT:
 
2881
        case DECIMAL_RESULT:
 
2882
        {
 
2883
          String *res= arguments[i]->val_str(&buffers[i]);
 
2884
          if (arguments[i]->null_value)
 
2885
            continue;
 
2886
          f_args.args[i]= (char*) res->c_ptr();
 
2887
          f_args.lengths[i]= res->length();
 
2888
          break;
 
2889
        }
 
2890
        case INT_RESULT:
 
2891
          *((longlong*) to)= arguments[i]->val_int();
 
2892
          if (arguments[i]->null_value)
 
2893
            continue;
 
2894
          f_args.args[i]= to;
 
2895
          to+= ALIGN_SIZE(sizeof(longlong));
 
2896
          break;
 
2897
        case REAL_RESULT:
 
2898
          *((double*) to)= arguments[i]->val_real();
 
2899
          if (arguments[i]->null_value)
 
2900
            continue;
 
2901
          f_args.args[i]= to;
 
2902
          to+= ALIGN_SIZE(sizeof(double));
 
2903
          break;
 
2904
        case ROW_RESULT:
 
2905
        default:
 
2906
          // This case should never be chosen
 
2907
          assert(0);
 
2908
          break;
 
2909
        }
 
2910
      }
 
2911
    }
 
2912
    Udf_func_init init= u_d->func_init;
 
2913
    if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
 
2914
    {
 
2915
      my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2916
               u_d->name.str, init_msg_buff);
 
2917
      return(true);
 
2918
    }
 
2919
    func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
 
2920
    func->maybe_null=initid.maybe_null;
 
2921
    const_item_cache=initid.const_item;
 
2922
    /* 
 
2923
      Keep used_tables_cache in sync with const_item_cache.
 
2924
      See the comment in Item_udf_func::update_used tables.
 
2925
    */  
 
2926
    if (!const_item_cache && !used_tables_cache)
 
2927
      used_tables_cache= RAND_TABLE_BIT;
 
2928
    func->decimals=min(initid.decimals,NOT_FIXED_DEC);
 
2929
  }
 
2930
  initialized=1;
 
2931
  if (error)
 
2932
  {
 
2933
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
 
2934
             u_d->name.str, ER(ER_UNKNOWN_ERROR));
 
2935
    return(true);
 
2936
  }
 
2937
  return(false);
 
2938
}
 
2939
 
 
2940
 
 
2941
bool udf_handler::get_arguments()
 
2942
{
 
2943
  if (error)
 
2944
    return 1;                                   // Got an error earlier
 
2945
  char *to= num_buffer;
 
2946
  uint str_count=0;
 
2947
  for (uint i=0; i < f_args.arg_count; i++)
 
2948
  {
 
2949
    f_args.args[i]=0;
 
2950
    switch (f_args.arg_type[i]) {
 
2951
    case STRING_RESULT:
 
2952
    case DECIMAL_RESULT:
 
2953
      {
 
2954
        String *res=args[i]->val_str(&buffers[str_count++]);
 
2955
        if (!(args[i]->null_value))
 
2956
        {
 
2957
          f_args.args[i]=    (char*) res->ptr();
 
2958
          f_args.lengths[i]= res->length();
 
2959
          break;
 
2960
        }
 
2961
      }
 
2962
    case INT_RESULT:
 
2963
      *((longlong*) to) = args[i]->val_int();
 
2964
      if (!args[i]->null_value)
 
2965
      {
 
2966
        f_args.args[i]=to;
 
2967
        to+= ALIGN_SIZE(sizeof(longlong));
 
2968
      }
 
2969
      break;
 
2970
    case REAL_RESULT:
 
2971
      *((double*) to)= args[i]->val_real();
 
2972
      if (!args[i]->null_value)
 
2973
      {
 
2974
        f_args.args[i]=to;
 
2975
        to+= ALIGN_SIZE(sizeof(double));
 
2976
      }
 
2977
      break;
 
2978
    case ROW_RESULT:
 
2979
    default:
 
2980
      // This case should never be chosen
 
2981
      assert(0);
 
2982
      break;
 
2983
    }
 
2984
  }
 
2985
  return 0;
 
2986
}
 
2987
 
 
2988
/**
 
2989
  @return
 
2990
    (String*)NULL in case of NULL values
 
2991
*/
 
2992
String *udf_handler::val_str(String *str,String *save_str)
 
2993
{
 
2994
  uchar is_null_tmp=0;
 
2995
  ulong res_length;
 
2996
 
 
2997
  if (get_arguments())
 
2998
    return(0);
 
2999
  char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
3000
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
3001
    u_d->func;
 
3002
 
 
3003
  if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
 
3004
  {                                             // This happens VERY seldom
 
3005
    if (str->alloc(MAX_FIELD_WIDTH))
 
3006
    {
 
3007
      error=1;
 
3008
      return(0);
 
3009
    }
 
3010
  }
 
3011
  char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
 
3012
                 &is_null_tmp, &error);
 
3013
  if (is_null_tmp || !res || error)             // The !res is for safety
 
3014
  {
 
3015
    return(0);
 
3016
  }
 
3017
  if (res == str->ptr())
 
3018
  {
 
3019
    str->length(res_length);
 
3020
    return(str);
 
3021
  }
 
3022
  save_str->set(res, res_length, str->charset());
 
3023
  return(save_str);
 
3024
}
 
3025
 
 
3026
 
 
3027
/*
 
3028
  For the moment, UDF functions are returning DECIMAL values as strings
 
3029
*/
 
3030
 
 
3031
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
 
3032
{
 
3033
  char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
 
3034
  ulong res_length= DECIMAL_MAX_STR_LENGTH;
 
3035
 
 
3036
  if (get_arguments())
 
3037
  {
 
3038
    *null_value=1;
 
3039
    return 0;
 
3040
  }
 
3041
  char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
 
3042
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
 
3043
    u_d->func;
 
3044
 
 
3045
  char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
 
3046
  if (is_null || error)
 
3047
  {
 
3048
    *null_value= 1;
 
3049
    return 0;
 
3050
  }
 
3051
  end= res+ res_length;
 
3052
  str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
 
3053
  return dec_buf;
 
3054
}
 
3055
 
 
3056
 
 
3057
void Item_udf_func::cleanup()
 
3058
{
 
3059
  udf.cleanup();
 
3060
  Item_func::cleanup();
 
3061
}
 
3062
 
 
3063
 
 
3064
void Item_udf_func::print(String *str, enum_query_type query_type)
 
3065
{
 
3066
  str->append(func_name());
 
3067
  str->append('(');
 
3068
  for (uint i=0 ; i < arg_count ; i++)
 
3069
  {
 
3070
    if (i != 0)
 
3071
      str->append(',');
 
3072
    args[i]->print_item_w_name(str, query_type);
 
3073
  }
 
3074
  str->append(')');
 
3075
}
 
3076
 
 
3077
 
 
3078
double Item_func_udf_float::val_real()
 
3079
{
 
3080
  assert(fixed == 1);
 
3081
  return(udf.val(&null_value));
 
3082
}
 
3083
 
 
3084
 
 
3085
String *Item_func_udf_float::val_str(String *str)
 
3086
{
 
3087
  assert(fixed == 1);
 
3088
  double nr= val_real();
 
3089
  if (null_value)
 
3090
    return 0;                                   /* purecov: inspected */
 
3091
  str->set_real(nr,decimals,&my_charset_bin);
 
3092
  return str;
 
3093
}
 
3094
 
 
3095
 
 
3096
longlong Item_func_udf_int::val_int()
 
3097
{
 
3098
  assert(fixed == 1);
 
3099
  return(udf.val_int(&null_value));
 
3100
}
 
3101
 
 
3102
 
 
3103
String *Item_func_udf_int::val_str(String *str)
 
3104
{
 
3105
  assert(fixed == 1);
 
3106
  longlong nr=val_int();
 
3107
  if (null_value)
 
3108
    return 0;
 
3109
  str->set_int(nr, unsigned_flag, &my_charset_bin);
 
3110
  return str;
 
3111
}
 
3112
 
 
3113
 
 
3114
longlong Item_func_udf_decimal::val_int()
 
3115
{
 
3116
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3117
  longlong result;
 
3118
  if (null_value)
 
3119
    return 0;
 
3120
  my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
3121
  return result;
 
3122
}
 
3123
 
 
3124
 
 
3125
double Item_func_udf_decimal::val_real()
 
3126
{
 
3127
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3128
  double result;
 
3129
  if (null_value)
 
3130
    return 0.0;
 
3131
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
 
3132
  return result;
 
3133
}
 
3134
 
 
3135
 
 
3136
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
 
3137
{
 
3138
  assert(fixed == 1);
 
3139
  return(udf.val_decimal(&null_value, dec_buf));
 
3140
}
 
3141
 
 
3142
 
 
3143
String *Item_func_udf_decimal::val_str(String *str)
 
3144
{
 
3145
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
 
3146
  if (null_value)
 
3147
    return 0;
 
3148
  if (str->length() < DECIMAL_MAX_STR_LENGTH)
 
3149
    str->length(DECIMAL_MAX_STR_LENGTH);
 
3150
  my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
 
3151
  my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
 
3152
  return str;
 
3153
}
 
3154
 
 
3155
 
 
3156
void Item_func_udf_decimal::fix_length_and_dec()
 
3157
{
 
3158
  fix_num_length_and_dec();
 
3159
}
 
3160
 
 
3161
 
 
3162
/* Default max_length is max argument length */
 
3163
 
 
3164
void Item_func_udf_str::fix_length_and_dec()
 
3165
{
 
3166
  max_length=0;
 
3167
  for (uint i = 0; i < arg_count; i++)
 
3168
    set_if_bigger(max_length,args[i]->max_length);
 
3169
  return;
 
3170
}
 
3171
 
 
3172
String *Item_func_udf_str::val_str(String *str)
 
3173
{
 
3174
  assert(fixed == 1);
 
3175
  String *res=udf.val_str(str,&str_value);
 
3176
  null_value = !res;
 
3177
  return res;
 
3178
}
 
3179
 
 
3180
 
 
3181
/**
 
3182
  @note
 
3183
  This has to come last in the udf_handler methods, or C for AIX
 
3184
  version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
 
3185
*/
 
3186
 
 
3187
udf_handler::~udf_handler()
 
3188
{
 
3189
  /* Everything should be properly cleaned up by this moment. */
 
3190
  assert(not_original || !(initialized || buffers));
496
3191
}
497
3192
 
498
3193
/*
504
3199
 
505
3200
class User_level_lock
506
3201
{
507
 
  unsigned char *key;
 
3202
  uchar *key;
508
3203
  size_t key_length;
509
3204
 
510
3205
public:
514
3209
  my_thread_id thread_id;
515
3210
  void set_thread(THD *thd) { thread_id= thd->thread_id; }
516
3211
 
517
 
  User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id) 
 
3212
  User_level_lock(const uchar *key_arg,uint length, ulong id) 
518
3213
    :key_length(length),count(1),locked(1), thread_id(id)
519
3214
  {
520
 
    key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
 
3215
    key= (uchar*) my_memdup(key_arg,length,MYF(0));
521
3216
    pthread_cond_init(&cond,NULL);
522
3217
    if (key)
523
3218
    {
524
 
      if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
 
3219
      if (my_hash_insert(&hash_user_locks,(uchar*) this))
525
3220
      {
526
 
        free(key);
 
3221
        my_free(key,MYF(0));
527
3222
        key=0;
528
3223
      }
529
3224
    }
532
3227
  {
533
3228
    if (key)
534
3229
    {
535
 
      hash_delete(&hash_user_locks,(unsigned char*) this);
536
 
      free(key);
 
3230
      hash_delete(&hash_user_locks,(uchar*) this);
 
3231
      my_free(key, MYF(0));
537
3232
    }
538
3233
    pthread_cond_destroy(&cond);
539
3234
  }
540
3235
  inline bool initialized() { return key != 0; }
541
3236
  friend void item_user_lock_release(User_level_lock *ull);
542
 
  friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
543
 
                            bool not_used);
 
3237
  friend uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3238
                            my_bool not_used);
544
3239
};
545
3240
 
546
 
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
547
 
                   bool not_used __attribute__((unused)))
 
3241
uchar *ull_get_key(const User_level_lock *ull, size_t *length,
 
3242
                   my_bool not_used __attribute__((unused)))
548
3243
{
549
3244
  *length= ull->key_length;
550
3245
  return ull->key;
556
3251
void item_user_lock_init(void)
557
3252
{
558
3253
  pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
559
 
  hash_init(&hash_user_locks, system_charset_info,
 
3254
  hash_init(&hash_user_locks,system_charset_info,
560
3255
            16,0,0,(hash_get_key) ull_get_key,NULL,0);
561
3256
  item_user_lock_inited= 1;
562
3257
}
586
3281
  on the slave.
587
3282
*/
588
3283
 
589
 
int64_t Item_master_pos_wait::val_int()
 
3284
longlong Item_master_pos_wait::val_int()
590
3285
{
591
3286
  assert(fixed == 1);
592
3287
  THD* thd = current_thd;
599
3294
    null_value = 1;
600
3295
    return 0;
601
3296
  }
602
 
  int64_t pos = (ulong)args[1]->val_int();
603
 
  int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
 
3297
#ifdef HAVE_REPLICATION
 
3298
  longlong pos = (ulong)args[1]->val_int();
 
3299
  longlong timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
604
3300
  if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
605
3301
  {
606
3302
    null_value = 1;
607
3303
    event_count=0;
608
3304
  }
 
3305
#endif
609
3306
  return event_count;
610
3307
}
611
3308
 
612
3309
#ifdef EXTRA_DEBUG
613
 
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
 
3310
void debug_sync_point(const char* lock_name, uint lock_timeout)
614
3311
{
615
3312
}
616
3313
 
617
3314
#endif
618
3315
 
619
3316
 
620
 
int64_t Item_func_last_insert_id::val_int()
 
3317
longlong Item_func_last_insert_id::val_int()
621
3318
{
622
3319
  THD *thd= current_thd;
623
3320
  assert(fixed == 1);
624
3321
  if (arg_count)
625
3322
  {
626
 
    int64_t value= args[0]->val_int();
 
3323
    longlong value= args[0]->val_int();
627
3324
    null_value= args[0]->null_value;
628
3325
    /*
629
3326
      LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
648
3345
 
649
3346
/* This function is just used to test speed of different functions */
650
3347
 
651
 
int64_t Item_func_benchmark::val_int()
 
3348
longlong Item_func_benchmark::val_int()
652
3349
{
653
3350
  assert(fixed == 1);
654
3351
  char buff[MAX_FIELD_WIDTH];
655
3352
  String tmp(buff,sizeof(buff), &my_charset_bin);
656
3353
  my_decimal tmp_decimal;
657
3354
  THD *thd=current_thd;
658
 
  uint64_t loop_count;
 
3355
  ulonglong loop_count;
659
3356
 
660
 
  loop_count= (uint64_t) args[0]->val_int();
 
3357
  loop_count= (ulonglong) args[0]->val_int();
661
3358
 
662
3359
  if (args[0]->null_value ||
663
 
      (!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
 
3360
      (!args[0]->unsigned_flag && (((longlong) loop_count) < 0)))
664
3361
  {
665
3362
    if (!args[0]->null_value)
666
3363
    {
667
3364
      char buff[22];
668
 
      llstr(((int64_t) loop_count), buff);
669
 
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
3365
      llstr(((longlong) loop_count), buff);
 
3366
      push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
670
3367
                          ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
671
3368
                          "count", buff, "benchmark");
672
3369
    }
676
3373
  }
677
3374
 
678
3375
  null_value=0;
679
 
  for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
 
3376
  for (ulonglong loop=0 ; loop < loop_count && !thd->killed; loop++)
680
3377
  {
681
3378
    switch (args[1]->result_type()) {
682
3379
    case REAL_RESULT:
718
3415
{
719
3416
  user_var_entry *entry;
720
3417
 
721
 
  if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
 
3418
  if (!(entry = (user_var_entry*) hash_search(hash, (uchar*) name.str,
722
3419
                                              name.length)) &&
723
3420
      create_if_not_exists)
724
3421
  {
725
 
    uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
 
3422
    uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
726
3423
    if (!hash_inited(hash))
727
3424
      return 0;
728
3425
    if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
748
3445
    entry->used_query_id=current_thd->query_id;
749
3446
    entry->type=STRING_RESULT;
750
3447
    memcpy(entry->name.str, name.str, name.length+1);
751
 
    if (my_hash_insert(hash,(unsigned char*) entry))
 
3448
    if (my_hash_insert(hash,(uchar*) entry))
752
3449
    {
753
 
      free((char*) entry);
 
3450
      my_free((char*) entry,MYF(0));
754
3451
      return 0;
755
3452
    }
756
3453
  }
817
3514
    column read set or to register used fields in a view
818
3515
*/
819
3516
 
820
 
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
 
3517
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
821
3518
{
822
3519
  if (result_field)
823
3520
  {
824
 
    Table *table= (Table *) arg;
 
3521
    TABLE *table= (TABLE *) arg;
825
3522
    if (result_field->table == table || !table)
826
3523
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
827
 
    if (result_field->vcol_info && result_field->vcol_info->expr_item)
828
 
      return result_field->vcol_info->
829
 
               expr_item->walk(&Item::register_field_in_read_map, 1, arg);
830
 
  }
831
 
  return 0;
832
 
}
833
 
 
834
 
 
835
 
/*
836
 
  Mark field in bitmap supplied as *arg
837
 
 
838
 
*/
839
 
 
840
 
bool Item_func_set_user_var::register_field_in_bitmap(unsigned char *arg)
841
 
{
842
 
  MY_BITMAP *bitmap = (MY_BITMAP *) arg;
843
 
  assert(bitmap);
844
 
  if (result_field)
845
 
  {
846
 
    bitmap_set_bit(bitmap, result_field->field_index);
847
3524
  }
848
3525
  return 0;
849
3526
}
870
3547
*/
871
3548
 
872
3549
static bool
873
 
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
874
 
            Item_result type, const CHARSET_INFO * const cs, Derivation dv,
 
3550
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
 
3551
            Item_result type, CHARSET_INFO *cs, Derivation dv,
875
3552
            bool unsigned_arg)
876
3553
{
877
3554
  if (set_null)
878
3555
  {
879
3556
    char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
880
3557
    if (entry->value && entry->value != pos)
881
 
      free(entry->value);
 
3558
      my_free(entry->value,MYF(0));
882
3559
    entry->value= 0;
883
3560
    entry->length= 0;
884
3561
  }
893
3570
      if (entry->value != pos)
894
3571
      {
895
3572
        if (entry->value)
896
 
          free(entry->value);
 
3573
          my_free(entry->value,MYF(0));
897
3574
        entry->value=pos;
898
3575
      }
899
3576
    }
930
3607
 
931
3608
 
932
3609
bool
933
 
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
 
3610
Item_func_set_user_var::update_hash(void *ptr, uint length,
934
3611
                                    Item_result res_type,
935
 
                                    const CHARSET_INFO * const cs, Derivation dv,
 
3612
                                    CHARSET_INFO *cs, Derivation dv,
936
3613
                                    bool unsigned_arg)
937
3614
{
938
3615
  /*
953
3630
 
954
3631
/** Get the value of a variable as a double. */
955
3632
 
956
 
double user_var_entry::val_real(bool *null_value)
 
3633
double user_var_entry::val_real(my_bool *null_value)
957
3634
{
958
3635
  if ((*null_value= (value == 0)))
959
3636
    return 0.0;
962
3639
  case REAL_RESULT:
963
3640
    return *(double*) value;
964
3641
  case INT_RESULT:
965
 
    return (double) *(int64_t*) value;
 
3642
    return (double) *(longlong*) value;
966
3643
  case DECIMAL_RESULT:
967
3644
  {
968
3645
    double result;
981
3658
 
982
3659
/** Get the value of a variable as an integer. */
983
3660
 
984
 
int64_t user_var_entry::val_int(bool *null_value) const
 
3661
longlong user_var_entry::val_int(my_bool *null_value) const
985
3662
{
986
3663
  if ((*null_value= (value == 0)))
987
 
    return 0L;
 
3664
    return 0LL;
988
3665
 
989
3666
  switch (type) {
990
3667
  case REAL_RESULT:
991
 
    return (int64_t) *(double*) value;
 
3668
    return (longlong) *(double*) value;
992
3669
  case INT_RESULT:
993
 
    return *(int64_t*) value;
 
3670
    return *(longlong*) value;
994
3671
  case DECIMAL_RESULT:
995
3672
  {
996
 
    int64_t result;
 
3673
    longlong result;
997
3674
    my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
998
3675
    return result;
999
3676
  }
1006
3683
    assert(1);                          // Impossible
1007
3684
    break;
1008
3685
  }
1009
 
  return 0L;                                    // Impossible
 
3686
  return 0LL;                                   // Impossible
1010
3687
}
1011
3688
 
1012
3689
 
1013
3690
/** Get the value of a variable as a string. */
1014
3691
 
1015
 
String *user_var_entry::val_str(bool *null_value, String *str,
1016
 
                                uint32_t decimals)
 
3692
String *user_var_entry::val_str(my_bool *null_value, String *str,
 
3693
                                uint decimals)
1017
3694
{
1018
3695
  if ((*null_value= (value == 0)))
1019
3696
    return (String*) 0;
1024
3701
    break;
1025
3702
  case INT_RESULT:
1026
3703
    if (!unsigned_flag)
1027
 
      str->set(*(int64_t*) value, &my_charset_bin);
 
3704
      str->set(*(longlong*) value, &my_charset_bin);
1028
3705
    else
1029
 
      str->set(*(uint64_t*) value, &my_charset_bin);
 
3706
      str->set(*(ulonglong*) value, &my_charset_bin);
1030
3707
    break;
1031
3708
  case DECIMAL_RESULT:
1032
3709
    my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
1043
3720
 
1044
3721
/** Get the value of a variable as a decimal. */
1045
3722
 
1046
 
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
 
3723
my_decimal *user_var_entry::val_decimal(my_bool *null_value, my_decimal *val)
1047
3724
{
1048
3725
  if ((*null_value= (value == 0)))
1049
3726
    return 0;
1053
3730
    double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
1054
3731
    break;
1055
3732
  case INT_RESULT:
1056
 
    int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
 
3733
    int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) value, 0, val);
1057
3734
    break;
1058
3735
  case DECIMAL_RESULT:
1059
3736
    val= (my_decimal *)value;
1201
3878
  return entry->val_real(&null_value);
1202
3879
}
1203
3880
 
1204
 
int64_t Item_func_set_user_var::val_int()
 
3881
longlong Item_func_set_user_var::val_int()
1205
3882
{
1206
3883
  assert(fixed == 1);
1207
3884
  check(0);
1235
3912
  return entry->val_real(&null_value);
1236
3913
}
1237
3914
 
1238
 
int64_t Item_func_set_user_var::val_int_result()
 
3915
longlong Item_func_set_user_var::val_int_result()
1239
3916
{
1240
3917
  assert(fixed == 1);
1241
3918
  check(true);
1359
4036
      (result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
1360
4037
  {
1361
4038
    String *result;
1362
 
    const CHARSET_INFO * const cs= collation.collation;
 
4039
    CHARSET_INFO *cs= collation.collation;
1363
4040
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
1364
4041
    str_value.set_quick(buff, sizeof(buff), cs);
1365
4042
    result= entry->val_str(&null_value, &str_value, decimals);
1395
4072
  }
1396
4073
  else
1397
4074
  {
1398
 
    int64_t nr= entry->val_int(&null_value);
 
4075
    longlong nr= entry->val_int(&null_value);
1399
4076
    if (null_value)
1400
4077
      return set_field_to_null_with_conversions(field, no_conversions);
1401
4078
    field->set_notnull();
1433
4110
}
1434
4111
 
1435
4112
 
1436
 
int64_t Item_func_get_user_var::val_int()
 
4113
longlong Item_func_get_user_var::val_int()
1437
4114
{
1438
4115
  assert(fixed == 1);
1439
4116
  if (!var_entry)
1440
 
    return 0L;                          // No such variable
 
4117
    return 0LL;                         // No such variable
1441
4118
  return (var_entry->val_int(&null_value));
1442
4119
}
1443
4120
 
1476
4153
    calling statement. We must log all such variables even if they are 
1477
4154
    not involved in table-updating statements.
1478
4155
  */
1479
 
  if (!(opt_bin_log && is_update_query(sql_command)))
 
4156
  if (!(opt_bin_log && 
 
4157
       (is_update_query(sql_command) || thd->in_sub_stmt)))
1480
4158
  {
1481
4159
    *out_entry= var_entry;
1482
4160
    return 0;
1529
4207
    return 0;
1530
4208
  }
1531
4209
 
1532
 
  uint32_t size;
 
4210
  uint size;
1533
4211
  /*
1534
4212
    First we need to store value of var_entry, when the next situation
1535
4213
    appears:
1566
4244
  }
1567
4245
  /* Mark that this variable has been used by this query */
1568
4246
  var_entry->used_query_id= thd->query_id;
1569
 
  if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
 
4247
  if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
1570
4248
    goto err;
1571
4249
 
1572
4250
  *out_entry= var_entry;
1643
4321
 
1644
4322
 
1645
4323
void Item_func_get_user_var::print(String *str,
1646
 
                                   enum_query_type query_type __attribute__((unused)))
 
4324
                                   enum_query_type query_type __attribute__((__unused__)))
1647
4325
{
1648
4326
  str->append(STRING_WITH_LEN("(@"));
1649
4327
  str->append(name.str,name.length);
1652
4330
 
1653
4331
 
1654
4332
bool Item_func_get_user_var::eq(const Item *item,
1655
 
                                bool binary_cmp __attribute__((unused))) const
 
4333
                                bool binary_cmp __attribute__((__unused__))) const
1656
4334
{
1657
4335
  /* Assume we don't have rtti */
1658
4336
  if (this == item)
1685
4363
}
1686
4364
 
1687
4365
 
1688
 
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
 
4366
void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs)
1689
4367
{
1690
4368
  ::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
1691
4369
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
1692
4370
}
1693
4371
 
1694
4372
 
1695
 
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
1696
 
                                           const CHARSET_INFO * const cs)
 
4373
void Item_user_var_as_out_param::set_value(const char *str, uint length,
 
4374
                                           CHARSET_INFO* cs)
1697
4375
{
1698
4376
  ::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
1699
4377
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
1707
4385
}
1708
4386
 
1709
4387
 
1710
 
int64_t Item_user_var_as_out_param::val_int()
1711
 
{
1712
 
  assert(0);
1713
 
  return 0;
1714
 
}
1715
 
 
1716
 
 
1717
 
String* Item_user_var_as_out_param::val_str(String *str __attribute__((unused)))
1718
 
{
1719
 
  assert(0);
1720
 
  return 0;
1721
 
}
1722
 
 
1723
 
 
1724
 
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((unused)))
 
4388
longlong Item_user_var_as_out_param::val_int()
 
4389
{
 
4390
  assert(0);
 
4391
  return 0;
 
4392
}
 
4393
 
 
4394
 
 
4395
String* Item_user_var_as_out_param::val_str(String *str __attribute__((__unused__)))
 
4396
{
 
4397
  assert(0);
 
4398
  return 0;
 
4399
}
 
4400
 
 
4401
 
 
4402
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((__unused__)))
1725
4403
{
1726
4404
  assert(0);
1727
4405
  return 0;
1729
4407
 
1730
4408
 
1731
4409
void Item_user_var_as_out_param::print(String *str,
1732
 
                                       enum_query_type query_type __attribute__((unused)))
 
4410
                                       enum_query_type query_type __attribute__((__unused__)))
1733
4411
{
1734
4412
  str->append('@');
1735
4413
  str->append(name.str,name.length);
1771
4449
  return var->is_written_to_binlog(var_type);
1772
4450
}
1773
4451
 
1774
 
int64_t Item_func_bit_xor::val_int()
 
4452
longlong Item_func_bit_xor::val_int()
1775
4453
{
1776
4454
  assert(fixed == 1);
1777
 
  uint64_t arg1= (uint64_t) args[0]->val_int();
1778
 
  uint64_t arg2= (uint64_t) args[1]->val_int();
 
4455
  ulonglong arg1= (ulonglong) args[0]->val_int();
 
4456
  ulonglong arg2= (ulonglong) args[1]->val_int();
1779
4457
  if ((null_value= (args[0]->null_value || args[1]->null_value)))
1780
4458
    return 0;
1781
 
  return (int64_t) (arg1 ^ arg2);
 
4459
  return (longlong) (arg1 ^ arg2);
1782
4460
}
1783
4461
 
1784
4462
 
1849
4527
    0           Already taken, or error
1850
4528
*/
1851
4529
 
1852
 
int64_t Item_func_is_free_lock::val_int()
 
4530
longlong Item_func_is_free_lock::val_int()
1853
4531
{
1854
4532
  assert(fixed == 1);
1855
4533
  String *res=args[0]->val_str(&value);
1863
4541
  }
1864
4542
  
1865
4543
  pthread_mutex_lock(&LOCK_user_locks);
1866
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4544
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
1867
4545
                                       (size_t) res->length());
1868
4546
  pthread_mutex_unlock(&LOCK_user_locks);
1869
4547
  if (!ull || !ull->locked)
1871
4549
  return 0;
1872
4550
}
1873
4551
 
1874
 
int64_t Item_func_is_used_lock::val_int()
 
4552
longlong Item_func_is_used_lock::val_int()
1875
4553
{
1876
4554
  assert(fixed == 1);
1877
4555
  String *res=args[0]->val_str(&value);
1882
4560
    return 0;
1883
4561
  
1884
4562
  pthread_mutex_lock(&LOCK_user_locks);
1885
 
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
4563
  ull= (User_level_lock *) hash_search(&hash_user_locks, (uchar*) res->ptr(),
1886
4564
                                       (size_t) res->length());
1887
4565
  pthread_mutex_unlock(&LOCK_user_locks);
1888
4566
  if (!ull || !ull->locked)
1893
4571
}
1894
4572
 
1895
4573
 
1896
 
int64_t Item_func_row_count::val_int()
 
4574
longlong Item_func_row_count::val_int()
1897
4575
{
1898
4576
  assert(fixed == 1);
1899
4577
  THD *thd= current_thd;
1901
4579
  return thd->row_count_func;
1902
4580
}
1903
4581
 
1904
 
int64_t Item_func_found_rows::val_int()
 
4582
longlong Item_func_found_rows::val_int()
1905
4583
{
1906
4584
  assert(fixed == 1);
1907
4585
  THD *thd= current_thd;