~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

  • Committer: Monty Taylor
  • Date: 2008-10-09 22:38:27 UTC
  • mto: This revision was merged to the branch mainline in revision 497.
  • Revision ID: monty@inaugust.com-20081009223827-bc9gvpiplsmvpwyq
Moved test() to its own file.
Made a new function to possibly replace int10_to_str.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** Copyright (C) 2000-2003 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
/**
 
18
  @file
 
19
 
 
20
  @brief
 
21
  This file defines all numerical functions
 
22
*/
 
23
 
 
24
#include <drizzled/server_includes.h>
 
25
#include "rpl_mi.h"
 
26
#include <mysys/my_bit.h>
 
27
#include <drizzled/drizzled_error_messages.h>
 
28
 
 
29
bool check_reserved_words(LEX_STRING *name)
 
30
{
 
31
  if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
 
32
      !my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
 
33
      !my_strcasecmp(system_charset_info, name->str, "SESSION"))
 
34
    return true;
 
35
  return false;
 
36
}
 
37
 
 
38
 
 
39
/**
 
40
  @return
 
41
    true if item is a constant
 
42
*/
 
43
 
 
44
bool
 
45
eval_const_cond(COND *cond)
 
46
{
 
47
  return ((Item_func*) cond)->val_int() ? true : false;
 
48
}
 
49
 
 
50
 
 
51
void Item_func::fix_num_length_and_dec()
 
52
{
 
53
  uint32_t fl_length= 0;
 
54
  decimals=0;
 
55
  for (uint32_t i=0 ; i < arg_count ; i++)
 
56
  {
 
57
    set_if_bigger(decimals,args[i]->decimals);
 
58
    set_if_bigger(fl_length, args[i]->max_length);
 
59
  }
 
60
  max_length=float_length(decimals);
 
61
  if (fl_length > max_length)
 
62
  {
 
63
    decimals= NOT_FIXED_DEC;
 
64
    max_length= float_length(NOT_FIXED_DEC);
 
65
  }
 
66
}
 
67
 
 
68
/**
 
69
  Set max_length/decimals of function if function is fixed point and
 
70
  result length/precision depends on argument ones.
 
71
*/
 
72
 
 
73
void Item_func::count_decimal_length()
 
74
{
 
75
  int max_int_part= 0;
 
76
  decimals= 0;
 
77
  unsigned_flag= 1;
 
78
  for (uint32_t i=0 ; i < arg_count ; i++)
 
79
  {
 
80
    set_if_bigger(decimals, args[i]->decimals);
 
81
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
 
82
    set_if_smaller(unsigned_flag, args[i]->unsigned_flag);
 
83
  }
 
84
  int precision= cmin(max_int_part + decimals, DECIMAL_MAX_PRECISION);
 
85
  max_length= my_decimal_precision_to_length(precision, decimals,
 
86
                                             unsigned_flag);
 
87
}
 
88
 
 
89
 
 
90
/**
 
91
  Set max_length of if it is maximum length of its arguments.
 
92
*/
 
93
 
 
94
void Item_func::count_only_length()
 
95
{
 
96
  max_length= 0;
 
97
  unsigned_flag= 0;
 
98
  for (uint32_t i=0 ; i < arg_count ; i++)
 
99
  {
 
100
    set_if_bigger(max_length, args[i]->max_length);
 
101
    set_if_bigger(unsigned_flag, args[i]->unsigned_flag);
 
102
  }
 
103
}
 
104
 
 
105
 
 
106
/**
 
107
  Set max_length/decimals of function if function is floating point and
 
108
  result length/precision depends on argument ones.
 
109
*/
 
110
 
 
111
void Item_func::count_real_length()
 
112
{
 
113
  uint32_t length= 0;
 
114
  decimals= 0;
 
115
  max_length= 0;
 
116
  for (uint32_t i=0 ; i < arg_count ; i++)
 
117
  {
 
118
    if (decimals != NOT_FIXED_DEC)
 
119
    {
 
120
      set_if_bigger(decimals, args[i]->decimals);
 
121
      set_if_bigger(length, (args[i]->max_length - args[i]->decimals));
 
122
    }
 
123
    set_if_bigger(max_length, args[i]->max_length);
 
124
  }
 
125
  if (decimals != NOT_FIXED_DEC)
 
126
  {
 
127
    max_length= length;
 
128
    length+= decimals;
 
129
    if (length < max_length)  // If previous operation gave overflow
 
130
      max_length= UINT32_MAX;
 
131
    else
 
132
      max_length= length;
 
133
  }
 
134
}
 
135
 
 
136
 
 
137
 
 
138
void Item_func::signal_divide_by_null()
 
139
{
 
140
  THD *thd= current_thd;
 
141
  push_warning(thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR, ER_DIVISION_BY_ZERO, ER(ER_DIVISION_BY_ZERO));
 
142
  null_value= 1;
 
143
}
 
144
 
 
145
 
 
146
Item *Item_func::get_tmp_table_item(THD *thd)
 
147
{
 
148
  if (!with_sum_func && !const_item() && functype() != SUSERVAR_FUNC)
 
149
    return new Item_field(result_field);
 
150
  return copy_or_same(thd);
 
151
}
 
152
 
 
153
 
 
154
 
 
155
void Item_func_connection_id::fix_length_and_dec()
 
156
{
 
157
  Item_int_func::fix_length_and_dec();
 
158
  max_length= 10;
 
159
}
 
160
 
 
161
 
 
162
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
 
163
{
 
164
  if (Item_int_func::fix_fields(thd, ref))
 
165
    return true;
 
166
  thd->thread_specific_used= true;
 
167
  value= thd->variables.pseudo_thread_id;
 
168
  return false;
 
169
}
 
170
 
 
171
 
 
172
/**
 
173
  Set result type for a numeric function of one argument
 
174
  (can be also used by a numeric function of many arguments, if the result
 
175
  type depends only on the first argument)
 
176
*/
 
177
 
 
178
void Item_func_num1::find_num_type()
 
179
{
 
180
  switch (hybrid_type= args[0]->result_type()) {
 
181
  case INT_RESULT:
 
182
    unsigned_flag= args[0]->unsigned_flag;
 
183
    break;
 
184
  case STRING_RESULT:
 
185
  case REAL_RESULT:
 
186
    hybrid_type= REAL_RESULT;
 
187
    max_length= float_length(decimals);
 
188
    break;
 
189
  case DECIMAL_RESULT:
 
190
    break;
 
191
  default:
 
192
    assert(0);
 
193
  }
 
194
  return;
 
195
}
 
196
 
 
197
 
 
198
void Item_func_num1::fix_num_length_and_dec()
 
199
{
 
200
  decimals= args[0]->decimals;
 
201
  max_length= args[0]->max_length;
 
202
}
 
203
 
 
204
 
 
205
void Item_func_signed::print(String *str, enum_query_type query_type)
 
206
{
 
207
  str->append(STRING_WITH_LEN("cast("));
 
208
  args[0]->print(str, query_type);
 
209
  str->append(STRING_WITH_LEN(" as signed)"));
 
210
 
 
211
}
 
212
 
 
213
int64_t Item_func_signed::val_int_from_str(int *error)
 
214
{
 
215
  char buff[MAX_FIELD_WIDTH], *end, *start;
 
216
  uint32_t length;
 
217
  String tmp(buff,sizeof(buff), &my_charset_bin), *res;
 
218
  int64_t value;
 
219
 
 
220
  /*
 
221
    For a string result, we must first get the string and then convert it
 
222
    to a int64_t
 
223
  */
 
224
 
 
225
  if (!(res= args[0]->val_str(&tmp)))
 
226
  {
 
227
    null_value= 1;
 
228
    *error= 0;
 
229
    return 0;
 
230
  }
 
231
  null_value= 0;
 
232
  start= (char *)res->ptr();
 
233
  length= res->length();
 
234
 
 
235
  end= start + length;
 
236
  value= my_strtoll10(start, &end, error);
 
237
  if (*error > 0 || end != start+ length)
 
238
  {
 
239
    char err_buff[128];
 
240
    String err_tmp(err_buff,(uint32_t) sizeof(err_buff), system_charset_info);
 
241
    err_tmp.copy(start, length, system_charset_info);
 
242
    push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
243
                        ER_TRUNCATED_WRONG_VALUE,
 
244
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
 
245
                        err_tmp.c_ptr());
 
246
  }
 
247
  return value;
 
248
}
 
249
 
 
250
 
 
251
int64_t Item_func_signed::val_int()
 
252
{
 
253
  int64_t value;
 
254
  int error;
 
255
 
 
256
  if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
257
      args[0]->result_as_int64_t())
 
258
  {
 
259
    value= args[0]->val_int();
 
260
    null_value= args[0]->null_value; 
 
261
    return value;
 
262
  }
 
263
 
 
264
  value= val_int_from_str(&error);
 
265
  if (value < 0 && error == 0)
 
266
  {
 
267
    push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
268
                 "Cast to signed converted positive out-of-range integer to "
 
269
                 "it's negative complement");
 
270
  }
 
271
  return value;
 
272
}
 
273
 
 
274
 
 
275
void Item_func_unsigned::print(String *str, enum_query_type query_type)
 
276
{
 
277
  str->append(STRING_WITH_LEN("cast("));
 
278
  args[0]->print(str, query_type);
 
279
  str->append(STRING_WITH_LEN(" as unsigned)"));
 
280
 
 
281
}
 
282
 
 
283
 
 
284
int64_t Item_func_unsigned::val_int()
 
285
{
 
286
  int64_t value;
 
287
  int error;
 
288
 
 
289
  if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
 
290
  {
 
291
    my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
 
292
    if (!(null_value= args[0]->null_value))
 
293
      my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
 
294
    else
 
295
      value= 0;
 
296
    return value;
 
297
  }
 
298
  else if (args[0]->cast_to_int_type() != STRING_RESULT ||
 
299
           args[0]->result_as_int64_t())
 
300
  {
 
301
    value= args[0]->val_int();
 
302
    null_value= args[0]->null_value; 
 
303
    return value;
 
304
  }
 
305
 
 
306
  value= val_int_from_str(&error);
 
307
  if (error < 0)
 
308
    push_warning(current_thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_UNKNOWN_ERROR,
 
309
                 "Cast to unsigned converted negative integer to it's "
 
310
                 "positive complement");
 
311
  return value;
 
312
}
 
313
 
 
314
 
 
315
String *Item_decimal_typecast::val_str(String *str)
 
316
{
 
317
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
318
  if (null_value)
 
319
    return NULL;
 
320
  my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
 
321
  return str;
 
322
}
 
323
 
 
324
 
 
325
double Item_decimal_typecast::val_real()
 
326
{
 
327
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
328
  double res;
 
329
  if (null_value)
 
330
    return 0.0;
 
331
  my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
 
332
  return res;
 
333
}
 
334
 
 
335
 
 
336
int64_t Item_decimal_typecast::val_int()
 
337
{
 
338
  my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
 
339
  int64_t res;
 
340
  if (null_value)
 
341
    return 0;
 
342
  my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
 
343
  return res;
 
344
}
 
345
 
 
346
 
 
347
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
 
348
{
 
349
  my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
 
350
  bool sign;
 
351
  uint32_t precision;
 
352
 
 
353
  if ((null_value= args[0]->null_value))
 
354
    return NULL;
 
355
  my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, false, dec);
 
356
  sign= dec->sign();
 
357
  if (unsigned_flag)
 
358
  {
 
359
    if (sign)
 
360
    {
 
361
      my_decimal_set_zero(dec);
 
362
      goto err;
 
363
    }
 
364
  }
 
365
  precision= my_decimal_length_to_precision(max_length,
 
366
                                            decimals, unsigned_flag);
 
367
  if (precision - decimals < (uint) my_decimal_intg(dec))
 
368
  {
 
369
    max_my_decimal(dec, precision, decimals);
 
370
    dec->sign(sign);
 
371
    goto err;
 
372
  }
 
373
  return dec;
 
374
 
 
375
err:
 
376
  push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
377
                      ER_WARN_DATA_OUT_OF_RANGE,
 
378
                      ER(ER_WARN_DATA_OUT_OF_RANGE),
 
379
                      name, 1);
 
380
  return dec;
 
381
}
 
382
 
 
383
 
 
384
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
 
385
{
 
386
  char len_buf[20*3 + 1];
 
387
  char *end;
 
388
 
 
389
  uint32_t precision= my_decimal_length_to_precision(max_length, decimals,
 
390
                                                 unsigned_flag);
 
391
  str->append(STRING_WITH_LEN("cast("));
 
392
  args[0]->print(str, query_type);
 
393
  str->append(STRING_WITH_LEN(" as decimal("));
 
394
 
 
395
  end=int10_to_str(precision, len_buf,10);
 
396
  str->append(len_buf, (uint32_t) (end - len_buf));
 
397
 
 
398
  str->append(',');
 
399
 
 
400
  end=int10_to_str(decimals, len_buf,10);
 
401
  str->append(len_buf, (uint32_t) (end - len_buf));
 
402
 
 
403
  str->append(')');
 
404
  str->append(')');
 
405
}
 
406
 
 
407
 
 
408
double Item_func_plus::real_op()
 
409
{
 
410
  double value= args[0]->val_real() + args[1]->val_real();
 
411
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
412
    return 0.0;
 
413
  return fix_result(value);
 
414
}
 
415
 
 
416
 
 
417
int64_t Item_func_plus::int_op()
 
418
{
 
419
  int64_t value=args[0]->val_int()+args[1]->val_int();
 
420
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
421
    return 0;
 
422
  return value;
 
423
}
 
424
 
 
425
 
 
426
/**
 
427
  Calculate plus of two decimals.
 
428
 
 
429
  @param decimal_value  Buffer that can be used to store result
 
430
 
 
431
  @retval
 
432
    0  Value was NULL;  In this case null_value is set
 
433
  @retval
 
434
    \# Value of operation as a decimal
 
435
*/
 
436
 
 
437
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
 
438
{
 
439
  my_decimal value1, *val1;
 
440
  my_decimal value2, *val2;
 
441
  val1= args[0]->val_decimal(&value1);
 
442
  if ((null_value= args[0]->null_value))
 
443
    return 0;
 
444
  val2= args[1]->val_decimal(&value2);
 
445
  if (!(null_value= (args[1]->null_value ||
 
446
                     (my_decimal_add(E_DEC_FATAL_ERROR, decimal_value, val1,
 
447
                                     val2) > 3))))
 
448
    return decimal_value;
 
449
  return 0;
 
450
}
 
451
 
 
452
/**
 
453
  Set precision of results for additive operations (+ and -)
 
454
*/
 
455
void Item_func_additive_op::result_precision()
 
456
{
 
457
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
458
  int max_int_part= cmax(args[0]->decimal_precision() - args[0]->decimals,
 
459
                        args[1]->decimal_precision() - args[1]->decimals);
 
460
  int precision= cmin(max_int_part + 1 + decimals, DECIMAL_MAX_PRECISION);
 
461
 
 
462
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
463
  if (result_type() == INT_RESULT)
 
464
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
465
  else
 
466
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
467
  max_length= my_decimal_precision_to_length(precision, decimals,
 
468
                                             unsigned_flag);
 
469
}
 
470
 
 
471
 
 
472
/**
 
473
  The following function is here to allow the user to force
 
474
  subtraction of UNSIGNED BIGINT to return negative values.
 
475
*/
 
476
 
 
477
void Item_func_minus::fix_length_and_dec()
 
478
{
 
479
  Item_num_op::fix_length_and_dec();
 
480
  if (unsigned_flag)
 
481
    unsigned_flag= 0;
 
482
}
 
483
 
 
484
 
 
485
double Item_func_minus::real_op()
 
486
{
 
487
  double value= args[0]->val_real() - args[1]->val_real();
 
488
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
489
    return 0.0;
 
490
  return fix_result(value);
 
491
}
 
492
 
 
493
 
 
494
int64_t Item_func_minus::int_op()
 
495
{
 
496
  int64_t value=args[0]->val_int() - args[1]->val_int();
 
497
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
498
    return 0;
 
499
  return value;
 
500
}
 
501
 
 
502
 
 
503
/**
 
504
  See Item_func_plus::decimal_op for comments.
 
505
*/
 
506
 
 
507
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
 
508
{
 
509
  my_decimal value1, *val1;
 
510
  my_decimal value2, *val2= 
 
511
 
 
512
  val1= args[0]->val_decimal(&value1);
 
513
  if ((null_value= args[0]->null_value))
 
514
    return 0;
 
515
  val2= args[1]->val_decimal(&value2);
 
516
  if (!(null_value= (args[1]->null_value ||
 
517
                     (my_decimal_sub(E_DEC_FATAL_ERROR, decimal_value, val1,
 
518
                                     val2) > 3))))
 
519
    return decimal_value;
 
520
  return 0;
 
521
}
 
522
 
 
523
 
 
524
double Item_func_mul::real_op()
 
525
{
 
526
  assert(fixed == 1);
 
527
  double value= args[0]->val_real() * args[1]->val_real();
 
528
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
529
    return 0.0;
 
530
  return fix_result(value);
 
531
}
 
532
 
 
533
 
 
534
int64_t Item_func_mul::int_op()
 
535
{
 
536
  assert(fixed == 1);
 
537
  int64_t value=args[0]->val_int()*args[1]->val_int();
 
538
  if ((null_value=args[0]->null_value || args[1]->null_value))
 
539
    return 0;
 
540
  return value;
 
541
}
 
542
 
 
543
 
 
544
/** See Item_func_plus::decimal_op for comments. */
 
545
 
 
546
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
 
547
{
 
548
  my_decimal value1, *val1;
 
549
  my_decimal value2, *val2;
 
550
  val1= args[0]->val_decimal(&value1);
 
551
  if ((null_value= args[0]->null_value))
 
552
    return 0;
 
553
  val2= args[1]->val_decimal(&value2);
 
554
  if (!(null_value= (args[1]->null_value ||
 
555
                     (my_decimal_mul(E_DEC_FATAL_ERROR, decimal_value, val1,
 
556
                                    val2) > 3))))
 
557
    return decimal_value;
 
558
  return 0;
 
559
}
 
560
 
 
561
 
 
562
void Item_func_mul::result_precision()
 
563
{
 
564
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
565
  if (result_type() == INT_RESULT)
 
566
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
567
  else
 
568
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
569
  decimals= cmin(args[0]->decimals + args[1]->decimals, DECIMAL_MAX_SCALE);
 
570
  int precision= cmin(args[0]->decimal_precision() + args[1]->decimal_precision(),
 
571
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
572
  max_length= my_decimal_precision_to_length(precision, decimals,unsigned_flag);
 
573
}
 
574
 
 
575
 
 
576
double Item_func_div::real_op()
 
577
{
 
578
  assert(fixed == 1);
 
579
  double value= args[0]->val_real();
 
580
  double val2= args[1]->val_real();
 
581
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
582
    return 0.0;
 
583
  if (val2 == 0.0)
 
584
  {
 
585
    signal_divide_by_null();
 
586
    return 0.0;
 
587
  }
 
588
  return fix_result(value/val2);
 
589
}
 
590
 
 
591
 
 
592
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
 
593
{
 
594
  my_decimal value1, *val1;
 
595
  my_decimal value2, *val2;
 
596
  int err;
 
597
 
 
598
  val1= args[0]->val_decimal(&value1);
 
599
  if ((null_value= args[0]->null_value))
 
600
    return 0;
 
601
  val2= args[1]->val_decimal(&value2);
 
602
  if ((null_value= args[1]->null_value))
 
603
    return 0;
 
604
  if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
605
                           val1, val2, prec_increment)) > 3)
 
606
  {
 
607
    if (err == E_DEC_DIV_ZERO)
 
608
      signal_divide_by_null();
 
609
    null_value= 1;
 
610
    return 0;
 
611
  }
 
612
  return decimal_value;
 
613
}
 
614
 
 
615
 
 
616
void Item_func_div::result_precision()
 
617
{
 
618
  uint32_t precision=cmin(args[0]->decimal_precision() + prec_increment,
 
619
                     (unsigned int)DECIMAL_MAX_PRECISION);
 
620
  /* Integer operations keep unsigned_flag if one of arguments is unsigned */
 
621
  if (result_type() == INT_RESULT)
 
622
    unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
 
623
  else
 
624
    unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
 
625
  decimals= cmin(args[0]->decimals + prec_increment, (unsigned int)DECIMAL_MAX_SCALE);
 
626
  max_length= my_decimal_precision_to_length(precision, decimals,
 
627
                                             unsigned_flag);
 
628
}
 
629
 
 
630
 
 
631
void Item_func_div::fix_length_and_dec()
 
632
{
 
633
  prec_increment= current_thd->variables.div_precincrement;
 
634
  Item_num_op::fix_length_and_dec();
 
635
  switch(hybrid_type) {
 
636
  case REAL_RESULT:
 
637
  {
 
638
    decimals=cmax(args[0]->decimals,args[1]->decimals)+prec_increment;
 
639
    set_if_smaller(decimals, NOT_FIXED_DEC);
 
640
    max_length=args[0]->max_length - args[0]->decimals + decimals;
 
641
    uint32_t tmp=float_length(decimals);
 
642
    set_if_smaller(max_length,tmp);
 
643
    break;
 
644
  }
 
645
  case INT_RESULT:
 
646
    hybrid_type= DECIMAL_RESULT;
 
647
    result_precision();
 
648
    break;
 
649
  case DECIMAL_RESULT:
 
650
    result_precision();
 
651
    break;
 
652
  default:
 
653
    assert(0);
 
654
  }
 
655
  maybe_null= 1; // devision by zero
 
656
  return;
 
657
}
 
658
 
 
659
 
 
660
/* Integer division */
 
661
int64_t Item_func_int_div::val_int()
 
662
{
 
663
  assert(fixed == 1);
 
664
  int64_t value=args[0]->val_int();
 
665
  int64_t val2=args[1]->val_int();
 
666
  if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
667
    return 0;
 
668
  if (val2 == 0)
 
669
  {
 
670
    signal_divide_by_null();
 
671
    return 0;
 
672
  }
 
673
  return (unsigned_flag ?
 
674
          (uint64_t) value / (uint64_t) val2 :
 
675
          value / val2);
 
676
}
 
677
 
 
678
 
 
679
void Item_func_int_div::fix_length_and_dec()
 
680
{
 
681
  Item_result argtype= args[0]->result_type();
 
682
  /* use precision ony for the data type it is applicable for and valid */
 
683
  max_length=args[0]->max_length -
 
684
    (argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
 
685
     args[0]->decimals : 0);
 
686
  maybe_null=1;
 
687
  unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
 
688
}
 
689
 
 
690
 
 
691
int64_t Item_func_mod::int_op()
 
692
{
 
693
  assert(fixed == 1);
 
694
  int64_t value=  args[0]->val_int();
 
695
  int64_t val2= args[1]->val_int();
 
696
  int64_t result;
 
697
 
 
698
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
699
    return 0; /* purecov: inspected */
 
700
  if (val2 == 0)
 
701
  {
 
702
    signal_divide_by_null();
 
703
    return 0;
 
704
  }
 
705
 
 
706
  if (args[0]->unsigned_flag)
 
707
    result= args[1]->unsigned_flag ? 
 
708
      ((uint64_t) value) % ((uint64_t) val2) : ((uint64_t) value) % val2;
 
709
  else
 
710
    result= args[1]->unsigned_flag ?
 
711
      value % ((uint64_t) val2) : value % val2;
 
712
 
 
713
  return result;
 
714
}
 
715
 
 
716
double Item_func_mod::real_op()
 
717
{
 
718
  assert(fixed == 1);
 
719
  double value= args[0]->val_real();
 
720
  double val2=  args[1]->val_real();
 
721
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
722
    return 0.0; /* purecov: inspected */
 
723
  if (val2 == 0.0)
 
724
  {
 
725
    signal_divide_by_null();
 
726
    return 0.0;
 
727
  }
 
728
  return fmod(value,val2);
 
729
}
 
730
 
 
731
 
 
732
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
 
733
{
 
734
  my_decimal value1, *val1;
 
735
  my_decimal value2, *val2;
 
736
 
 
737
  val1= args[0]->val_decimal(&value1);
 
738
  if ((null_value= args[0]->null_value))
 
739
    return 0;
 
740
  val2= args[1]->val_decimal(&value2);
 
741
  if ((null_value= args[1]->null_value))
 
742
    return 0;
 
743
  switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
 
744
                         val1, val2)) {
 
745
  case E_DEC_TRUNCATED:
 
746
  case E_DEC_OK:
 
747
    return decimal_value;
 
748
  case E_DEC_DIV_ZERO:
 
749
    signal_divide_by_null();
 
750
  default:
 
751
    null_value= 1;
 
752
    return 0;
 
753
  }
 
754
}
 
755
 
 
756
 
 
757
void Item_func_mod::result_precision()
 
758
{
 
759
  decimals= cmax(args[0]->decimals, args[1]->decimals);
 
760
  max_length= cmax(args[0]->max_length, args[1]->max_length);
 
761
}
 
762
 
 
763
 
 
764
void Item_func_mod::fix_length_and_dec()
 
765
{
 
766
  Item_num_op::fix_length_and_dec();
 
767
  maybe_null= 1;
 
768
  unsigned_flag= args[0]->unsigned_flag;
 
769
}
 
770
 
 
771
 
 
772
double Item_func_neg::real_op()
 
773
{
 
774
  double value= args[0]->val_real();
 
775
  null_value= args[0]->null_value;
 
776
  return -value;
 
777
}
 
778
 
 
779
 
 
780
int64_t Item_func_neg::int_op()
 
781
{
 
782
  int64_t value= args[0]->val_int();
 
783
  null_value= args[0]->null_value;
 
784
  return -value;
 
785
}
 
786
 
 
787
 
 
788
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
 
789
{
 
790
  my_decimal val, *value= args[0]->val_decimal(&val);
 
791
  if (!(null_value= args[0]->null_value))
 
792
  {
 
793
    my_decimal2decimal(value, decimal_value);
 
794
    my_decimal_neg(decimal_value);
 
795
    return decimal_value;
 
796
  }
 
797
  return 0;
 
798
}
 
799
 
 
800
 
 
801
void Item_func_neg::fix_num_length_and_dec()
 
802
{
 
803
  decimals= args[0]->decimals;
 
804
  /* 1 add because sign can appear */
 
805
  max_length= args[0]->max_length + 1;
 
806
}
 
807
 
 
808
 
 
809
void Item_func_neg::fix_length_and_dec()
 
810
{
 
811
  Item_func_num1::fix_length_and_dec();
 
812
 
 
813
  /*
 
814
    If this is in integer context keep the context as integer if possible
 
815
    (This is how multiplication and other integer functions works)
 
816
    Use val() to get value as arg_type doesn't mean that item is
 
817
    Item_int or Item_real due to existence of Item_param.
 
818
  */
 
819
  if (hybrid_type == INT_RESULT && args[0]->const_item())
 
820
  {
 
821
    int64_t val= args[0]->val_int();
 
822
    if ((uint64_t) val >= (uint64_t) INT64_MIN &&
 
823
        ((uint64_t) val != (uint64_t) INT64_MIN ||
 
824
          args[0]->type() != INT_ITEM))        
 
825
    {
 
826
      /*
 
827
        Ensure that result is converted to DECIMAL, as int64_t can't hold
 
828
        the negated number
 
829
      */
 
830
      hybrid_type= DECIMAL_RESULT;
 
831
    }
 
832
  }
 
833
  unsigned_flag= 0;
 
834
  return;
 
835
}
 
836
 
 
837
 
 
838
double Item_func_abs::real_op()
 
839
{
 
840
  double value= args[0]->val_real();
 
841
  null_value= args[0]->null_value;
 
842
  return fabs(value);
 
843
}
 
844
 
 
845
 
 
846
int64_t Item_func_abs::int_op()
 
847
{
 
848
  int64_t value= args[0]->val_int();
 
849
  if ((null_value= args[0]->null_value))
 
850
    return 0;
 
851
  return (value >= 0) || unsigned_flag ? value : -value;
 
852
}
 
853
 
 
854
 
 
855
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
 
856
{
 
857
  my_decimal val, *value= args[0]->val_decimal(&val);
 
858
  if (!(null_value= args[0]->null_value))
 
859
  {
 
860
    my_decimal2decimal(value, decimal_value);
 
861
    if (decimal_value->sign())
 
862
      my_decimal_neg(decimal_value);
 
863
    return decimal_value;
 
864
  }
 
865
  return 0;
 
866
}
 
867
 
 
868
 
 
869
void Item_func_abs::fix_length_and_dec()
 
870
{
 
871
  Item_func_num1::fix_length_and_dec();
 
872
  unsigned_flag= args[0]->unsigned_flag;
 
873
}
 
874
 
 
875
 
 
876
/** Gateway to natural LOG function. */
 
877
double Item_func_ln::val_real()
 
878
{
 
879
  assert(fixed == 1);
 
880
  double value= args[0]->val_real();
 
881
  if ((null_value= args[0]->null_value))
 
882
    return 0.0;
 
883
  if (value <= 0.0)
 
884
  {
 
885
    signal_divide_by_null();
 
886
    return 0.0;
 
887
  }
 
888
  return log(value);
 
889
}
 
890
 
 
891
/** 
 
892
  Extended but so slower LOG function.
 
893
 
 
894
  We have to check if all values are > zero and first one is not one
 
895
  as these are the cases then result is not a number.
 
896
*/ 
 
897
double Item_func_log::val_real()
 
898
{
 
899
  assert(fixed == 1);
 
900
  double value= args[0]->val_real();
 
901
  if ((null_value= args[0]->null_value))
 
902
    return 0.0;
 
903
  if (value <= 0.0)
 
904
  {
 
905
    signal_divide_by_null();
 
906
    return 0.0;
 
907
  }
 
908
  if (arg_count == 2)
 
909
  {
 
910
    double value2= args[1]->val_real();
 
911
    if ((null_value= args[1]->null_value))
 
912
      return 0.0;
 
913
    if (value2 <= 0.0 || value == 1.0)
 
914
    {
 
915
      signal_divide_by_null();
 
916
      return 0.0;
 
917
    }
 
918
    return log(value2) / log(value);
 
919
  }
 
920
  return log(value);
 
921
}
 
922
 
 
923
double Item_func_log2::val_real()
 
924
{
 
925
  assert(fixed == 1);
 
926
  double value= args[0]->val_real();
 
927
 
 
928
  if ((null_value=args[0]->null_value))
 
929
    return 0.0;
 
930
  if (value <= 0.0)
 
931
  {
 
932
    signal_divide_by_null();
 
933
    return 0.0;
 
934
  }
 
935
  return log(value) / M_LN2;
 
936
}
 
937
 
 
938
double Item_func_log10::val_real()
 
939
{
 
940
  assert(fixed == 1);
 
941
  double value= args[0]->val_real();
 
942
  if ((null_value= args[0]->null_value))
 
943
    return 0.0;
 
944
  if (value <= 0.0)
 
945
  {
 
946
    signal_divide_by_null();
 
947
    return 0.0;
 
948
  }
 
949
  return log10(value);
 
950
}
 
951
 
 
952
double Item_func_exp::val_real()
 
953
{
 
954
  assert(fixed == 1);
 
955
  double value= args[0]->val_real();
 
956
  if ((null_value=args[0]->null_value))
 
957
    return 0.0; /* purecov: inspected */
 
958
  return fix_result(exp(value));
 
959
}
 
960
 
 
961
double Item_func_sqrt::val_real()
 
962
{
 
963
  assert(fixed == 1);
 
964
  double value= args[0]->val_real();
 
965
  if ((null_value=(args[0]->null_value || value < 0)))
 
966
    return 0.0; /* purecov: inspected */
 
967
  return sqrt(value);
 
968
}
 
969
 
 
970
double Item_func_pow::val_real()
 
971
{
 
972
  assert(fixed == 1);
 
973
  double value= args[0]->val_real();
 
974
  double val2= args[1]->val_real();
 
975
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
 
976
    return 0.0; /* purecov: inspected */
 
977
  return fix_result(pow(value,val2));
 
978
}
 
979
 
 
980
// Trigonometric functions
 
981
 
 
982
double Item_func_acos::val_real()
 
983
{
 
984
  assert(fixed == 1);
 
985
  // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
 
986
  volatile double value= args[0]->val_real();
 
987
  if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
988
    return 0.0;
 
989
  return acos(value);
 
990
}
 
991
 
 
992
double Item_func_asin::val_real()
 
993
{
 
994
  assert(fixed == 1);
 
995
  // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
 
996
  volatile double value= args[0]->val_real();
 
997
  if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
 
998
    return 0.0;
 
999
  return asin(value);
 
1000
}
 
1001
 
 
1002
double Item_func_atan::val_real()
 
1003
{
 
1004
  assert(fixed == 1);
 
1005
  double value= args[0]->val_real();
 
1006
  if ((null_value=args[0]->null_value))
 
1007
    return 0.0;
 
1008
  if (arg_count == 2)
 
1009
  {
 
1010
    double val2= args[1]->val_real();
 
1011
    if ((null_value=args[1]->null_value))
 
1012
      return 0.0;
 
1013
    return fix_result(atan2(value,val2));
 
1014
  }
 
1015
  return atan(value);
 
1016
}
 
1017
 
 
1018
double Item_func_cos::val_real()
 
1019
{
 
1020
  assert(fixed == 1);
 
1021
  double value= args[0]->val_real();
 
1022
  if ((null_value=args[0]->null_value))
 
1023
    return 0.0;
 
1024
  return cos(value);
 
1025
}
 
1026
 
 
1027
double Item_func_sin::val_real()
 
1028
{
 
1029
  assert(fixed == 1);
 
1030
  double value= args[0]->val_real();
 
1031
  if ((null_value=args[0]->null_value))
 
1032
    return 0.0;
 
1033
  return sin(value);
 
1034
}
 
1035
 
 
1036
double Item_func_tan::val_real()
 
1037
{
 
1038
  assert(fixed == 1);
 
1039
  double value= args[0]->val_real();
 
1040
  if ((null_value=args[0]->null_value))
 
1041
    return 0.0;
 
1042
  return fix_result(tan(value));
 
1043
}
 
1044
 
 
1045
 
 
1046
// Shift-functions, same as << and >> in C/C++
 
1047
 
 
1048
 
 
1049
int64_t Item_func_shift_left::val_int()
 
1050
{
 
1051
  assert(fixed == 1);
 
1052
  uint32_t shift;
 
1053
  uint64_t res= ((uint64_t) args[0]->val_int() <<
 
1054
                  (shift=(uint) args[1]->val_int()));
 
1055
  if (args[0]->null_value || args[1]->null_value)
 
1056
  {
 
1057
    null_value=1;
 
1058
    return 0;
 
1059
  }
 
1060
  null_value=0;
 
1061
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0L);
 
1062
}
 
1063
 
 
1064
int64_t Item_func_shift_right::val_int()
 
1065
{
 
1066
  assert(fixed == 1);
 
1067
  uint32_t shift;
 
1068
  uint64_t res= (uint64_t) args[0]->val_int() >>
 
1069
    (shift=(uint) args[1]->val_int());
 
1070
  if (args[0]->null_value || args[1]->null_value)
 
1071
  {
 
1072
    null_value=1;
 
1073
    return 0;
 
1074
  }
 
1075
  null_value=0;
 
1076
  return (shift < sizeof(int64_t)*8 ? (int64_t) res : 0);
 
1077
}
 
1078
 
 
1079
 
 
1080
int64_t Item_func_bit_neg::val_int()
 
1081
{
 
1082
  assert(fixed == 1);
 
1083
  uint64_t res= (uint64_t) args[0]->val_int();
 
1084
  if ((null_value=args[0]->null_value))
 
1085
    return 0;
 
1086
  return ~res;
 
1087
}
 
1088
 
 
1089
 
 
1090
// Conversion functions
 
1091
 
 
1092
void Item_func_integer::fix_length_and_dec()
 
1093
{
 
1094
  max_length=args[0]->max_length - args[0]->decimals+1;
 
1095
  uint32_t tmp=float_length(decimals);
 
1096
  set_if_smaller(max_length,tmp);
 
1097
  decimals=0;
 
1098
}
 
1099
 
 
1100
void Item_func_int_val::fix_num_length_and_dec()
 
1101
{
 
1102
  max_length= args[0]->max_length - (args[0]->decimals ?
 
1103
                                     args[0]->decimals + 1 :
 
1104
                                     0) + 2;
 
1105
  uint32_t tmp= float_length(decimals);
 
1106
  set_if_smaller(max_length,tmp);
 
1107
  decimals= 0;
 
1108
}
 
1109
 
 
1110
 
 
1111
void Item_func_int_val::find_num_type()
 
1112
{
 
1113
  switch(hybrid_type= args[0]->result_type())
 
1114
  {
 
1115
  case STRING_RESULT:
 
1116
  case REAL_RESULT:
 
1117
    hybrid_type= REAL_RESULT;
 
1118
    max_length= float_length(decimals);
 
1119
    break;
 
1120
  case INT_RESULT:
 
1121
  case DECIMAL_RESULT:
 
1122
    /*
 
1123
      -2 because in most high position can't be used any digit for int64_t
 
1124
      and one position for increasing value during operation
 
1125
    */
 
1126
    if ((args[0]->max_length - args[0]->decimals) >=
 
1127
        (DECIMAL_LONGLONG_DIGITS - 2))
 
1128
    {
 
1129
      hybrid_type= DECIMAL_RESULT;
 
1130
    }
 
1131
    else
 
1132
    {
 
1133
      unsigned_flag= args[0]->unsigned_flag;
 
1134
      hybrid_type= INT_RESULT;
 
1135
    }
 
1136
    break;
 
1137
  default:
 
1138
    assert(0);
 
1139
  }
 
1140
  return;
 
1141
}
 
1142
 
 
1143
 
 
1144
int64_t Item_func_ceiling::int_op()
 
1145
{
 
1146
  int64_t result;
 
1147
  switch (args[0]->result_type()) {
 
1148
  case INT_RESULT:
 
1149
    result= args[0]->val_int();
 
1150
    null_value= args[0]->null_value;
 
1151
    break;
 
1152
  case DECIMAL_RESULT:
 
1153
  {
 
1154
    my_decimal dec_buf, *dec;
 
1155
    if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
 
1156
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1157
    else
 
1158
      result= 0;
 
1159
    break;
 
1160
  }
 
1161
  default:
 
1162
    result= (int64_t)Item_func_ceiling::real_op();
 
1163
  };
 
1164
  return result;
 
1165
}
 
1166
 
 
1167
 
 
1168
double Item_func_ceiling::real_op()
 
1169
{
 
1170
  /*
 
1171
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1172
    bug)
 
1173
  */
 
1174
  volatile double value= args[0]->val_real();
 
1175
  null_value= args[0]->null_value;
 
1176
  return ceil(value);
 
1177
}
 
1178
 
 
1179
 
 
1180
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
 
1181
{
 
1182
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1183
  if (!(null_value= (args[0]->null_value ||
 
1184
                     my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
 
1185
                                        decimal_value) > 1)))
 
1186
    return decimal_value;
 
1187
  return 0;
 
1188
}
 
1189
 
 
1190
 
 
1191
int64_t Item_func_floor::int_op()
 
1192
{
 
1193
  int64_t result;
 
1194
  switch (args[0]->result_type()) {
 
1195
  case INT_RESULT:
 
1196
    result= args[0]->val_int();
 
1197
    null_value= args[0]->null_value;
 
1198
    break;
 
1199
  case DECIMAL_RESULT:
 
1200
  {
 
1201
    my_decimal dec_buf, *dec;
 
1202
    if ((dec= Item_func_floor::decimal_op(&dec_buf)))
 
1203
      my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
 
1204
    else
 
1205
      result= 0;
 
1206
    break;
 
1207
  }
 
1208
  default:
 
1209
    result= (int64_t)Item_func_floor::real_op();
 
1210
  };
 
1211
  return result;
 
1212
}
 
1213
 
 
1214
 
 
1215
double Item_func_floor::real_op()
 
1216
{
 
1217
  /*
 
1218
    the volatile's for BUG #3051 to calm optimizer down (because of gcc's
 
1219
    bug)
 
1220
  */
 
1221
  volatile double value= args[0]->val_real();
 
1222
  null_value= args[0]->null_value;
 
1223
  return floor(value);
 
1224
}
 
1225
 
 
1226
 
 
1227
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
 
1228
{
 
1229
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1230
  if (!(null_value= (args[0]->null_value ||
 
1231
                     my_decimal_floor(E_DEC_FATAL_ERROR, value,
 
1232
                                      decimal_value) > 1)))
 
1233
    return decimal_value;
 
1234
  return 0;
 
1235
}
 
1236
 
 
1237
 
 
1238
void Item_func_round::fix_length_and_dec()
 
1239
{
 
1240
  int      decimals_to_set;
 
1241
  int64_t val1;
 
1242
  bool     val1_unsigned;
 
1243
  
 
1244
  unsigned_flag= args[0]->unsigned_flag;
 
1245
  if (!args[1]->const_item())
 
1246
  {
 
1247
    max_length= args[0]->max_length;
 
1248
    decimals= args[0]->decimals;
 
1249
    if (args[0]->result_type() == DECIMAL_RESULT)
 
1250
    {
 
1251
      max_length++;
 
1252
      hybrid_type= DECIMAL_RESULT;
 
1253
    }
 
1254
    else
 
1255
      hybrid_type= REAL_RESULT;
 
1256
    return;
 
1257
  }
 
1258
 
 
1259
  val1= args[1]->val_int();
 
1260
  val1_unsigned= args[1]->unsigned_flag;
 
1261
  if (val1 < 0)
 
1262
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
 
1263
  else
 
1264
    decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
 
1265
 
 
1266
  if (args[0]->decimals == NOT_FIXED_DEC)
 
1267
  {
 
1268
    max_length= args[0]->max_length;
 
1269
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1270
    hybrid_type= REAL_RESULT;
 
1271
    return;
 
1272
  }
 
1273
  
 
1274
  switch (args[0]->result_type()) {
 
1275
  case REAL_RESULT:
 
1276
  case STRING_RESULT:
 
1277
    hybrid_type= REAL_RESULT;
 
1278
    decimals= cmin(decimals_to_set, NOT_FIXED_DEC);
 
1279
    max_length= float_length(decimals);
 
1280
    break;
 
1281
  case INT_RESULT:
 
1282
    if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
 
1283
    {
 
1284
      int length_can_increase= test(!truncate && (val1 < 0) && !val1_unsigned);
 
1285
      max_length= args[0]->max_length + length_can_increase;
 
1286
      /* Here we can keep INT_RESULT */
 
1287
      hybrid_type= INT_RESULT;
 
1288
      decimals= 0;
 
1289
      break;
 
1290
    }
 
1291
    /* fall through */
 
1292
  case DECIMAL_RESULT:
 
1293
  {
 
1294
    hybrid_type= DECIMAL_RESULT;
 
1295
    decimals_to_set= cmin(DECIMAL_MAX_SCALE, decimals_to_set);
 
1296
    int decimals_delta= args[0]->decimals - decimals_to_set;
 
1297
    int precision= args[0]->decimal_precision();
 
1298
    int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
 
1299
 
 
1300
    precision-= decimals_delta - length_increase;
 
1301
    decimals= cmin(decimals_to_set, DECIMAL_MAX_SCALE);
 
1302
    max_length= my_decimal_precision_to_length(precision, decimals,
 
1303
                                               unsigned_flag);
 
1304
    break;
 
1305
  }
 
1306
  default:
 
1307
    assert(0); /* This result type isn't handled */
 
1308
  }
 
1309
}
 
1310
 
 
1311
double my_double_round(double value, int64_t dec, bool dec_unsigned,
 
1312
                       bool truncate)
 
1313
{
 
1314
  double tmp;
 
1315
  bool dec_negative= (dec < 0) && !dec_unsigned;
 
1316
  uint64_t abs_dec= dec_negative ? -dec : dec;
 
1317
  /*
 
1318
    tmp2 is here to avoid return the value with 80 bit precision
 
1319
    This will fix that the test round(0.1,1) = round(0.1,1) is true
 
1320
  */
 
1321
  volatile double tmp2;
 
1322
 
 
1323
  tmp=(abs_dec < array_elements(log_10) ?
 
1324
       log_10[abs_dec] : pow(10.0,(double) abs_dec));
 
1325
 
 
1326
  if (dec_negative && std::isinf(tmp))
 
1327
    tmp2= 0;
 
1328
  else if (!dec_negative && std::isinf(value * tmp))
 
1329
    tmp2= value;
 
1330
  else if (truncate)
 
1331
  {
 
1332
    if (value >= 0)
 
1333
      tmp2= dec < 0 ? floor(value/tmp)*tmp : floor(value*tmp)/tmp;
 
1334
    else
 
1335
      tmp2= dec < 0 ? ceil(value/tmp)*tmp : ceil(value*tmp)/tmp;
 
1336
  }
 
1337
  else
 
1338
    tmp2=dec < 0 ? rint(value/tmp)*tmp : rint(value*tmp)/tmp;
 
1339
  return tmp2;
 
1340
}
 
1341
 
 
1342
 
 
1343
double Item_func_round::real_op()
 
1344
{
 
1345
  double value= args[0]->val_real();
 
1346
 
 
1347
  if (!(null_value= args[0]->null_value || args[1]->null_value))
 
1348
    return my_double_round(value, args[1]->val_int(), args[1]->unsigned_flag,
 
1349
                           truncate);
 
1350
 
 
1351
  return 0.0;
 
1352
}
 
1353
 
 
1354
/*
 
1355
  Rounds a given value to a power of 10 specified as the 'to' argument,
 
1356
  avoiding overflows when the value is close to the uint64_t range boundary.
 
1357
*/
 
1358
 
 
1359
static inline uint64_t my_unsigned_round(uint64_t value, uint64_t to)
 
1360
{
 
1361
  uint64_t tmp= value / to * to;
 
1362
  return (value - tmp < (to >> 1)) ? tmp : tmp + to;
 
1363
}
 
1364
 
 
1365
 
 
1366
int64_t Item_func_round::int_op()
 
1367
{
 
1368
  int64_t value= args[0]->val_int();
 
1369
  int64_t dec= args[1]->val_int();
 
1370
  decimals= 0;
 
1371
  uint64_t abs_dec;
 
1372
  if ((null_value= args[0]->null_value || args[1]->null_value))
 
1373
    return 0;
 
1374
  if ((dec >= 0) || args[1]->unsigned_flag)
 
1375
    return value; // integer have not digits after point
 
1376
 
 
1377
  abs_dec= -dec;
 
1378
  int64_t tmp;
 
1379
  
 
1380
  if(abs_dec >= array_elements(log_10_int))
 
1381
    return 0;
 
1382
  
 
1383
  tmp= log_10_int[abs_dec];
 
1384
  
 
1385
  if (truncate)
 
1386
    value= (unsigned_flag) ?
 
1387
      ((uint64_t) value / tmp) * tmp : (value / tmp) * tmp;
 
1388
  else
 
1389
    value= (unsigned_flag || value >= 0) ?
 
1390
      my_unsigned_round((uint64_t) value, tmp) :
 
1391
      -(int64_t) my_unsigned_round((uint64_t) -value, tmp);
 
1392
  return value;
 
1393
}
 
1394
 
 
1395
 
 
1396
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
 
1397
{
 
1398
  my_decimal val, *value= args[0]->val_decimal(&val);
 
1399
  int64_t dec= args[1]->val_int();
 
1400
  if (dec >= 0 || args[1]->unsigned_flag)
 
1401
    dec= cmin(dec, (int64_t) decimals);
 
1402
  else if (dec < INT_MIN)
 
1403
    dec= INT_MIN;
 
1404
    
 
1405
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
 
1406
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
 
1407
                                      truncate, decimal_value) > 1))) 
 
1408
  {
 
1409
    decimal_value->frac= decimals;
 
1410
    return decimal_value;
 
1411
  }
 
1412
  return 0;
 
1413
}
 
1414
 
 
1415
 
 
1416
void Item_func_rand::seed_random(Item *arg)
 
1417
{
 
1418
  /*
 
1419
    TODO: do not do reinit 'rand' for every execute of PS/SP if
 
1420
    args[0] is a constant.
 
1421
  */
 
1422
  uint32_t tmp= (uint32_t) arg->val_int();
 
1423
  randominit(rand, (uint32_t) (tmp*0x10001L+55555555L),
 
1424
             (uint32_t) (tmp*0x10000001L));
 
1425
}
 
1426
 
 
1427
 
 
1428
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
 
1429
{
 
1430
  if (Item_real_func::fix_fields(thd, ref))
 
1431
    return true;
 
1432
  used_tables_cache|= RAND_TABLE_BIT;
 
1433
  if (arg_count)
 
1434
  {                                     // Only use argument once in query
 
1435
    /*
 
1436
      No need to send a Rand log event if seed was given eg: RAND(seed),
 
1437
      as it will be replicated in the query as such.
 
1438
    */
 
1439
    if (!rand && !(rand= (struct rand_struct*)
 
1440
                   thd->alloc(sizeof(*rand))))
 
1441
      return true;
 
1442
 
 
1443
    if (args[0]->const_item())
 
1444
      seed_random (args[0]);
 
1445
  }
 
1446
  else
 
1447
  {
 
1448
    /*
 
1449
      Save the seed only the first time RAND() is used in the query
 
1450
      Once events are forwarded rather than recreated,
 
1451
      the following can be skipped if inside the slave thread
 
1452
    */
 
1453
    if (!thd->rand_used)
 
1454
    {
 
1455
      thd->rand_used= 1;
 
1456
      thd->rand_saved_seed1= thd->rand.seed1;
 
1457
      thd->rand_saved_seed2= thd->rand.seed2;
 
1458
    }
 
1459
    rand= &thd->rand;
 
1460
  }
 
1461
  return false;
 
1462
}
 
1463
 
 
1464
void Item_func_rand::update_used_tables()
 
1465
{
 
1466
  Item_real_func::update_used_tables();
 
1467
  used_tables_cache|= RAND_TABLE_BIT;
 
1468
}
 
1469
 
 
1470
 
 
1471
double Item_func_rand::val_real()
 
1472
{
 
1473
  assert(fixed == 1);
 
1474
  if (arg_count && !args[0]->const_item())
 
1475
    seed_random (args[0]);
 
1476
  return my_rnd(rand);
 
1477
}
 
1478
 
 
1479
int64_t Item_func_sign::val_int()
 
1480
{
 
1481
  assert(fixed == 1);
 
1482
  double value= args[0]->val_real();
 
1483
  null_value=args[0]->null_value;
 
1484
  return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
 
1485
}
 
1486
 
 
1487
 
 
1488
double Item_func_units::val_real()
 
1489
{
 
1490
  assert(fixed == 1);
 
1491
  double value= args[0]->val_real();
 
1492
  if ((null_value=args[0]->null_value))
 
1493
    return 0;
 
1494
  return value*mul+add;
 
1495
}
 
1496
 
 
1497
 
 
1498
void Item_func_min_max::fix_length_and_dec()
 
1499
{
 
1500
  int max_int_part=0;
 
1501
  bool datetime_found= false;
 
1502
  decimals=0;
 
1503
  max_length=0;
 
1504
  maybe_null=0;
 
1505
  cmp_type=args[0]->result_type();
 
1506
 
 
1507
  for (uint32_t i=0 ; i < arg_count ; i++)
 
1508
  {
 
1509
    set_if_bigger(max_length, args[i]->max_length);
 
1510
    set_if_bigger(decimals, args[i]->decimals);
 
1511
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
 
1512
    if (args[i]->maybe_null)
 
1513
      maybe_null=1;
 
1514
    cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
 
1515
    if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
 
1516
    {
 
1517
      datetime_found= true;
 
1518
      if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
 
1519
        datetime_item= args[i];
 
1520
    }
 
1521
  }
 
1522
  if (cmp_type == STRING_RESULT)
 
1523
  {
 
1524
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
 
1525
    if (datetime_found)
 
1526
    {
 
1527
      thd= current_thd;
 
1528
      compare_as_dates= true;
 
1529
    }
 
1530
  }
 
1531
  else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
 
1532
    max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
 
1533
                                            unsigned_flag);
 
1534
  cached_field_type= agg_field_type(args, arg_count);
 
1535
}
 
1536
 
 
1537
 
 
1538
/*
 
1539
  Compare item arguments in the DATETIME context.
 
1540
 
 
1541
  SYNOPSIS
 
1542
    cmp_datetimes()
 
1543
    value [out]   found least/greatest DATE/DATETIME value
 
1544
 
 
1545
  DESCRIPTION
 
1546
    Compare item arguments as DATETIME values and return the index of the
 
1547
    least/greatest argument in the arguments array.
 
1548
    The correct integer DATE/DATETIME value of the found argument is
 
1549
    stored to the value pointer, if latter is provided.
 
1550
 
 
1551
  RETURN
 
1552
   0    If one of arguments is NULL
 
1553
   #    index of the least/greatest argument
 
1554
*/
 
1555
 
 
1556
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
 
1557
{
 
1558
  uint64_t min_max= 0;
 
1559
  uint32_t min_max_idx= 0;
 
1560
 
 
1561
  for (uint32_t i=0; i < arg_count ; i++)
 
1562
  {
 
1563
    Item **arg= args + i;
 
1564
    bool is_null;
 
1565
    uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
 
1566
    if ((null_value= args[i]->null_value))
 
1567
      return 0;
 
1568
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
1569
    {
 
1570
      min_max= res;
 
1571
      min_max_idx= i;
 
1572
    }
 
1573
  }
 
1574
  if (value)
 
1575
  {
 
1576
    *value= min_max;
 
1577
    if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
 
1578
      *value/= 1000000L;
 
1579
  }
 
1580
  return min_max_idx;
 
1581
}
 
1582
 
 
1583
 
 
1584
String *Item_func_min_max::val_str(String *str)
 
1585
{
 
1586
  assert(fixed == 1);
 
1587
  if (compare_as_dates)
 
1588
  {
 
1589
    String *str_res;
 
1590
    uint32_t min_max_idx= cmp_datetimes(NULL);
 
1591
    if (null_value)
 
1592
      return 0;
 
1593
    str_res= args[min_max_idx]->val_str(str);
 
1594
    str_res->set_charset(collation.collation);
 
1595
    return str_res;
 
1596
  }
 
1597
  switch (cmp_type) {
 
1598
  case INT_RESULT:
 
1599
  {
 
1600
    int64_t nr=val_int();
 
1601
    if (null_value)
 
1602
      return 0;
 
1603
    str->set_int(nr, unsigned_flag, &my_charset_bin);
 
1604
    return str;
 
1605
  }
 
1606
  case DECIMAL_RESULT:
 
1607
  {
 
1608
    my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
 
1609
    if (null_value)
 
1610
      return 0;
 
1611
    my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
 
1612
    return str;
 
1613
  }
 
1614
  case REAL_RESULT:
 
1615
  {
 
1616
    double nr= val_real();
 
1617
    if (null_value)
 
1618
      return 0; /* purecov: inspected */
 
1619
    str->set_real(nr,decimals,&my_charset_bin);
 
1620
    return str;
 
1621
  }
 
1622
  case STRING_RESULT:
 
1623
  {
 
1624
    String *res= NULL;
 
1625
 
 
1626
    for (uint32_t i=0; i < arg_count ; i++)
 
1627
    {
 
1628
      if (i == 0)
 
1629
        res=args[i]->val_str(str);
 
1630
      else
 
1631
      {
 
1632
        String *res2;
 
1633
        res2= args[i]->val_str(res == str ? &tmp_value : str);
 
1634
        if (res2)
 
1635
        {
 
1636
          int cmp= sortcmp(res,res2,collation.collation);
 
1637
          if ((cmp_sign < 0 ? cmp : -cmp) < 0)
 
1638
            res=res2;
 
1639
        }
 
1640
      }
 
1641
      if ((null_value= args[i]->null_value))
 
1642
        return 0;
 
1643
    }
 
1644
    res->set_charset(collation.collation);
 
1645
    return res;
 
1646
  }
 
1647
  case ROW_RESULT:
 
1648
  default:
 
1649
    // This case should never be chosen
 
1650
    assert(0);
 
1651
    return 0;
 
1652
  }
 
1653
  return 0;                                     // Keep compiler happy
 
1654
}
 
1655
 
 
1656
 
 
1657
double Item_func_min_max::val_real()
 
1658
{
 
1659
  assert(fixed == 1);
 
1660
  double value=0.0;
 
1661
  if (compare_as_dates)
 
1662
  {
 
1663
    uint64_t result= 0;
 
1664
    (void)cmp_datetimes(&result);
 
1665
    return (double)result;
 
1666
  }
 
1667
  for (uint32_t i=0; i < arg_count ; i++)
 
1668
  {
 
1669
    if (i == 0)
 
1670
      value= args[i]->val_real();
 
1671
    else
 
1672
    {
 
1673
      double tmp= args[i]->val_real();
 
1674
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
1675
        value=tmp;
 
1676
    }
 
1677
    if ((null_value= args[i]->null_value))
 
1678
      break;
 
1679
  }
 
1680
  return value;
 
1681
}
 
1682
 
 
1683
 
 
1684
int64_t Item_func_min_max::val_int()
 
1685
{
 
1686
  assert(fixed == 1);
 
1687
  int64_t value=0;
 
1688
  if (compare_as_dates)
 
1689
  {
 
1690
    uint64_t result= 0;
 
1691
    (void)cmp_datetimes(&result);
 
1692
    return (int64_t)result;
 
1693
  }
 
1694
  for (uint32_t i=0; i < arg_count ; i++)
 
1695
  {
 
1696
    if (i == 0)
 
1697
      value=args[i]->val_int();
 
1698
    else
 
1699
    {
 
1700
      int64_t tmp=args[i]->val_int();
 
1701
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
1702
        value=tmp;
 
1703
    }
 
1704
    if ((null_value= args[i]->null_value))
 
1705
      break;
 
1706
  }
 
1707
  return value;
 
1708
}
 
1709
 
 
1710
 
 
1711
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
 
1712
{
 
1713
  assert(fixed == 1);
 
1714
  my_decimal tmp_buf, *tmp, *res= NULL;
 
1715
 
 
1716
  if (compare_as_dates)
 
1717
  {
 
1718
    uint64_t value= 0;
 
1719
    (void)cmp_datetimes(&value);
 
1720
    uint64_t2decimal(value, dec);
 
1721
    return dec;
 
1722
  }
 
1723
  for (uint32_t i=0; i < arg_count ; i++)
 
1724
  {
 
1725
    if (i == 0)
 
1726
      res= args[i]->val_decimal(dec);
 
1727
    else
 
1728
    {
 
1729
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
 
1730
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
 
1731
      {
 
1732
        if (tmp == &tmp_buf)
 
1733
        {
 
1734
          /* Move value out of tmp_buf as this will be reused on next loop */
 
1735
          my_decimal2decimal(tmp, dec);
 
1736
          res= dec;
 
1737
        }
 
1738
        else
 
1739
          res= tmp;
 
1740
      }
 
1741
    }
 
1742
    if ((null_value= args[i]->null_value))
 
1743
    {
 
1744
      res= 0;
 
1745
      break;
 
1746
    }
 
1747
  }
 
1748
  return res;
 
1749
}
 
1750
 
 
1751
 
 
1752
int64_t Item_func_length::val_int()
 
1753
{
 
1754
  assert(fixed == 1);
 
1755
  String *res=args[0]->val_str(&value);
 
1756
  if (!res)
 
1757
  {
 
1758
    null_value=1;
 
1759
    return 0; /* purecov: inspected */
 
1760
  }
 
1761
  null_value=0;
 
1762
  return (int64_t) res->length();
 
1763
}
 
1764
 
 
1765
 
 
1766
int64_t Item_func_char_length::val_int()
 
1767
{
 
1768
  assert(fixed == 1);
 
1769
  String *res=args[0]->val_str(&value);
 
1770
  if (!res)
 
1771
  {
 
1772
    null_value=1;
 
1773
    return 0; /* purecov: inspected */
 
1774
  }
 
1775
  null_value=0;
 
1776
  return (int64_t) res->numchars();
 
1777
}
 
1778
 
 
1779
 
 
1780
int64_t Item_func_coercibility::val_int()
 
1781
{
 
1782
  assert(fixed == 1);
 
1783
  null_value= 0;
 
1784
  return (int64_t) args[0]->collation.derivation;
 
1785
}
 
1786
 
 
1787
 
 
1788
void Item_func_locate::fix_length_and_dec()
 
1789
{
 
1790
  max_length= MY_INT32_NUM_DECIMAL_DIGITS;
 
1791
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
 
1792
}
 
1793
 
 
1794
 
 
1795
int64_t Item_func_locate::val_int()
 
1796
{
 
1797
  assert(fixed == 1);
 
1798
  String *a=args[0]->val_str(&value1);
 
1799
  String *b=args[1]->val_str(&value2);
 
1800
  if (!a || !b)
 
1801
  {
 
1802
    null_value=1;
 
1803
    return 0; /* purecov: inspected */
 
1804
  }
 
1805
  null_value=0;
 
1806
  /* must be int64_t to avoid truncation */
 
1807
  int64_t start=  0; 
 
1808
  int64_t start0= 0;
 
1809
  my_match_t match;
 
1810
 
 
1811
  if (arg_count == 3)
 
1812
  {
 
1813
    start0= start= args[2]->val_int() - 1;
 
1814
 
 
1815
    if ((start < 0) || (start > a->length()))
 
1816
      return 0;
 
1817
 
 
1818
    /* start is now sufficiently valid to pass to charpos function */
 
1819
    start= a->charpos((int) start);
 
1820
 
 
1821
    if (start + b->length() > a->length())
 
1822
      return 0;
 
1823
  }
 
1824
 
 
1825
  if (!b->length())                             // Found empty string at start
 
1826
    return start + 1;
 
1827
  
 
1828
  if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
 
1829
                                            a->ptr()+start,
 
1830
                                            (uint) (a->length()-start),
 
1831
                                            b->ptr(), b->length(),
 
1832
                                            &match, 1))
 
1833
    return 0;
 
1834
  return (int64_t) match.mb_len + start0 + 1;
 
1835
}
 
1836
 
 
1837
 
 
1838
void Item_func_locate::print(String *str, enum_query_type query_type)
 
1839
{
 
1840
  str->append(STRING_WITH_LEN("locate("));
 
1841
  args[1]->print(str, query_type);
 
1842
  str->append(',');
 
1843
  args[0]->print(str, query_type);
 
1844
  if (arg_count == 3)
 
1845
  {
 
1846
    str->append(',');
 
1847
    args[2]->print(str, query_type);
 
1848
  }
 
1849
  str->append(')');
 
1850
}
 
1851
 
 
1852
 
 
1853
int64_t Item_func_field::val_int()
 
1854
{
 
1855
  assert(fixed == 1);
 
1856
 
 
1857
  if (cmp_type == STRING_RESULT)
 
1858
  {
 
1859
    String *field;
 
1860
    if (!(field= args[0]->val_str(&value)))
 
1861
      return 0;
 
1862
    for (uint32_t i=1 ; i < arg_count ; i++)
 
1863
    {
 
1864
      String *tmp_value=args[i]->val_str(&tmp);
 
1865
      if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
 
1866
        return (int64_t) (i);
 
1867
    }
 
1868
  }
 
1869
  else if (cmp_type == INT_RESULT)
 
1870
  {
 
1871
    int64_t val= args[0]->val_int();
 
1872
    if (args[0]->null_value)
 
1873
      return 0;
 
1874
    for (uint32_t i=1; i < arg_count ; i++)
 
1875
    {
 
1876
      if (val == args[i]->val_int() && !args[i]->null_value)
 
1877
        return (int64_t) (i);
 
1878
    }
 
1879
  }
 
1880
  else if (cmp_type == DECIMAL_RESULT)
 
1881
  {
 
1882
    my_decimal dec_arg_buf, *dec_arg,
 
1883
               dec_buf, *dec= args[0]->val_decimal(&dec_buf);
 
1884
    if (args[0]->null_value)
 
1885
      return 0;
 
1886
    for (uint32_t i=1; i < arg_count; i++)
 
1887
    {
 
1888
      dec_arg= args[i]->val_decimal(&dec_arg_buf);
 
1889
      if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
 
1890
        return (int64_t) (i);
 
1891
    }
 
1892
  }
 
1893
  else
 
1894
  {
 
1895
    double val= args[0]->val_real();
 
1896
    if (args[0]->null_value)
 
1897
      return 0;
 
1898
    for (uint32_t i=1; i < arg_count ; i++)
 
1899
    {
 
1900
      if (val == args[i]->val_real() && !args[i]->null_value)
 
1901
        return (int64_t) (i);
 
1902
    }
 
1903
  }
 
1904
  return 0;
 
1905
}
 
1906
 
 
1907
 
 
1908
void Item_func_field::fix_length_and_dec()
 
1909
{
 
1910
  maybe_null=0; max_length=3;
 
1911
  cmp_type= args[0]->result_type();
 
1912
  for (uint32_t i=1; i < arg_count ; i++)
 
1913
    cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
 
1914
  if (cmp_type == STRING_RESULT)
 
1915
    agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1);
 
1916
}
 
1917
 
 
1918
 
 
1919
int64_t Item_func_ascii::val_int()
 
1920
{
 
1921
  assert(fixed == 1);
 
1922
  String *res=args[0]->val_str(&value);
 
1923
  if (!res)
 
1924
  {
 
1925
    null_value=1;
 
1926
    return 0;
 
1927
  }
 
1928
  null_value=0;
 
1929
  return (int64_t) (res->length() ? (unsigned char) (*res)[0] : (unsigned char) 0);
 
1930
}
 
1931
 
 
1932
int64_t Item_func_ord::val_int()
 
1933
{
 
1934
  assert(fixed == 1);
 
1935
  String *res=args[0]->val_str(&value);
 
1936
  if (!res)
 
1937
  {
 
1938
    null_value=1;
 
1939
    return 0;
 
1940
  }
 
1941
  null_value=0;
 
1942
  if (!res->length()) return 0;
 
1943
#ifdef USE_MB
 
1944
  if (use_mb(res->charset()))
 
1945
  {
 
1946
    register const char *str=res->ptr();
 
1947
    register uint32_t n=0, l=my_ismbchar(res->charset(),str,str+res->length());
 
1948
    if (!l)
 
1949
      return (int64_t)((unsigned char) *str);
 
1950
    while (l--)
 
1951
      n=(n<<8)|(uint32_t)((unsigned char) *str++);
 
1952
    return (int64_t) n;
 
1953
  }
 
1954
#endif
 
1955
  return (int64_t) ((unsigned char) (*res)[0]);
 
1956
}
 
1957
 
 
1958
        /* Search after a string in a string of strings separated by ',' */
 
1959
        /* Returns number of found type >= 1 or 0 if not found */
 
1960
        /* This optimizes searching in enums to bit testing! */
 
1961
 
 
1962
void Item_func_find_in_set::fix_length_and_dec()
 
1963
{
 
1964
  decimals=0;
 
1965
  max_length=3;                                 // 1-999
 
1966
  agg_arg_charsets(cmp_collation, args, 2, MY_COLL_CMP_CONV, 1);
 
1967
}
 
1968
 
 
1969
static const char separator=',';
 
1970
 
 
1971
int64_t Item_func_find_in_set::val_int()
 
1972
{
 
1973
  assert(fixed == 1);
 
1974
  if (enum_value)
 
1975
  {
 
1976
    uint64_t tmp=(uint64_t) args[1]->val_int();
 
1977
    if (!(null_value=args[1]->null_value || args[0]->null_value))
 
1978
    {
 
1979
      if (tmp & enum_bit)
 
1980
        return enum_value;
 
1981
    }
 
1982
    return 0L;
 
1983
  }
 
1984
 
 
1985
  String *find=args[0]->val_str(&value);
 
1986
  String *buffer=args[1]->val_str(&value2);
 
1987
  if (!find || !buffer)
 
1988
  {
 
1989
    null_value=1;
 
1990
    return 0; /* purecov: inspected */
 
1991
  }
 
1992
  null_value=0;
 
1993
 
 
1994
  int diff;
 
1995
  if ((diff=buffer->length() - find->length()) >= 0)
 
1996
  {
 
1997
    my_wc_t wc;
 
1998
    const CHARSET_INFO * const cs= cmp_collation.collation;
 
1999
    const char *str_begin= buffer->ptr();
 
2000
    const char *str_end= buffer->ptr();
 
2001
    const char *real_end= str_end+buffer->length();
 
2002
    const unsigned char *find_str= (const unsigned char *) find->ptr();
 
2003
    uint32_t find_str_len= find->length();
 
2004
    int position= 0;
 
2005
    while (1)
 
2006
    {
 
2007
      int symbol_len;
 
2008
      if ((symbol_len= cs->cset->mb_wc(cs, &wc, (unsigned char*) str_end, 
 
2009
                                       (unsigned char*) real_end)) > 0)
 
2010
      {
 
2011
        const char *substr_end= str_end + symbol_len;
 
2012
        bool is_last_item= (substr_end == real_end);
 
2013
        bool is_separator= (wc == (my_wc_t) separator);
 
2014
        if (is_separator || is_last_item)
 
2015
        {
 
2016
          position++;
 
2017
          if (is_last_item && !is_separator)
 
2018
            str_end= substr_end;
 
2019
          if (!my_strnncoll(cs, (const unsigned char *) str_begin,
 
2020
                            str_end - str_begin,
 
2021
                            find_str, find_str_len))
 
2022
            return (int64_t) position;
 
2023
          else
 
2024
            str_begin= substr_end;
 
2025
        }
 
2026
        str_end= substr_end;
 
2027
      }
 
2028
      else if (str_end - str_begin == 0 &&
 
2029
               find_str_len == 0 &&
 
2030
               wc == (my_wc_t) separator)
 
2031
        return (int64_t) ++position;
 
2032
      else
 
2033
        return 0L;
 
2034
    }
 
2035
  }
 
2036
  return 0;
 
2037
}
 
2038
 
 
2039
int64_t Item_func_bit_count::val_int()
 
2040
{
 
2041
  assert(fixed == 1);
 
2042
  uint64_t value= (uint64_t) args[0]->val_int();
 
2043
  if ((null_value= args[0]->null_value))
 
2044
    return 0; /* purecov: inspected */
 
2045
  return (int64_t) my_count_bits(value);
 
2046
}
 
2047
 
 
2048
/*
 
2049
** User level locks
 
2050
*/
 
2051
 
 
2052
pthread_mutex_t LOCK_user_locks;
 
2053
static HASH hash_user_locks;
 
2054
 
 
2055
class User_level_lock
 
2056
{
 
2057
  unsigned char *key;
 
2058
  size_t key_length;
 
2059
 
 
2060
public:
 
2061
  int count;
 
2062
  bool locked;
 
2063
  pthread_cond_t cond;
 
2064
  my_thread_id thread_id;
 
2065
  void set_thread(THD *thd) { thread_id= thd->thread_id; }
 
2066
 
 
2067
  User_level_lock(const unsigned char *key_arg,uint32_t length, ulong id) 
 
2068
    :key_length(length),count(1),locked(1), thread_id(id)
 
2069
  {
 
2070
    key= (unsigned char*) my_memdup(key_arg,length,MYF(0));
 
2071
    pthread_cond_init(&cond,NULL);
 
2072
    if (key)
 
2073
    {
 
2074
      if (my_hash_insert(&hash_user_locks,(unsigned char*) this))
 
2075
      {
 
2076
        free(key);
 
2077
        key=0;
 
2078
      }
 
2079
    }
 
2080
  }
 
2081
  ~User_level_lock()
 
2082
  {
 
2083
    if (key)
 
2084
    {
 
2085
      hash_delete(&hash_user_locks,(unsigned char*) this);
 
2086
      free(key);
 
2087
    }
 
2088
    pthread_cond_destroy(&cond);
 
2089
  }
 
2090
  inline bool initialized() { return key != 0; }
 
2091
  friend void item_user_lock_release(User_level_lock *ull);
 
2092
  friend unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
 
2093
                            bool not_used);
 
2094
};
 
2095
 
 
2096
unsigned char *ull_get_key(const User_level_lock *ull, size_t *length,
 
2097
                   bool not_used __attribute__((unused)))
 
2098
{
 
2099
  *length= ull->key_length;
 
2100
  return ull->key;
 
2101
}
 
2102
 
 
2103
 
 
2104
static bool item_user_lock_inited= 0;
 
2105
 
 
2106
void item_user_lock_init(void)
 
2107
{
 
2108
  pthread_mutex_init(&LOCK_user_locks,MY_MUTEX_INIT_SLOW);
 
2109
  hash_init(&hash_user_locks, system_charset_info,
 
2110
            16,0,0,(hash_get_key) ull_get_key,NULL,0);
 
2111
  item_user_lock_inited= 1;
 
2112
}
 
2113
 
 
2114
void item_user_lock_free(void)
 
2115
{
 
2116
  if (item_user_lock_inited)
 
2117
  {
 
2118
    item_user_lock_inited= 0;
 
2119
    hash_free(&hash_user_locks);
 
2120
    pthread_mutex_destroy(&LOCK_user_locks);
 
2121
  }
 
2122
}
 
2123
 
 
2124
void item_user_lock_release(User_level_lock *ull)
 
2125
{
 
2126
  ull->locked=0;
 
2127
  ull->thread_id= 0;
 
2128
  if (--ull->count)
 
2129
    pthread_cond_signal(&ull->cond);
 
2130
  else
 
2131
    delete ull;
 
2132
}
 
2133
 
 
2134
/**
 
2135
  Wait until we are at or past the given position in the master binlog
 
2136
  on the slave.
 
2137
*/
 
2138
 
 
2139
int64_t Item_master_pos_wait::val_int()
 
2140
{
 
2141
  assert(fixed == 1);
 
2142
  THD* thd = current_thd;
 
2143
  String *log_name = args[0]->val_str(&value);
 
2144
  int event_count= 0;
 
2145
 
 
2146
  null_value=0;
 
2147
  if (thd->slave_thread || !log_name || !log_name->length())
 
2148
  {
 
2149
    null_value = 1;
 
2150
    return 0;
 
2151
  }
 
2152
  int64_t pos = (ulong)args[1]->val_int();
 
2153
  int64_t timeout = (arg_count==3) ? args[2]->val_int() : 0 ;
 
2154
  if ((event_count = active_mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
 
2155
  {
 
2156
    null_value = 1;
 
2157
    event_count=0;
 
2158
  }
 
2159
  return event_count;
 
2160
}
 
2161
 
 
2162
#ifdef EXTRA_DEBUG
 
2163
void debug_sync_point(const char* lock_name, uint32_t lock_timeout)
 
2164
{
 
2165
}
 
2166
 
 
2167
#endif
 
2168
 
 
2169
 
 
2170
int64_t Item_func_last_insert_id::val_int()
 
2171
{
 
2172
  THD *thd= current_thd;
 
2173
  assert(fixed == 1);
 
2174
  if (arg_count)
 
2175
  {
 
2176
    int64_t value= args[0]->val_int();
 
2177
    null_value= args[0]->null_value;
 
2178
    /*
 
2179
      LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
 
2180
      documented in the manual. We don't want to touch
 
2181
      first_successful_insert_id_in_cur_stmt because it would make
 
2182
      LAST_INSERT_ID(X) take precedence over an generated auto_increment
 
2183
      value for this row.
 
2184
    */
 
2185
    thd->arg_of_last_insert_id_function= true;
 
2186
    thd->first_successful_insert_id_in_prev_stmt= value;
 
2187
    return value;
 
2188
  }
 
2189
  return thd->read_first_successful_insert_id_in_prev_stmt();
 
2190
}
 
2191
 
 
2192
 
 
2193
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
 
2194
{
 
2195
  return Item_int_func::fix_fields(thd, ref);
 
2196
}
 
2197
 
 
2198
 
 
2199
/* This function is just used to test speed of different functions */
 
2200
 
 
2201
int64_t Item_func_benchmark::val_int()
 
2202
{
 
2203
  assert(fixed == 1);
 
2204
  char buff[MAX_FIELD_WIDTH];
 
2205
  String tmp(buff,sizeof(buff), &my_charset_bin);
 
2206
  my_decimal tmp_decimal;
 
2207
  THD *thd=current_thd;
 
2208
  uint64_t loop_count;
 
2209
 
 
2210
  loop_count= (uint64_t) args[0]->val_int();
 
2211
 
 
2212
  if (args[0]->null_value ||
 
2213
      (!args[0]->unsigned_flag && (((int64_t) loop_count) < 0)))
 
2214
  {
 
2215
    if (!args[0]->null_value)
 
2216
    {
 
2217
      char buff[22];
 
2218
      llstr(((int64_t) loop_count), buff);
 
2219
      push_warning_printf(current_thd, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
 
2220
                          ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
 
2221
                          "count", buff, "benchmark");
 
2222
    }
 
2223
 
 
2224
    null_value= 1;
 
2225
    return 0;
 
2226
  }
 
2227
 
 
2228
  null_value=0;
 
2229
  for (uint64_t loop=0 ; loop < loop_count && !thd->killed; loop++)
 
2230
  {
 
2231
    switch (args[1]->result_type()) {
 
2232
    case REAL_RESULT:
 
2233
      (void) args[1]->val_real();
 
2234
      break;
 
2235
    case INT_RESULT:
 
2236
      (void) args[1]->val_int();
 
2237
      break;
 
2238
    case STRING_RESULT:
 
2239
      (void) args[1]->val_str(&tmp);
 
2240
      break;
 
2241
    case DECIMAL_RESULT:
 
2242
      (void) args[1]->val_decimal(&tmp_decimal);
 
2243
      break;
 
2244
    case ROW_RESULT:
 
2245
    default:
 
2246
      // This case should never be chosen
 
2247
      assert(0);
 
2248
      return 0;
 
2249
    }
 
2250
  }
 
2251
  return 0;
 
2252
}
 
2253
 
 
2254
 
 
2255
void Item_func_benchmark::print(String *str, enum_query_type query_type)
 
2256
{
 
2257
  str->append(STRING_WITH_LEN("benchmark("));
 
2258
  args[0]->print(str, query_type);
 
2259
  str->append(',');
 
2260
  args[1]->print(str, query_type);
 
2261
  str->append(')');
 
2262
}
 
2263
 
 
2264
#define extra_size sizeof(double)
 
2265
 
 
2266
static user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
 
2267
                                    bool create_if_not_exists)
 
2268
{
 
2269
  user_var_entry *entry;
 
2270
 
 
2271
  if (!(entry = (user_var_entry*) hash_search(hash, (unsigned char*) name.str,
 
2272
                                              name.length)) &&
 
2273
      create_if_not_exists)
 
2274
  {
 
2275
    uint32_t size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
 
2276
    if (!hash_inited(hash))
 
2277
      return 0;
 
2278
    if (!(entry = (user_var_entry*) my_malloc(size,MYF(MY_WME | ME_FATALERROR))))
 
2279
      return 0;
 
2280
    entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
 
2281
      extra_size;
 
2282
    entry->name.length=name.length;
 
2283
    entry->value=0;
 
2284
    entry->length=0;
 
2285
    entry->update_query_id=0;
 
2286
    entry->collation.set(NULL, DERIVATION_IMPLICIT, 0);
 
2287
    entry->unsigned_flag= 0;
 
2288
    /*
 
2289
      If we are here, we were called from a SET or a query which sets a
 
2290
      variable. Imagine it is this:
 
2291
      INSERT INTO t SELECT @a:=10, @a:=@a+1.
 
2292
      Then when we have a Item_func_get_user_var (because of the @a+1) so we
 
2293
      think we have to write the value of @a to the binlog. But before that,
 
2294
      we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
 
2295
      the variable as "already logged" (line below) so that it won't be logged
 
2296
      by Item_func_get_user_var (because that's not necessary).
 
2297
    */
 
2298
    entry->used_query_id=current_thd->query_id;
 
2299
    entry->type=STRING_RESULT;
 
2300
    memcpy(entry->name.str, name.str, name.length+1);
 
2301
    if (my_hash_insert(hash,(unsigned char*) entry))
 
2302
    {
 
2303
      free((char*) entry);
 
2304
      return 0;
 
2305
    }
 
2306
  }
 
2307
  return entry;
 
2308
}
 
2309
 
 
2310
/*
 
2311
  When a user variable is updated (in a SET command or a query like
 
2312
  SELECT @a:= ).
 
2313
*/
 
2314
 
 
2315
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
 
2316
{
 
2317
  assert(fixed == 0);
 
2318
  /* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
 
2319
  if (Item_func::fix_fields(thd, ref) ||
 
2320
      !(entry= get_variable(&thd->user_vars, name, 1)))
 
2321
    return true;
 
2322
  /* 
 
2323
     Remember the last query which updated it, this way a query can later know
 
2324
     if this variable is a constant item in the query (it is if update_query_id
 
2325
     is different from query_id).
 
2326
  */
 
2327
  entry->update_query_id= thd->query_id;
 
2328
  /*
 
2329
    As it is wrong and confusing to associate any 
 
2330
    character set with NULL, @a should be latin2
 
2331
    after this query sequence:
 
2332
 
 
2333
      SET @a=_latin2'string';
 
2334
      SET @a=NULL;
 
2335
 
 
2336
    I.e. the second query should not change the charset
 
2337
    to the current default value, but should keep the 
 
2338
    original value assigned during the first query.
 
2339
    In order to do it, we don't copy charset
 
2340
    from the argument if the argument is NULL
 
2341
    and the variable has previously been initialized.
 
2342
  */
 
2343
  null_item= (args[0]->type() == NULL_ITEM);
 
2344
  if (!entry->collation.collation || !null_item)
 
2345
    entry->collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
 
2346
  collation.set(entry->collation.collation, DERIVATION_IMPLICIT);
 
2347
  cached_result_type= args[0]->result_type();
 
2348
  return false;
 
2349
}
 
2350
 
 
2351
 
 
2352
void
 
2353
Item_func_set_user_var::fix_length_and_dec()
 
2354
{
 
2355
  maybe_null=args[0]->maybe_null;
 
2356
  max_length=args[0]->max_length;
 
2357
  decimals=args[0]->decimals;
 
2358
  collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
 
2359
}
 
2360
 
 
2361
 
 
2362
/*
 
2363
  Mark field in read_map
 
2364
 
 
2365
  NOTES
 
2366
    This is used by filesort to register used fields in a a temporary
 
2367
    column read set or to register used fields in a view
 
2368
*/
 
2369
 
 
2370
bool Item_func_set_user_var::register_field_in_read_map(unsigned char *arg)
 
2371
{
 
2372
  if (result_field)
 
2373
  {
 
2374
    Table *table= (Table *) arg;
 
2375
    if (result_field->table == table || !table)
 
2376
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
 
2377
  }
 
2378
  return 0;
 
2379
}
 
2380
 
 
2381
 
 
2382
/**
 
2383
  Set value to user variable.
 
2384
 
 
2385
  @param entry          pointer to structure representing variable
 
2386
  @param set_null       should we set NULL value ?
 
2387
  @param ptr            pointer to buffer with new value
 
2388
  @param length         length of new value
 
2389
  @param type           type of new value
 
2390
  @param cs             charset info for new value
 
2391
  @param dv             derivation for new value
 
2392
  @param unsigned_arg   indiates if a value of type INT_RESULT is unsigned
 
2393
 
 
2394
  @note Sets error and fatal error if allocation fails.
 
2395
 
 
2396
  @retval
 
2397
    false   success
 
2398
  @retval
 
2399
    true    failure
 
2400
*/
 
2401
 
 
2402
static bool
 
2403
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint32_t length,
 
2404
            Item_result type, const CHARSET_INFO * const cs, Derivation dv,
 
2405
            bool unsigned_arg)
 
2406
{
 
2407
  if (set_null)
 
2408
  {
 
2409
    char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
2410
    if (entry->value && entry->value != pos)
 
2411
      free(entry->value);
 
2412
    entry->value= 0;
 
2413
    entry->length= 0;
 
2414
  }
 
2415
  else
 
2416
  {
 
2417
    if (type == STRING_RESULT)
 
2418
      length++;                                 // Store strings with end \0
 
2419
    if (length <= extra_size)
 
2420
    {
 
2421
      /* Save value in value struct */
 
2422
      char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
2423
      if (entry->value != pos)
 
2424
      {
 
2425
        if (entry->value)
 
2426
          free(entry->value);
 
2427
        entry->value=pos;
 
2428
      }
 
2429
    }
 
2430
    else
 
2431
    {
 
2432
      /* Allocate variable */
 
2433
      if (entry->length != length)
 
2434
      {
 
2435
        char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
 
2436
        if (entry->value == pos)
 
2437
          entry->value=0;
 
2438
        entry->value= (char*) my_realloc(entry->value, length,
 
2439
                                         MYF(MY_ALLOW_ZERO_PTR | MY_WME |
 
2440
                                             ME_FATALERROR));
 
2441
        if (!entry->value)
 
2442
          return 1;
 
2443
      }
 
2444
    }
 
2445
    if (type == STRING_RESULT)
 
2446
    {
 
2447
      length--;                                 // Fix length change above
 
2448
      entry->value[length]= 0;                  // Store end \0
 
2449
    }
 
2450
    memcpy(entry->value,ptr,length);
 
2451
    if (type == DECIMAL_RESULT)
 
2452
      ((my_decimal*)entry->value)->fix_buffer_pointer();
 
2453
    entry->length= length;
 
2454
    entry->collation.set(cs, dv);
 
2455
    entry->unsigned_flag= unsigned_arg;
 
2456
  }
 
2457
  entry->type=type;
 
2458
  return 0;
 
2459
}
 
2460
 
 
2461
 
 
2462
bool
 
2463
Item_func_set_user_var::update_hash(void *ptr, uint32_t length,
 
2464
                                    Item_result res_type,
 
2465
                                    const CHARSET_INFO * const cs, Derivation dv,
 
2466
                                    bool unsigned_arg)
 
2467
{
 
2468
  /*
 
2469
    If we set a variable explicitely to NULL then keep the old
 
2470
    result type of the variable
 
2471
  */
 
2472
  if ((null_value= args[0]->null_value) && null_item)
 
2473
    res_type= entry->type;                      // Don't change type of item
 
2474
  if (::update_hash(entry, (null_value= args[0]->null_value),
 
2475
                    ptr, length, res_type, cs, dv, unsigned_arg))
 
2476
  {
 
2477
    null_value= 1;
 
2478
    return 1;
 
2479
  }
 
2480
  return 0;
 
2481
}
 
2482
 
 
2483
 
 
2484
/** Get the value of a variable as a double. */
 
2485
 
 
2486
double user_var_entry::val_real(bool *null_value)
 
2487
{
 
2488
  if ((*null_value= (value == 0)))
 
2489
    return 0.0;
 
2490
 
 
2491
  switch (type) {
 
2492
  case REAL_RESULT:
 
2493
    return *(double*) value;
 
2494
  case INT_RESULT:
 
2495
    return (double) *(int64_t*) value;
 
2496
  case DECIMAL_RESULT:
 
2497
  {
 
2498
    double result;
 
2499
    my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
 
2500
    return result;
 
2501
  }
 
2502
  case STRING_RESULT:
 
2503
    return my_atof(value);                      // This is null terminated
 
2504
  case ROW_RESULT:
 
2505
    assert(1);                          // Impossible
 
2506
    break;
 
2507
  }
 
2508
  return 0.0;                                   // Impossible
 
2509
}
 
2510
 
 
2511
 
 
2512
/** Get the value of a variable as an integer. */
 
2513
 
 
2514
int64_t user_var_entry::val_int(bool *null_value) const
 
2515
{
 
2516
  if ((*null_value= (value == 0)))
 
2517
    return 0L;
 
2518
 
 
2519
  switch (type) {
 
2520
  case REAL_RESULT:
 
2521
    return (int64_t) *(double*) value;
 
2522
  case INT_RESULT:
 
2523
    return *(int64_t*) value;
 
2524
  case DECIMAL_RESULT:
 
2525
  {
 
2526
    int64_t result;
 
2527
    my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
 
2528
    return result;
 
2529
  }
 
2530
  case STRING_RESULT:
 
2531
  {
 
2532
    int error;
 
2533
    return my_strtoll10(value, (char**) 0, &error);// String is null terminated
 
2534
  }
 
2535
  case ROW_RESULT:
 
2536
    assert(1);                          // Impossible
 
2537
    break;
 
2538
  }
 
2539
  return 0L;                                    // Impossible
 
2540
}
 
2541
 
 
2542
 
 
2543
/** Get the value of a variable as a string. */
 
2544
 
 
2545
String *user_var_entry::val_str(bool *null_value, String *str,
 
2546
                                uint32_t decimals)
 
2547
{
 
2548
  if ((*null_value= (value == 0)))
 
2549
    return (String*) 0;
 
2550
 
 
2551
  switch (type) {
 
2552
  case REAL_RESULT:
 
2553
    str->set_real(*(double*) value, decimals, &my_charset_bin);
 
2554
    break;
 
2555
  case INT_RESULT:
 
2556
    if (!unsigned_flag)
 
2557
      str->set(*(int64_t*) value, &my_charset_bin);
 
2558
    else
 
2559
      str->set(*(uint64_t*) value, &my_charset_bin);
 
2560
    break;
 
2561
  case DECIMAL_RESULT:
 
2562
    my_decimal2string(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, 0, 0, str);
 
2563
    break;
 
2564
  case STRING_RESULT:
 
2565
    if (str->copy(value, length, collation.collation))
 
2566
      str= 0;                                   // EOM error
 
2567
  case ROW_RESULT:
 
2568
    assert(1);                          // Impossible
 
2569
    break;
 
2570
  }
 
2571
  return(str);
 
2572
}
 
2573
 
 
2574
/** Get the value of a variable as a decimal. */
 
2575
 
 
2576
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
 
2577
{
 
2578
  if ((*null_value= (value == 0)))
 
2579
    return 0;
 
2580
 
 
2581
  switch (type) {
 
2582
  case REAL_RESULT:
 
2583
    double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
 
2584
    break;
 
2585
  case INT_RESULT:
 
2586
    int2my_decimal(E_DEC_FATAL_ERROR, *(int64_t*) value, 0, val);
 
2587
    break;
 
2588
  case DECIMAL_RESULT:
 
2589
    val= (my_decimal *)value;
 
2590
    break;
 
2591
  case STRING_RESULT:
 
2592
    str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
 
2593
    break;
 
2594
  case ROW_RESULT:
 
2595
    assert(1);                          // Impossible
 
2596
    break;
 
2597
  }
 
2598
  return(val);
 
2599
}
 
2600
 
 
2601
/**
 
2602
  This functions is invoked on SET \@variable or
 
2603
  \@variable:= expression.
 
2604
 
 
2605
  Evaluate (and check expression), store results.
 
2606
 
 
2607
  @note
 
2608
    For now it always return OK. All problem with value evaluating
 
2609
    will be caught by thd->is_error() check in sql_set_variables().
 
2610
 
 
2611
  @retval
 
2612
    false OK.
 
2613
*/
 
2614
 
 
2615
bool
 
2616
Item_func_set_user_var::check(bool use_result_field)
 
2617
{
 
2618
  if (use_result_field && !result_field)
 
2619
    use_result_field= false;
 
2620
 
 
2621
  switch (cached_result_type) {
 
2622
  case REAL_RESULT:
 
2623
  {
 
2624
    save_result.vreal= use_result_field ? result_field->val_real() :
 
2625
                        args[0]->val_real();
 
2626
    break;
 
2627
  }
 
2628
  case INT_RESULT:
 
2629
  {
 
2630
    save_result.vint= use_result_field ? result_field->val_int() :
 
2631
                       args[0]->val_int();
 
2632
    unsigned_flag= use_result_field ? ((Field_num*)result_field)->unsigned_flag:
 
2633
                    args[0]->unsigned_flag;
 
2634
    break;
 
2635
  }
 
2636
  case STRING_RESULT:
 
2637
  {
 
2638
    save_result.vstr= use_result_field ? result_field->val_str(&value) :
 
2639
                       args[0]->val_str(&value);
 
2640
    break;
 
2641
  }
 
2642
  case DECIMAL_RESULT:
 
2643
  {
 
2644
    save_result.vdec= use_result_field ?
 
2645
                       result_field->val_decimal(&decimal_buff) :
 
2646
                       args[0]->val_decimal(&decimal_buff);
 
2647
    break;
 
2648
  }
 
2649
  case ROW_RESULT:
 
2650
  default:
 
2651
    // This case should never be chosen
 
2652
    assert(0);
 
2653
    break;
 
2654
  }
 
2655
  return(false);
 
2656
}
 
2657
 
 
2658
 
 
2659
/**
 
2660
  This functions is invoked on
 
2661
  SET \@variable or \@variable:= expression.
 
2662
 
 
2663
  @note
 
2664
    We have to store the expression as such in the variable, independent of
 
2665
    the value method used by the user
 
2666
 
 
2667
  @retval
 
2668
    0   OK
 
2669
  @retval
 
2670
    1   EOM Error
 
2671
 
 
2672
*/
 
2673
 
 
2674
bool
 
2675
Item_func_set_user_var::update()
 
2676
{
 
2677
  bool res= false;
 
2678
 
 
2679
  switch (cached_result_type) {
 
2680
  case REAL_RESULT:
 
2681
  {
 
2682
    res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
 
2683
                     REAL_RESULT, &my_charset_bin, DERIVATION_IMPLICIT, 0);
 
2684
    break;
 
2685
  }
 
2686
  case INT_RESULT:
 
2687
  {
 
2688
    res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
 
2689
                     INT_RESULT, &my_charset_bin, DERIVATION_IMPLICIT,
 
2690
                     unsigned_flag);
 
2691
    break;
 
2692
  }
 
2693
  case STRING_RESULT:
 
2694
  {
 
2695
    if (!save_result.vstr)                                      // Null value
 
2696
      res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin,
 
2697
                       DERIVATION_IMPLICIT, 0);
 
2698
    else
 
2699
      res= update_hash((void*) save_result.vstr->ptr(),
 
2700
                       save_result.vstr->length(), STRING_RESULT,
 
2701
                       save_result.vstr->charset(),
 
2702
                       DERIVATION_IMPLICIT, 0);
 
2703
    break;
 
2704
  }
 
2705
  case DECIMAL_RESULT:
 
2706
  {
 
2707
    if (!save_result.vdec)                                      // Null value
 
2708
      res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin,
 
2709
                       DERIVATION_IMPLICIT, 0);
 
2710
    else
 
2711
      res= update_hash((void*) save_result.vdec,
 
2712
                       sizeof(my_decimal), DECIMAL_RESULT,
 
2713
                       &my_charset_bin, DERIVATION_IMPLICIT, 0);
 
2714
    break;
 
2715
  }
 
2716
  case ROW_RESULT:
 
2717
  default:
 
2718
    // This case should never be chosen
 
2719
    assert(0);
 
2720
    break;
 
2721
  }
 
2722
  return(res);
 
2723
}
 
2724
 
 
2725
 
 
2726
double Item_func_set_user_var::val_real()
 
2727
{
 
2728
  assert(fixed == 1);
 
2729
  check(0);
 
2730
  update();                                     // Store expression
 
2731
  return entry->val_real(&null_value);
 
2732
}
 
2733
 
 
2734
int64_t Item_func_set_user_var::val_int()
 
2735
{
 
2736
  assert(fixed == 1);
 
2737
  check(0);
 
2738
  update();                                     // Store expression
 
2739
  return entry->val_int(&null_value);
 
2740
}
 
2741
 
 
2742
String *Item_func_set_user_var::val_str(String *str)
 
2743
{
 
2744
  assert(fixed == 1);
 
2745
  check(0);
 
2746
  update();                                     // Store expression
 
2747
  return entry->val_str(&null_value, str, decimals);
 
2748
}
 
2749
 
 
2750
 
 
2751
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
 
2752
{
 
2753
  assert(fixed == 1);
 
2754
  check(0);
 
2755
  update();                                     // Store expression
 
2756
  return entry->val_decimal(&null_value, val);
 
2757
}
 
2758
 
 
2759
 
 
2760
double Item_func_set_user_var::val_result()
 
2761
{
 
2762
  assert(fixed == 1);
 
2763
  check(true);
 
2764
  update();                                     // Store expression
 
2765
  return entry->val_real(&null_value);
 
2766
}
 
2767
 
 
2768
int64_t Item_func_set_user_var::val_int_result()
 
2769
{
 
2770
  assert(fixed == 1);
 
2771
  check(true);
 
2772
  update();                                     // Store expression
 
2773
  return entry->val_int(&null_value);
 
2774
}
 
2775
 
 
2776
String *Item_func_set_user_var::str_result(String *str)
 
2777
{
 
2778
  assert(fixed == 1);
 
2779
  check(true);
 
2780
  update();                                     // Store expression
 
2781
  return entry->val_str(&null_value, str, decimals);
 
2782
}
 
2783
 
 
2784
 
 
2785
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
 
2786
{
 
2787
  assert(fixed == 1);
 
2788
  check(true);
 
2789
  update();                                     // Store expression
 
2790
  return entry->val_decimal(&null_value, val);
 
2791
}
 
2792
 
 
2793
 
 
2794
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
 
2795
{
 
2796
  str->append(STRING_WITH_LEN("(@"));
 
2797
  str->append(name.str, name.length);
 
2798
  str->append(STRING_WITH_LEN(":="));
 
2799
  args[0]->print(str, query_type);
 
2800
  str->append(')');
 
2801
}
 
2802
 
 
2803
 
 
2804
void Item_func_set_user_var::print_as_stmt(String *str,
 
2805
                                           enum_query_type query_type)
 
2806
{
 
2807
  str->append(STRING_WITH_LEN("set @"));
 
2808
  str->append(name.str, name.length);
 
2809
  str->append(STRING_WITH_LEN(":="));
 
2810
  args[0]->print(str, query_type);
 
2811
  str->append(')');
 
2812
}
 
2813
 
 
2814
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
 
2815
{
 
2816
  if (result_field)
 
2817
  {
 
2818
    check(1);
 
2819
    update();
 
2820
    return protocol->store(result_field);
 
2821
  }
 
2822
  return Item::send(protocol, str_arg);
 
2823
}
 
2824
 
 
2825
void Item_func_set_user_var::make_field(Send_field *tmp_field)
 
2826
{
 
2827
  if (result_field)
 
2828
  {
 
2829
    result_field->make_field(tmp_field);
 
2830
    assert(tmp_field->table_name != 0);
 
2831
    if (Item::name)
 
2832
      tmp_field->col_name=Item::name;               // Use user supplied name
 
2833
  }
 
2834
  else
 
2835
    Item::make_field(tmp_field);
 
2836
}
 
2837
 
 
2838
 
 
2839
/*
 
2840
  Save the value of a user variable into a field
 
2841
 
 
2842
  SYNOPSIS
 
2843
    save_in_field()
 
2844
      field           target field to save the value to
 
2845
      no_conversion   flag indicating whether conversions are allowed
 
2846
 
 
2847
  DESCRIPTION
 
2848
    Save the function value into a field and update the user variable
 
2849
    accordingly. If a result field is defined and the target field doesn't
 
2850
    coincide with it then the value from the result field will be used as
 
2851
    the new value of the user variable.
 
2852
 
 
2853
    The reason to have this method rather than simply using the result
 
2854
    field in the val_xxx() methods is that the value from the result field
 
2855
    not always can be used when the result field is defined.
 
2856
    Let's consider the following cases:
 
2857
    1) when filling a tmp table the result field is defined but the value of it
 
2858
    is undefined because it has to be produced yet. Thus we can't use it.
 
2859
    2) on execution of an INSERT ... SELECT statement the save_in_field()
 
2860
    function will be called to fill the data in the new record. If the SELECT
 
2861
    part uses a tmp table then the result field is defined and should be
 
2862
    used in order to get the correct result.
 
2863
 
 
2864
    The difference between the SET_USER_VAR function and regular functions
 
2865
    like CONCAT is that the Item_func objects for the regular functions are
 
2866
    replaced by Item_field objects after the values of these functions have
 
2867
    been stored in a tmp table. Yet an object of the Item_field class cannot
 
2868
    be used to update a user variable.
 
2869
    Due to this we have to handle the result field in a special way here and
 
2870
    in the Item_func_set_user_var::send() function.
 
2871
 
 
2872
  RETURN VALUES
 
2873
    false       Ok
 
2874
    true        Error
 
2875
*/
 
2876
 
 
2877
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
 
2878
                                          bool can_use_result_field)
 
2879
{
 
2880
  bool use_result_field= (!can_use_result_field ? 0 :
 
2881
                          (result_field && result_field != field));
 
2882
  int error;
 
2883
 
 
2884
  /* Update the value of the user variable */
 
2885
  check(use_result_field);
 
2886
  update();
 
2887
 
 
2888
  if (result_type() == STRING_RESULT ||
 
2889
      (result_type() == REAL_RESULT && field->result_type() == STRING_RESULT))
 
2890
  {
 
2891
    String *result;
 
2892
    const CHARSET_INFO * const cs= collation.collation;
 
2893
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
 
2894
    str_value.set_quick(buff, sizeof(buff), cs);
 
2895
    result= entry->val_str(&null_value, &str_value, decimals);
 
2896
 
 
2897
    if (null_value)
 
2898
    {
 
2899
      str_value.set_quick(0, 0, cs);
 
2900
      return set_field_to_null_with_conversions(field, no_conversions);
 
2901
    }
 
2902
 
 
2903
    /* NOTE: If null_value == false, "result" must be not NULL.  */
 
2904
 
 
2905
    field->set_notnull();
 
2906
    error=field->store(result->ptr(),result->length(),cs);
 
2907
    str_value.set_quick(0, 0, cs);
 
2908
  }
 
2909
  else if (result_type() == REAL_RESULT)
 
2910
  {
 
2911
    double nr= entry->val_real(&null_value);
 
2912
    if (null_value)
 
2913
      return set_field_to_null(field);
 
2914
    field->set_notnull();
 
2915
    error=field->store(nr);
 
2916
  }
 
2917
  else if (result_type() == DECIMAL_RESULT)
 
2918
  {
 
2919
    my_decimal decimal_value;
 
2920
    my_decimal *val= entry->val_decimal(&null_value, &decimal_value);
 
2921
    if (null_value)
 
2922
      return set_field_to_null(field);
 
2923
    field->set_notnull();
 
2924
    error=field->store_decimal(val);
 
2925
  }
 
2926
  else
 
2927
  {
 
2928
    int64_t nr= entry->val_int(&null_value);
 
2929
    if (null_value)
 
2930
      return set_field_to_null_with_conversions(field, no_conversions);
 
2931
    field->set_notnull();
 
2932
    error=field->store(nr, unsigned_flag);
 
2933
  }
 
2934
  return error;
 
2935
}
 
2936
 
 
2937
 
 
2938
String *
 
2939
Item_func_get_user_var::val_str(String *str)
 
2940
{
 
2941
  assert(fixed == 1);
 
2942
  if (!var_entry)
 
2943
    return((String*) 0);                        // No such variable
 
2944
  return(var_entry->val_str(&null_value, str, decimals));
 
2945
}
 
2946
 
 
2947
 
 
2948
double Item_func_get_user_var::val_real()
 
2949
{
 
2950
  assert(fixed == 1);
 
2951
  if (!var_entry)
 
2952
    return 0.0;                                 // No such variable
 
2953
  return (var_entry->val_real(&null_value));
 
2954
}
 
2955
 
 
2956
 
 
2957
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
 
2958
{
 
2959
  assert(fixed == 1);
 
2960
  if (!var_entry)
 
2961
    return 0;
 
2962
  return var_entry->val_decimal(&null_value, dec);
 
2963
}
 
2964
 
 
2965
 
 
2966
int64_t Item_func_get_user_var::val_int()
 
2967
{
 
2968
  assert(fixed == 1);
 
2969
  if (!var_entry)
 
2970
    return 0L;                          // No such variable
 
2971
  return (var_entry->val_int(&null_value));
 
2972
}
 
2973
 
 
2974
 
 
2975
/**
 
2976
  Get variable by name and, if necessary, put the record of variable 
 
2977
  use into the binary log.
 
2978
 
 
2979
  When a user variable is invoked from an update query (INSERT, UPDATE etc),
 
2980
  stores this variable and its value in thd->user_var_events, so that it can be
 
2981
  written to the binlog (will be written just before the query is written, see
 
2982
  log.cc).
 
2983
 
 
2984
  @param      thd        Current thread
 
2985
  @param      name       Variable name
 
2986
  @param[out] out_entry  variable structure or NULL. The pointer is set
 
2987
                         regardless of whether function succeeded or not.
 
2988
 
 
2989
  @retval
 
2990
    0  OK
 
2991
  @retval
 
2992
    1  Failed to put appropriate record into binary log
 
2993
 
 
2994
*/
 
2995
 
 
2996
int get_var_with_binlog(THD *thd, enum_sql_command sql_command,
 
2997
                        LEX_STRING &name, user_var_entry **out_entry)
 
2998
{
 
2999
  BINLOG_USER_VAR_EVENT *user_var_event;
 
3000
  user_var_entry *var_entry;
 
3001
  var_entry= get_variable(&thd->user_vars, name, 0);
 
3002
 
 
3003
  /*
 
3004
    Any reference to user-defined variable which is done from stored
 
3005
    function or trigger affects their execution and the execution of the
 
3006
    calling statement. We must log all such variables even if they are 
 
3007
    not involved in table-updating statements.
 
3008
  */
 
3009
  if (!(opt_bin_log && is_update_query(sql_command)))
 
3010
  {
 
3011
    *out_entry= var_entry;
 
3012
    return 0;
 
3013
  }
 
3014
 
 
3015
  if (!var_entry)
 
3016
  {
 
3017
    /*
 
3018
      If the variable does not exist, it's NULL, but we want to create it so
 
3019
      that it gets into the binlog (if it didn't, the slave could be
 
3020
      influenced by a variable of the same name previously set by another
 
3021
      thread).
 
3022
      We create it like if it had been explicitly set with SET before.
 
3023
      The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
 
3024
      sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
 
3025
      in dispatch_command()). Instead of building a one-element list to pass to
 
3026
      sql_set_variables(), we could instead manually call check() and update();
 
3027
      this would save memory and time; but calling sql_set_variables() makes
 
3028
      one unique place to maintain (sql_set_variables()). 
 
3029
 
 
3030
      Manipulation with lex is necessary since free_underlaid_joins
 
3031
      is going to release memory belonging to the main query.
 
3032
    */
 
3033
 
 
3034
    List<set_var_base> tmp_var_list;
 
3035
    LEX *sav_lex= thd->lex, lex_tmp;
 
3036
    thd->lex= &lex_tmp;
 
3037
    lex_start(thd);
 
3038
    tmp_var_list.push_back(new set_var_user(new Item_func_set_user_var(name,
 
3039
                                                                       new Item_null())));
 
3040
    /* Create the variable */
 
3041
    if (sql_set_variables(thd, &tmp_var_list))
 
3042
    {
 
3043
      thd->lex= sav_lex;
 
3044
      goto err;
 
3045
    }
 
3046
    thd->lex= sav_lex;
 
3047
    if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
 
3048
      goto err;
 
3049
  }
 
3050
  else if (var_entry->used_query_id == thd->query_id ||
 
3051
           mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
 
3052
  {
 
3053
    /* 
 
3054
       If this variable was already stored in user_var_events by this query
 
3055
       (because it's used in more than one place in the query), don't store
 
3056
       it.
 
3057
    */
 
3058
    *out_entry= var_entry;
 
3059
    return 0;
 
3060
  }
 
3061
 
 
3062
  uint32_t size;
 
3063
  /*
 
3064
    First we need to store value of var_entry, when the next situation
 
3065
    appears:
 
3066
    > set @a:=1;
 
3067
    > insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
 
3068
    We have to write to binlog value @a= 1.
 
3069
 
 
3070
    We allocate the user_var_event on user_var_events_alloc pool, not on
 
3071
    the this-statement-execution pool because in SPs user_var_event objects 
 
3072
    may need to be valid after current [SP] statement execution pool is
 
3073
    destroyed.
 
3074
  */
 
3075
  size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
 
3076
  if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
 
3077
        alloc_root(thd->user_var_events_alloc, size)))
 
3078
    goto err;
 
3079
 
 
3080
  user_var_event->value= (char*) user_var_event +
 
3081
    ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
 
3082
  user_var_event->user_var_event= var_entry;
 
3083
  user_var_event->type= var_entry->type;
 
3084
  user_var_event->charset_number= var_entry->collation.collation->number;
 
3085
  if (!var_entry->value)
 
3086
  {
 
3087
    /* NULL value*/
 
3088
    user_var_event->length= 0;
 
3089
    user_var_event->value= 0;
 
3090
  }
 
3091
  else
 
3092
  {
 
3093
    user_var_event->length= var_entry->length;
 
3094
    memcpy(user_var_event->value, var_entry->value,
 
3095
           var_entry->length);
 
3096
  }
 
3097
  /* Mark that this variable has been used by this query */
 
3098
  var_entry->used_query_id= thd->query_id;
 
3099
  if (insert_dynamic(&thd->user_var_events, (unsigned char*) &user_var_event))
 
3100
    goto err;
 
3101
 
 
3102
  *out_entry= var_entry;
 
3103
  return 0;
 
3104
 
 
3105
err:
 
3106
  *out_entry= var_entry;
 
3107
  return 1;
 
3108
}
 
3109
 
 
3110
void Item_func_get_user_var::fix_length_and_dec()
 
3111
{
 
3112
  THD *thd=current_thd;
 
3113
  int error;
 
3114
  maybe_null=1;
 
3115
  decimals=NOT_FIXED_DEC;
 
3116
  max_length=MAX_BLOB_WIDTH;
 
3117
 
 
3118
  error= get_var_with_binlog(thd, thd->lex->sql_command, name, &var_entry);
 
3119
 
 
3120
  /*
 
3121
    If the variable didn't exist it has been created as a STRING-type.
 
3122
    'var_entry' is NULL only if there occured an error during the call to
 
3123
    get_var_with_binlog.
 
3124
  */
 
3125
  if (var_entry)
 
3126
  {
 
3127
    m_cached_result_type= var_entry->type;
 
3128
    unsigned_flag= var_entry->unsigned_flag;
 
3129
    max_length= var_entry->length;
 
3130
 
 
3131
    collation.set(var_entry->collation);
 
3132
    switch(m_cached_result_type) {
 
3133
    case REAL_RESULT:
 
3134
      max_length= DBL_DIG + 8;
 
3135
      break;
 
3136
    case INT_RESULT:
 
3137
      max_length= MAX_BIGINT_WIDTH;
 
3138
      decimals=0;
 
3139
      break;
 
3140
    case STRING_RESULT:
 
3141
      max_length= MAX_BLOB_WIDTH;
 
3142
      break;
 
3143
    case DECIMAL_RESULT:
 
3144
      max_length= DECIMAL_MAX_STR_LENGTH;
 
3145
      decimals= DECIMAL_MAX_SCALE;
 
3146
      break;
 
3147
    case ROW_RESULT:                            // Keep compiler happy
 
3148
    default:
 
3149
      assert(0);
 
3150
      break;
 
3151
    }
 
3152
  }
 
3153
  else
 
3154
  {
 
3155
    collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
 
3156
    null_value= 1;
 
3157
    m_cached_result_type= STRING_RESULT;
 
3158
    max_length= MAX_BLOB_WIDTH;
 
3159
  }
 
3160
}
 
3161
 
 
3162
 
 
3163
bool Item_func_get_user_var::const_item() const
 
3164
{
 
3165
  return (!var_entry || current_thd->query_id != var_entry->update_query_id);
 
3166
}
 
3167
 
 
3168
 
 
3169
enum Item_result Item_func_get_user_var::result_type() const
 
3170
{
 
3171
  return m_cached_result_type;
 
3172
}
 
3173
 
 
3174
 
 
3175
void Item_func_get_user_var::print(String *str,
 
3176
                                   enum_query_type query_type __attribute__((unused)))
 
3177
{
 
3178
  str->append(STRING_WITH_LEN("(@"));
 
3179
  str->append(name.str,name.length);
 
3180
  str->append(')');
 
3181
}
 
3182
 
 
3183
 
 
3184
bool Item_func_get_user_var::eq(const Item *item,
 
3185
                                bool binary_cmp __attribute__((unused))) const
 
3186
{
 
3187
  /* Assume we don't have rtti */
 
3188
  if (this == item)
 
3189
    return 1;                                   // Same item is same.
 
3190
  /* Check if other type is also a get_user_var() object */
 
3191
  if (item->type() != FUNC_ITEM ||
 
3192
      ((Item_func*) item)->functype() != functype())
 
3193
    return 0;
 
3194
  Item_func_get_user_var *other=(Item_func_get_user_var*) item;
 
3195
  return (name.length == other->name.length &&
 
3196
          !memcmp(name.str, other->name.str, name.length));
 
3197
}
 
3198
 
 
3199
 
 
3200
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
 
3201
{
 
3202
  assert(fixed == 0);
 
3203
  if (Item::fix_fields(thd, ref) ||
 
3204
      !(entry= get_variable(&thd->user_vars, name, 1)))
 
3205
    return true;
 
3206
  entry->type= STRING_RESULT;
 
3207
  /*
 
3208
    Let us set the same collation which is used for loading
 
3209
    of fields in LOAD DATA INFILE.
 
3210
    (Since Item_user_var_as_out_param is used only there).
 
3211
  */
 
3212
  entry->collation.set(thd->variables.collation_database);
 
3213
  entry->update_query_id= thd->query_id;
 
3214
  return false;
 
3215
}
 
3216
 
 
3217
 
 
3218
void Item_user_var_as_out_param::set_null_value(const CHARSET_INFO * const cs)
 
3219
{
 
3220
  ::update_hash(entry, true, 0, 0, STRING_RESULT, cs,
 
3221
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
 
3222
}
 
3223
 
 
3224
 
 
3225
void Item_user_var_as_out_param::set_value(const char *str, uint32_t length,
 
3226
                                           const CHARSET_INFO * const cs)
 
3227
{
 
3228
  ::update_hash(entry, false, (void*)str, length, STRING_RESULT, cs,
 
3229
                DERIVATION_IMPLICIT, 0 /* unsigned_arg */);
 
3230
}
 
3231
 
 
3232
 
 
3233
double Item_user_var_as_out_param::val_real()
 
3234
{
 
3235
  assert(0);
 
3236
  return 0.0;
 
3237
}
 
3238
 
 
3239
 
 
3240
int64_t Item_user_var_as_out_param::val_int()
 
3241
{
 
3242
  assert(0);
 
3243
  return 0;
 
3244
}
 
3245
 
 
3246
 
 
3247
String* Item_user_var_as_out_param::val_str(String *str __attribute__((unused)))
 
3248
{
 
3249
  assert(0);
 
3250
  return 0;
 
3251
}
 
3252
 
 
3253
 
 
3254
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer __attribute__((unused)))
 
3255
{
 
3256
  assert(0);
 
3257
  return 0;
 
3258
}
 
3259
 
 
3260
 
 
3261
void Item_user_var_as_out_param::print(String *str,
 
3262
                                       enum_query_type query_type __attribute__((unused)))
 
3263
{
 
3264
  str->append('@');
 
3265
  str->append(name.str,name.length);
 
3266
}
 
3267
 
 
3268
 
 
3269
Item_func_get_system_var::
 
3270
Item_func_get_system_var(sys_var *var_arg, enum_var_type var_type_arg,
 
3271
                       LEX_STRING *component_arg, const char *name_arg,
 
3272
                       size_t name_len_arg)
 
3273
  :var(var_arg), var_type(var_type_arg), component(*component_arg)
 
3274
{
 
3275
  /* set_name() will allocate the name */
 
3276
  set_name(name_arg, name_len_arg, system_charset_info);
 
3277
}
 
3278
 
 
3279
 
 
3280
bool
 
3281
Item_func_get_system_var::fix_fields(THD *thd, Item **ref)
 
3282
{
 
3283
  Item *item;
 
3284
 
 
3285
  /*
 
3286
    Evaluate the system variable and substitute the result (a basic constant)
 
3287
    instead of this item. If the variable can not be evaluated,
 
3288
    the error is reported in sys_var::item().
 
3289
  */
 
3290
  if (!(item= var->item(thd, var_type, &component)))
 
3291
    return(1);                             // Impossible
 
3292
  item->set_name(name, 0, system_charset_info); // don't allocate a new name
 
3293
  thd->change_item_tree(ref, item);
 
3294
 
 
3295
  return(0);
 
3296
}
 
3297
 
 
3298
 
 
3299
bool Item_func_get_system_var::is_written_to_binlog()
 
3300
{
 
3301
  return var->is_written_to_binlog(var_type);
 
3302
}
 
3303
 
 
3304
int64_t Item_func_bit_xor::val_int()
 
3305
{
 
3306
  assert(fixed == 1);
 
3307
  uint64_t arg1= (uint64_t) args[0]->val_int();
 
3308
  uint64_t arg2= (uint64_t) args[1]->val_int();
 
3309
  if ((null_value= (args[0]->null_value || args[1]->null_value)))
 
3310
    return 0;
 
3311
  return (int64_t) (arg1 ^ arg2);
 
3312
}
 
3313
 
 
3314
 
 
3315
/***************************************************************************
 
3316
  System variables
 
3317
****************************************************************************/
 
3318
 
 
3319
/**
 
3320
  Return value of an system variable base[.name] as a constant item.
 
3321
 
 
3322
  @param thd                    Thread handler
 
3323
  @param var_type               global / session
 
3324
  @param name                   Name of base or system variable
 
3325
  @param component              Component.
 
3326
 
 
3327
  @note
 
3328
    If component.str = 0 then the variable name is in 'name'
 
3329
 
 
3330
  @return
 
3331
    - 0  : error
 
3332
    - #  : constant item
 
3333
*/
 
3334
 
 
3335
 
 
3336
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
 
3337
                     LEX_STRING component)
 
3338
{
 
3339
  sys_var *var;
 
3340
  LEX_STRING *base_name, *component_name;
 
3341
 
 
3342
  if (component.str)
 
3343
  {
 
3344
    base_name= &component;
 
3345
    component_name= &name;
 
3346
  }
 
3347
  else
 
3348
  {
 
3349
    base_name= &name;
 
3350
    component_name= &component;                 // Empty string
 
3351
  }
 
3352
 
 
3353
  if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
 
3354
    return 0;
 
3355
  if (component.str)
 
3356
  {
 
3357
    if (!var->is_struct())
 
3358
    {
 
3359
      my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
 
3360
      return 0;
 
3361
    }
 
3362
  }
 
3363
 
 
3364
  set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
 
3365
 
 
3366
  return new Item_func_get_system_var(var, var_type, component_name,
 
3367
                                      NULL, 0);
 
3368
}
 
3369
 
 
3370
 
 
3371
/**
 
3372
  Check a user level lock.
 
3373
 
 
3374
  Sets null_value=true on error.
 
3375
 
 
3376
  @retval
 
3377
    1           Available
 
3378
  @retval
 
3379
    0           Already taken, or error
 
3380
*/
 
3381
 
 
3382
int64_t Item_func_is_free_lock::val_int()
 
3383
{
 
3384
  assert(fixed == 1);
 
3385
  String *res=args[0]->val_str(&value);
 
3386
  User_level_lock *ull;
 
3387
 
 
3388
  null_value=0;
 
3389
  if (!res || !res->length())
 
3390
  {
 
3391
    null_value=1;
 
3392
    return 0;
 
3393
  }
 
3394
  
 
3395
  pthread_mutex_lock(&LOCK_user_locks);
 
3396
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
3397
                                       (size_t) res->length());
 
3398
  pthread_mutex_unlock(&LOCK_user_locks);
 
3399
  if (!ull || !ull->locked)
 
3400
    return 1;
 
3401
  return 0;
 
3402
}
 
3403
 
 
3404
int64_t Item_func_is_used_lock::val_int()
 
3405
{
 
3406
  assert(fixed == 1);
 
3407
  String *res=args[0]->val_str(&value);
 
3408
  User_level_lock *ull;
 
3409
 
 
3410
  null_value=1;
 
3411
  if (!res || !res->length())
 
3412
    return 0;
 
3413
  
 
3414
  pthread_mutex_lock(&LOCK_user_locks);
 
3415
  ull= (User_level_lock *) hash_search(&hash_user_locks, (unsigned char*) res->ptr(),
 
3416
                                       (size_t) res->length());
 
3417
  pthread_mutex_unlock(&LOCK_user_locks);
 
3418
  if (!ull || !ull->locked)
 
3419
    return 0;
 
3420
 
 
3421
  null_value=0;
 
3422
  return ull->thread_id;
 
3423
}
 
3424
 
 
3425
 
 
3426
int64_t Item_func_row_count::val_int()
 
3427
{
 
3428
  assert(fixed == 1);
 
3429
  THD *thd= current_thd;
 
3430
 
 
3431
  return thd->row_count_func;
 
3432
}
 
3433
 
 
3434
int64_t Item_func_found_rows::val_int()
 
3435
{
 
3436
  assert(fixed == 1);
 
3437
  THD *thd= current_thd;
 
3438
 
 
3439
  return thd->found_rows();
 
3440
}