item_strfunc.cc 139 KB
Newer Older
1
/*
Sergei Golubchik's avatar
Sergei Golubchik committed
2
   Copyright (c) 2000, 2013, Oracle and/or its affiliates.
Sergei Golubchik's avatar
Sergei Golubchik committed
3
   Copyright (c) 2009, 2013, Monty Program Ab.
unknown's avatar
unknown committed
4

unknown's avatar
unknown committed
5 6
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
7
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
8

unknown's avatar
unknown committed
9 10 11 12
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
13

unknown's avatar
unknown committed
14 15
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
16 17
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/
unknown's avatar
unknown committed
18

unknown's avatar
unknown committed
19 20 21 22 23 24 25 26 27
/**
  @file

  @brief
  This file defines all string functions

  @warning
    Some string functions don't always put and end-null on a String.
    (This shouldn't be needed)
unknown's avatar
unknown committed
28 29
*/

30
#ifdef USE_PRAGMA_IMPLEMENTATION
unknown's avatar
unknown committed
31 32 33
#pragma implementation				// gcc: Class implementation
#endif

34
#include <my_global.h>                          // HAVE_*
35

36 37
/* May include caustic 3rd-party defs. Use early, so it can override nothing */
#include "sha2.h"
38 39 40 41 42 43 44 45 46

#include "sql_priv.h"
/*
  It is necessary to include set_var.h instead of item.h because there
  are dependencies on include order for set_var.h and item.h. This
  will be resolved later.
*/
#include "sql_class.h"                          // set_var.h: THD
#include "set_var.h"
Sergei Golubchik's avatar
Sergei Golubchik committed
47 48
#include "sql_base.h"
#include "sql_time.h"
49 50 51 52
#include "sql_acl.h"                            // SUPER_ACL
#include "des_key_file.h"       // st_des_keyschedule, st_des_keyblock
#include "password.h"           // my_make_scrambled_password,
                                // my_make_scrambled_password_323
unknown's avatar
unknown committed
53
#include <m_ctype.h>
54
#include <base64.h>
55
#include <my_md5.h>
56
#include "sha1.h"
unknown's avatar
unknown committed
57
C_MODE_START
58
#include "../mysys/my_static.h"			// For soundex_map
unknown's avatar
unknown committed
59
C_MODE_END
60
#include "sql_show.h"                           // append_identifier
unknown's avatar
unknown committed
61
#include <sql_repl.h>
62
#include "sql_statistics.h"
unknown's avatar
unknown committed
63

64
size_t username_char_length= 80;
65

66 67 68 69 70 71 72 73 74
/*
  For the Items which have only val_str_ascii() method
  and don't have their own "native" val_str(),
  we provide a "wrapper" method to convert from ASCII
  to Item character set when it's necessary.
  Conversion happens only in case of "tricky" Item character set (e.g. UCS2).
  Normally conversion does not happen, and val_str_ascii() is immediately
  returned instead.
*/
75
String *Item_func::val_str_from_val_str_ascii(String *str, String *str2)
76 77 78 79
{
  DBUG_ASSERT(fixed == 1);

  if (!(collation.collation->state & MY_CS_NONASCII))
80 81 82 83 84 85
  {
    String *res= val_str_ascii(str);
    if (res)
      res->set_charset(collation.collation);
    return res;
  }
86
  
87
  DBUG_ASSERT(str != str2);
88 89
  
  uint errors;
90
  String *res= val_str_ascii(str);
91 92 93
  if (!res)
    return 0;
  
94 95 96
  if ((null_value= str2->copy(res->ptr(), res->length(),
                              &my_charset_latin1, collation.collation,
                              &errors)))
97 98
    return 0;
  
99
  return str2;
100 101 102
}


unknown's avatar
unknown committed
103 104 105 106 107 108 109
bool Item_str_func::fix_fields(THD *thd, Item **ref)
{
  bool res= Item_func::fix_fields(thd, ref);
  /*
    In Item_str_func::check_well_formed_result() we may set null_value
    flag on the same condition as in test() below.
  */
110
  maybe_null= maybe_null || thd->is_strict_mode();
unknown's avatar
unknown committed
111 112 113 114
  return res;
}


115 116 117
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
{
  DBUG_ASSERT(fixed == 1);
118 119 120
  StringBuffer<64> tmp;
  String *res= val_str(&tmp);
  return res ? decimal_from_string_with_check(decimal_value, res) : 0;
121 122 123
}


124
double Item_str_func::val_real()
unknown's avatar
unknown committed
125
{
126
  DBUG_ASSERT(fixed == 1);
127 128 129
  StringBuffer<64> tmp;
  String *res= val_str(&tmp);
  return res ? double_from_string_with_check(res) : 0.0;
unknown's avatar
unknown committed
130 131
}

132

unknown's avatar
unknown committed
133 134
longlong Item_str_func::val_int()
{
135
  DBUG_ASSERT(fixed == 1);
136 137 138
  StringBuffer<22> tmp;
  String *res= val_str(&tmp);
  return res ? longlong_from_string_with_check(res) : 0;
unknown's avatar
unknown committed
139 140 141
}


142
String *Item_func_md5::val_str_ascii(String *str)
unknown's avatar
unknown committed
143
{
144
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
145 146 147
  String * sptr= args[0]->val_str(str);
  if (sptr)
  {
unknown's avatar
unknown committed
148
    uchar digest[16];
unknown's avatar
unknown committed
149 150

    null_value=0;
Sergei Golubchik's avatar
Sergei Golubchik committed
151
    compute_md5_hash(digest, (const char *) sptr->ptr(), sptr->length());
unknown's avatar
unknown committed
152 153 154 155 156
    if (str->alloc(32))				// Ensure that memory is free
    {
      null_value=1;
      return 0;
    }
Konstantin Osipov's avatar
Konstantin Osipov committed
157
    array_to_hex((char *) str->ptr(), digest, 16);
Sergei Golubchik's avatar
Sergei Golubchik committed
158
    str->set_charset(&my_charset_numeric);
unknown's avatar
unknown committed
159 160 161 162 163 164 165 166
    str->length((uint) 32);
    return str;
  }
  null_value=1;
  return 0;
}


167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/*
  The MD5()/SHA() functions treat their parameter as being a case sensitive.
  Thus we set binary collation on it so different instances of MD5() will be
  compared properly.
*/
static CHARSET_INFO *get_checksum_charset(const char *csname)
{
  CHARSET_INFO *cs= get_charset_by_csname(csname, MY_CS_BINSORT, MYF(0));
  if (!cs)
  {
    // Charset has no binary collation: use my_charset_bin.
    cs= &my_charset_bin;
  }
  return cs;
}


unknown's avatar
unknown committed
184 185
void Item_func_md5::fix_length_and_dec()
{
186 187
  CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
  args[0]->collation.set(cs, DERIVATION_COERCIBLE);
188
  fix_length_and_charset(32, default_charset());
unknown's avatar
unknown committed
189 190
}

191

192
String *Item_func_sha::val_str_ascii(String *str)
193
{
194
  DBUG_ASSERT(fixed == 1);
195 196 197
  String * sptr= args[0]->val_str(str);
  if (sptr)  /* If we got value different from NULL */
  {
198
    /* Temporary buffer to store 160bit digest */
unknown's avatar
unknown committed
199
    uint8 digest[SHA1_HASH_SIZE];
200
    compute_sha1_hash(digest, (const char *) sptr->ptr(), sptr->length());
201
    /* Ensure that memory is free and we got result */
202
    if (!str->alloc(SHA1_HASH_SIZE*2))
203
    {
Konstantin Osipov's avatar
Konstantin Osipov committed
204
      array_to_hex((char *) str->ptr(), digest, SHA1_HASH_SIZE);
Sergei Golubchik's avatar
Sergei Golubchik committed
205
      str->set_charset(&my_charset_numeric);
206 207 208 209
      str->length((uint)  SHA1_HASH_SIZE*2);
      null_value=0;
      return str;
    }
210
  }
211 212 213 214 215 216
  null_value=1;
  return 0;
}

void Item_func_sha::fix_length_and_dec()
{
217 218
  CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
  args[0]->collation.set(cs, DERIVATION_COERCIBLE);
219 220
  // size of hex representation of hash
  fix_length_and_charset(SHA1_HASH_SIZE * 2, default_charset());
221
}
222

223
String *Item_func_sha2::val_str_ascii(String *str)
Konstantin Osipov's avatar
Konstantin Osipov committed
224 225
{
  DBUG_ASSERT(fixed == 1);
226
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
Konstantin Osipov's avatar
Konstantin Osipov committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  unsigned char digest_buf[SHA512_DIGEST_LENGTH];
  String *input_string;
  unsigned char *input_ptr;
  size_t input_len;
  uint digest_length= 0;

  str->set_charset(&my_charset_bin);

  input_string= args[0]->val_str(str);
  if (input_string == NULL)
  {
    null_value= TRUE;
    return (String *) NULL;
  }

  null_value= args[0]->null_value;
  if (null_value)
    return (String *) NULL;

  input_ptr= (unsigned char *) input_string->ptr();
  input_len= input_string->length();

  switch ((uint) args[1]->val_int()) {
#ifndef OPENSSL_NO_SHA512
  case 512:
    digest_length= SHA512_DIGEST_LENGTH;
    (void) SHA512(input_ptr, input_len, digest_buf);
    break;
  case 384:
    digest_length= SHA384_DIGEST_LENGTH;
    (void) SHA384(input_ptr, input_len, digest_buf);
    break;
#endif
#ifndef OPENSSL_NO_SHA256
  case 224:
    digest_length= SHA224_DIGEST_LENGTH;
    (void) SHA224(input_ptr, input_len, digest_buf);
    break;
  case 256:
  case 0: // SHA-256 is the default
    digest_length= SHA256_DIGEST_LENGTH;
    (void) SHA256(input_ptr, input_len, digest_buf);
    break;
#endif
  default:
    if (!args[1]->const_item())
273 274 275
    {
      THD *thd= current_thd;
      push_warning_printf(thd,
276 277
                          Sql_condition::WARN_LEVEL_WARN,
                          ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
278 279 280
                          ER_THD(thd, ER_WRONG_PARAMETERS_TO_NATIVE_FCT),
                          "sha2");
    }
Konstantin Osipov's avatar
Konstantin Osipov committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    null_value= TRUE;
    return NULL;
  }

  /* 
    Since we're subverting the usual String methods, we must make sure that
    the destination has space for the bytes we're about to write.
  */
  str->realloc((uint) digest_length*2 + 1); /* Each byte as two nybbles */

  /* Convert the large number to a string-hex representation. */
  array_to_hex((char *) str->ptr(), digest_buf, digest_length);

  /* We poked raw bytes in.  We must inform the the String of its length. */
  str->length((uint) digest_length*2); /* Each byte as two nybbles */

  null_value= FALSE;
  return str;

#else
301 302 303 304 305 306
  THD *thd= current_thd;
  push_warning_printf(thd,
                      Sql_condition::WARN_LEVEL_WARN,
                      ER_FEATURE_DISABLED,
                      ER_THD(thd, ER_FEATURE_DISABLED),
                      "sha2", "--with-ssl");
Konstantin Osipov's avatar
Konstantin Osipov committed
307 308
  null_value= TRUE;
  return (String *) NULL;
309
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
Konstantin Osipov's avatar
Konstantin Osipov committed
310 311 312 313 314
}


void Item_func_sha2::fix_length_and_dec()
{
unknown's avatar
unknown committed
315
  maybe_null= 1;
Konstantin Osipov's avatar
Konstantin Osipov committed
316 317
  max_length = 0;

318
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
Konstantin Osipov's avatar
Konstantin Osipov committed
319 320 321 322 323
  int sha_variant= args[1]->const_item() ? args[1]->val_int() : 512;

  switch (sha_variant) {
#ifndef OPENSSL_NO_SHA512
  case 512:
324
    fix_length_and_charset(SHA512_DIGEST_LENGTH * 2, default_charset());
Konstantin Osipov's avatar
Konstantin Osipov committed
325 326
    break;
  case 384:
327
    fix_length_and_charset(SHA384_DIGEST_LENGTH * 2, default_charset());
Konstantin Osipov's avatar
Konstantin Osipov committed
328 329 330 331 332
    break;
#endif
#ifndef OPENSSL_NO_SHA256
  case 256:
  case 0: // SHA-256 is the default
333
    fix_length_and_charset(SHA256_DIGEST_LENGTH * 2, default_charset());
Konstantin Osipov's avatar
Konstantin Osipov committed
334 335
    break;
  case 224:
336
    fix_length_and_charset(SHA224_DIGEST_LENGTH * 2, default_charset());
Konstantin Osipov's avatar
Konstantin Osipov committed
337 338 339
    break;
#endif
  default:
340 341 342 343 344 345
    THD *thd= current_thd;
    push_warning_printf(thd,
                        Sql_condition::WARN_LEVEL_WARN,
                        ER_WRONG_PARAMETERS_TO_NATIVE_FCT,
                        ER_THD(thd, ER_WRONG_PARAMETERS_TO_NATIVE_FCT),
                        "sha2");
Konstantin Osipov's avatar
Konstantin Osipov committed
346 347
  }

348 349
  CHARSET_INFO *cs= get_checksum_charset(args[0]->collation.collation->csname);
  args[0]->collation.set(cs, DERIVATION_COERCIBLE);
Konstantin Osipov's avatar
Konstantin Osipov committed
350 351

#else
352 353 354 355 356 357
  THD *thd= current_thd;
  push_warning_printf(thd,
                      Sql_condition::WARN_LEVEL_WARN,
                      ER_FEATURE_DISABLED,
                      ER_THD(thd, ER_FEATURE_DISABLED),
                      "sha2", "--with-ssl");
358
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
Konstantin Osipov's avatar
Konstantin Osipov committed
359
}
360 361

/* Implementation of AES encryption routines */
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
void Item_aes_crypt::create_key(String *user_key, uchar *real_key)
{
  uchar *real_key_end= real_key + AES_KEY_LENGTH / 8;
  uchar *ptr;
  const char *sptr= user_key->ptr();
  const char *key_end= sptr + user_key->length();

  bzero(real_key, AES_KEY_LENGTH / 8);

  for (ptr= real_key; sptr < key_end; ptr++, sptr++)
  {
    if (ptr == real_key_end)
      ptr= real_key;
    *ptr ^= (uchar) *sptr;
  }
}
378

379 380

String *Item_aes_crypt::val_str(String *str)
381
{
382
  DBUG_ASSERT(fixed == 1);
383 384 385 386 387 388
  StringBuffer<80> user_key_buf;
  String *sptr= args[0]->val_str(str);
  String *user_key=  args[1]->val_str(&user_key_buf);
  uint32 aes_length;

  if (sptr && user_key) // we need both arguments to be not NULL
389 390
  {
    null_value=0;
391
    aes_length=my_aes_get_size(MY_AES_ECB, sptr->length());
392

393
    if (!str_value.alloc(aes_length))		// Ensure that memory is free
394
    {
395 396 397
      uchar rkey[AES_KEY_LENGTH / 8];
      create_key(user_key, rkey);

398
      if (!my_aes_crypt(MY_AES_ECB, what, (uchar*)sptr->ptr(), sptr->length(),
399
                 (uchar*)str_value.ptr(), &aes_length,
400
                 rkey, AES_KEY_LENGTH / 8, 0, 0))
401
      {
402 403
        str_value.length((uint) aes_length);
        return &str_value;
404 405 406 407 408 409 410 411 412
      }
    }
  }
  null_value=1;
  return 0;
}

void Item_func_aes_encrypt::fix_length_and_dec()
{
413 414
  max_length=my_aes_get_size(MY_AES_ECB, args[0]->max_length);
  what= ENCRYPTION_FLAG_ENCRYPT;
415 416
}

417

418 419
void Item_func_aes_decrypt::fix_length_and_dec()
{
420 421 422
  max_length=args[0]->max_length;
  maybe_null= 1;
  what= ENCRYPTION_FLAG_DECRYPT;
423
}
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458

void Item_func_to_base64::fix_length_and_dec()
{
  maybe_null= args[0]->maybe_null;
  collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
  if (args[0]->max_length > (uint) base64_encode_max_arg_length())
  {
    maybe_null= 1;
    fix_char_length_ulonglong((ulonglong) base64_encode_max_arg_length());
  }
  else
  {
    int length= base64_needed_encoded_length((int) args[0]->max_length);
    DBUG_ASSERT(length > 0);
    fix_char_length_ulonglong((ulonglong) length - 1);
  }
}


String *Item_func_to_base64::val_str_ascii(String *str)
{
  String *res= args[0]->val_str(str);
  bool too_long= false;
  int length;
  if (!res ||
      res->length() > (uint) base64_encode_max_arg_length() ||
      (too_long=
       ((uint) (length= base64_needed_encoded_length((int) res->length())) >
        current_thd->variables.max_allowed_packet)) ||
      tmp_value.alloc((uint) length))
  {
    null_value= 1; // NULL input, too long input, or OOM.
    if (too_long)
    {
459 460
      THD *thd= current_thd;
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
461
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
462 463 464
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(),
                          thd->variables.max_allowed_packet);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    }
    return 0;
  }
  base64_encode(res->ptr(), (int) res->length(), (char*) tmp_value.ptr());
  DBUG_ASSERT(length > 0);
  tmp_value.length((uint) length - 1); // Without trailing '\0'
  null_value= 0;
  return &tmp_value;
}


void Item_func_from_base64::fix_length_and_dec()
{
  if (args[0]->max_length > (uint) base64_decode_max_arg_length())
  {
    fix_char_length_ulonglong((ulonglong) base64_decode_max_arg_length());
  }
  else
  {
    int length= base64_needed_decoded_length((int) args[0]->max_length);
    fix_char_length_ulonglong((ulonglong) length);
  }
  maybe_null= 1; // Can be NULL, e.g. in case of badly formed input string
}


String *Item_func_from_base64::val_str(String *str)
{
  String *res= args[0]->val_str_ascii(str);
  int length;
  const char *end_ptr;

497 498 499 500 501 502 503
  if (!res)
    goto err;

  if (res->length() > (uint) base64_decode_max_arg_length() ||
      ((uint) (length= base64_needed_decoded_length((int) res->length())) >
       current_thd->variables.max_allowed_packet))
  {
504 505
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
506
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
507 508 509
                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                        func_name(),
                        thd->variables.max_allowed_packet);
510 511 512 513 514 515 516
    goto err;
  }

  if (tmp_value.alloc((uint) length))
    goto err;

  if ((length= base64_decode(res->ptr(), (int) res->length(),
517 518 519
                             (char *) tmp_value.ptr(), &end_ptr, 0)) < 0 ||
      end_ptr < res->ptr() + res->length())
  {
520 521 522
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                        ER_BAD_BASE64_DATA, ER_THD(thd, ER_BAD_BASE64_DATA),
523 524
                        end_ptr - res->ptr());
    goto err;
525
  }
526

527 528 529
  tmp_value.length((uint) length);
  null_value= 0;
  return &tmp_value;
530 531 532
err:
  null_value= 1; // NULL input, too long input, OOM, or badly formed input
  return 0;
533
}
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
///////////////////////////////////////////////////////////////////////////////


const char *histogram_types[] =
           {"SINGLE_PREC_HB", "DOUBLE_PREC_HB", 0};
static TYPELIB hystorgam_types_typelib=
  { array_elements(histogram_types),
    "histogram_types",
    histogram_types, NULL};
const char *representation_by_type[]= {"%.3f", "%.5f"};

String *Item_func_decode_histogram::val_str(String *str)
{
  DBUG_ASSERT(fixed == 1);
  char buff[STRING_BUFFER_USUAL_SIZE];
  String *res, tmp(buff, sizeof(buff), &my_charset_bin);
  int type;

  tmp.length(0);
553
  if (!(res= args[0]->val_str(&tmp)) ||
554 555 556 557 558 559 560 561 562
      (type= find_type(res->c_ptr_safe(),
                       &hystorgam_types_typelib, MYF(0))) <= 0)
  {
    null_value= 1;
    return 0;
  }
  type--;

  tmp.length(0);
563
  if (!(res= args[1]->val_str(&tmp)))
564 565 566 567 568 569 570 571 572 573
  {
    null_value= 1;
    return 0;
  }
  if (type == DOUBLE_PREC_HB && res->length() % 2 != 0)
    res->length(res->length() - 1); // one byte is unused

  double prev= 0.0;
  uint i;
  str->length(0);
574
  char numbuf[32];
575 576 577 578 579 580 581 582 583 584
  const uchar *p= (uchar*)res->c_ptr();
  for (i= 0; i < res->length(); i++)
  {
    double val;
    switch (type)
    {
    case SINGLE_PREC_HB:
      val= p[i] / ((double)((1 << 8) - 1));
      break;
    case DOUBLE_PREC_HB:
585
      val= uint2korr(p + i) / ((double)((1 << 16) - 1));
586 587 588 589 590 591 592 593 594 595
      i++;
      break;
    default:
      val= 0;
      DBUG_ASSERT(0);
    }
    /* show delta with previous value */
    int size= my_snprintf(numbuf, sizeof(numbuf),
                          representation_by_type[type], val - prev);
    str->append(numbuf, size);
596
    str->append(",");
597 598
    prev= val;
  }
599 600 601 602
  /* show delta with max */
  int size= my_snprintf(numbuf, sizeof(numbuf),
                        representation_by_type[type], 1.0 - prev);
  str->append(numbuf, size);
603 604 605 606 607 608 609

  null_value=0;
  return str;
}


///////////////////////////////////////////////////////////////////////////////
610

unknown's avatar
unknown committed
611
/**
612
  Concatenate args with the following premises:
unknown's avatar
unknown committed
613
  If only one arg (which is ok), return value of arg;
614
  Don't reallocate val_str() if not absolute necessary.
unknown's avatar
unknown committed
615 616 617 618
*/

String *Item_func_concat::val_str(String *str)
{
619
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
620 621
  String *res,*res2,*use_as_buff;
  uint i;
622
  bool is_const= 0;
unknown's avatar
unknown committed
623 624 625 626 627

  null_value=0;
  if (!(res=args[0]->val_str(str)))
    goto null;
  use_as_buff= &tmp_value;
628
  is_const= args[0]->const_item();
unknown's avatar
unknown committed
629 630 631 632 633 634
  for (i=1 ; i < arg_count ; i++)
  {
    if (res->length() == 0)
    {
      if (!(res=args[i]->val_str(str)))
	goto null;
635 636 637 638 639
      /*
       CONCAT accumulates its result in the result of its the first
       non-empty argument. Because of this we need is_const to be 
       evaluated only for it.
      */
640
      is_const= args[i]->const_item();
unknown's avatar
unknown committed
641 642 643 644 645 646 647
    }
    else
    {
      if (!(res2=args[i]->val_str(use_as_buff)))
	goto null;
      if (res2->length() == 0)
	continue;
unknown's avatar
unknown committed
648 649
      if (res->length()+res2->length() >
	  current_thd->variables.max_allowed_packet)
650
      {
651 652
        THD *thd= current_thd;
	push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
653
			    ER_WARN_ALLOWED_PACKET_OVERFLOWED,
654 655 656
			    ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                            func_name(),
			    thd->variables.max_allowed_packet);
657 658
	goto null;
      }
659
      if (!is_const && res->alloced_length() >= res->length()+res2->length())
unknown's avatar
unknown committed
660 661 662 663 664
      {						// Use old buffer
	res->append(*res2);
      }
      else if (str->alloced_length() >= res->length()+res2->length())
      {
665
	if (str->ptr() == res2->ptr())
unknown's avatar
unknown committed
666 667 668 669 670 671
	  str->replace(0,0,*res);
	else
	{
	  str->copy(*res);
	  str->append(*res2);
	}
672 673
        res= str;
        use_as_buff= &tmp_value;
unknown's avatar
unknown committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
      }
      else if (res == &tmp_value)
      {
	if (res->append(*res2))			// Must be a blob
	  goto null;
      }
      else if (res2 == &tmp_value)
      {						// This can happend only 1 time
	if (tmp_value.replace(0,0,*res))
	  goto null;
	res= &tmp_value;
	use_as_buff=str;			// Put next arg here
      }
      else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
	       res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
      {
	/*
	  This happens really seldom:
	  In this case res2 is sub string of tmp_value.  We will
	  now work in place in tmp_value to set it to res | res2
	*/
	/* Chop the last characters in tmp_value that isn't in res2 */
	tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
			 res2->length());
	/* Place res2 at start of tmp_value, remove chars before res2 */
	if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
			      *res))
	  goto null;
	res= &tmp_value;
	use_as_buff=str;			// Put next arg here
      }
      else
      {						// Two big const strings
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
        /*
          NOTE: We should be prudent in the initial allocation unit -- the
          size of the arguments is a function of data distribution, which
          can be any. Instead of overcommitting at the first row, we grow
          the allocated amount by the factor of 2. This ensures that no
          more than 25% of memory will be overcommitted on average.
        */

        uint concat_len= res->length() + res2->length();

        if (tmp_value.alloced_length() < concat_len)
        {
          if (tmp_value.alloced_length() == 0)
          {
            if (tmp_value.alloc(concat_len))
              goto null;
          }
          else
          {
726
            uint new_len = MY_MAX(tmp_value.alloced_length() * 2, concat_len);
727 728 729 730 731 732 733

            if (tmp_value.realloc(new_len))
              goto null;
          }
        }

	if (tmp_value.copy(*res) || tmp_value.append(*res2))
unknown's avatar
unknown committed
734
	  goto null;
735

unknown's avatar
unknown committed
736 737 738
	res= &tmp_value;
	use_as_buff=str;
      }
739
      is_const= 0;
unknown's avatar
unknown committed
740 741
    }
  }
742
  res->set_charset(collation.collation);
unknown's avatar
unknown committed
743 744 745 746 747 748 749 750 751 752
  return res;

null:
  null_value=1;
  return 0;
}


void Item_func_concat::fix_length_and_dec()
{
753
  ulonglong char_length= 0;
754

755
  if (agg_arg_charsets_for_string_result(collation, args, arg_count))
unknown's avatar
unknown committed
756 757
    return;

unknown's avatar
unknown committed
758
  for (uint i=0 ; i < arg_count ; i++)
759
    char_length+= args[i]->max_char_length();
760

761
  fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
762 763
}

unknown's avatar
unknown committed
764 765
/**
  @details
unknown's avatar
unknown committed
766 767
  Function des_encrypt() by tonu@spam.ee & monty
  Works only if compiled with OpenSSL library support.
unknown's avatar
unknown committed
768 769 770 771 772
  @return
    A binary string where first character is CHAR(128 | key-number).
    If one uses a string key key_number is 127.
    Encryption result is longer than original by formula:
  @code new_length= org_length + (8-(org_length % 8))+1 @endcode
773 774
*/

775 776
String *Item_func_des_encrypt::val_str(String *str)
{
777
  DBUG_ASSERT(fixed == 1);
778
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
779
  uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
780
  DES_cblock ivec;
unknown's avatar
unknown committed
781 782
  struct st_des_keyblock keyblock;
  struct st_des_keyschedule keyschedule;
unknown's avatar
unknown committed
783 784
  const char *append_str="********";
  uint key_number, res_length, tail;
unknown's avatar
unknown committed
785
  String *res= args[0]->val_str(str);
786

unknown's avatar
unknown committed
787 788
  if ((null_value= args[0]->null_value))
    return 0;                                   // ENCRYPT(NULL) == NULL
unknown's avatar
unknown committed
789
  if ((res_length=res->length()) == 0)
790
    return make_empty_result();
unknown's avatar
unknown committed
791
  if (arg_count == 1)
unknown's avatar
unknown committed
792 793
  {
    /* Protect against someone doing FLUSH DES_KEY_FILE */
Marc Alff's avatar
Marc Alff committed
794
    mysql_mutex_lock(&LOCK_des_key_file);
unknown's avatar
unknown committed
795
    keyschedule= des_keyschedule[key_number=des_default_key];
Marc Alff's avatar
Marc Alff committed
796
    mysql_mutex_unlock(&LOCK_des_key_file);
unknown's avatar
unknown committed
797
  }
unknown's avatar
unknown committed
798
  else if (args[1]->result_type() == INT_RESULT)
unknown's avatar
unknown committed
799 800 801 802
  {
    key_number= (uint) args[1]->val_int();
    if (key_number > 9)
      goto error;
Marc Alff's avatar
Marc Alff committed
803
    mysql_mutex_lock(&LOCK_des_key_file);
unknown's avatar
unknown committed
804
    keyschedule= des_keyschedule[key_number];
Marc Alff's avatar
Marc Alff committed
805
    mysql_mutex_unlock(&LOCK_des_key_file);
806
  }
unknown's avatar
unknown committed
807 808 809 810 811
  else
  {
    String *keystr=args[1]->val_str(&tmp_value);
    if (!keystr)
      goto error;
unknown's avatar
unknown committed
812
    key_number=127;				// User key string
unknown's avatar
unknown committed
813 814

    /* We make good 24-byte (168 bit) key from given plaintext key with MD5 */
unknown's avatar
unknown committed
815
    bzero((char*) &ivec,sizeof(ivec));
816
    if (!EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
unknown's avatar
unknown committed
817
		   (uchar*) keystr->ptr(), (int) keystr->length(),
818 819
		   1, (uchar*) &keyblock,ivec))
      goto error;
820 821 822
    DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
    DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
    DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
unknown's avatar
unknown committed
823 824
  }

825
  /*
unknown's avatar
unknown committed
826
     The problem: DES algorithm requires original data to be in 8-bytes
827 828
     chunks. Missing bytes get filled with '*'s and result of encryption
     can be up to 8 bytes longer than original string. When decrypted,
unknown's avatar
unknown committed
829
     we do not know the size of original string :(
unknown's avatar
unknown committed
830 831
     We add one byte with value 0x1..0x8 as the last byte of the padded
     string marking change of string length.
unknown's avatar
unknown committed
832 833
  */

834
  tail= 8 - (res_length % 8);                   // 1..8 marking extra length
unknown's avatar
unknown committed
835
  res_length+=tail;
Sergey Petrunya's avatar
Sergey Petrunya committed
836 837
  if (tmp_arg.realloc(res_length))
    goto error;
838 839
  tmp_arg.length(0);
  tmp_arg.append(res->ptr(), res->length());
840
  code= ER_OUT_OF_RESOURCES;
841
  if (tmp_arg.append(append_str, tail) || tmp_value.alloc(res_length+1))
unknown's avatar
unknown committed
842
    goto error;
843 844
  tmp_arg[res_length-1]=tail;                   // save extra length
  tmp_value.realloc(res_length+1);
unknown's avatar
unknown committed
845
  tmp_value.length(res_length+1);
846
  tmp_value.set_charset(&my_charset_bin);
unknown's avatar
unknown committed
847
  tmp_value[0]=(char) (128 | key_number);
unknown's avatar
unknown committed
848
  // Real encryption
unknown's avatar
unknown committed
849
  bzero((char*) &ivec,sizeof(ivec));
850
  DES_ede3_cbc_encrypt((const uchar*) (tmp_arg.ptr()),
unknown's avatar
unknown committed
851
		       (uchar*) (tmp_value.ptr()+1),
unknown's avatar
unknown committed
852
		       res_length,
853 854 855
		       &keyschedule.ks1,
		       &keyschedule.ks2,
		       &keyschedule.ks3,
unknown's avatar
unknown committed
856 857 858 859
		       &ivec, TRUE);
  return &tmp_value;

error:
860 861 862 863
  THD *thd= current_thd;
  push_warning_printf(thd,Sql_condition::WARN_LEVEL_WARN,
                      code, ER_THD(thd, code),
                      "des_encrypt");
864
#else
865 866 867
  THD *thd= current_thd;
  push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                      ER_FEATURE_DISABLED, ER_THD(thd, ER_FEATURE_DISABLED),
Konstantin Osipov's avatar
Konstantin Osipov committed
868
                      "des_encrypt", "--with-ssl");
869
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
870 871 872 873
  null_value=1;
  return 0;
}

unknown's avatar
unknown committed
874

875 876
String *Item_func_des_decrypt::val_str(String *str)
{
877
  DBUG_ASSERT(fixed == 1);
878
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
879
  uint code= ER_WRONG_PARAMETERS_TO_PROCEDURE;
880
  DES_cblock ivec;
unknown's avatar
unknown committed
881 882
  struct st_des_keyblock keyblock;
  struct st_des_keyschedule keyschedule;
unknown's avatar
unknown committed
883
  String *res= args[0]->val_str(str);
884
  uint length,tail;
885

886
  if ((null_value= args[0]->null_value))
unknown's avatar
unknown committed
887
    return 0;
888
  length= res->length();
unknown's avatar
unknown committed
889
  if (length < 9 || (length % 8) != 1 || !((*res)[0] & 128))
unknown's avatar
unknown committed
890
    return res;				// Skip decryption if not encrypted
unknown's avatar
unknown committed
891

unknown's avatar
unknown committed
892
  if (arg_count == 1)			// If automatic uncompression
893
  {
unknown's avatar
unknown committed
894
    uint key_number=(uint) (*res)[0] & 127;
unknown's avatar
unknown committed
895
    // Check if automatic key and that we have privilege to uncompress using it
896 897
    if (!(current_thd->security_ctx->master_access & SUPER_ACL) ||
        key_number > 9)
unknown's avatar
unknown committed
898
      goto error;
899

Marc Alff's avatar
Marc Alff committed
900
    mysql_mutex_lock(&LOCK_des_key_file);
unknown's avatar
unknown committed
901
    keyschedule= des_keyschedule[key_number];
Marc Alff's avatar
Marc Alff committed
902
    mysql_mutex_unlock(&LOCK_des_key_file);
903
  }
unknown's avatar
unknown committed
904 905 906 907
  else
  {
    // We make good 24-byte (168 bit) key from given plaintext key with MD5
    String *keystr=args[1]->val_str(&tmp_value);
unknown's avatar
unknown committed
908
    if (!keystr)
unknown's avatar
unknown committed
909
      goto error;
unknown's avatar
unknown committed
910

unknown's avatar
unknown committed
911
    bzero((char*) &ivec,sizeof(ivec));
912
    if (!EVP_BytesToKey(EVP_des_ede3_cbc(),EVP_md5(),NULL,
unknown's avatar
unknown committed
913
		   (uchar*) keystr->ptr(),(int) keystr->length(),
914 915
		   1,(uchar*) &keyblock,ivec))
      goto error;
unknown's avatar
unknown committed
916
    // Here we set all 64-bit keys (56 effective) one by one
917 918
    DES_set_key_unchecked(&keyblock.key1,&keyschedule.ks1);
    DES_set_key_unchecked(&keyblock.key2,&keyschedule.ks2);
unknown's avatar
unknown committed
919
    DES_set_key_unchecked(&keyblock.key3,&keyschedule.ks3);
unknown's avatar
unknown committed
920
  }
921
  code= ER_OUT_OF_RESOURCES;
unknown's avatar
unknown committed
922
  if (tmp_value.alloc(length-1))
unknown's avatar
unknown committed
923
    goto error;
unknown's avatar
unknown committed
924 925

  bzero((char*) &ivec,sizeof(ivec));
926
  DES_ede3_cbc_encrypt((const uchar*) res->ptr()+1,
unknown's avatar
unknown committed
927
		       (uchar*) (tmp_value.ptr()),
unknown's avatar
unknown committed
928
		       length-1,
929 930 931
		       &keyschedule.ks1,
		       &keyschedule.ks2,
		       &keyschedule.ks3,
unknown's avatar
unknown committed
932
		       &ivec, FALSE);
unknown's avatar
unknown committed
933 934
  /* Restore old length of key */
  if ((tail=(uint) (uchar) tmp_value[length-2]) > 8)
935
    goto wrong_key;				     // Wrong key
unknown's avatar
unknown committed
936
  tmp_value.length(length-1-tail);
937
  tmp_value.set_charset(&my_charset_bin);
unknown's avatar
unknown committed
938 939 940
  return &tmp_value;

error:
941 942 943 944 945 946
  {
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                        code, ER_THD(thd, code),
                        "des_decrypt");
  }
947 948
wrong_key:
#else
949 950 951 952 953 954
  {
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                        ER_FEATURE_DISABLED, ER_THD(thd, ER_FEATURE_DISABLED),
                        "des_decrypt", "--with-ssl");
  }
955
#endif /* defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY) */
956 957 958 959 960
  null_value=1;
  return 0;
}


unknown's avatar
unknown committed
961
/**
unknown's avatar
unknown committed
962 963
  concat with separator. First arg is the separator
  concat_ws takes at least two arguments.
unknown's avatar
unknown committed
964 965 966 967
*/

String *Item_func_concat_ws::val_str(String *str)
{
968
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
969
  char tmp_str_buff[10];
970
  String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
unknown's avatar
unknown committed
971 972
         *sep_str, *res, *res2,*use_as_buff;
  uint i;
973
  bool is_const= 0;
974
  THD *thd= 0;
unknown's avatar
unknown committed
975 976

  null_value=0;
977
  if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
unknown's avatar
unknown committed
978 979 980
    goto null;

  use_as_buff= &tmp_value;
unknown's avatar
unknown committed
981
  str->length(0);				// QQ; Should be removed
982
  res=str;                                      // If 0 arg_count
unknown's avatar
unknown committed
983

984
  // Skip until non-null argument is found.
unknown's avatar
unknown committed
985
  // If not, return the empty string
986
  for (i=1; i < arg_count; i++)
987
    if ((res= args[i]->val_str(str)))
988
    {
989
      is_const= args[i]->const_item();
990
      break;
991 992
    }

unknown's avatar
unknown committed
993
  if (i ==  arg_count)
994
    return make_empty_result();
unknown's avatar
unknown committed
995 996 997

  for (i++; i < arg_count ; i++)
  {
998 999
    if (!(res2= args[i]->val_str(use_as_buff)))
      continue;					// Skip NULL
unknown's avatar
unknown committed
1000

1001 1002
    if (!thd)
      thd= current_thd;
unknown's avatar
unknown committed
1003
    if (res->length() + sep_str->length() + res2->length() >
1004
	thd->variables.max_allowed_packet)
1005
    {
1006
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1007
			  ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1008 1009 1010
			  ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(),
			  thd->variables.max_allowed_packet);
1011 1012
      goto null;
    }
1013
    if (!is_const && res->alloced_length() >=
unknown's avatar
unknown committed
1014 1015 1016 1017 1018 1019 1020
	res->length() + sep_str->length() + res2->length())
    {						// Use old buffer
      res->append(*sep_str);			// res->length() > 0 always
      res->append(*res2);
    }
    else if (str->alloced_length() >=
	     res->length() + sep_str->length() + res2->length())
unknown's avatar
unknown committed
1021
    {
1022
      /* We have room in str;  We can't get any errors here */
1023 1024
      if (str->ptr() == res2->ptr())
      {						// This is quite uncommon!
1025 1026 1027 1028 1029 1030 1031 1032 1033
	str->replace(0,0,*sep_str);
	str->replace(0,0,*res);
      }
      else
      {
	str->copy(*res);
	str->append(*sep_str);
	str->append(*res2);
      }
unknown's avatar
unknown committed
1034 1035 1036 1037 1038
      res=str;
      use_as_buff= &tmp_value;
    }
    else if (res == &tmp_value)
    {
1039
      if (res->append(*sep_str) || res->append(*res2))
unknown's avatar
unknown committed
1040 1041
	goto null; // Must be a blob
    }
1042 1043 1044 1045 1046 1047 1048
    else if (res2 == &tmp_value)
    {						// This can happend only 1 time
      if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
	goto null;
      res= &tmp_value;
      use_as_buff=str;				// Put next arg here
    }
unknown's avatar
unknown committed
1049
    else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
1050
	     res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
unknown's avatar
unknown committed
1051 1052 1053 1054
    {
      /*
	This happens really seldom:
	In this case res2 is sub string of tmp_value.  We will
1055
	now work in place in tmp_value to set it to res | sep_str | res2
unknown's avatar
unknown committed
1056 1057 1058 1059 1060 1061
      */
      /* Chop the last characters in tmp_value that isn't in res2 */
      tmp_value.length((uint32) (res2->ptr() - tmp_value.ptr()) +
		       res2->length());
      /* Place res2 at start of tmp_value, remove chars before res2 */
      if (tmp_value.replace(0,(uint32) (res2->ptr() - tmp_value.ptr()),
1062 1063
			    *res) ||
	  tmp_value.replace(res->length(),0, *sep_str))
unknown's avatar
unknown committed
1064 1065 1066 1067 1068 1069
	goto null;
      res= &tmp_value;
      use_as_buff=str;			// Put next arg here
    }
    else
    {						// Two big const strings
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
      /*
        NOTE: We should be prudent in the initial allocation unit -- the
        size of the arguments is a function of data distribution, which can
        be any. Instead of overcommitting at the first row, we grow the
        allocated amount by the factor of 2. This ensures that no more than
        25% of memory will be overcommitted on average.
      */

      uint concat_len= res->length() + sep_str->length() + res2->length();

      if (tmp_value.alloced_length() < concat_len)
      {
        if (tmp_value.alloced_length() == 0)
        {
          if (tmp_value.alloc(concat_len))
            goto null;
        }
        else
        {
1089
          uint new_len = MY_MAX(tmp_value.alloced_length() * 2, concat_len);
1090 1091 1092 1093 1094 1095 1096

          if (tmp_value.realloc(new_len))
            goto null;
        }
      }

      if (tmp_value.copy(*res) ||
unknown's avatar
unknown committed
1097 1098 1099 1100 1101
	  tmp_value.append(*sep_str) ||
	  tmp_value.append(*res2))
	goto null;
      res= &tmp_value;
      use_as_buff=str;
unknown's avatar
unknown committed
1102 1103
    }
  }
1104
  res->set_charset(collation.collation);
unknown's avatar
unknown committed
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
  return res;

null:
  null_value=1;
  return 0;
}


void Item_func_concat_ws::fix_length_and_dec()
{
1115
  ulonglong char_length;
1116

1117
  if (agg_arg_charsets_for_string_result(collation, args, arg_count))
1118 1119
    return;

unknown's avatar
unknown committed
1120 1121 1122 1123 1124
  /*
     arg_count cannot be less than 2,
     it is done on parser level in sql_yacc.yy
     so, (arg_count - 2) is safe here.
  */
1125
  char_length= (ulonglong) args[0]->max_char_length() * (arg_count - 2);
1126
  for (uint i=1 ; i < arg_count ; i++)
1127
    char_length+= args[i]->max_char_length();
1128

1129
  fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
1130 1131
}

unknown's avatar
unknown committed
1132 1133 1134

String *Item_func_reverse::val_str(String *str)
{
1135
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
1136
  String *res = args[0]->val_str(str);
unknown's avatar
unknown committed
1137
  char *ptr, *end, *tmp;
unknown's avatar
unknown committed
1138 1139 1140 1141 1142

  if ((null_value=args[0]->null_value))
    return 0;
  /* An empty string is a special case as the string pointer may be null */
  if (!res->length())
1143
    return make_empty_result();
unknown's avatar
unknown committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  if (tmp_value.alloced_length() < res->length() &&
      tmp_value.realloc(res->length()))
  {
    null_value= 1;
    return 0;
  }
  tmp_value.length(res->length());
  tmp_value.set_charset(res->charset());
  ptr= (char *) res->ptr();
  end= ptr + res->length();
  tmp= (char *) tmp_value.ptr() + tmp_value.length();
unknown's avatar
unknown committed
1155
#ifdef USE_MB
1156
  if (use_mb(res->charset()))
unknown's avatar
unknown committed
1157 1158 1159 1160
  {
    register uint32 l;
    while (ptr < end)
    {
unknown's avatar
unknown committed
1161 1162 1163
      if ((l= my_ismbchar(res->charset(),ptr,end)))
      {
        tmp-= l;
1164
        DBUG_ASSERT(tmp >= tmp_value.ptr());
unknown's avatar
unknown committed
1165 1166 1167
        memcpy(tmp,ptr,l);
        ptr+= l;
      }
unknown's avatar
unknown committed
1168
      else
unknown's avatar
unknown committed
1169
        *--tmp= *ptr++;
unknown's avatar
unknown committed
1170 1171 1172 1173 1174 1175
    }
  }
  else
#endif /* USE_MB */
  {
    while (ptr < end)
unknown's avatar
unknown committed
1176
      *--tmp= *ptr++;
unknown's avatar
unknown committed
1177
  }
unknown's avatar
unknown committed
1178
  return &tmp_value;
unknown's avatar
unknown committed
1179 1180 1181 1182 1183
}


void Item_func_reverse::fix_length_and_dec()
{
1184
  agg_arg_charsets_for_string_result(collation, args, 1);
1185
  DBUG_ASSERT(collation.collation != NULL);
1186
  fix_char_length(args[0]->max_char_length());
unknown's avatar
unknown committed
1187 1188
}

unknown's avatar
unknown committed
1189 1190 1191 1192
/**
  Replace all occurences of string2 in string1 with string3.

  Don't reallocate val_str() if not needed.
unknown's avatar
unknown committed
1193

unknown's avatar
unknown committed
1194 1195 1196
  @todo
    Fix that this works with binary strings when using USE_MB 
*/
unknown's avatar
unknown committed
1197 1198 1199

String *Item_func_replace::val_str(String *str)
{
1200
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
1201
  String *res,*res2,*res3;
unknown's avatar
unknown committed
1202
  int offset;
unknown's avatar
unknown committed
1203 1204 1205 1206 1207
  uint from_length,to_length;
  bool alloced=0;
#ifdef USE_MB
  const char *ptr,*end,*strend,*search,*search_end;
  register uint32 l;
1208
  bool binary_cmp;
unknown's avatar
unknown committed
1209
#endif
1210
  THD *thd= 0;
unknown's avatar
unknown committed
1211 1212 1213 1214 1215 1216 1217 1218 1219

  null_value=0;
  res=args[0]->val_str(str);
  if (args[0]->null_value)
    goto null;
  res2=args[1]->val_str(&tmp_value);
  if (args[1]->null_value)
    goto null;

1220 1221
  res->set_charset(collation.collation);

1222
#ifdef USE_MB
1223
  binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
1224 1225
#endif

unknown's avatar
unknown committed
1226 1227 1228 1229 1230 1231
  if (res2->length() == 0)
    return res;
#ifndef USE_MB
  if ((offset=res->strstr(*res2)) < 0)
    return res;
#else
unknown's avatar
unknown committed
1232
  offset=0;
1233
  if (binary_cmp && (offset=res->strstr(*res2)) < 0)
unknown's avatar
unknown committed
1234 1235 1236 1237 1238 1239 1240 1241
    return res;
#endif
  if (!(res3=args[2]->val_str(&tmp_value2)))
    goto null;
  from_length= res2->length();
  to_length=   res3->length();

#ifdef USE_MB
1242
  if (!binary_cmp)
unknown's avatar
unknown committed
1243 1244 1245 1246
  {
    search=res2->ptr();
    search_end=search+from_length;
redo:
1247
    DBUG_ASSERT(res->ptr() || !offset);
unknown's avatar
unknown committed
1248 1249
    ptr=res->ptr()+offset;
    strend=res->ptr()+res->length();
1250 1251 1252 1253 1254 1255
    /*
      In some cases val_str() can return empty string
      with ptr() == NULL and length() == 0.
      Let's check strend to avoid overflow.
    */
    end= strend ? strend - from_length + 1 : NULL;
unknown's avatar
unknown committed
1256 1257
    while (ptr < end)
    {
1258 1259 1260 1261 1262 1263 1264
      if (*ptr == *search)
      {
        register char *i,*j;
        i=(char*) ptr+1; j=(char*) search+1;
        while (j != search_end)
          if (*i++ != *j++) goto skip;
        offset= (int) (ptr-res->ptr());
1265 1266 1267 1268

        if (!thd)
          thd= current_thd;

1269
        if (res->length()-from_length + to_length >
1270
            thd->variables.max_allowed_packet)
unknown's avatar
unknown committed
1271
        {
1272 1273 1274 1275 1276
          push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                              ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                              ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                              func_name(),
                              thd->variables.max_allowed_packet);
1277

1278
          goto null;
unknown's avatar
unknown committed
1279
        }
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
        if (!alloced)
        {
          alloced=1;
          res=copy_if_not_alloced(str,res,res->length()+to_length);
        }
        res->replace((uint) offset,from_length,*res3);
        offset+=(int) to_length;
        goto redo;
      }
  skip:
      if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
      else ++ptr;
unknown's avatar
unknown committed
1292 1293 1294 1295
    }
  }
  else
#endif /* USE_MB */
1296
  {
1297
    thd= current_thd;
unknown's avatar
unknown committed
1298 1299
    do
    {
unknown's avatar
unknown committed
1300
      if (res->length()-from_length + to_length >
1301
	  thd->variables.max_allowed_packet)
1302
      {
1303
	push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
1304
			    ER_WARN_ALLOWED_PACKET_OVERFLOWED,
1305 1306 1307
			    ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                            func_name(),
			    thd->variables.max_allowed_packet);
unknown's avatar
unknown committed
1308
        goto null;
1309
      }
unknown's avatar
unknown committed
1310 1311 1312 1313 1314 1315 1316 1317
      if (!alloced)
      {
        alloced=1;
        res=copy_if_not_alloced(str,res,res->length()+to_length);
      }
      res->replace((uint) offset,from_length,*res3);
      offset+=(int) to_length;
    }
1318
    while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
1319
  }
unknown's avatar
unknown committed
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
  return res;

null:
  null_value=1;
  return 0;
}


void Item_func_replace::fix_length_and_dec()
{
1330 1331 1332
  ulonglong char_length= (ulonglong) args[0]->max_char_length();
  int diff=(int) (args[2]->max_char_length() - args[1]->max_char_length());
  if (diff > 0 && args[1]->max_char_length())
unknown's avatar
unknown committed
1333
  {						// Calculate of maxreplaces
1334 1335
    ulonglong max_substrs= char_length / args[1]->max_char_length();
    char_length+= max_substrs * (uint) diff;
unknown's avatar
unknown committed
1336
  }
1337

1338
  if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 3))
unknown's avatar
unknown committed
1339
    return;
1340
  fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
1341 1342 1343
}


1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
/*********************************************************************/
void Item_func_regexp_replace::fix_length_and_dec()
{
  if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 3))
    return;
  max_length= MAX_BLOB_WIDTH;
  re.init(collation.collation, 0, 10);
  re.fix_owner(this, args[0], args[1]);
}


/*
  Traverse through the replacement string and append to "str".
  Sub-pattern references \0 .. \9 are recognized, which are replaced
  to the chunks of the source string.
*/
bool Item_func_regexp_replace::append_replacement(String *str,
                                                  const LEX_CSTRING *source,
                                                  const LEX_CSTRING *replace)
{
  const char *beg= replace->str;
  const char *end= beg + replace->length;
  CHARSET_INFO *cs= re.library_charset();

  for ( ; ; )
  {
    my_wc_t wc;
    int cnv, n;

    if ((cnv= cs->cset->mb_wc(cs, &wc, (const uchar *) beg,
                                       (const uchar *) end)) < 1)
      break; /* End of line */
    beg+= cnv;

    if (wc != '\\')
    {
      if (str->append(beg - cnv, cnv, cs))
        return true;
      continue;
    }

    if ((cnv= cs->cset->mb_wc(cs, &wc, (const uchar *) beg,
                                       (const uchar *) end)) < 1)
      break; /* End of line */
    beg+= cnv;

1390
    if ((n= ((int) wc) - '0') >= 0 && n <= 9)
1391
    {
1392 1393 1394 1395 1396 1397 1398
      if (n < re.nsubpatterns())
      {
        /* A valid sub-pattern reference found */
        int pbeg= re.subpattern_start(n), plength= re.subpattern_end(n) - pbeg;
        if (str->append(source->str + pbeg, plength, cs))
          return true;
      }
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527
    }
    else
    {
      /*
         A non-digit character following after '\'.
         Just add the character itself.
       */
      if (str->append(beg - cnv, cnv, cs))
        return false;
    }
  }
  return false;
}


String *Item_func_regexp_replace::val_str(String *str)
{
  DBUG_ASSERT(fixed == 1);
  char buff0[MAX_FIELD_WIDTH];
  char buff2[MAX_FIELD_WIDTH];
  String tmp0(buff0,sizeof(buff0),&my_charset_bin);
  String tmp2(buff2,sizeof(buff2),&my_charset_bin);
  String *source= args[0]->val_str(&tmp0);
  String *replace= args[2]->val_str(&tmp2);
  LEX_CSTRING src, rpl;
  int startoffset= 0;

  if ((null_value= (args[0]->null_value || args[2]->null_value ||
                    re.recompile(args[1]))))
    return (String *) 0;

  if (!(source= re.convert_if_needed(source, &re.subject_converter)) ||
      !(replace= re.convert_if_needed(replace, &re.replace_converter)))
    goto err;

  src= source->lex_cstring();
  rpl= replace->lex_cstring();

  str->length(0);
  str->set_charset(collation.collation);

  for ( ; ; ) // Iterate through all matches
  {

    if (re.exec(src.str, src.length, startoffset))
      goto err;

    if (!re.match() || re.subpattern_length(0) == 0)
    {
      /* 
        No match or an empty match.
        Append the rest of the source string
        starting from startoffset until the end of the source.
      */
      if (str->append(src.str + startoffset, src.length - startoffset, re.library_charset()))
        goto err;
      return str;
    }

    /*
      Append prefix, the part before the matching pattern.
      starting from startoffset until the next match
    */
    if (str->append(src.str + startoffset, re.subpattern_start(0) - startoffset, re.library_charset()))
      goto err;

    // Append replacement
    if (append_replacement(str, &src, &rpl))
      goto err;

    // Set the new start point as the end of previous match
    startoffset= re.subpattern_end(0);
  }
  return str;

err:
  null_value= true;
  return (String *) 0;
}


void Item_func_regexp_substr::fix_length_and_dec()
{
  if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 2))
    return;
  fix_char_length(args[0]->max_char_length());
  re.init(collation.collation, 0, 10);
  re.fix_owner(this, args[0], args[1]);
}


String *Item_func_regexp_substr::val_str(String *str)
{
  DBUG_ASSERT(fixed == 1);
  char buff0[MAX_FIELD_WIDTH];
  String tmp0(buff0,sizeof(buff0),&my_charset_bin);
  String *source= args[0]->val_str(&tmp0);

  if ((null_value= (args[0]->null_value || re.recompile(args[1]))))
    return (String *) 0;

  if (!(source= re.convert_if_needed(source, &re.subject_converter)))
    goto err;

  str->length(0);
  str->set_charset(collation.collation);

  if (re.exec(source->ptr(), source->length(), 0))
    goto err;

  if (!re.match())
    return str;

  if (str->append(source->ptr() + re.subpattern_start(0),
                  re.subpattern_end(0) - re.subpattern_start(0),
                  re.library_charset()))
    goto err;

  return str;

err:
  null_value= true;
  return (String *) 0;
}


/************************************************************************/


unknown's avatar
unknown committed
1528 1529
String *Item_func_insert::val_str(String *str)
{
1530
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
1531
  String *res,*res2;
1532
  longlong start, length;  /* must be longlong to avoid truncation */
unknown's avatar
unknown committed
1533 1534 1535 1536

  null_value=0;
  res=args[0]->val_str(str);
  res2=args[3]->val_str(&tmp_value);
1537 1538 1539
  start= args[1]->val_int() - 1;
  length= args[2]->val_int();

unknown's avatar
unknown committed
1540 1541 1542
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
      args[3]->null_value)
    goto null; /* purecov: inspected */
1543

unknown's avatar
unknown committed
1544
  if ((start < 0) || (start > res->length()))
1545
    return res;                                 // Wrong param; skip insert
unknown's avatar
unknown committed
1546 1547
  if ((length < 0) || (length > res->length()))
    length= res->length();
1548

1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
  /*
    There is one exception not handled (intentionaly) by the character set
    aggregation code. If one string is strong side and is binary, and
    another one is weak side and is a multi-byte character string,
    then we need to operate on the second string in terms on bytes when
    calling ::numchars() and ::charpos(), rather than in terms of characters.
    Lets substitute its character set to binary.
  */
  if (collation.collation == &my_charset_bin)
  {
    res->set_charset(&my_charset_bin);
    res2->set_charset(&my_charset_bin);
  }

1563
  /* start and length are now sufficiently valid to pass to charpos function */
1564 1565
   start= res->charpos((int) start);
   length= res->charpos((int) length, (uint32) start);
1566 1567

  /* Re-testing with corrected params */
1568
  if (start + 1 > res->length()) // remember, start = args[1].val_int() - 1
unknown's avatar
unknown committed
1569
    return res; /* purecov: inspected */        // Wrong param; skip insert
1570 1571 1572
  if (length > res->length() - start)
    length= res->length() - start;

1573
  {
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
    THD *thd= current_thd;
    if ((ulonglong) (res->length() - length + res2->length()) >
        (ulonglong) thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(), thd->variables.max_allowed_packet);
      goto null;
    }
1584
  }
unknown's avatar
unknown committed
1585
  res=copy_if_not_alloced(str,res,res->length());
1586
  res->replace((uint32) start,(uint32) length,*res2);
unknown's avatar
unknown committed
1587 1588 1589 1590 1591 1592 1593 1594 1595
  return res;
null:
  null_value=1;
  return 0;
}


void Item_func_insert::fix_length_and_dec()
{
1596
  ulonglong char_length;
1597

1598
  // Handle character set for args[0] and args[3].
1599
  if (agg_arg_charsets_for_string_result(collation, args, 2, 3))
unknown's avatar
unknown committed
1600
    return;
1601 1602 1603
  char_length= ((ulonglong) args[0]->max_char_length() +
                (ulonglong) args[3]->max_char_length());
  fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
1604 1605 1606
}


1607
String *Item_str_conv::val_str(String *str)
unknown's avatar
unknown committed
1608
{
1609
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
1610 1611 1612 1613 1614 1615 1616
  String *res;
  if (!(res=args[0]->val_str(str)))
  {
    null_value=1; /* purecov: inspected */
    return 0; /* purecov: inspected */
  }
  null_value=0;
1617
  if (multiply == 1)
unknown's avatar
unknown committed
1618
  {
1619
    uint len;
1620
    res= copy_if_not_alloced(&tmp_value, res, res->length());
1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
                                        (char*) res->ptr(), res->length());
    DBUG_ASSERT(len <= res->length());
    res->length(len);
  }
  else
  {
    uint len= res->length() * multiply;
    tmp_value.alloc(len);
    tmp_value.set_charset(collation.collation);
    len= converter(collation.collation, (char*) res->ptr(), res->length(),
                                        (char*) tmp_value.ptr(), len);
    tmp_value.length(len);
    res= &tmp_value;
unknown's avatar
unknown committed
1635 1636 1637 1638 1639
  }
  return res;
}


1640 1641
void Item_func_lcase::fix_length_and_dec()
{
1642
  agg_arg_charsets_for_string_result(collation, args, 1);
1643
  DBUG_ASSERT(collation.collation != NULL);
1644 1645
  multiply= collation.collation->casedn_multiply;
  converter= collation.collation->cset->casedn;
1646
  fix_char_length_ulonglong((ulonglong) args[0]->max_char_length() * multiply);
1647 1648 1649 1650
}

void Item_func_ucase::fix_length_and_dec()
{
1651
  agg_arg_charsets_for_string_result(collation, args, 1);
1652
  DBUG_ASSERT(collation.collation != NULL);
1653 1654
  multiply= collation.collation->caseup_multiply;
  converter= collation.collation->cset->caseup;
1655
  fix_char_length_ulonglong((ulonglong) args[0]->max_char_length() * multiply);
1656 1657 1658
}


unknown's avatar
unknown committed
1659 1660
String *Item_func_left::val_str(String *str)
{
1661
  DBUG_ASSERT(fixed == 1);
1662 1663 1664 1665
  String *res= args[0]->val_str(str);

  /* must be longlong to avoid truncation */
  longlong length= args[1]->val_int();
1666
  uint char_pos;
unknown's avatar
unknown committed
1667

unknown's avatar
unknown committed
1668
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
unknown's avatar
unknown committed
1669
    return 0;
1670 1671 1672

  /* if "unsigned_flag" is set, we have a *huge* positive number. */
  if ((length <= 0) && (!args[1]->unsigned_flag))
1673
    return make_empty_result();
1674
  if ((res->length() <= (ulonglong) length) ||
1675
      (res->length() <= (char_pos= res->charpos((int) length))))
unknown's avatar
a fix  
unknown committed
1676
    return res;
1677 1678 1679

  tmp_value.set(*res, 0, char_pos);
  return &tmp_value;
unknown's avatar
unknown committed
1680 1681 1682 1683 1684
}


void Item_str_func::left_right_max_length()
{
1685
  uint32 char_length= args[0]->max_char_length();
unknown's avatar
unknown committed
1686 1687
  if (args[1]->const_item())
  {
1688
    int length= (int) args[1]->val_int();
1689
    if (args[1]->null_value || length <= 0)
1690
      char_length=0;
unknown's avatar
unknown committed
1691
    else
1692
      set_if_smaller(char_length, (uint) length);
unknown's avatar
unknown committed
1693
  }
1694
  fix_char_length(char_length);
unknown's avatar
unknown committed
1695 1696 1697 1698 1699
}


void Item_func_left::fix_length_and_dec()
{
1700
  agg_arg_charsets_for_string_result(collation, args, 1);
1701
  DBUG_ASSERT(collation.collation != NULL);
unknown's avatar
unknown committed
1702 1703 1704 1705 1706 1707
  left_right_max_length();
}


String *Item_func_right::val_str(String *str)
{
1708
  DBUG_ASSERT(fixed == 1);
1709 1710 1711
  String *res= args[0]->val_str(str);
  /* must be longlong to avoid truncation */
  longlong length= args[1]->val_int();
unknown's avatar
unknown committed
1712

unknown's avatar
unknown committed
1713
  if ((null_value=(args[0]->null_value || args[1]->null_value)))
unknown's avatar
unknown committed
1714
    return 0; /* purecov: inspected */
1715 1716 1717

  /* if "unsigned_flag" is set, we have a *huge* positive number. */
  if ((length <= 0) && (!args[1]->unsigned_flag))
1718
    return make_empty_result(); /* purecov: inspected */
1719 1720

  if (res->length() <= (ulonglong) length)
unknown's avatar
unknown committed
1721
    return res; /* purecov: inspected */
unknown's avatar
unknown committed
1722

unknown's avatar
unknown committed
1723 1724
  uint start=res->numchars();
  if (start <= (uint) length)
unknown's avatar
unknown committed
1725
    return res;
unknown's avatar
unknown committed
1726
  start=res->charpos(start - (uint) length);
unknown's avatar
unknown committed
1727
  tmp_value.set(*res,start,res->length()-start);
unknown's avatar
unknown committed
1728 1729 1730 1731 1732 1733
  return &tmp_value;
}


void Item_func_right::fix_length_and_dec()
{
1734
  agg_arg_charsets_for_string_result(collation, args, 1);
1735
  DBUG_ASSERT(collation.collation != NULL);
unknown's avatar
unknown committed
1736 1737 1738 1739 1740 1741
  left_right_max_length();
}


String *Item_func_substr::val_str(String *str)
{
1742
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
1743
  String *res  = args[0]->val_str(str);
1744 1745 1746 1747 1748 1749
  /* must be longlong to avoid truncation */
  longlong start= args[1]->val_int();
  /* Assumes that the maximum length of a String is < INT_MAX32. */
  /* Limit so that code sees out-of-bound value properly. */
  longlong length= arg_count == 3 ? args[2]->val_int() : INT_MAX32;
  longlong tmp_length;
unknown's avatar
unknown committed
1750 1751 1752 1753

  if ((null_value=(args[0]->null_value || args[1]->null_value ||
		   (arg_count == 3 && args[2]->null_value))))
    return 0; /* purecov: inspected */
1754

1755 1756 1757
  /* Negative or zero length, will return empty string. */
  if ((arg_count == 3) && (length <= 0) && 
      (length == 0 || !args[2]->unsigned_flag))
1758
    return make_empty_result();
unknown's avatar
unknown committed
1759

1760 1761 1762 1763 1764 1765 1766
  /* Assumes that the maximum length of a String is < INT_MAX32. */
  /* Set here so that rest of code sees out-of-bound value as such. */
  if ((length <= 0) || (length > INT_MAX32))
    length= INT_MAX32;

  /* if "unsigned_flag" is set, we have a *huge* positive number. */
  /* Assumes that the maximum length of a String is < INT_MAX32. */
1767 1768
  if ((!args[1]->unsigned_flag && (start < INT_MIN32 || start > INT_MAX32)) ||
      (args[1]->unsigned_flag && ((ulonglong) start > INT_MAX32)))
1769
    return make_empty_result();
unknown's avatar
unknown committed
1770

1771
  start= ((start < 0) ? res->numchars() + start : start - 1);
1772
  start= res->charpos((int) start);
1773
  if ((start < 0) || ((uint) start + 1 > res->length()))
1774
    return make_empty_result();
1775

1776
  length= res->charpos((int) length, (uint32) start);
1777
  tmp_length= res->length() - start;
1778
  length= MY_MIN(length, tmp_length);
1779

1780
  if (!start && (longlong) res->length() == length)
unknown's avatar
unknown committed
1781
    return res;
1782
  tmp_value.set(*res, (uint32) start, (uint32) length);
unknown's avatar
unknown committed
1783 1784 1785 1786 1787 1788 1789 1790
  return &tmp_value;
}


void Item_func_substr::fix_length_and_dec()
{
  max_length=args[0]->max_length;

1791
  agg_arg_charsets_for_string_result(collation, args, 1);
1792
  DBUG_ASSERT(collation.collation != NULL);
unknown's avatar
unknown committed
1793 1794
  if (args[1]->const_item())
  {
1795
    int32 start= (int32) args[1]->val_int();
1796 1797 1798
    if (args[1]->null_value)
      max_length= 0;
    else if (start < 0)
1799
      max_length= ((uint)(-start) > max_length) ? 0 : (uint)(-start);
unknown's avatar
unknown committed
1800
    else
1801
      max_length-= MY_MIN((uint)(start - 1), max_length);
unknown's avatar
unknown committed
1802 1803 1804
  }
  if (arg_count == 3 && args[2]->const_item())
  {
1805
    int32 length= (int32) args[2]->val_int();
1806
    if (args[2]->null_value || length <= 0)
unknown's avatar
unknown committed
1807 1808 1809 1810
      max_length=0; /* purecov: inspected */
    else
      set_if_smaller(max_length,(uint) length);
  }
1811
  max_length*= collation.collation->mbmaxlen;
unknown's avatar
unknown committed
1812 1813 1814
}


1815 1816
void Item_func_substr_index::fix_length_and_dec()
{ 
1817
  if (agg_arg_charsets_for_string_result_with_comparison(collation, args, 2))
unknown's avatar
unknown committed
1818
    return;
1819
  fix_char_length(args[0]->max_char_length());
1820 1821 1822
}


unknown's avatar
unknown committed
1823 1824
String *Item_func_substr_index::val_str(String *str)
{
1825
  DBUG_ASSERT(fixed == 1);
1826 1827
  char buff[MAX_FIELD_WIDTH];
  String tmp(buff,sizeof(buff),system_charset_info);
1828
  String *res= args[0]->val_str(str);
1829
  String *delimiter= args[1]->val_str(&tmp);
1830
  int32 count= (int32) args[2]->val_int();
unknown's avatar
unknown committed
1831 1832 1833 1834 1835 1836 1837 1838
  uint offset;

  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
  {					// string and/or delim are null
    null_value=1;
    return 0;
  }
  null_value=0;
1839 1840
  uint delimiter_length= delimiter->length();
  if (!res->length() || !delimiter_length || !count)
1841
    return make_empty_result();		// Wrong parameters
unknown's avatar
unknown committed
1842

1843 1844
  res->set_charset(collation.collation);

unknown's avatar
unknown committed
1845
#ifdef USE_MB
1846
  if (use_mb(res->charset()))
unknown's avatar
unknown committed
1847
  {
1848 1849 1850 1851 1852
    const char *ptr= res->ptr();
    const char *strend= ptr+res->length();
    const char *end= strend-delimiter_length+1;
    const char *search= delimiter->ptr();
    const char *search_end= search+delimiter_length;
unknown's avatar
unknown committed
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
    int32 n=0,c=count,pass;
    register uint32 l;
    for (pass=(count>0);pass<2;++pass)
    {
      while (ptr < end)
      {
        if (*ptr == *search)
        {
	  register char *i,*j;
	  i=(char*) ptr+1; j=(char*) search+1;
	  while (j != search_end)
1864
	    if (*i++ != *j++) goto skip;
unknown's avatar
unknown committed
1865 1866
	  if (pass==0) ++n;
	  else if (!--c) break;
1867
	  ptr+= delimiter_length;
unknown's avatar
unknown committed
1868 1869
	  continue;
	}
1870
    skip:
1871
        if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
unknown's avatar
unknown committed
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
        else ++ptr;
      } /* either not found or got total number when count<0 */
      if (pass == 0) /* count<0 */
      {
        c+=n+1;
        if (c<=0) return res; /* not found, return original string */
        ptr=res->ptr();
      }
      else
      {
        if (c) return res; /* Not found, return original string */
        if (count>0) /* return left part */
        {
unknown's avatar
unknown committed
1885
	  tmp_value.set(*res,0,(ulong) (ptr-res->ptr()));
unknown's avatar
unknown committed
1886 1887 1888
        }
        else /* return right part */
        {
1889
	  ptr+= delimiter_length;
unknown's avatar
unknown committed
1890
	  tmp_value.set(*res,(ulong) (ptr-res->ptr()), (ulong) (strend-ptr));
unknown's avatar
unknown committed
1891 1892 1893 1894 1895 1896 1897 1898 1899
        }
      }
    }
  }
  else
#endif /* USE_MB */
  {
    if (count > 0)
    {					// start counting from the beginning
1900
      for (offset=0; ; offset+= delimiter_length)
unknown's avatar
unknown committed
1901
      {
1902
	if ((int) (offset= res->strstr(*delimiter, offset)) < 0)
unknown's avatar
unknown committed
1903 1904 1905 1906 1907 1908 1909 1910 1911
	  return res;			// Didn't find, return org string
	if (!--count)
	{
	  tmp_value.set(*res,0,offset);
	  break;
	}
      }
    }
    else
1912
    {
unknown's avatar
unknown committed
1913 1914 1915 1916
      /*
        Negative index, start counting at the end
      */
      for (offset=res->length(); offset ;)
unknown's avatar
unknown committed
1917
      {
unknown's avatar
unknown committed
1918 1919 1920 1921 1922
        /* 
          this call will result in finding the position pointing to one 
          address space less than where the found substring is located
          in res
        */
1923
	if ((int) (offset= res->strrstr(*delimiter, offset)) < 0)
unknown's avatar
unknown committed
1924
	  return res;			// Didn't find, return org string
unknown's avatar
unknown committed
1925 1926 1927 1928
        /*
          At this point, we've searched for the substring
          the number of times as supplied by the index value
        */
unknown's avatar
unknown committed
1929 1930
	if (!++count)
	{
1931
	  offset+= delimiter_length;
unknown's avatar
unknown committed
1932 1933 1934 1935
	  tmp_value.set(*res,offset,res->length()- offset);
	  break;
	}
      }
1936 1937
      if (count)
        return res;                     // Didn't find, return org string
unknown's avatar
unknown committed
1938 1939
    }
  }
1940 1941 1942 1943 1944 1945
  /*
    We always mark tmp_value as const so that if val_str() is called again
    on this object, we don't disrupt the contents of tmp_value when it was
    derived from another String.
  */
  tmp_value.mark_as_const();
unknown's avatar
unknown committed
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
  return (&tmp_value);
}

/*
** The trim functions are extension to ANSI SQL because they trim substrings
** They ltrim() and rtrim() functions are optimized for 1 byte strings
** They also return the original string if possible, else they return
** a substring that points at the original string.
*/


String *Item_func_ltrim::val_str(String *str)
{
1959
  DBUG_ASSERT(fixed == 1);
1960 1961 1962
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
  String tmp(buff,sizeof(buff),system_charset_info);
  String *res, *remove_str;
1963
  uint UNINIT_VAR(remove_length);
unknown's avatar
unknown committed
1964

1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
  res= args[0]->val_str(str);
  if ((null_value=args[0]->null_value))
    return 0;
  remove_str= &remove;                          /* Default value. */
  if (arg_count == 2)
  {
    remove_str= args[1]->val_str(&tmp);
    if ((null_value= args[1]->null_value))
      return 0;
  }

  if ((remove_length= remove_str->length()) == 0 ||
unknown's avatar
unknown committed
1977
      remove_length > res->length())
1978
    return non_trimmed_value(res);
unknown's avatar
unknown committed
1979

1980 1981
  ptr= (char*) res->ptr();
  end= ptr+res->length();
unknown's avatar
unknown committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991
  if (remove_length == 1)
  {
    char chr=(*remove_str)[0];
    while (ptr != end && *ptr == chr)
      ptr++;
  }
  else
  {
    const char *r_ptr=remove_str->ptr();
    end-=remove_length;
1992
    while (ptr <= end && !memcmp(ptr, r_ptr, remove_length))
unknown's avatar
unknown committed
1993 1994 1995 1996
      ptr+=remove_length;
    end+=remove_length;
  }
  if (ptr == res->ptr())
1997 1998
    return non_trimmed_value(res);
  return trimmed_value(res, (uint32) (ptr - res->ptr()), (uint32) (end - ptr));
unknown's avatar
unknown committed
1999 2000 2001 2002 2003
}


String *Item_func_rtrim::val_str(String *str)
{
2004
  DBUG_ASSERT(fixed == 1);
2005 2006 2007
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
  String tmp(buff, sizeof(buff), system_charset_info);
  String *res, *remove_str;
2008
  uint UNINIT_VAR(remove_length);
unknown's avatar
unknown committed
2009

2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
  res= args[0]->val_str(str);
  if ((null_value=args[0]->null_value))
    return 0;
  remove_str= &remove;                          /* Default value. */
  if (arg_count == 2)
  {
    remove_str= args[1]->val_str(&tmp);
    if ((null_value= args[1]->null_value))
      return 0;
  }

  if ((remove_length= remove_str->length()) == 0 ||
unknown's avatar
unknown committed
2022
      remove_length > res->length())
2023
    return non_trimmed_value(res);
unknown's avatar
unknown committed
2024

2025 2026
  ptr= (char*) res->ptr();
  end= ptr+res->length();
unknown's avatar
unknown committed
2027 2028 2029 2030 2031 2032 2033 2034
#ifdef USE_MB
  char *p=ptr;
  register uint32 l;
#endif
  if (remove_length == 1)
  {
    char chr=(*remove_str)[0];
#ifdef USE_MB
2035
    if (use_mb(collation.collation))
unknown's avatar
unknown committed
2036 2037 2038
    {
      while (ptr < end)
      {
2039
	if ((l= my_ismbchar(collation.collation, ptr, end))) ptr+= l, p=ptr;
unknown's avatar
unknown committed
2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
	else ++ptr;
      }
      ptr=p;
    }
#endif
    while (ptr != end  && end[-1] == chr)
      end--;
  }
  else
  {
    const char *r_ptr=remove_str->ptr();
#ifdef USE_MB
2052
    if (use_mb(collation.collation))
unknown's avatar
unknown committed
2053 2054 2055 2056
    {
  loop:
      while (ptr + remove_length < end)
      {
2057
	if ((l= my_ismbchar(collation.collation, ptr, end))) ptr+= l;
unknown's avatar
unknown committed
2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
	else ++ptr;
      }
      if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
      {
	end-=remove_length;
	ptr=p;
	goto loop;
      }
    }
    else
#endif /* USE_MB */
    {
2070 2071
      while (ptr + remove_length <= end &&
	     !memcmp(end-remove_length, r_ptr, remove_length))
unknown's avatar
unknown committed
2072 2073 2074 2075
	end-=remove_length;
    }
  }
  if (end == res->ptr()+res->length())
2076 2077
    return non_trimmed_value(res);
  return trimmed_value(res, 0, (uint32) (end - res->ptr()));
unknown's avatar
unknown committed
2078 2079 2080 2081 2082
}


String *Item_func_trim::val_str(String *str)
{
2083
  DBUG_ASSERT(fixed == 1);
2084 2085 2086 2087
  char buff[MAX_FIELD_WIDTH], *ptr, *end;
  const char *r_ptr;
  String tmp(buff, sizeof(buff), system_charset_info);
  String *res, *remove_str;
2088
  uint UNINIT_VAR(remove_length);
unknown's avatar
unknown committed
2089

2090 2091 2092 2093
  res= args[0]->val_str(str);
  if ((null_value=args[0]->null_value))
    return 0;
  remove_str= &remove;                          /* Default value. */
unknown's avatar
unknown committed
2094 2095 2096 2097 2098 2099
  if (arg_count == 2)
  {
    remove_str= args[1]->val_str(&tmp);
    if ((null_value= args[1]->null_value))
      return 0;
  }
unknown's avatar
unknown committed
2100

2101
  if ((remove_length= remove_str->length()) == 0 ||
unknown's avatar
unknown committed
2102
      remove_length > res->length())
2103
    return non_trimmed_value(res);
unknown's avatar
unknown committed
2104

2105 2106 2107
  ptr= (char*) res->ptr();
  end= ptr+res->length();
  r_ptr= remove_str->ptr();
2108 2109
  while (ptr+remove_length <= end && !memcmp(ptr,r_ptr,remove_length))
    ptr+=remove_length;
unknown's avatar
unknown committed
2110
#ifdef USE_MB
2111
  if (use_mb(collation.collation))
unknown's avatar
unknown committed
2112 2113 2114 2115 2116 2117
  {
    char *p=ptr;
    register uint32 l;
 loop:
    while (ptr + remove_length < end)
    {
2118
      if ((l= my_ismbchar(collation.collation, ptr, end)))
2119 2120 2121
        ptr+= l;
      else
        ++ptr;
unknown's avatar
unknown committed
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
    }
    if (ptr + remove_length == end && !memcmp(ptr,r_ptr,remove_length))
    {
      end-=remove_length;
      ptr=p;
      goto loop;
    }
    ptr=p;
  }
  else
#endif /* USE_MB */
  {
2134
    while (ptr + remove_length <= end &&
unknown's avatar
unknown committed
2135 2136 2137 2138
	   !memcmp(end-remove_length,r_ptr,remove_length))
      end-=remove_length;
  }
  if (ptr == res->ptr() && end == ptr+res->length())
2139 2140
    return non_trimmed_value(res);
  return trimmed_value(res, (uint32) (ptr - res->ptr()), (uint32) (end - ptr));
unknown's avatar
unknown committed
2141 2142
}

2143 2144 2145 2146
void Item_func_trim::fix_length_and_dec()
{
  if (arg_count == 1)
  {
2147
    agg_arg_charsets_for_string_result(collation, args, 1);
2148
    DBUG_ASSERT(collation.collation != NULL);
2149
    remove.set_charset(collation.collation);
2150 2151 2152 2153
    remove.set_ascii(" ",1);
  }
  else
  {
2154 2155
    // Handle character set for args[1] and args[0].
    // Note that we pass args[1] as the first item, and args[0] as the second.
2156 2157
    if (agg_arg_charsets_for_string_result_with_comparison(collation,
                                                           &args[1], 2, -1))
unknown's avatar
unknown committed
2158
      return;
2159
  }
2160
  fix_char_length(args[0]->max_char_length());
2161 2162
}

2163
void Item_func_trim::print(String *str, enum_query_type query_type)
2164 2165 2166
{
  if (arg_count == 1)
  {
2167
    Item_func::print(str, query_type);
2168 2169 2170 2171 2172 2173
    return;
  }
  str->append(Item_func_trim::func_name());
  str->append('(');
  str->append(mode_name());
  str->append(' ');
2174
  args[1]->print(str, query_type);
2175
  str->append(STRING_WITH_LEN(" from "));
2176
  args[0]->print(str, query_type);
2177 2178 2179
  str->append(')');
}

2180

2181
/* Item_func_password */
unknown's avatar
unknown committed
2182

2183
bool Item_func_password::fix_fields(THD *thd, Item **ref)
unknown's avatar
unknown committed
2184
{
2185 2186 2187
  if (deflt)
    alg= (thd->variables.old_passwords ? OLD : NEW);
  return Item_str_ascii_func::fix_fields(thd, ref);
unknown's avatar
unknown committed
2188 2189
}

2190
String *Item_func_password::val_str_ascii(String *str)
2191
{
2192
  DBUG_ASSERT(fixed == 1);
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211
  String *res= args[0]->val_str(str); 
  switch (alg){
  case NEW:
    if (args[0]->null_value || res->length() == 0)
      return make_empty_result();
    my_make_scrambled_password(tmp_value, res->ptr(), res->length());
    str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH, &my_charset_latin1);
    break;
  case OLD:
    if ((null_value=args[0]->null_value))
      return 0;
    if (res->length() == 0)
      return make_empty_result();
    my_make_scrambled_password_323(tmp_value, res->ptr(), res->length());
    str->set(tmp_value, SCRAMBLED_PASSWORD_CHAR_LENGTH_323, &my_charset_latin1);
    break;
  default:
    DBUG_ASSERT(0);
  }
2212 2213 2214
  return str;
}

2215 2216
char *Item_func_password::alloc(THD *thd, const char *password,
                                size_t pass_len, enum PW_Alg al)
unknown's avatar
unknown committed
2217
{
2218 2219 2220 2221 2222 2223 2224 2225
  char *buff= (char *) thd->alloc((al==NEW)?
                                  SCRAMBLED_PASSWORD_CHAR_LENGTH + 1:
                                  SCRAMBLED_PASSWORD_CHAR_LENGTH_323 + 1);
  if (!buff)
    return NULL;

  switch (al) {
  case NEW:
Sergei Golubchik's avatar
Sergei Golubchik committed
2226 2227
    my_make_scrambled_password(buff, password, pass_len);
    break;
2228
  case OLD:
2229
    my_make_scrambled_password_323(buff, password, pass_len);
2230 2231 2232 2233
    break;
  default:
    DBUG_ASSERT(0);
  }
unknown's avatar
unknown committed
2234 2235
  return buff;
}
2236 2237


2238

unknown's avatar
unknown committed
2239
#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
unknown's avatar
unknown committed
2240 2241 2242

String *Item_func_encrypt::val_str(String *str)
{
2243
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2244 2245 2246 2247 2248 2249 2250
  String *res  =args[0]->val_str(str);

#ifdef HAVE_CRYPT
  char salt[3],*salt_ptr;
  if ((null_value=args[0]->null_value))
    return 0;
  if (res->length() == 0)
2251
    return make_empty_result();
unknown's avatar
unknown committed
2252 2253 2254
  if (arg_count == 1)
  {					// generate random salt
    time_t timestamp=current_thd->query_start();
unknown's avatar
unknown committed
2255 2256
    salt[0] = bin_to_ascii( (ulong) timestamp & 0x3f);
    salt[1] = bin_to_ascii(( (ulong) timestamp >> 5) & 0x3f);
unknown's avatar
unknown committed
2257 2258 2259 2260 2261 2262 2263 2264
    salt[2] = 0;
    salt_ptr=salt;
  }
  else
  {					// obtain salt from the first two bytes
    String *salt_str=args[1]->val_str(&tmp_value);
    if ((null_value= (args[1]->null_value || salt_str->length() < 2)))
      return 0;
2265
    salt_ptr= salt_str->c_ptr_safe();
unknown's avatar
unknown committed
2266
  }
Marc Alff's avatar
Marc Alff committed
2267
  mysql_mutex_lock(&LOCK_crypt);
2268
  char *tmp= crypt(res->c_ptr_safe(),salt_ptr);
2269 2270
  if (!tmp)
  {
Marc Alff's avatar
Marc Alff committed
2271
    mysql_mutex_unlock(&LOCK_crypt);
2272 2273 2274
    null_value= 1;
    return 0;
  }
2275
  str->set(tmp, (uint) strlen(tmp), &my_charset_bin);
unknown's avatar
unknown committed
2276
  str->copy();
Marc Alff's avatar
Marc Alff committed
2277
  mysql_mutex_unlock(&LOCK_crypt);
unknown's avatar
unknown committed
2278 2279 2280 2281 2282 2283 2284
  return str;
#else
  null_value=1;
  return 0;
#endif	/* HAVE_CRYPT */
}

2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299
bool Item_func_encode::seed()
{
  char buf[80];
  ulong rand_nr[2];
  String *key, tmp(buf, sizeof(buf), system_charset_info);

  if (!(key= args[1]->val_str(&tmp)))
    return TRUE;

  hash_password(rand_nr, key->ptr(), key->length());
  sql_crypt.init(rand_nr);

  return FALSE;
}

unknown's avatar
unknown committed
2300 2301 2302
void Item_func_encode::fix_length_and_dec()
{
  max_length=args[0]->max_length;
2303
  maybe_null=args[0]->maybe_null || args[1]->maybe_null;
2304
  collation.set(&my_charset_bin);
2305 2306 2307
  /* Precompute the seed state if the item is constant. */
  seeded= args[1]->const_item() &&
          (args[1]->result_type() == STRING_RESULT) && !seed();
unknown's avatar
unknown committed
2308 2309 2310 2311 2312
}

String *Item_func_encode::val_str(String *str)
{
  String *res;
2313 2314
  DBUG_ASSERT(fixed == 1);

unknown's avatar
unknown committed
2315 2316
  if (!(res=args[0]->val_str(str)))
  {
2317 2318
    null_value= 1;
    return NULL;
unknown's avatar
unknown committed
2319
  }
2320

2321
  if (!seeded && seed())
2322
  {
2323 2324
    null_value= 1;
    return NULL;
2325 2326
  }

2327 2328
  null_value= 0;
  res= copy_if_not_alloced(str, res, res->length());
Davi Arnaut's avatar
Davi Arnaut committed
2329
  crypto_transform(res);
2330 2331
  sql_crypt.reinit();

unknown's avatar
unknown committed
2332 2333 2334
  return res;
}

Davi Arnaut's avatar
Davi Arnaut committed
2335
void Item_func_encode::crypto_transform(String *res)
unknown's avatar
unknown committed
2336
{
2337 2338 2339
  sql_crypt.encode((char*) res->ptr(),res->length());
  res->set_charset(&my_charset_bin);
}
2340

Davi Arnaut's avatar
Davi Arnaut committed
2341
void Item_func_decode::crypto_transform(String *res)
2342
{
unknown's avatar
unknown committed
2343 2344 2345
  sql_crypt.decode((char*) res->ptr(),res->length());
}

2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357
Item *Item_func_sysconst::safe_charset_converter(THD *thd,
                                                 CHARSET_INFO *tocs)
{
  /*
    In default, virtual functions or constraint expressions, the value
    of a sysconst is not constant
  */
  if (thd->in_stored_expression)
    return Item_str_func::safe_charset_converter(thd, tocs);
  return const_charset_converter(thd, tocs, true, fully_qualified_func_name());
}

unknown's avatar
unknown committed
2358 2359 2360

String *Item_func_database::val_str(String *str)
{
2361
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2362
  THD *thd= current_thd;
unknown's avatar
unknown committed
2363
  if (thd->db == NULL)
2364
  {
2365 2366
    null_value= 1;
    return 0;
2367
  }
unknown's avatar
unknown committed
2368
  else
unknown's avatar
unknown committed
2369
    str->copy(thd->db, thd->db_length, system_charset_info);
unknown's avatar
unknown committed
2370 2371 2372
  return str;
}

unknown's avatar
unknown committed
2373

unknown's avatar
unknown committed
2374
/**
2375 2376 2377
  @note USER() is replicated correctly if binlog_format=ROW or (as of
  BUG#28086) binlog_format=MIXED, but is incorrectly replicated to ''
  if binlog_format=STATEMENT.
2378
*/
2379 2380

bool Item_func_user::init(THD *thd, const char *user, const char *host)
unknown's avatar
unknown committed
2381
{
2382
  DBUG_ENTER("Item_func_user::init");
2383
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2384

2385 2386
  /* Check if we have already calculated the value for this thread */
  if (thd->query_id == last_query_id)
2387 2388 2389
    DBUG_RETURN(FALSE);
  DBUG_PRINT("enter", ("user: '%s'  host: '%s'", user,host));

2390 2391 2392
  last_query_id= thd->query_id;
  null_value= 0;

2393 2394
  // For system threads (e.g. replication SQL thread) user may be empty
  if (user)
2395
  {
2396
    CHARSET_INFO *cs= system_charset_info;
2397
    size_t res_length= (strlen(user)+strlen(host)+2) * cs->mbmaxlen;
2398

2399
    if (cached_value.alloc((uint) res_length))
2400 2401
    {
      null_value=1;
2402
      DBUG_RETURN(TRUE);
2403
    }
2404

2405 2406 2407
    cached_value.set_charset(cs);
    res_length=cs->cset->snprintf(cs, (char*)cached_value.ptr(),
                                  (uint) res_length,
2408
                                  "%s@%s", user, host);
2409 2410
    cached_value.length((uint) res_length);
    cached_value.mark_as_const();
2411
  }
2412
  else
2413 2414
    cached_value.set("", 0, system_charset_info);
  DBUG_RETURN(FALSE);
2415 2416
}

2417
String *Item_func_user::val_str(String *str)
2418
{
2419 2420
  THD *thd= current_thd;
  init(thd, thd->main_security_ctx.user, thd->main_security_ctx.host_or_ip);
2421
  return null_value ? 0 : &cached_value;
2422 2423
}

2424
String *Item_func_current_user::val_str(String *str)
2425
{
2426 2427 2428 2429
  THD *thd= current_thd;
  Security_context *ctx= (context->security_ctx ?
                          context->security_ctx : thd->security_ctx);
  init(thd, ctx->priv_user, ctx->priv_host);
2430
  return null_value ? 0 : &cached_value;
unknown's avatar
unknown committed
2431 2432
}

2433

Sergei Golubchik's avatar
Sergei Golubchik committed
2434 2435
bool Item_func_current_role::fix_fields(THD *thd, Item **ref)
{
2436 2437
  return Item_func_sysconst::fix_fields(thd,ref) || init(thd);
}
Sergei Golubchik's avatar
Sergei Golubchik committed
2438

2439 2440
bool Item_func_current_role::init(THD *thd)
{
Sergei Golubchik's avatar
Sergei Golubchik committed
2441 2442 2443
  Security_context *ctx= context->security_ctx
                          ? context->security_ctx : thd->security_ctx;

2444 2445
  if (ctx->priv_role[0])
  {
2446 2447 2448
    if (str_value.copy(ctx->priv_role, strlen(ctx->priv_role),
                       system_charset_info))
      return 1;
Sergei Golubchik's avatar
Sergei Golubchik committed
2449

2450 2451
    return 0;
  }
2452
  null_value= 1;
Sergei Golubchik's avatar
Sergei Golubchik committed
2453 2454 2455
  return 0;
}

2456 2457 2458 2459 2460 2461 2462 2463 2464
String *Item_func_current_role::val_str(String *)
{
  return (null_value ? 0 : &str_value);
}

int Item_func_current_role::save_in_field(Field *field, bool no_conversions)
{
  return save_str_value_in_field(field, &str_value);
}
2465

unknown's avatar
unknown committed
2466 2467
void Item_func_soundex::fix_length_and_dec()
{
2468
  uint32 char_length= args[0]->max_char_length();
2469
  agg_arg_charsets_for_string_result(collation, args, 1);
2470 2471 2472
  DBUG_ASSERT(collation.collation != NULL);
  set_if_bigger(char_length, 4);
  fix_char_length(char_length);
2473
  tmp_value.set_charset(collation.collation);
unknown's avatar
unknown committed
2474 2475 2476
}


unknown's avatar
unknown committed
2477
/**
2478 2479 2480 2481
  If alpha, map input letter to soundex code.
  If not alpha and remove_garbage is set then skip to next char
  else return 0
*/
unknown's avatar
unknown committed
2482

2483
static int soundex_toupper(int ch)
unknown's avatar
unknown committed
2484
{
2485 2486 2487
  return (ch >= 'a' && ch <= 'z') ? ch - 'a' + 'A' : ch;
}

2488 2489

static char get_scode(int wc)
2490
{
2491
  int ch= soundex_toupper(wc);
unknown's avatar
unknown committed
2492 2493 2494 2495 2496 2497 2498 2499 2500
  if (ch < 'A' || ch > 'Z')
  {
					// Thread extended alfa (country spec)
    return '0';				// as vokal
  }
  return(soundex_map[ch-'A']);
}


2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
static bool my_uni_isalpha(int wc)
{
  /*
    Return true for all Basic Latin letters: a..z A..Z.
    Return true for all Unicode characters with code higher than U+00C0:
    - characters between 'z' and U+00C0 are controls and punctuations.
    - "U+00C0 LATIN CAPITAL LETTER A WITH GRAVE" is the first letter after 'z'.
  */
  return (wc >= 'a' && wc <= 'z') ||
         (wc >= 'A' && wc <= 'Z') ||
         (wc >= 0xC0);
}


unknown's avatar
unknown committed
2515 2516
String *Item_func_soundex::val_str(String *str)
{
2517
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2518 2519
  String *res  =args[0]->val_str(str);
  char last_ch,ch;
2520
  CHARSET_INFO *cs= collation.collation;
2521 2522 2523
  my_wc_t wc;
  uint nchars;
  int rc;
unknown's avatar
unknown committed
2524

2525
  if ((null_value= args[0]->null_value))
unknown's avatar
unknown committed
2526 2527
    return 0; /* purecov: inspected */

2528
  if (tmp_value.alloc(MY_MAX(res->length(), 4 * cs->mbminlen)))
unknown's avatar
unknown committed
2529
    return str; /* purecov: inspected */
unknown's avatar
unknown committed
2530
  char *to= (char *) tmp_value.ptr();
2531 2532
  char *to_end= to + tmp_value.alloced_length();
  char *from= (char *) res->ptr(), *end= from + res->length();
unknown's avatar
unknown committed
2533
  
2534
  for ( ; ; ) /* Skip pre-space */
unknown's avatar
unknown committed
2535
  {
2536
    if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
2537
      return make_empty_result(); /* EOL or invalid byte sequence */
2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
    
    if (rc == 1 && cs->ctype)
    {
      /* Single byte letter found */
      if (my_isalpha(cs, *from))
      {
        last_ch= get_scode(*from);       // Code of the first letter
        *to++= soundex_toupper(*from++); // Copy first letter
        break;
      }
      from++;
    }
    else
    {
      from+= rc;
      if (my_uni_isalpha(wc))
      {
        /* Multibyte letter found */
        wc= soundex_toupper(wc);
        last_ch= get_scode(wc);     // Code of the first letter
        if ((rc= cs->cset->wc_mb(cs, wc, (uchar*) to, (uchar*) to_end)) <= 0)
        {
          /* Extra safety - should not really happen */
          DBUG_ASSERT(false);
2562
          return make_empty_result();
2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591
        }
        to+= rc;
        break;
      }
    }
  }
  
  /*
     last_ch is now set to the first 'double-letter' check.
     loop on input letters until end of input
  */
  for (nchars= 1 ; ; )
  {
    if ((rc= cs->cset->mb_wc(cs, &wc, (uchar*) from, (uchar*) end)) <= 0)
      break; /* EOL or invalid byte sequence */

    if (rc == 1 && cs->ctype)
    {
      if (!my_isalpha(cs, *from++))
        continue;
    }
    else
    {
      from+= rc;
      if (!my_uni_isalpha(wc))
        continue;
    }
    
    ch= get_scode(wc);
unknown's avatar
unknown committed
2592 2593
    if ((ch != '0') && (ch != last_ch)) // if not skipped or double
    {
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605
      // letter, copy to output
      if ((rc= cs->cset->wc_mb(cs, (my_wc_t) ch,
                               (uchar*) to, (uchar*) to_end)) <= 0)
      {
        // Extra safety - should not really happen
        DBUG_ASSERT(false);
        break;
      }
      to+= rc;
      nchars++;
      last_ch= ch;  // save code of last input letter
    }               // for next double-letter check
unknown's avatar
unknown committed
2606
  }
2607 2608 2609 2610 2611 2612 2613 2614 2615
  
  /* Pad up to 4 characters with DIGIT ZERO, if the string is shorter */
  if (nchars < 4) 
  {
    uint nbytes= (4 - nchars) * cs->mbminlen;
    cs->cset->fill(cs, to, nbytes, '0');
    to+= nbytes;
  }

unknown's avatar
unknown committed
2616 2617
  tmp_value.length((uint) (to-tmp_value.ptr()));
  return &tmp_value;
unknown's avatar
unknown committed
2618 2619 2620
}


unknown's avatar
unknown committed
2621 2622 2623 2624
/**
  Change a number to format '3,333,333,333.000'.

  This should be 'internationalized' sometimes.
unknown's avatar
unknown committed
2625 2626
*/

2627 2628
const int FORMAT_MAX_DECIMALS= 30;

2629 2630

MY_LOCALE *Item_func_format::get_locale(Item *item)
2631
{
2632
  DBUG_ASSERT(arg_count == 3);
2633
  String tmp, *locale_name= args[2]->val_str_ascii(&tmp);
2634 2635 2636 2637
  MY_LOCALE *lc;
  if (!locale_name ||
      !(lc= my_locale_by_name(locale_name->c_ptr_safe())))
  {
2638 2639
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
2640
                        ER_UNKNOWN_LOCALE,
2641
                        ER_THD(thd, ER_UNKNOWN_LOCALE),
2642 2643 2644 2645
                        locale_name ? locale_name->c_ptr_safe() : "NULL");
    lc= &my_locale_en_US;
  }
  return lc;
2646 2647 2648
}

void Item_func_format::fix_length_and_dec()
unknown's avatar
unknown committed
2649
{
2650 2651
  uint32 char_length= args[0]->max_char_length();
  uint32 max_sep_count= (char_length / 3) + (decimals ? 1 : 0) + /*sign*/1;
2652
  collation.set(default_charset());
2653
  fix_char_length(char_length + max_sep_count + decimals);
2654 2655 2656 2657
  if (arg_count == 3)
    locale= args[2]->basic_const_item() ? get_locale(args[2]) : NULL;
  else
    locale= &my_locale_en_US; /* Two arguments */
unknown's avatar
unknown committed
2658 2659 2660
}


unknown's avatar
unknown committed
2661 2662 2663
/**
  @todo
  This needs to be fixed for multi-byte character set where numbers
unknown's avatar
unknown committed
2664 2665 2666
  are stored in more than one byte
*/

2667
String *Item_func_format::val_str_ascii(String *str)
unknown's avatar
unknown committed
2668
{
2669 2670 2671 2672 2673
  uint32 str_length;
  /* Number of decimal digits */
  int dec;
  /* Number of characters used to represent the decimals, including '.' */
  uint32 dec_length;
2674
  MY_LOCALE *lc;
2675
  DBUG_ASSERT(fixed == 1);
2676

unknown's avatar
unknown committed
2677
  dec= (int) args[1]->val_int();
2678 2679 2680 2681 2682 2683
  if (args[1]->null_value)
  {
    null_value=1;
    return NULL;
  }

2684 2685
  lc= locale ? locale : get_locale(args[2]);

2686 2687 2688
  dec= set_zone(dec, 0, FORMAT_MAX_DECIMALS);
  dec_length= dec ? dec+1 : 0;
  null_value=0;
2689

2690 2691 2692 2693 2694
  if (args[0]->result_type() == DECIMAL_RESULT ||
      args[0]->result_type() == INT_RESULT)
  {
    my_decimal dec_val, rnd_dec, *res;
    res= args[0]->val_decimal(&dec_val);
2695 2696
    if ((null_value=args[0]->null_value))
      return 0; /* purecov: inspected */
2697
    my_decimal_round(E_DEC_FATAL_ERROR, res, dec, false, &rnd_dec);
2698 2699 2700 2701 2702 2703 2704 2705
    my_decimal2string(E_DEC_FATAL_ERROR, &rnd_dec, 0, 0, 0, str);
    str_length= str->length();
  }
  else
  {
    double nr= args[0]->val_real();
    if ((null_value=args[0]->null_value))
      return 0; /* purecov: inspected */
2706
    nr= my_double_round(nr, (longlong) dec, FALSE, FALSE);
2707
    str->set_real(nr, dec, &my_charset_numeric);
2708
    if (isnan(nr) || my_isinf(nr))
2709 2710 2711
      return str;
    str_length=str->length();
  }
2712 2713 2714
  /* We need this test to handle 'nan' and short values */
  if (lc->grouping[0] > 0 &&
      str_length >= dec_length + 1 + lc->grouping[0])
unknown's avatar
unknown committed
2715
  {
2716 2717
    /* We need space for ',' between each group of digits as well. */
    char buf[2 * FLOATING_POINT_BUFFER];
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734
    int count;
    const char *grouping= lc->grouping;
    char sign_length= *str->ptr() == '-' ? 1 : 0;
    const char *src= str->ptr() + str_length - dec_length - 1;
    const char *src_begin= str->ptr() + sign_length;
    char *dst= buf + sizeof(buf);
    
    /* Put the fractional part */
    if (dec)
    {
      dst-= (dec + 1);
      *dst= lc->decimal_point;
      memcpy(dst + 1, src + 2, dec);
    }
    
    /* Put the integer part with grouping */
    for (count= *grouping; src >= src_begin; count--)
unknown's avatar
unknown committed
2735
    {
2736 2737 2738 2739 2740
      /*
        When *grouping==0x80 (which means "end of grouping")
        count will be initialized to -1 and
        we'll never get into this "if" anymore.
      */
2741
      if (count == 0)
2742 2743 2744 2745 2746 2747 2748 2749
      {
        *--dst= lc->thousand_sep;
        if (grouping[1])
          grouping++;
        count= *grouping;
      }
      DBUG_ASSERT(dst > buf);
      *--dst= *src--;
unknown's avatar
unknown committed
2750
    }
2751 2752 2753 2754 2755 2756
    
    if (sign_length) /* Put '-' */
      *--dst= *str->ptr();
    
    /* Put the rest of the integer part without grouping */
    str->copy(dst, buf + sizeof(buf) - dst, &my_charset_latin1);
unknown's avatar
unknown committed
2757
  }
2758 2759 2760 2761 2762 2763
  else if (dec_length && lc->decimal_point != '.')
  {
    /*
      For short values without thousands (<1000)
      replace decimal point to localized value.
    */
2764
    DBUG_ASSERT(dec_length <= str_length);
2765 2766
    ((char*) str->ptr())[str_length - dec_length]= lc->decimal_point;
  }
unknown's avatar
unknown committed
2767 2768 2769 2770
  return str;
}


2771
void Item_func_format::print(String *str, enum_query_type query_type)
2772
{
2773
  str->append(STRING_WITH_LEN("format("));
2774
  args[0]->print(str, query_type);
2775
  str->append(',');
2776
  args[1]->print(str, query_type);
2777 2778 2779 2780 2781
  if(arg_count > 2)
  {
    str->append(',');
    args[2]->print(str,query_type);
  }
2782 2783 2784
  str->append(')');
}

unknown's avatar
unknown committed
2785 2786
void Item_func_elt::fix_length_and_dec()
{
2787
  uint32 char_length= 0;
unknown's avatar
unknown committed
2788
  decimals=0;
2789

2790
  if (agg_arg_charsets_for_string_result(collation, args + 1, arg_count - 1))
unknown's avatar
unknown committed
2791 2792
    return;

2793
  for (uint i= 1 ; i < arg_count ; i++)
unknown's avatar
unknown committed
2794
  {
2795
    set_if_bigger(char_length, args[i]->max_char_length());
unknown's avatar
unknown committed
2796 2797
    set_if_bigger(decimals,args[i]->decimals);
  }
2798
  fix_char_length(char_length);
2799
  maybe_null=1;					// NULL if wrong first arg
unknown's avatar
unknown committed
2800 2801 2802
}


2803
double Item_func_elt::val_real()
unknown's avatar
unknown committed
2804
{
2805
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2806
  uint tmp;
2807
  null_value=1;
2808
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
unknown's avatar
unknown committed
2809
    return 0.0;
2810
  double result= args[tmp]->val_real();
unknown's avatar
unknown committed
2811
  null_value= args[tmp]->null_value;
2812
  return result;
unknown's avatar
unknown committed
2813 2814
}

unknown's avatar
unknown committed
2815

unknown's avatar
unknown committed
2816 2817
longlong Item_func_elt::val_int()
{
2818
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2819
  uint tmp;
2820
  null_value=1;
2821
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
unknown's avatar
unknown committed
2822
    return 0;
unknown's avatar
unknown committed
2823 2824 2825

  longlong result= args[tmp]->val_int();
  null_value= args[tmp]->null_value;
2826
  return result;
unknown's avatar
unknown committed
2827 2828
}

unknown's avatar
unknown committed
2829

unknown's avatar
unknown committed
2830 2831
String *Item_func_elt::val_str(String *str)
{
2832
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2833
  uint tmp;
2834
  null_value=1;
2835
  if ((tmp=(uint) args[0]->val_int()) == 0 || tmp >= arg_count)
unknown's avatar
unknown committed
2836
    return NULL;
2837

unknown's avatar
unknown committed
2838 2839 2840 2841
  String *result= args[tmp]->val_str(str);
  if (result)
    result->set_charset(collation.collation);
  null_value= args[tmp]->null_value;
2842
  return result;
unknown's avatar
unknown committed
2843 2844 2845 2846 2847
}


void Item_func_make_set::fix_length_and_dec()
{
Sergei Golubchik's avatar
Sergei Golubchik committed
2848
  uint32 char_length= arg_count - 2; /* Separators */
unknown's avatar
unknown committed
2849

Sergei Golubchik's avatar
Sergei Golubchik committed
2850
  if (agg_arg_charsets_for_string_result(collation, args + 1, arg_count - 1))
unknown's avatar
unknown committed
2851 2852
    return;
  
2853
  for (uint i=1 ; i < arg_count ; i++)
2854 2855
    char_length+= args[i]->max_char_length();
  fix_char_length(char_length);
unknown's avatar
unknown committed
2856 2857 2858 2859 2860
}


String *Item_func_make_set::val_str(String *str)
{
2861
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2862 2863
  ulonglong bits;
  bool first_found=0;
2864
  Item **ptr=args+1;
2865
  String *result= make_empty_result();
unknown's avatar
unknown committed
2866

2867 2868
  bits=args[0]->val_int();
  if ((null_value=args[0]->null_value))
unknown's avatar
unknown committed
2869 2870
    return NULL;

2871 2872
  if (arg_count < 65)
    bits &= ((ulonglong) 1 << (arg_count-1))-1;
unknown's avatar
unknown committed
2873 2874 2875 2876 2877 2878

  for (; bits; bits >>= 1, ptr++)
  {
    if (bits & 1)
    {
      String *res= (*ptr)->val_str(str);
unknown's avatar
unknown committed
2879
      if (res)					// Skip nulls
unknown's avatar
unknown committed
2880 2881 2882 2883 2884 2885 2886 2887 2888
      {
	if (!first_found)
	{					// First argument
	  first_found=1;
	  if (res != str)
	    result=res;				// Use original string
	  else
	  {
	    if (tmp_str.copy(*res))		// Don't use 'str'
2889
              return make_empty_result();
unknown's avatar
unknown committed
2890 2891 2892 2893 2894 2895 2896 2897 2898
	    result= &tmp_str;
	  }
	}
	else
	{
	  if (result != &tmp_str)
	  {					// Copy data to tmp_str
	    if (tmp_str.alloc(result->length()+res->length()+1) ||
		tmp_str.copy(*result))
2899
              return make_empty_result();
unknown's avatar
unknown committed
2900 2901
	    result= &tmp_str;
	  }
2902
	  if (tmp_str.append(STRING_WITH_LEN(","), &my_charset_bin) || tmp_str.append(*res))
2903
            return make_empty_result();
unknown's avatar
unknown committed
2904 2905 2906 2907 2908 2909 2910 2911 2912 2913
	}
      }
    }
  }
  return result;
}


String *Item_func_char::val_str(String *str)
{
2914
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
2915
  str->length(0);
2916
  str->set_charset(collation.collation);
unknown's avatar
unknown committed
2917 2918 2919 2920
  for (uint i=0 ; i < arg_count ; i++)
  {
    int32 num=(int32) args[i]->val_int();
    if (!args[i]->null_value)
2921
    {
Alexander Barkov's avatar
Alexander Barkov committed
2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941
      char tmp[4];
      if (num & 0xFF000000L)
      {
        mi_int4store(tmp, num);
        str->append(tmp, 4, &my_charset_bin);
      }
      else if (num & 0xFF0000L)
      {
        mi_int3store(tmp, num);
        str->append(tmp, 3, &my_charset_bin);
      }
      else if (num & 0xFF00L)
      {
        mi_int2store(tmp, num);
        str->append(tmp, 2, &my_charset_bin);
      }
      else
      {
        tmp[0]= (char) num;
        str->append(tmp, 1, &my_charset_bin);
unknown's avatar
unknown committed
2942
      }
2943
    }
unknown's avatar
unknown committed
2944 2945
  }
  str->realloc(str->length());			// Add end 0 (for Purify)
2946
  return check_well_formed_result(str);
unknown's avatar
unknown committed
2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
}


inline String* alloc_buffer(String *res,String *str,String *tmp_value,
			    ulong length)
{
  if (res->alloced_length() < length)
  {
    if (str->alloced_length() >= length)
    {
      (void) str->copy(*res);
      str->length(length);
      return str;
    }
2961 2962 2963 2964 2965
    if (tmp_value->alloc(length))
      return 0;
    (void) tmp_value->copy(*res);
    tmp_value->length(length);
    return tmp_value;
unknown's avatar
unknown committed
2966 2967 2968 2969 2970 2971 2972 2973
  }
  res->length(length);
  return res;
}


void Item_func_repeat::fix_length_and_dec()
{
2974
  agg_arg_charsets_for_string_result(collation, args, 1);
2975
  DBUG_ASSERT(collation.collation != NULL);
unknown's avatar
unknown committed
2976 2977
  if (args[1]->const_item())
  {
2978 2979 2980 2981 2982
    /* must be longlong to avoid truncation */
    longlong count= args[1]->val_int();

    /* Assumes that the maximum length of a String is < INT_MAX32. */
    /* Set here so that rest of code sees out-of-bound value as such. */
2983 2984 2985
    if (args[1]->null_value)
      count= 0;
    else if (count > INT_MAX32)
2986 2987
      count= INT_MAX32;

2988 2989
    ulonglong char_length= (ulonglong) args[0]->max_char_length() * count;
    fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
2990 2991 2992
  }
  else
  {
2993
    max_length= MAX_BLOB_WIDTH;
2994
    maybe_null= 1;
unknown's avatar
unknown committed
2995 2996 2997
  }
}

unknown's avatar
unknown committed
2998 2999 3000
/**
  Item_func_repeat::str is carefully written to avoid reallocs
  as much as possible at the cost of a local buffer
unknown's avatar
unknown committed
3001 3002 3003 3004
*/

String *Item_func_repeat::val_str(String *str)
{
3005
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3006 3007
  uint length,tot_length;
  char *to;
3008
  /* must be longlong to avoid truncation */
3009
  longlong count= args[1]->val_int();
3010 3011
  String *res= args[0]->val_str(str);

unknown's avatar
unknown committed
3012 3013
  if (args[0]->null_value || args[1]->null_value)
    goto err;				// string and/or delim are null
3014
  null_value= 0;
3015

3016
  if (count <= 0 && (count == 0 || !args[1]->unsigned_flag))
3017
    return make_empty_result();
3018

3019 3020 3021 3022
  /* Assumes that the maximum length of a String is < INT_MAX32. */
  /* Bounds check on count:  If this is triggered, we will error. */
  if ((ulonglong) count > INT_MAX32)
    count= INT_MAX32;
unknown's avatar
unknown committed
3023 3024 3025
  if (count == 1)			// To avoid reallocs
    return res;
  length=res->length();
3026

unknown's avatar
unknown committed
3027
  // Safe length check
3028
  {
3029 3030 3031 3032 3033 3034 3035 3036 3037
    THD *thd= current_thd;
    if (length > thd->variables.max_allowed_packet / (uint) count)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(), thd->variables.max_allowed_packet);
      goto err;
    }
3038
  }
unknown's avatar
unknown committed
3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
  tot_length= length*(uint) count;
  if (!(res= alloc_buffer(res,str,&tmp_value,tot_length)))
    goto err;

  to=(char*) res->ptr()+length;
  while (--count)
  {
    memcpy(to,res->ptr(),length);
    to+=length;
  }
  return (res);

err:
  null_value=1;
  return 0;
}


3057 3058
void Item_func_space::fix_length_and_dec()
{
Sergei Golubchik's avatar
Sergei Golubchik committed
3059
  collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
3060 3061 3062 3063 3064 3065 3066
  if (args[0]->const_item())
  {
    /* must be longlong to avoid truncation */
    longlong count= args[0]->val_int();
    if (args[0]->null_value)
      goto end;
    /*
Sergei Golubchik's avatar
Sergei Golubchik committed
3067 3068
     Assumes that the maximum length of a String is < INT_MAX32.
     Set here so that rest of code sees out-of-bound value as such.
3069 3070 3071
    */
    if (count > INT_MAX32)
      count= INT_MAX32;
Sergei Golubchik's avatar
Sergei Golubchik committed
3072
    fix_char_length_ulonglong(count);
3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085
    return;
  }

end:
  max_length= MAX_BLOB_WIDTH;
  maybe_null= 1;
}


String *Item_func_space::val_str(String *str)
{
  uint tot_length;
  longlong count= args[0]->val_int();
3086
  CHARSET_INFO *cs= collation.collation;
Sergei Golubchik's avatar
Sergei Golubchik committed
3087

3088 3089 3090 3091 3092 3093 3094
  if (args[0]->null_value)
    goto err;				// string and/or delim are null
  null_value= 0;

  if (count <= 0 && (count == 0 || !args[0]->unsigned_flag))
    return make_empty_result();
  /*
Sergei Golubchik's avatar
Sergei Golubchik committed
3095 3096
   Assumes that the maximum length of a String is < INT_MAX32.
   Bounds check on count:  If this is triggered, we will error.
3097 3098 3099 3100 3101 3102 3103
  */
  if ((ulonglong) count > INT_MAX32)
    count= INT_MAX32;

  // Safe length check
  tot_length= (uint) count * cs->mbminlen;
  {
3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114
    THD *thd= current_thd;
    if (tot_length > thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(),
                          thd->variables.max_allowed_packet);
      goto err;
    }
  }
3115 3116 3117 3118 3119
  if (str->alloc(tot_length))
    goto err;
  str->length(tot_length);
  str->set_charset(cs);
  cs->cset->fill(cs, (char*) str->ptr(), tot_length, ' ');
Sergei Golubchik's avatar
Sergei Golubchik committed
3120
  return str;
3121 3122 3123 3124 3125 3126 3127

err:
  null_value= 1;
  return 0;
}


unknown's avatar
unknown committed
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138
void Item_func_binlog_gtid_pos::fix_length_and_dec()
{
  collation.set(system_charset_info);
  max_length= MAX_BLOB_WIDTH;
  maybe_null= 1;
}


String *Item_func_binlog_gtid_pos::val_str(String *str)
{
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3139 3140 3141 3142 3143
#ifndef HAVE_REPLICATION
  null_value= 0;
  str->copy("", 0, system_charset_info);
  return str;
#else
unknown's avatar
unknown committed
3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163
  String name_str, *name;
  longlong pos;

  if (args[0]->null_value || args[1]->null_value)
    goto err;

  name= args[0]->val_str(&name_str);
  pos= args[1]->val_int();

  if (pos < 0 || pos > UINT_MAX32)
    goto err;

  if (gtid_state_from_binlog_pos(name->c_ptr_safe(), (uint32)pos, str))
    goto err;
  null_value= 0;
  return str;

err:
  null_value= 1;
  return NULL;
unknown's avatar
unknown committed
3164
#endif  /* !HAVE_REPLICATION */
unknown's avatar
unknown committed
3165 3166 3167
}


unknown's avatar
unknown committed
3168 3169
void Item_func_rpad::fix_length_and_dec()
{
3170
  // Handle character set for args[0] and args[2].
3171
  if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2))
3172
    return;
unknown's avatar
unknown committed
3173 3174
  if (args[1]->const_item())
  {
3175 3176 3177 3178
    ulonglong char_length= (ulonglong) args[1]->val_int();
    DBUG_ASSERT(collation.collation->mbmaxlen > 0);
    /* Assumes that the maximum length of a String is < INT_MAX32. */
    /* Set here so that rest of code sees out-of-bound value as such. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3179 3180 3181
    if (args[1]->null_value)
      char_length= 0;
    else if (char_length > INT_MAX32)
3182 3183
      char_length= INT_MAX32;
    fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
3184 3185 3186
  }
  else
  {
3187
    max_length= MAX_BLOB_WIDTH;
3188
    maybe_null= 1;
unknown's avatar
unknown committed
3189 3190 3191 3192 3193 3194
  }
}


String *Item_func_rpad::val_str(String *str)
{
3195
  DBUG_ASSERT(fixed == 1);
3196
  uint32 res_byte_length,res_char_length,pad_char_length,pad_byte_length;
unknown's avatar
unknown committed
3197 3198
  char *to;
  const char *ptr_pad;
3199 3200 3201 3202 3203 3204
  /* must be longlong to avoid truncation */
  longlong count= args[1]->val_int();
  longlong byte_count;
  String *res= args[0]->val_str(str);
  String *rpad= args[2]->val_str(&rpad_str);

3205 3206 3207 3208
  if (!res || args[1]->null_value || !rpad || 
      ((count < 0) && !args[1]->unsigned_flag))
    goto err;
  null_value=0;
3209 3210
  /* Assumes that the maximum length of a String is < INT_MAX32. */
  /* Set here so that rest of code sees out-of-bound value as such. */
3211
  if ((ulonglong) count > INT_MAX32)
3212
    count= INT_MAX32;
3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226
  /*
    There is one exception not handled (intentionaly) by the character set
    aggregation code. If one string is strong side and is binary, and
    another one is weak side and is a multi-byte character string,
    then we need to operate on the second string in terms on bytes when
    calling ::numchars() and ::charpos(), rather than in terms of characters.
    Lets substitute its character set to binary.
  */
  if (collation.collation == &my_charset_bin)
  {
    res->set_charset(&my_charset_bin);
    rpad->set_charset(&my_charset_bin);
  }

3227
  if (count <= (res_char_length= res->numchars()))
3228
  {						// String to pad is big enough
3229
    res->length(res->charpos((int) count));	// Shorten result if longer
3230 3231
    return (res);
  }
3232
  pad_char_length= rpad->numchars();
3233 3234

  byte_count= count * collation.collation->mbmaxlen;
3235
  {
3236 3237 3238 3239 3240 3241 3242 3243 3244
    THD *thd= current_thd;
    if ((ulonglong) byte_count > thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(), thd->variables.max_allowed_packet);
      goto err;
    }
3245
  }
unknown's avatar
unknown committed
3246
  if (args[2]->null_value || !pad_char_length)
unknown's avatar
unknown committed
3247
    goto err;
3248
  res_byte_length= res->length();	/* Must be done before alloc_buffer */
3249
  if (!(res= alloc_buffer(res,str,&tmp_value, (ulong) byte_count)))
unknown's avatar
unknown committed
3250 3251
    goto err;

3252
  to= (char*) res->ptr()+res_byte_length;
unknown's avatar
unknown committed
3253
  ptr_pad=rpad->ptr();
3254 3255 3256
  pad_byte_length= rpad->length();
  count-= res_char_length;
  for ( ; (uint32) count > pad_char_length; count-= pad_char_length)
unknown's avatar
unknown committed
3257
  {
3258 3259
    memcpy(to,ptr_pad,pad_byte_length);
    to+= pad_byte_length;
unknown's avatar
unknown committed
3260
  }
3261 3262
  if (count)
  {
3263
    pad_byte_length= rpad->charpos((int) count);
3264 3265 3266
    memcpy(to,ptr_pad,(size_t) pad_byte_length);
    to+= pad_byte_length;
  }
3267
  res->length((uint) (to- (char*) res->ptr()));
unknown's avatar
unknown committed
3268 3269 3270 3271 3272 3273 3274 3275 3276 3277
  return (res);

 err:
  null_value=1;
  return 0;
}


void Item_func_lpad::fix_length_and_dec()
{
3278
  // Handle character set for args[0] and args[2].
3279
  if (agg_arg_charsets_for_string_result(collation, &args[0], 2, 2))
3280
    return;
3281
  
unknown's avatar
unknown committed
3282 3283
  if (args[1]->const_item())
  {
3284 3285 3286 3287
    ulonglong char_length= (ulonglong) args[1]->val_int();
    DBUG_ASSERT(collation.collation->mbmaxlen > 0);
    /* Assumes that the maximum length of a String is < INT_MAX32. */
    /* Set here so that rest of code sees out-of-bound value as such. */
Sergei Golubchik's avatar
Sergei Golubchik committed
3288 3289 3290
    if (args[1]->null_value)
      char_length= 0;
    else if (char_length > INT_MAX32)
3291 3292
      char_length= INT_MAX32;
    fix_char_length_ulonglong(char_length);
unknown's avatar
unknown committed
3293 3294 3295
  }
  else
  {
3296
    max_length= MAX_BLOB_WIDTH;
3297
    maybe_null= 1;
unknown's avatar
unknown committed
3298 3299 3300 3301 3302 3303
  }
}


String *Item_func_lpad::val_str(String *str)
{
3304
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3305
  uint32 res_char_length,pad_char_length;
3306 3307 3308
  /* must be longlong to avoid truncation */
  longlong count= args[1]->val_int();
  longlong byte_count;
3309 3310
  String *res= args[0]->val_str(&tmp_value);
  String *pad= args[2]->val_str(&lpad_str);
unknown's avatar
unknown committed
3311

3312 3313 3314 3315
  if (!res || args[1]->null_value || !pad ||  
      ((count < 0) && !args[1]->unsigned_flag))
    goto err;  
  null_value=0;
3316 3317
  /* Assumes that the maximum length of a String is < INT_MAX32. */
  /* Set here so that rest of code sees out-of-bound value as such. */
3318
  if ((ulonglong) count > INT_MAX32)
3319
    count= INT_MAX32;
3320

3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334
  /*
    There is one exception not handled (intentionaly) by the character set
    aggregation code. If one string is strong side and is binary, and
    another one is weak side and is a multi-byte character string,
    then we need to operate on the second string in terms on bytes when
    calling ::numchars() and ::charpos(), rather than in terms of characters.
    Lets substitute its character set to binary.
  */
  if (collation.collation == &my_charset_bin)
  {
    res->set_charset(&my_charset_bin);
    pad->set_charset(&my_charset_bin);
  }

3335 3336 3337 3338
  res_char_length= res->numchars();

  if (count <= res_char_length)
  {
3339
    res->length(res->charpos((int) count));
3340
    return res;
3341
  }
3342 3343 3344 3345
  
  pad_char_length= pad->numchars();
  byte_count= count * collation.collation->mbmaxlen;
  
3346
  {
3347 3348 3349 3350 3351 3352 3353 3354 3355
    THD *thd= current_thd;
    if ((ulonglong) byte_count > thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(), thd->variables.max_allowed_packet);
      goto err;
    }
3356 3357
  }

3358 3359
  if (args[2]->null_value || !pad_char_length ||
      str->alloc((uint32) byte_count))
unknown's avatar
unknown committed
3360
    goto err;
3361 3362 3363 3364 3365
  
  str->length(0);
  str->set_charset(collation.collation);
  count-= res_char_length;
  while (count >= pad_char_length)
unknown's avatar
unknown committed
3366
  {
3367 3368
    str->append(*pad);
    count-= pad_char_length;
unknown's avatar
unknown committed
3369
  }
3370
  if (count > 0)
3371
    str->append(pad->ptr(), pad->charpos((int) count), collation.collation);
3372

3373 3374 3375
  str->append(*res);
  null_value= 0;
  return str;
unknown's avatar
unknown committed
3376

3377 3378
err:
  null_value= 1;
unknown's avatar
unknown committed
3379 3380 3381 3382 3383 3384
  return 0;
}


String *Item_func_conv::val_str(String *str)
{
3385
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3386 3387 3388 3389 3390
  String *res= args[0]->val_str(str);
  char *endptr,ans[65],*ptr;
  longlong dec;
  int from_base= (int) args[1]->val_int();
  int to_base= (int) args[2]->val_int();
3391
  int err;
unknown's avatar
unknown committed
3392

3393
  // Note that abs(INT_MIN) is undefined.
unknown's avatar
unknown committed
3394
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
3395
      from_base == INT_MIN || to_base == INT_MIN ||
unknown's avatar
unknown committed
3396 3397 3398
      abs(to_base) > 36 || abs(to_base) < 2 ||
      abs(from_base) > 36 || abs(from_base) < 2 || !(res->length()))
  {
3399 3400
    null_value= 1;
    return NULL;
unknown's avatar
unknown committed
3401
  }
3402
  null_value= 0;
unknown's avatar
unknown committed
3403
  unsigned_flag= !(from_base < 0);
3404 3405 3406 3407 3408 3409 3410 3411 3412 3413

  if (args[0]->field_type() == MYSQL_TYPE_BIT) 
  {
    /* 
     Special case: The string representation of BIT doesn't resemble the
     decimal representation, so we shouldn't change it to string and then to
     decimal. 
    */
    dec= args[0]->val_int();
  }
unknown's avatar
unknown committed
3414
  else
3415 3416 3417 3418 3419 3420 3421 3422 3423
  {
    if (from_base < 0)
      dec= my_strntoll(res->charset(), res->ptr(), res->length(),
                       -from_base, &endptr, &err);
    else
      dec= (longlong) my_strntoull(res->charset(), res->ptr(), res->length(),
                                   from_base, &endptr, &err);
  }

3424 3425
  if (!(ptr= longlong2str(dec, ans, to_base)) ||
      str->copy(ans, (uint32) (ptr - ans), default_charset()))
3426 3427 3428 3429
  {
    null_value= 1;
    return NULL;
  }
unknown's avatar
unknown committed
3430 3431 3432
  return str;
}

unknown's avatar
unknown committed
3433

3434 3435
String *Item_func_conv_charset::val_str(String *str)
{
3436
  DBUG_ASSERT(fixed == 1);
3437 3438
  if (use_cached_value)
    return null_value ? 0 : &str_value;
3439
  String *arg= args[0]->val_str(str);
3440 3441
  String_copier_for_item copier(current_thd);
  return ((null_value= args[0]->null_value ||
3442
                       copier.copy_with_warn(collation.collation, &tmp_value,
3443 3444 3445
                                             arg->charset(), arg->ptr(),
                                             arg->length(), arg->length()))) ?
    0 : &tmp_value;
3446 3447 3448 3449
}

void Item_func_conv_charset::fix_length_and_dec()
{
3450
  DBUG_ASSERT(collation.derivation == DERIVATION_IMPLICIT);
3451
  fix_char_length(args[0]->max_char_length());
3452 3453
}

3454
void Item_func_conv_charset::print(String *str, enum_query_type query_type)
3455
{
3456
  str->append(STRING_WITH_LEN("convert("));
3457
  args[0]->print(str, query_type);
3458
  str->append(STRING_WITH_LEN(" using "));
3459
  str->append(collation.collation->csname);
3460 3461
  str->append(')');
}
unknown's avatar
unknown committed
3462 3463 3464

String *Item_func_set_collation::val_str(String *str)
{
3465
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3466
  str=args[0]->val_str(str);
unknown's avatar
unknown committed
3467 3468
  if ((null_value=args[0]->null_value))
    return 0;
3469
  str->set_charset(collation.collation);
unknown's avatar
unknown committed
3470 3471 3472
  return str;
}

3473
void Item_func_set_collation::fix_length_and_dec()
unknown's avatar
unknown committed
3474
{
unknown's avatar
unknown committed
3475 3476
  CHARSET_INFO *set_collation;
  const char *colname;
3477
  String tmp, *str= args[1]->val_str(&tmp);
unknown's avatar
unknown committed
3478
  colname= str->c_ptr();
unknown's avatar
unknown committed
3479
  if (colname == binary_keyword)
3480
    set_collation= get_charset_by_csname(args[0]->collation.collation->csname,
unknown's avatar
unknown committed
3481 3482
					 MY_CS_BINSORT,MYF(0));
  else
3483
  {
3484
    if (!(set_collation= mysqld_collation_get_by_name(colname)))  
3485 3486
      return;
  }
unknown's avatar
unknown committed
3487

3488 3489
  if (!set_collation || 
      !my_charset_same(args[0]->collation.collation,set_collation))
3490
  {
3491 3492
    my_error(ER_COLLATION_CHARSET_MISMATCH, MYF(0),
             colname, args[0]->collation.collation->csname);
3493
    return;
3494
  }
3495 3496
  collation.set(set_collation, DERIVATION_EXPLICIT,
                args[0]->collation.repertoire);
3497
  max_length= args[0]->max_length;
unknown's avatar
unknown committed
3498 3499
}

3500

3501 3502 3503 3504 3505 3506 3507 3508
bool Item_func_set_collation::eq(const Item *item, bool binary_cmp) const
{
  /* Assume we don't have rtti */
  if (this == item)
    return 1;
  if (item->type() != FUNC_ITEM)
    return 0;
  Item_func *item_func=(Item_func*) item;
3509
  if (arg_count != item_func->argument_count() ||
3510
      functype() != item_func->functype())
3511 3512
    return 0;
  Item_func_set_collation *item_func_sc=(Item_func_set_collation*) item;
3513
  if (collation.collation != item_func_sc->collation.collation)
3514 3515 3516 3517 3518 3519 3520
    return 0;
  for (uint i=0; i < arg_count ; i++)
    if (!args[i]->eq(item_func_sc->args[i], binary_cmp))
      return 0;
  return 1;
}

3521

3522
void Item_func_set_collation::print(String *str, enum_query_type query_type)
3523 3524
{
  str->append('(');
3525
  args[0]->print(str, query_type);
3526
  str->append(STRING_WITH_LEN(" collate "));
3527 3528
  DBUG_ASSERT(args[1]->basic_const_item() &&
              args[1]->type() == Item::STRING_ITEM);
3529
  ((Item_string *)args[1])->print_value(str);
3530 3531 3532
  str->append(')');
}

3533 3534
String *Item_func_charset::val_str(String *str)
{
3535
  DBUG_ASSERT(fixed == 1);
3536
  uint dummy_errors;
3537

3538
  CHARSET_INFO *cs= args[0]->charset_for_protocol(); 
3539
  null_value= 0;
3540
  str->copy(cs->csname, (uint) strlen(cs->csname),
3541
	    &my_charset_latin1, collation.collation, &dummy_errors);
unknown's avatar
unknown committed
3542 3543 3544 3545 3546
  return str;
}

String *Item_func_collation::val_str(String *str)
{
3547
  DBUG_ASSERT(fixed == 1);
3548
  uint dummy_errors;
3549
  CHARSET_INFO *cs= args[0]->charset_for_protocol(); 
unknown's avatar
unknown committed
3550

3551
  null_value= 0;
3552
  str->copy(cs->name, (uint) strlen(cs->name),
3553
	    &my_charset_latin1, collation.collation, &dummy_errors);
3554 3555
  return str;
}
unknown's avatar
unknown committed
3556 3557


3558 3559 3560 3561 3562 3563 3564 3565 3566
void Item_func_weight_string::fix_length_and_dec()
{
  CHARSET_INFO *cs= args[0]->collation.collation;
  collation.set(&my_charset_bin, args[0]->collation.derivation);
  flags= my_strxfrm_flag_normalize(flags, cs->levels_for_order);
  /* 
    Use result_length if it was given explicitly in constructor,
    otherwise calculate max_length using argument's max_length
    and "nweights".
3567 3568 3569 3570 3571
  */
  if (!(max_length= result_length))
  {
    uint char_length;
    char_length= ((cs->state & MY_CS_STRNXFRM_BAD_NWEIGHTS) || !nweights) ?
3572
                 args[0]->max_char_length() : nweights * cs->levels_for_order;
3573 3574
    max_length= cs->coll->strnxfrmlen(cs, char_length * cs->mbmaxlen);
  }
3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595
  maybe_null= 1;
}


/* Return a weight_string according to collation */
String *Item_func_weight_string::val_str(String *str)
{
  String *res;
  CHARSET_INFO *cs= args[0]->collation.collation;
  uint tmp_length, frm_length;
  DBUG_ASSERT(fixed == 1);

  if (args[0]->result_type() != STRING_RESULT ||
      !(res= args[0]->val_str(str)))
    goto nl;
  
  /*
    Use result_length if it was given in constructor
    explicitly, otherwise calculate result length
    from argument and "nweights".
  */
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624
  if (!(tmp_length= result_length))
  {
    uint char_length;
    if (cs->state & MY_CS_STRNXFRM_BAD_NWEIGHTS)
    {
      /*
        latin2_czech_cs and cp1250_czech_cs do not support
        the "nweights" limit in strnxfrm(). Use the full length.
      */
      char_length= res->length();
    }
    else
    {
      /*
        If we don't need to pad the result with spaces, then it should be
        OK to calculate character length of the argument approximately:
        "res->length() / cs->mbminlen" can return a number that is 
        bigger than the real number of characters in the string, so
        we'll allocate a little bit more memory but avoid calling
        the slow res->numchars().
        In case if we do need to pad with spaces, we call res->numchars()
        to know the true number of characters.
      */
      if (!(char_length= nweights))
        char_length= (flags & MY_STRXFRM_PAD_WITH_SPACE) ?
                      res->numchars() : (res->length() / cs->mbminlen);
    }
    tmp_length= cs->coll->strnxfrmlen(cs, char_length * cs->mbmaxlen);
  }
3625 3626

  {
3627 3628 3629 3630 3631 3632 3633 3634 3635 3636
    THD *thd= current_thd;
    if (tmp_length > current_thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(),
                          thd->variables.max_allowed_packet);
      goto nl;
    }
3637 3638 3639 3640 3641 3642 3643 3644 3645
  }
  if (tmp_value.alloc(tmp_length))
    goto nl;

  frm_length= cs->coll->strnxfrm(cs,
                                 (uchar *) tmp_value.ptr(), tmp_length,
                                 nweights ? nweights : tmp_length,
                                 (const uchar *) res->ptr(), res->length(),
                                 flags);
3646 3647
  DBUG_ASSERT(frm_length <= tmp_length);

3648 3649 3650 3651 3652 3653 3654 3655 3656 3657
  tmp_value.length(frm_length);
  null_value= 0;
  return &tmp_value;

nl:
  null_value= 1;
  return 0;
}


3658
String *Item_func_hex::val_str_ascii(String *str)
unknown's avatar
unknown committed
3659
{
unknown's avatar
unknown committed
3660
  String *res;
3661
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3662 3663
  if (args[0]->result_type() != STRING_RESULT)
  {
3664
    ulonglong dec;
unknown's avatar
unknown committed
3665
    char ans[65],*ptr;
3666
    /* Return hex of unsigned longlong value */
unknown's avatar
unknown committed
3667 3668
    if (args[0]->result_type() == REAL_RESULT ||
        args[0]->result_type() == DECIMAL_RESULT)
3669
    {
unknown's avatar
unknown committed
3670
      double val= args[0]->val_real();
3671 3672 3673 3674 3675 3676 3677 3678 3679
      if ((val <= (double) LONGLONG_MIN) || 
          (val >= (double) (ulonglong) ULONGLONG_MAX))
        dec=  ~(longlong) 0;
      else
        dec= (ulonglong) (val + (val > 0 ? 0.5 : -0.5));
    }
    else
      dec= (ulonglong) args[0]->val_int();

unknown's avatar
unknown committed
3680 3681
    if ((null_value= args[0]->null_value))
      return 0;
3682 3683 3684 3685
    
    if (!(ptr= longlong2str(dec, ans, 16)) ||
        str->copy(ans,(uint32) (ptr - ans),
        &my_charset_numeric))
Martin Hansson's avatar
Martin Hansson committed
3686
      return make_empty_result();		// End of memory
unknown's avatar
unknown committed
3687 3688 3689 3690
    return str;
  }

  /* Convert given string to a hex string, character by character */
unknown's avatar
unknown committed
3691 3692
  res= args[0]->val_str(str);
  if (!res || tmp_value.alloc(res->length()*2+1))
unknown's avatar
unknown committed
3693 3694 3695 3696 3697 3698
  {
    null_value=1;
    return 0;
  }
  null_value=0;
  tmp_value.length(res->length()*2);
3699
  tmp_value.set_charset(&my_charset_latin1);
unknown's avatar
unknown committed
3700 3701

  octet2hex((char*) tmp_value.ptr(), res->ptr(), res->length());
unknown's avatar
unknown committed
3702 3703 3704
  return &tmp_value;
}

unknown's avatar
unknown committed
3705
  /** Convert given hex string to a binary string. */
unknown's avatar
unknown committed
3706

unknown's avatar
unknown committed
3707 3708
String *Item_func_unhex::val_str(String *str)
{
unknown's avatar
unknown committed
3709 3710 3711 3712
  const char *from, *end;
  char *to;
  String *res;
  uint length;
3713
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3714

unknown's avatar
unknown committed
3715 3716
  res= args[0]->val_str(str);
  if (!res || tmp_value.alloc(length= (1+res->length())/2))
unknown's avatar
unknown committed
3717 3718 3719 3720
  {
    null_value=1;
    return 0;
  }
unknown's avatar
unknown committed
3721

unknown's avatar
unknown committed
3722 3723 3724
  from= res->ptr();
  null_value= 0;
  tmp_value.length(length);
unknown's avatar
unknown committed
3725 3726 3727
  to= (char*) tmp_value.ptr();
  if (res->length() % 2)
  {
unknown's avatar
unknown committed
3728 3729 3730
    int hex_char;
    *to++= hex_char= hexchar_to_int(*from++);
    if ((null_value= (hex_char == -1)))
unknown's avatar
unknown committed
3731 3732 3733 3734
      return 0;
  }
  for (end=res->ptr()+res->length(); from < end ; from+=2, to++)
  {
unknown's avatar
unknown committed
3735 3736 3737
    int hex_char;
    *to= (hex_char= hexchar_to_int(from[0])) << 4;
    if ((null_value= (hex_char == -1)))
unknown's avatar
unknown committed
3738
      return 0;
unknown's avatar
unknown committed
3739 3740
    *to|= hex_char= hexchar_to_int(from[1]);
    if ((null_value= (hex_char == -1)))
unknown's avatar
unknown committed
3741 3742 3743 3744
      return 0;
  }
  return &tmp_value;
}
unknown's avatar
unknown committed
3745

unknown's avatar
unknown committed
3746

3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781
#ifndef DBUG_OFF
String *Item_func_like_range::val_str(String *str)
{
  DBUG_ASSERT(fixed == 1);
  longlong nbytes= args[1]->val_int();
  String *res= args[0]->val_str(str);
  size_t min_len, max_len;
  CHARSET_INFO *cs= collation.collation;

  if (!res || args[0]->null_value || args[1]->null_value ||
      nbytes < 0 || nbytes > MAX_BLOB_WIDTH ||
      min_str.alloc(nbytes) || max_str.alloc(nbytes))
    goto err;
  null_value=0;

  if (cs->coll->like_range(cs, res->ptr(), res->length(),
                           '\\', '_', '%', nbytes,
                           (char*) min_str.ptr(), (char*) max_str.ptr(),
                           &min_len, &max_len))
    goto err;

  min_str.set_charset(collation.collation);
  max_str.set_charset(collation.collation);
  min_str.length(min_len);
  max_str.length(max_len);

  return is_min ? &min_str : &max_str;

err:
  null_value= 1;
  return 0;
}
#endif


3782
void Item_func_binary::print(String *str, enum_query_type query_type)
3783
{
3784
  str->append(STRING_WITH_LEN("cast("));
3785
  args[0]->print(str, query_type);
3786
  str->append(STRING_WITH_LEN(" as binary)"));
3787 3788 3789
}


unknown's avatar
unknown committed
3790 3791 3792 3793
#include <my_dir.h>				// For my_stat

String *Item_load_file::val_str(String *str)
{
3794
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
3795 3796 3797
  String *file_name;
  File file;
  MY_STAT stat_info;
3798
  char path[FN_REFLEN];
unknown's avatar
unknown committed
3799 3800
  DBUG_ENTER("load_file");

3801
  if (!(file_name= args[0]->val_str(str))
unknown's avatar
unknown committed
3802
#ifndef NO_EMBEDDED_ACCESS_CHECKS
3803
      || !(current_thd->security_ctx->master_access & FILE_ACL)
unknown's avatar
unknown committed
3804
#endif
3805
      )
unknown's avatar
unknown committed
3806
    goto err;
3807

3808
  (void) fn_format(path, file_name->c_ptr_safe(), mysql_real_data_home, "",
unknown's avatar
unknown committed
3809
		   MY_RELATIVE_PATH | MY_UNPACK_FILENAME);
3810

3811
  /* Read only allowed from within dir specified by secure_file_priv */
3812
  if (!is_secure_file_path(path))
3813 3814
    goto err;

Marc Alff's avatar
Marc Alff committed
3815
  if (!mysql_file_stat(key_file_loadfile, path, &stat_info, MYF(0)))
3816 3817
    goto err;

unknown's avatar
unknown committed
3818 3819 3820 3821 3822
  if (!(stat_info.st_mode & S_IROTH))
  {
    /* my_error(ER_TEXTFILE_NOT_READABLE, MYF(0), file_name->c_ptr()); */
    goto err;
  }
3823

unknown's avatar
unknown committed
3824
  {
3825 3826 3827 3828 3829 3830 3831 3832 3833
    THD *thd= current_thd;
    if (stat_info.st_size > (long) thd->variables.max_allowed_packet)
    {
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                          ER_WARN_ALLOWED_PACKET_OVERFLOWED,
                          ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
                          func_name(), thd->variables.max_allowed_packet);
      goto err;
    }
unknown's avatar
unknown committed
3834
  }
3835
  if (tmp_value.alloc((size_t)stat_info.st_size))
unknown's avatar
unknown committed
3836
    goto err;
Marc Alff's avatar
Marc Alff committed
3837 3838
  if ((file= mysql_file_open(key_file_loadfile,
                             file_name->ptr(), O_RDONLY, MYF(0))) < 0)
unknown's avatar
unknown committed
3839
    goto err;
Marc Alff's avatar
Marc Alff committed
3840 3841
  if (mysql_file_read(file, (uchar*) tmp_value.ptr(), stat_info.st_size,
                      MYF(MY_NABP)))
unknown's avatar
unknown committed
3842
  {
Marc Alff's avatar
Marc Alff committed
3843
    mysql_file_close(file, MYF(0));
unknown's avatar
unknown committed
3844 3845
    goto err;
  }
3846
  tmp_value.length((uint32)stat_info.st_size);
Marc Alff's avatar
Marc Alff committed
3847
  mysql_file_close(file, MYF(0));
unknown's avatar
unknown committed
3848
  null_value = 0;
unknown's avatar
unknown committed
3849
  DBUG_RETURN(&tmp_value);
unknown's avatar
unknown committed
3850 3851 3852 3853 3854 3855 3856 3857 3858

err:
  null_value = 1;
  DBUG_RETURN(0);
}


String* Item_func_export_set::val_str(String* str)
{
3859
  DBUG_ASSERT(fixed == 1);
3860 3861 3862 3863 3864
  String yes_buf, no_buf, sep_buf;
  const ulonglong the_set = (ulonglong) args[0]->val_int();
  const String *yes= args[1]->val_str(&yes_buf);
  const String *no= args[2]->val_str(&no_buf);
  const String *sep= NULL;
unknown's avatar
unknown committed
3865 3866 3867

  uint num_set_values = 64;
  str->length(0);
3868
  str->set_charset(collation.collation);
unknown's avatar
unknown committed
3869 3870 3871 3872

  /* Check if some argument is a NULL value */
  if (args[0]->null_value || args[1]->null_value || args[2]->null_value)
  {
3873 3874
    null_value= true;
    return NULL;
unknown's avatar
unknown committed
3875
  }
3876 3877 3878 3879
  /*
    Arg count can only be 3, 4 or 5 here. This is guaranteed from the
    grammar for EXPORT_SET()
  */
unknown's avatar
unknown committed
3880 3881 3882 3883 3884 3885 3886
  switch(arg_count) {
  case 5:
    num_set_values = (uint) args[4]->val_int();
    if (num_set_values > 64)
      num_set_values=64;
    if (args[4]->null_value)
    {
3887 3888
      null_value= true;
      return NULL;
unknown's avatar
unknown committed
3889 3890 3891 3892 3893
    }
    /* Fall through */
  case 4:
    if (!(sep = args[3]->val_str(&sep_buf)))	// Only true if NULL
    {
3894 3895
      null_value= true;
      return NULL;
unknown's avatar
unknown committed
3896 3897 3898
    }
    break;
  case 3:
3899 3900 3901
    {
      /* errors is not checked - assume "," can always be converted */
      uint errors;
3902 3903
      sep_buf.copy(STRING_WITH_LEN(","), &my_charset_bin,
                   collation.collation, &errors);
3904 3905
      sep = &sep_buf;
    }
3906 3907 3908
    break;
  default:
    DBUG_ASSERT(0); // cannot happen
unknown's avatar
unknown committed
3909
  }
3910 3911
  null_value= false;

3912 3913
  THD *thd= current_thd;
  const ulong max_allowed_packet= thd->variables.max_allowed_packet;
3914 3915
  const uint num_separators= num_set_values > 0 ? num_set_values - 1 : 0;
  const ulonglong max_total_length=
3916
    num_set_values * MY_MAX(yes->length(), no->length()) +
3917 3918 3919 3920
    num_separators * sep->length();

  if (unlikely(max_total_length > max_allowed_packet))
  {
3921
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
3922
                        ER_WARN_ALLOWED_PACKET_OVERFLOWED,
3923
                        ER_THD(thd, ER_WARN_ALLOWED_PACKET_OVERFLOWED),
3924 3925 3926 3927
                        func_name(), max_allowed_packet);
    null_value= true;
    return NULL;
  }
unknown's avatar
unknown committed
3928

3929 3930 3931
  uint ix;
  ulonglong mask;
  for (ix= 0, mask=0x1; ix < num_set_values; ++ix, mask = (mask << 1))
unknown's avatar
unknown committed
3932 3933 3934 3935 3936
  {
    if (the_set & mask)
      str->append(*yes);
    else
      str->append(*no);
3937
    if (ix != num_separators)
unknown's avatar
unknown committed
3938 3939 3940 3941 3942 3943 3944
      str->append(*sep);
  }
  return str;
}

void Item_func_export_set::fix_length_and_dec()
{
3945
  uint32 length= MY_MAX(args[1]->max_char_length(), args[2]->max_char_length());
3946
  uint32 sep_length= (arg_count > 3 ? args[3]->max_char_length() : 1);
3947

3948
  if (agg_arg_charsets_for_string_result(collation,
3949
                                         args + 1, MY_MIN(4, arg_count) - 1))
unknown's avatar
unknown committed
3950
    return;
3951
  fix_char_length(length * 64 + sep_length * 63);
unknown's avatar
unknown committed
3952 3953
}

unknown's avatar
unknown committed
3954

unknown's avatar
unknown committed
3955 3956 3957
#define get_esc_bit(mask, num) (1 & (*((mask) + ((num) >> 3))) >> ((num) & 7))

/**
3958 3959 3960
  QUOTE() function returns argument string in single quotes suitable for
  using in a SQL statement.

unknown's avatar
unknown committed
3961 3962 3963
  Adds a \\ before all characters that needs to be escaped in a SQL string.
  We also escape '^Z' (END-OF-FILE in windows) to avoid probelms when
  running commands from a file in windows.
3964

unknown's avatar
unknown committed
3965
  This function is very useful when you want to generate SQL statements.
3966

unknown's avatar
unknown committed
3967
  @note
3968 3969
    QUOTE(NULL) returns the string 'NULL' (4 letters, without quotes).

unknown's avatar
unknown committed
3970 3971 3972 3973
  @retval
    str	   Quoted string
  @retval
    NULL	   Out of memory.
3974
*/
3975

unknown's avatar
unknown committed
3976 3977
String *Item_func_quote::val_str(String *str)
{
3978
  DBUG_ASSERT(fixed == 1);
3979 3980 3981
  /*
    Bit mask that has 1 for set for the position of the following characters:
    0, \, ' and ^Z
3982
  */
3983

3984
  static uchar escmask[32]=
3985 3986 3987 3988 3989 3990
  {
    0x01, 0x00, 0x00, 0x04, 0x80, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  };
unknown's avatar
unknown committed
3991

3992 3993 3994 3995
  char *from, *to, *end, *start;
  String *arg= args[0]->val_str(str);
  uint arg_length, new_length;
  if (!arg)					// Null argument
3996
  {
3997 3998
    /* Return the string 'NULL' */
    str->copy(STRING_WITH_LEN("NULL"), collation.collation);
3999 4000 4001 4002
    null_value= 0;
    return str;
  }

4003
  arg_length= arg->length();
4004

4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015
  if (collation.collation->mbmaxlen == 1)
  {
    new_length= arg_length + 2; /* for beginning and ending ' signs */
    for (from= (char*) arg->ptr(), end= from + arg_length; from < end; from++)
      new_length+= get_esc_bit(escmask, (uchar) *from);
  }
  else
  {
    new_length= (arg_length * 2) +  /* For string characters */
                (2 * collation.collation->mbmaxlen); /* For quotes */
  }
unknown's avatar
unknown committed
4016

4017
  if (tmp_value.alloc(new_length))
4018
    goto null;
4019

4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
  if (collation.collation->mbmaxlen > 1)
  {
    CHARSET_INFO *cs= collation.collation;
    int mblen;
    uchar *to_end;
    to= (char*) tmp_value.ptr();
    to_end= (uchar*) to + new_length;

    /* Put leading quote */
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
      goto null;
    to+= mblen;

    for (start= (char*) arg->ptr(), end= start + arg_length; start < end; )
    {
      my_wc_t wc;
      bool escape;
      if ((mblen= cs->cset->mb_wc(cs, &wc, (uchar*) start, (uchar*) end)) <= 0)
        goto null;
      start+= mblen;
      switch (wc) {
        case 0:      escape= 1; wc= '0'; break;
        case '\032': escape= 1; wc= 'Z'; break;
        case '\'':   escape= 1; break;
        case '\\':   escape= 1; break;
        default:     escape= 0; break;
      }
      if (escape)
      {
        if ((mblen= cs->cset->wc_mb(cs, '\\', (uchar*) to, to_end)) <= 0)
          goto null;
        to+= mblen;
      }
      if ((mblen= cs->cset->wc_mb(cs, wc, (uchar*) to, to_end)) <= 0)
        goto null;
      to+= mblen;
    }

    /* Put trailing quote */
    if ((mblen= cs->cset->wc_mb(cs, '\'', (uchar *) to, to_end)) <= 0)
      goto null;
    to+= mblen;
    new_length= to - tmp_value.ptr();
    goto ret;
  }

4066
  /*
4067
    We replace characters from the end to the beginning
4068
  */
4069
  to= (char*) tmp_value.ptr() + new_length - 1;
4070
  *to--= '\'';
unknown's avatar
unknown committed
4071
  for (start= (char*) arg->ptr(),end= start + arg_length; end-- != start; to--)
unknown's avatar
unknown committed
4072
  {
4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094
    /*
      We can't use the bitmask here as we want to replace \O and ^Z with 0
      and Z
    */
    switch (*end)  {
    case 0:
      *to--= '0';
      *to=   '\\';
      break;
    case '\032':
      *to--= 'Z';
      *to=   '\\';
      break;
    case '\'':
    case '\\':
      *to--= *end;
      *to=   '\\';
      break;
    default:
      *to= *end;
      break;
    }
unknown's avatar
unknown committed
4095
  }
4096
  *to= '\'';
4097 4098

ret:
4099
  tmp_value.length(new_length);
unknown's avatar
unknown committed
4100
  tmp_value.set_charset(collation.collation);
4101
  null_value= 0;
4102
  return &tmp_value;
4103 4104 4105 4106

null:
  null_value= 1;
  return 0;
unknown's avatar
unknown committed
4107
}
unknown's avatar
unknown committed
4108

4109 4110
longlong Item_func_uncompressed_length::val_int()
{
4111
  DBUG_ASSERT(fixed == 1);
4112 4113 4114 4115 4116 4117 4118 4119
  String *res= args[0]->val_str(&value);
  if (!res)
  {
    null_value=1;
    return 0; /* purecov: inspected */
  }
  null_value=0;
  if (res->is_empty()) return 0;
unknown's avatar
unknown committed
4120 4121

  /*
4122 4123 4124 4125 4126
    If length is <= 4 bytes, data is corrupt. This is the best we can do
    to detect garbage input without decompressing it.
  */
  if (res->length() <= 4)
  {
4127 4128
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4129
                        ER_ZLIB_Z_DATA_ERROR,
4130
                        ER_THD(thd, ER_ZLIB_Z_DATA_ERROR));
4131 4132 4133 4134 4135 4136 4137
    null_value= 1;
    return 0;
  }

 /*
    res->ptr() using is safe because we have tested that string is at least
    5 bytes long.
unknown's avatar
unknown committed
4138 4139 4140 4141 4142 4143
    res->c_ptr() is not used because:
      - we do not need \0 terminated string to get first 4 bytes
      - c_ptr() tests simbol after string end (uninitialiozed memory) which
        confuse valgrind
  */
  return uint4korr(res->ptr()) & 0x3FFFFFFF;
4144 4145 4146 4147
}

longlong Item_func_crc32::val_int()
{
4148
  DBUG_ASSERT(fixed == 1);
4149 4150 4151 4152 4153 4154 4155
  String *res=args[0]->val_str(&value);
  if (!res)
  {
    null_value=1;
    return 0; /* purecov: inspected */
  }
  null_value=0;
4156
  return (longlong) my_checksum(0L, (uchar*)res->ptr(), res->length());
4157 4158
}

unknown's avatar
unknown committed
4159
#ifdef HAVE_COMPRESS
4160
#include "zlib.h"
unknown's avatar
unknown committed
4161 4162 4163

String *Item_func_compress::val_str(String *str)
{
unknown's avatar
unknown committed
4164
  int err= Z_OK, code;
4165
  size_t new_size;
unknown's avatar
unknown committed
4166 4167 4168
  String *res;
  Byte *body;
  char *tmp, *last_char;
4169
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
4170 4171

  if (!(res= args[0]->val_str(str)))
4172 4173 4174 4175
  {
    null_value= 1;
    return 0;
  }
4176
  null_value= 0;
4177 4178
  if (res->is_empty()) return res;

unknown's avatar
unknown committed
4179
  /*
unknown's avatar
unknown committed
4180
    Citation from zlib.h (comment for compress function):
unknown's avatar
unknown committed
4181 4182

    Compresses the source buffer into the destination buffer.  sourceLen is
unknown's avatar
unknown committed
4183 4184 4185 4186
    the byte length of the source buffer. Upon entry, destLen is the total
    size of the destination buffer, which must be at least 0.1% larger than
    sourceLen plus 12 bytes.
    We assume here that the buffer can't grow more than .25 %.
unknown's avatar
unknown committed
4187
  */
unknown's avatar
unknown committed
4188
  new_size= res->length() + res->length() / 5 + 12;
unknown's avatar
unknown committed
4189

unknown's avatar
unknown committed
4190 4191
  // Check new_size overflow: new_size <= res->length()
  if (((uint32) (new_size+5) <= res->length()) || 
4192 4193 4194 4195 4196
      buffer.realloc((uint32) new_size + 4 + 1))
  {
    null_value= 1;
    return 0;
  }
unknown's avatar
unknown committed
4197

unknown's avatar
unknown committed
4198
  body= ((Byte*)buffer.ptr()) + 4;
4199

unknown's avatar
unknown committed
4200
  // As far as we have checked res->is_empty() we can use ptr()
4201 4202
  if ((err= my_compress_buffer(body, &new_size, (const uchar *)res->ptr(),
                               res->length())) != Z_OK)
unknown's avatar
unknown committed
4203
  {
4204
    THD *thd= current_thd;
unknown's avatar
unknown committed
4205
    code= err==Z_MEM_ERROR ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_BUF_ERROR;
4206 4207
    push_warning(thd, Sql_condition::WARN_LEVEL_WARN, code,
                 ER_THD(thd, code));
unknown's avatar
unknown committed
4208 4209 4210
    null_value= 1;
    return 0;
  }
4211

unknown's avatar
unknown committed
4212
  tmp= (char*)buffer.ptr(); // int4store is a macro; avoid side effects
4213
  int4store(tmp, res->length() & 0x3FFFFFFF);
4214

unknown's avatar
unknown committed
4215 4216
  /* This is to ensure that things works for CHAR fields, which trim ' ': */
  last_char= ((char*)body)+new_size-1;
unknown's avatar
unknown committed
4217 4218 4219 4220 4221
  if (*last_char == ' ')
  {
    *++last_char= '.';
    new_size++;
  }
4222

4223
  buffer.length((uint32)new_size + 4);
unknown's avatar
unknown committed
4224 4225 4226
  return &buffer;
}

4227

unknown's avatar
unknown committed
4228 4229
String *Item_func_uncompress::val_str(String *str)
{
4230
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
4231
  String *res= args[0]->val_str(str);
4232 4233
  ulong new_size;
  int err;
unknown's avatar
unknown committed
4234
  uint code;
4235

4236 4237
  if (!res)
    goto err;
4238
  null_value= 0;
4239 4240 4241
  if (res->is_empty())
    return res;

4242 4243 4244
  /* If length is less than 4 bytes, data is corrupt */
  if (res->length() <= 4)
  {
4245 4246
    THD *thd= current_thd;
    push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4247
			ER_ZLIB_Z_DATA_ERROR,
4248
			ER_THD(thd, ER_ZLIB_Z_DATA_ERROR));
4249 4250 4251 4252
    goto err;
  }

  /* Size of uncompressed data is stored as first 4 bytes of field */
4253
  new_size= uint4korr(res->ptr()) & 0x3FFFFFFF;
4254
  if (new_size > current_thd->variables.max_allowed_packet)
unknown's avatar
unknown committed
4255
  {
4256 4257
    THD *thd= current_thd;
    push_warning_printf(thd,Sql_condition::WARN_LEVEL_WARN,
unknown's avatar
unknown committed
4258
			ER_TOO_BIG_FOR_UNCOMPRESS,
4259 4260
			ER_THD(thd, ER_TOO_BIG_FOR_UNCOMPRESS),
                        static_cast<int>(thd->variables.
4261
                                         max_allowed_packet));
4262
    goto err;
unknown's avatar
unknown committed
4263
  }
4264 4265
  if (buffer.realloc((uint32)new_size))
    goto err;
4266

4267
  if ((err= uncompress((Byte*)buffer.ptr(), &new_size,
4268
		       ((const Bytef*)res->ptr())+4,res->length()-4)) == Z_OK)
unknown's avatar
unknown committed
4269
  {
4270
    buffer.length((uint32) new_size);
unknown's avatar
unknown committed
4271 4272
    return &buffer;
  }
4273

4274 4275
  code= ((err == Z_BUF_ERROR) ? ER_ZLIB_Z_BUF_ERROR :
	 ((err == Z_MEM_ERROR) ? ER_ZLIB_Z_MEM_ERROR : ER_ZLIB_Z_DATA_ERROR));
4276 4277 4278 4279
  {
    THD *thd= current_thd;
    push_warning(thd, Sql_condition::WARN_LEVEL_WARN, code, ER_THD(thd, code));
  }
4280 4281

err:
unknown's avatar
unknown committed
4282 4283 4284 4285
  null_value= 1;
  return 0;
}
#endif
unknown's avatar
unknown committed
4286 4287 4288 4289


String *Item_func_uuid::val_str(String *str)
{
4290
  DBUG_ASSERT(fixed == 1);
unknown's avatar
unknown committed
4291
  uchar guid[MY_UUID_SIZE];
4292

unknown's avatar
unknown committed
4293 4294
  str->realloc(MY_UUID_STRING_LENGTH+1);
  str->length(MY_UUID_STRING_LENGTH);
unknown's avatar
unknown committed
4295
  str->set_charset(system_charset_info);
unknown's avatar
unknown committed
4296 4297 4298
  my_uuid(guid);
  my_uuid2str(guid, (char *)str->ptr());

unknown's avatar
unknown committed
4299 4300
  return str;
}
4301 4302


4303 4304 4305
Item_func_dyncol_create::Item_func_dyncol_create(THD *thd, List<Item> &args,
                                                 DYNCALL_CREATE_DEF *dfs):
  Item_str_func(thd, args), defs(dfs), vals(0), keys_num(NULL), keys_str(NULL),
4306
  names(FALSE), force_names(FALSE)
4307 4308 4309 4310 4311 4312 4313
{
  DBUG_ASSERT((args.elements & 0x1) == 0); // even number of arguments
}


bool Item_func_dyncol_create::fix_fields(THD *thd, Item **ref)
{
4314
  uint i;
4315
  bool res= Item_func::fix_fields(thd, ref); // no need Item_str_func here
4316 4317 4318 4319 4320
  if (!res)
  {
    vals= (DYNAMIC_COLUMN_VALUE *) alloc_root(thd->mem_root,
                                              sizeof(DYNAMIC_COLUMN_VALUE) *
                                              (arg_count / 2));
4321 4322 4323 4324
    for (i= 0;
         i + 1 < arg_count && args[i]->result_type() == INT_RESULT;
         i+= 2)
      ;
4325 4326 4327 4328 4329
    if (i + 1 < arg_count)
    {
      names= TRUE;
    }

4330
    keys_num= (uint *) alloc_root(thd->mem_root,
4331 4332 4333 4334
                               (sizeof(LEX_STRING) > sizeof(uint) ?
                                sizeof(LEX_STRING) :
                                sizeof(uint)) *
                               (arg_count / 2));
4335
    keys_str= (LEX_STRING *) keys_num;
unknown's avatar
unknown committed
4336
    status_var_increment(thd->status_var.feature_dynamic_columns);
4337
  }
4338
  return res || vals == 0 || keys_num == 0;
4339 4340 4341 4342 4343
}


void Item_func_dyncol_create::fix_length_and_dec()
{
4344
  max_length= MAX_BLOB_WIDTH;
4345
  maybe_null= TRUE;
4346 4347 4348 4349
  collation.set(&my_charset_bin);
  decimals= 0;
}

4350
bool Item_func_dyncol_create::prepare_arguments(THD *thd, bool force_names_arg)
4351 4352 4353 4354 4355 4356
{
  char buff[STRING_BUFFER_USUAL_SIZE];
  String *res, tmp(buff, sizeof(buff), &my_charset_bin);
  uint column_count= (arg_count / 2);
  uint i;
  my_decimal dtmp, *dres;
4357
  force_names= force_names_arg;
4358

4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385
  if (!(names || force_names))
  {
    for (i= 0; i < column_count; i++)
    {
      uint valpos= i * 2 + 1;
      DYNAMIC_COLUMN_TYPE type= defs[i].type;
      if (type == DYN_COL_NULL)
        switch (args[valpos]->field_type())
        {
        case MYSQL_TYPE_VARCHAR:
        case MYSQL_TYPE_ENUM:
        case MYSQL_TYPE_SET:
        case MYSQL_TYPE_TINY_BLOB:
        case MYSQL_TYPE_MEDIUM_BLOB:
        case MYSQL_TYPE_LONG_BLOB:
        case MYSQL_TYPE_BLOB:
        case MYSQL_TYPE_VAR_STRING:
        case MYSQL_TYPE_STRING:
        case MYSQL_TYPE_GEOMETRY:
          type= DYN_COL_STRING;
          break;
        default:
          break;
        }

      if (type == DYN_COL_STRING &&
          args[valpos]->type() == Item::FUNC_ITEM &&
unknown's avatar
unknown committed
4386
          ((Item_func *)args[valpos])->functype() == DYNCOL_FUNC)
4387 4388 4389 4390 4391 4392
      {
        force_names= 1;
        break;
      }
    }
  }
4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416

  /* get values */
  for (i= 0; i < column_count; i++)
  {
    uint valpos= i * 2 + 1;
    DYNAMIC_COLUMN_TYPE type= defs[i].type;
    if (type == DYN_COL_NULL) // auto detect
    {
      /*
        We don't have a default here to ensure we get a warning if
        one adds a new not handled MYSQL_TYPE_...
      */
      switch (args[valpos]->field_type()) {
      case MYSQL_TYPE_DECIMAL:
      case MYSQL_TYPE_NEWDECIMAL:
        type= DYN_COL_DECIMAL;
        break;
      case MYSQL_TYPE_TINY:
      case MYSQL_TYPE_SHORT:
      case MYSQL_TYPE_LONG:
      case MYSQL_TYPE_LONGLONG:
      case MYSQL_TYPE_INT24:
      case MYSQL_TYPE_YEAR:
      case MYSQL_TYPE_BIT:
4417
        type= args[valpos]->unsigned_flag ? DYN_COL_UINT : DYN_COL_INT;
4418 4419 4420 4421 4422 4423 4424 4425 4426
        break;
      case MYSQL_TYPE_FLOAT:
      case MYSQL_TYPE_DOUBLE:
        type= DYN_COL_DOUBLE;
        break;
      case MYSQL_TYPE_NULL:
        type= DYN_COL_NULL;
        break;
      case MYSQL_TYPE_TIMESTAMP:
4427
      case MYSQL_TYPE_TIMESTAMP2:
4428
      case MYSQL_TYPE_DATETIME:
4429
      case MYSQL_TYPE_DATETIME2:
4430 4431 4432 4433 4434 4435 4436
        type= DYN_COL_DATETIME;
	break;
      case MYSQL_TYPE_DATE:
      case MYSQL_TYPE_NEWDATE:
        type= DYN_COL_DATE;
        break;
      case MYSQL_TYPE_TIME:
4437
      case MYSQL_TYPE_TIME2:
4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453
        type= DYN_COL_TIME;
        break;
      case MYSQL_TYPE_VARCHAR:
      case MYSQL_TYPE_ENUM:
      case MYSQL_TYPE_SET:
      case MYSQL_TYPE_TINY_BLOB:
      case MYSQL_TYPE_MEDIUM_BLOB:
      case MYSQL_TYPE_LONG_BLOB:
      case MYSQL_TYPE_BLOB:
      case MYSQL_TYPE_VAR_STRING:
      case MYSQL_TYPE_STRING:
      case MYSQL_TYPE_GEOMETRY:
        type= DYN_COL_STRING;
        break;
      }
    }
4454 4455
    if (type == DYN_COL_STRING &&
        args[valpos]->type() == Item::FUNC_ITEM &&
unknown's avatar
unknown committed
4456
        ((Item_func *)args[valpos])->functype() == DYNCOL_FUNC)
4457 4458 4459 4460
    {
      DBUG_ASSERT(names || force_names);
      type= DYN_COL_DYNCOL;
    }
4461 4462 4463 4464 4465 4466 4467 4468
    if (names || force_names)
    {
      res= args[i * 2]->val_str(&tmp);
      if (res)
      {
        // guaranty UTF-8 string for names
        if (my_charset_same(res->charset(), &my_charset_utf8_general_ci))
        {
4469
          keys_str[i].length= res->length();
4470
          keys_str[i].str= thd->strmake(res->ptr(), res->length());
4471 4472 4473
        }
        else
        {
4474
          uint strlen= res->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
4475
          uint dummy_errors;
4476
          char *str= (char *) thd->alloc(strlen);
4477 4478
          if (str)
          {
4479
            keys_str[i].length=
4480 4481 4482
              copy_and_convert(str, strlen, &my_charset_utf8_general_ci,
                               res->ptr(), res->length(), res->charset(),
                               &dummy_errors);
4483
              keys_str[i].str= str;
4484 4485
          }
          else
4486
            keys_str[i].length= 0;
4487 4488 4489 4490 4491

        }
      }
      else
      {
4492 4493
        keys_str[i].length= 0;
        keys_str[i].str= NULL;
4494 4495 4496
      }
    }
    else
4497
      keys_num[i]= (uint) args[i * 2]->val_int();
4498 4499 4500 4501 4502 4503 4504
    if (args[i * 2]->null_value)
    {
      /* to make cleanup possible */
      for (; i < column_count; i++)
        vals[i].type= DYN_COL_NULL;
      return 1;
    }
4505 4506 4507 4508 4509 4510
    vals[i].type= type;
    switch (type) {
    case DYN_COL_NULL:
      DBUG_ASSERT(args[valpos]->field_type() == MYSQL_TYPE_NULL);
      break;
    case DYN_COL_INT:
4511
      vals[i].x.long_value= args[valpos]->val_int();
4512 4513
      break;
    case DYN_COL_UINT:
4514
      vals[i].x.ulong_value= args[valpos]->val_int();
4515 4516
      break;
    case DYN_COL_DOUBLE:
4517
      vals[i].x.double_value= args[valpos]->val_real();
4518
      break;
4519
    case DYN_COL_DYNCOL:
4520 4521
    case DYN_COL_STRING:
      res= args[valpos]->val_str(&tmp);
4522 4523
      if (res && defs[i].cs)
        res->set_charset(defs[i].cs);
4524
      if (res &&
4525
          (vals[i].x.string.value.str= thd->strmake(res->ptr(), res->length())))
4526
      {
4527 4528
	vals[i].x.string.value.length= res->length();
	vals[i].x.string.charset= res->charset();
4529 4530 4531 4532
      }
      else
      {
        args[valpos]->null_value= 1;            // In case of out of memory
4533 4534
        vals[i].x.string.value.str= NULL;
        vals[i].x.string.value.length= 0;         // just to be safe
4535 4536 4537 4538 4539
      }
      break;
    case DYN_COL_DECIMAL:
      if ((dres= args[valpos]->val_decimal(&dtmp)))
      {
unknown's avatar
unknown committed
4540
	mariadb_dyncol_prepare_decimal(&vals[i]);
4541 4542 4543 4544 4545 4546
        DBUG_ASSERT(vals[i].x.decimal.value.len == dres->len);
        vals[i].x.decimal.value.intg= dres->intg;
        vals[i].x.decimal.value.frac= dres->frac;
        vals[i].x.decimal.value.sign= dres->sign();
        memcpy(vals[i].x.decimal.buffer, dres->buf,
               sizeof(vals[i].x.decimal.buffer));
4547 4548 4549
      }
      else
      {
unknown's avatar
unknown committed
4550
	mariadb_dyncol_prepare_decimal(&vals[i]); // just to be safe
4551 4552 4553 4554 4555
        DBUG_ASSERT(args[valpos]->null_value);
      }
      break;
    case DYN_COL_DATETIME:
    case DYN_COL_DATE:
Sergei Golubchik's avatar
Sergei Golubchik committed
4556
      args[valpos]->get_date(&vals[i].x.time_value,
4557
                             sql_mode_for_dates(thd));
4558 4559
      break;
    case DYN_COL_TIME:
4560
      args[valpos]->get_time(&vals[i].x.time_value);
4561 4562 4563 4564 4565 4566 4567 4568 4569 4570
      break;
    default:
      DBUG_ASSERT(0);
      vals[i].type= DYN_COL_NULL;
    }
    if (vals[i].type != DYN_COL_NULL && args[valpos]->null_value)
    {
      vals[i].type= DYN_COL_NULL;
    }
  }
4571
  return FALSE;
4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582
}


String *Item_func_dyncol_create::val_str(String *str)
{
  DYNAMIC_COLUMN col;
  String *res;
  uint column_count= (arg_count / 2);
  enum enum_dyncol_func_result rc;
  DBUG_ASSERT((arg_count & 0x1) == 0); // even number of arguments

4583 4584
  /* FIXME: add thd argument to Item::val_str() */
  if (prepare_arguments(current_thd, FALSE))
4585 4586
  {
    res= NULL;
4587
    null_value= 1;
4588 4589 4590
  }
  else
  {
4591 4592 4593
    if ((rc= ((names || force_names) ?
              mariadb_dyncol_create_many_named(&col, column_count, keys_str,
                                               vals, TRUE) :
4594 4595
              mariadb_dyncol_create_many_num(&col, column_count, keys_num,
                                             vals, TRUE))))
4596 4597
    {
      dynamic_column_error_message(rc);
4598
      mariadb_dyncol_free(&col);
4599 4600 4601 4602 4603 4604 4605 4606
      res= NULL;
      null_value= TRUE;
    }
    else
    {
      /* Move result from DYNAMIC_COLUMN to str_value */
      char *ptr;
      size_t length, alloc_length;
unknown's avatar
unknown committed
4607
      dynstr_reassociate(&col, &ptr, &length, &alloc_length);
4608
      str_value.reset(ptr, length, alloc_length, &my_charset_bin);
4609 4610 4611
      res= &str_value;
      null_value= FALSE;
    }
4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638
  }

  return res;
}

void Item_func_dyncol_create::print_arguments(String *str,
                                              enum_query_type query_type)
{
  uint i;
  uint column_count= (arg_count / 2);
  for (i= 0; i < column_count; i++)
  {
    args[i*2]->print(str, query_type);
    str->append(',');
    args[i*2 + 1]->print(str, query_type);
    switch (defs[i].type) {
    case DYN_COL_NULL: // automatic type => write nothing
      break;
    case DYN_COL_INT:
      str->append(STRING_WITH_LEN(" AS int"));
      break;
    case DYN_COL_UINT:
      str->append(STRING_WITH_LEN(" AS unsigned int"));
      break;
    case DYN_COL_DOUBLE:
      str->append(STRING_WITH_LEN(" AS double"));
      break;
4639
    case DYN_COL_DYNCOL:
4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676
    case DYN_COL_STRING:
      str->append(STRING_WITH_LEN(" AS char"));
      if (defs[i].cs)
      {
        str->append(STRING_WITH_LEN(" charset "));
        str->append(defs[i].cs->csname);
        str->append(' ');
      }
      break;
    case DYN_COL_DECIMAL:
      str->append(STRING_WITH_LEN(" AS decimal"));
      break;
    case DYN_COL_DATETIME:
      str->append(STRING_WITH_LEN(" AS datetime"));
      break;
    case DYN_COL_DATE:
      str->append(STRING_WITH_LEN(" AS date"));
      break;
    case DYN_COL_TIME:
      str->append(STRING_WITH_LEN(" AS time"));
      break;
    }
    if (i < column_count - 1)
      str->append(',');
  }
}


void Item_func_dyncol_create::print(String *str,
                                    enum_query_type query_type)
{
  DBUG_ASSERT((arg_count & 0x1) == 0); // even number of arguments
  str->append(STRING_WITH_LEN("column_create("));
  print_arguments(str, query_type);
  str->append(')');
}

4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688
String *Item_func_dyncol_json::val_str(String *str)
{
  DYNAMIC_STRING json, col;
  String *res;
  enum enum_dyncol_func_result rc;

  res= args[0]->val_str(str);
  if (args[0]->null_value)
    goto null;

  col.str= (char *)res->ptr();
  col.length= res->length();
4689
  if ((rc= mariadb_dyncol_json(&col, &json)))
4690 4691 4692 4693 4694 4695 4696 4697 4698 4699
  {
    dynamic_column_error_message(rc);
    goto null;
  }
  bzero(&col, sizeof(col));
  {
    /* Move result from DYNAMIC_COLUMN to str */
    char *ptr;
    size_t length, alloc_length;
    dynstr_reassociate(&json, &ptr, &length, &alloc_length);
4700
    str->reset(ptr, length, alloc_length, &my_charset_utf8_general_ci);
4701 4702 4703 4704 4705 4706 4707 4708 4709
    null_value= FALSE;
  }
  return str;

null:
  bzero(&col, sizeof(col));
  null_value= TRUE;
  return NULL;
}
4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720

String *Item_func_dyncol_add::val_str(String *str)
{
  DYNAMIC_COLUMN col;
  String *res;
  uint column_count=  (arg_count / 2);
  enum enum_dyncol_func_result rc;
  DBUG_ASSERT((arg_count & 0x1) == 1); // odd number of arguments

  /* We store the packed data last */
  res= args[arg_count - 1]->val_str(str);
4721 4722 4723
  if (args[arg_count - 1]->null_value ||
      init_dynamic_string(&col, NULL, res->length() + STRING_BUFFER_USUAL_SIZE,
                          STRING_BUFFER_USUAL_SIZE))
4724 4725 4726 4727 4728
    goto null;

  col.length= res->length();
  memcpy(col.str, res->ptr(), col.length);

4729 4730
  /* FIXME: add thd argument to Item::val_str() */
  if (prepare_arguments(current_thd, mariadb_dyncol_has_names(&col)))
4731
    goto null;
4732

4733 4734 4735
  if ((rc= ((names || force_names) ?
            mariadb_dyncol_update_many_named(&col, column_count,
                                             keys_str, vals) :
4736 4737
            mariadb_dyncol_update_many_num(&col, column_count,
                                           keys_num, vals))))
4738 4739
  {
    dynamic_column_error_message(rc);
4740
    mariadb_dyncol_free(&col);
4741 4742 4743 4744 4745 4746 4747
    goto null;
  }

  {
    /* Move result from DYNAMIC_COLUMN to str */
    char *ptr;
    size_t length, alloc_length;
unknown's avatar
unknown committed
4748
    dynstr_reassociate(&col, &ptr, &length, &alloc_length);
4749
    str->reset(ptr, length, alloc_length, &my_charset_bin);
4750
    null_value= FALSE;
4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771
  }

  return str;

null:
  null_value= TRUE;
  return NULL;
}


void Item_func_dyncol_add::print(String *str,
                                 enum_query_type query_type)
{
  DBUG_ASSERT((arg_count & 0x1) == 1); // odd number of arguments
  str->append(STRING_WITH_LEN("column_create("));
  args[arg_count - 1]->print(str, query_type);
  str->append(',');
  print_arguments(str, query_type);
  str->append(')');
}

4772 4773 4774 4775 4776 4777 4778 4779

/**
  Get value for a column stored in a dynamic column

  @notes
  This function ensures that null_value is set correctly
*/

4780 4781
bool Item_dyncol_get::get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val,
                                    String *tmp)
4782 4783 4784
{
  DYNAMIC_COLUMN dyn_str;
  String *res;
4785 4786 4787 4788
  longlong num= 0;
  LEX_STRING buf, *name= NULL;
  char nmstrbuf[11];
  String nmbuf(nmstrbuf, sizeof(nmstrbuf), system_charset_info);
4789 4790
  enum enum_dyncol_func_result rc;

4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808
  if (args[1]->result_type() == INT_RESULT)
    num= args[1]->val_int();
  else
  {
    String *nm= args[1]->val_str(&nmbuf);
    if (!nm || args[1]->null_value)
    {
      null_value= 1;
      return 1;
    }

    if (my_charset_same(nm->charset(), &my_charset_utf8_general_ci))
    {
      buf.str= (char *) nm->ptr();
      buf.length= nm->length();
    }
    else
    {
4809
      uint strlen= nm->length() * my_charset_utf8_general_ci.mbmaxlen + 1;
4810
      uint dummy_errors;
4811
      buf.str= (char *) thd->alloc(strlen);
4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825
      if (buf.str)
      {
        buf.length=
          copy_and_convert(buf.str, strlen, &my_charset_utf8_general_ci,
                           nm->ptr(), nm->length(), nm->charset(),
                           &dummy_errors);
      }
      else
        buf.length= 0;
    }
    name= &buf;
  }


4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840
  if (args[1]->null_value || num < 0 || num > INT_MAX)
  {
    null_value= 1;
    return 1;
  }

  res= args[0]->val_str(tmp);
  if (args[0]->null_value)
  {
    null_value= 1;
    return 1;
  }

  dyn_str.str=   (char*) res->ptr();
  dyn_str.length= res->length();
4841
  if ((rc= ((name == NULL) ?
4842
            mariadb_dyncol_get_num(&dyn_str, (uint) num, val) :
4843
            mariadb_dyncol_get_named(&dyn_str, name, val))))
4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860
  {
    dynamic_column_error_message(rc);
    null_value= 1;
    return 1;
  }

  null_value= 0;
  return 0;                                     // ok
}


String *Item_dyncol_get::val_str(String *str_result)
{
  DYNAMIC_COLUMN_VALUE val;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmp(buff, sizeof(buff), &my_charset_bin);

4861
  if (get_dyn_value(current_thd, &val, &tmp))
4862 4863 4864 4865 4866 4867 4868
    return NULL;

  switch (val.type) {
  case DYN_COL_NULL:
    goto null;
  case DYN_COL_INT:
  case DYN_COL_UINT:
4869
    str_result->set_int(val.x.long_value, MY_TEST(val.type == DYN_COL_UINT),
4870 4871 4872
                       &my_charset_latin1);
    break;
  case DYN_COL_DOUBLE:
4873
    str_result->set_real(val.x.double_value, NOT_FIXED_DEC, &my_charset_latin1);
4874
    break;
4875
  case DYN_COL_DYNCOL:
4876
  case DYN_COL_STRING:
4877 4878
    if ((char*) tmp.ptr() <= val.x.string.value.str &&
        (char*) tmp.ptr() + tmp.length() >= val.x.string.value.str)
4879 4880
    {
      /* value is allocated in tmp buffer; We have to make a copy */
4881 4882
      str_result->copy(val.x.string.value.str, val.x.string.value.length,
                      val.x.string.charset);
4883 4884 4885 4886 4887 4888 4889 4890
    }
    else
    {
      /*
        It's safe to use the current value because it's either pointing
        into a field or in a buffer for another item and this buffer
        is not going to be deleted during expression evaluation
      */
4891 4892
      str_result->set(val.x.string.value.str, val.x.string.value.length,
                      val.x.string.charset);
4893 4894 4895 4896 4897
    }
    break;
  case DYN_COL_DECIMAL:
  {
    int res;
4898
    int length= decimal_string_size(&val.x.decimal.value);
4899 4900
    if (str_result->alloc(length))
      goto null;
4901
    if ((res= decimal2string(&val.x.decimal.value, (char*) str_result->ptr(),
4902 4903 4904 4905 4906
                             &length, 0, 0, ' ')) != E_DEC_OK)
    {
      char buff[40];
      int len= sizeof(buff);
      DBUG_ASSERT(length < (int)sizeof(buff));
4907
      decimal2string(&val.x.decimal.value, buff, &len, 0, 0, ' ');
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918
      decimal_operation_results(res, buff, "CHAR");
    }
    str_result->set_charset(&my_charset_latin1);
    str_result->length(length);
    break;
  }
  case DYN_COL_DATETIME:
  case DYN_COL_DATE:
  case DYN_COL_TIME:
  {
    int length;
4919 4920 4921 4922 4923
    /*
      We use AUTO_SEC_PART_DIGITS here to ensure that we do not loose
      any microseconds from the data. This is safe to do as we are
      asked to return the time argument as a string.
    */
4924
    if (str_result->alloc(MAX_DATE_STRING_REP_LENGTH) ||
4925
        !(length= my_TIME_to_str(&val.x.time_value, (char*) str_result->ptr(),
4926
                                 AUTO_SEC_PART_DIGITS)))
4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942
      goto null;
    str_result->set_charset(&my_charset_latin1);
    str_result->length(length);
    break;
  }
  }
  return str_result;

null:
  null_value= TRUE;
  return 0;
}


longlong Item_dyncol_get::val_int()
{
4943
  THD *thd= current_thd;
4944 4945 4946 4947
  DYNAMIC_COLUMN_VALUE val;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmp(buff, sizeof(buff), &my_charset_bin);

4948
  if (get_dyn_value(thd, &val, &tmp))
4949 4950 4951
    return 0;

  switch (val.type) {
4952
  case DYN_COL_DYNCOL:
4953 4954 4955 4956
  case DYN_COL_NULL:
    goto null;
  case DYN_COL_UINT:
    unsigned_flag= 1;            // Make it possible for caller to detect sign
4957
    return val.x.long_value;
4958 4959
  case DYN_COL_INT:
    unsigned_flag= 0;            // Make it possible for caller to detect sign
4960
    return val.x.long_value;
4961
  case DYN_COL_DOUBLE:
4962 4963
    return Converter_double_to_longlong_with_warn(thd, val.x.double_value,
                                                  unsigned_flag).result();
4964 4965 4966 4967
  case DYN_COL_STRING:
  {
    int error;
    longlong num;
4968
    char *end= val.x.string.value.str + val.x.string.value.length, *org_end= end;
4969

4970
    num= my_strtoll10(val.x.string.value.str, &end, &error);
4971 4972
    if (end != org_end || error > 0)
    {
4973
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
4974
                          ER_BAD_DATA,
4975
                          ER_THD(thd, ER_BAD_DATA),
4976 4977 4978
                          ErrConvString(val.x.string.value.str,
                                        val.x.string.value.length,
                                        val.x.string.charset).ptr(),
4979 4980 4981 4982 4983 4984 4985 4986
                          unsigned_flag ? "UNSIGNED INT" : "INT");
    }
    unsigned_flag= error >= 0;
    return num;
  }
  case DYN_COL_DECIMAL:
  {
    longlong num;
4987
    my_decimal2int(E_DEC_FATAL_ERROR, &val.x.decimal.value, unsigned_flag,
4988 4989 4990 4991 4992 4993
                   &num);
    return num;
  }
  case DYN_COL_DATETIME:
  case DYN_COL_DATE:
  case DYN_COL_TIME:
4994
    unsigned_flag= !val.x.time_value.neg;
Sergei Golubchik's avatar
Sergei Golubchik committed
4995
    if (unsigned_flag)
4996
      return TIME_to_ulonglong(&val.x.time_value);
Sergei Golubchik's avatar
Sergei Golubchik committed
4997
    else
4998
      return -(longlong)TIME_to_ulonglong(&val.x.time_value);
4999 5000 5001 5002 5003 5004 5005 5006 5007 5008
  }

null:
  null_value= TRUE;
  return 0;
}


double Item_dyncol_get::val_real()
{
5009
  THD *thd= current_thd;
5010 5011 5012 5013
  DYNAMIC_COLUMN_VALUE val;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmp(buff, sizeof(buff), &my_charset_bin);

5014
  if (get_dyn_value(thd, &val, &tmp))
5015 5016 5017
    return 0.0;

  switch (val.type) {
5018
  case DYN_COL_DYNCOL:
5019 5020 5021
  case DYN_COL_NULL:
    goto null;
  case DYN_COL_UINT:
5022
    return ulonglong2double(val.x.ulong_value);
5023
  case DYN_COL_INT:
5024
    return (double) val.x.long_value;
5025
  case DYN_COL_DOUBLE:
5026
    return (double) val.x.double_value;
5027 5028 5029 5030
  case DYN_COL_STRING:
  {
    int error;
    char *end;
5031 5032
    double res= my_strntod(val.x.string.charset, (char*) val.x.string.value.str,
                           val.x.string.value.length, &end, &error);
5033

5034
    if (end != (char*) val.x.string.value.str + val.x.string.value.length ||
5035 5036
        error)
    {
5037
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
5038
                          ER_BAD_DATA,
5039
                          ER_THD(thd, ER_BAD_DATA),
5040 5041 5042 5043
                          ErrConvString(val.x.string.value.str,
                                        val.x.string.value.length,
                                        val.x.string.charset).ptr(),
                          "DOUBLE");
5044 5045 5046 5047 5048 5049 5050
    }
    return res;
  }
  case DYN_COL_DECIMAL:
  {
    double res;
    /* This will always succeed */
5051
    decimal2double(&val.x.decimal.value, &res);
5052 5053 5054 5055 5056
    return res;
  }
  case DYN_COL_DATETIME:
  case DYN_COL_DATE:
  case DYN_COL_TIME:
5057
    return TIME_to_double(&val.x.time_value);
5058 5059 5060 5061 5062 5063 5064 5065 5066 5067
  }

null:
  null_value= TRUE;
  return 0.0;
}


my_decimal *Item_dyncol_get::val_decimal(my_decimal *decimal_value)
{
5068
  THD *thd= current_thd;
5069 5070 5071 5072
  DYNAMIC_COLUMN_VALUE val;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmp(buff, sizeof(buff), &my_charset_bin);

5073
  if (get_dyn_value(thd, &val, &tmp))
5074 5075 5076
    return NULL;

  switch (val.type) {
5077
  case DYN_COL_DYNCOL:
5078 5079 5080
  case DYN_COL_NULL:
    goto null;
  case DYN_COL_UINT:
5081
    int2my_decimal(E_DEC_FATAL_ERROR, val.x.long_value, TRUE, decimal_value);
5082 5083
    break;
  case DYN_COL_INT:
5084
    int2my_decimal(E_DEC_FATAL_ERROR, val.x.long_value, FALSE, decimal_value);
5085 5086
    break;
  case DYN_COL_DOUBLE:
5087
    double2my_decimal(E_DEC_FATAL_ERROR, val.x.double_value, decimal_value);
5088 5089 5090
    break;
  case DYN_COL_STRING:
  {
5091
    const char *end;
5092
    int rc;
5093
    rc= str2my_decimal(0, val.x.string.value.str, val.x.string.value.length,
5094 5095 5096
                       val.x.string.charset, decimal_value, &end);
    if (rc != E_DEC_OK ||
        end != val.x.string.value.str + val.x.string.value.length)
5097
    {
5098
      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
5099
                          ER_BAD_DATA,
5100
                          ER_THD(thd, ER_BAD_DATA),
5101 5102 5103 5104
                          ErrConvString(val.x.string.value.str,
                                        val.x.string.value.length,
                                        val.x.string.charset).ptr(),
                          "DECIMAL");
5105 5106 5107 5108
    }
    break;
  }
  case DYN_COL_DECIMAL:
5109
    decimal2my_decimal(&val.x.decimal.value, decimal_value);
5110 5111 5112 5113
    break;
  case DYN_COL_DATETIME:
  case DYN_COL_DATE:
  case DYN_COL_TIME:
Sergei Golubchik's avatar
Sergei Golubchik committed
5114
    decimal_value= TIME_to_my_decimal(&val.x.time_value, decimal_value);
5115 5116 5117 5118 5119 5120 5121 5122 5123 5124
    break;
  }
  return decimal_value;

null:
  null_value= TRUE;
  return 0;
}


5125
bool Item_dyncol_get::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
5126 5127 5128 5129 5130 5131
{
  DYNAMIC_COLUMN_VALUE val;
  char buff[STRING_BUFFER_USUAL_SIZE];
  String tmp(buff, sizeof(buff), &my_charset_bin);
  bool signed_value= 0;

5132
  if (get_dyn_value(current_thd, &val, &tmp))
5133
    return 1;                                   // Error
5134 5135

  switch (val.type) {
5136
  case DYN_COL_DYNCOL:
5137 5138 5139 5140 5141 5142
  case DYN_COL_NULL:
    goto null;
  case DYN_COL_INT:
    signed_value= 1;                                  // For error message
    /* fall_trough */
  case DYN_COL_UINT:
5143
    if (signed_value || val.x.ulong_value <= LONGLONG_MAX)
5144
    {
5145 5146 5147 5148
      bool neg= val.x.ulong_value > LONGLONG_MAX;
      if (int_to_datetime_with_warn(neg, neg ? -val.x.ulong_value :
                                                val.x.ulong_value,
                                    ltime, fuzzy_date, 0 /* TODO */))
Sergei Golubchik's avatar
Sergei Golubchik committed
5149 5150
        goto null;
      return 0;
5151
    }
Sergei Golubchik's avatar
Sergei Golubchik committed
5152
    /* let double_to_datetime_with_warn() issue the warning message */
5153
    val.x.double_value= static_cast<double>(ULONGLONG_MAX);
Sergei Golubchik's avatar
Sergei Golubchik committed
5154
    /* fall_trough */
5155
  case DYN_COL_DOUBLE:
5156
    if (double_to_datetime_with_warn(val.x.double_value, ltime, fuzzy_date,
Sergei Golubchik's avatar
Sergei Golubchik committed
5157
                                     0 /* TODO */))
5158 5159 5160
      goto null;
    return 0;
  case DYN_COL_DECIMAL:
5161
    if (decimal_to_datetime_with_warn((my_decimal*)&val.x.decimal.value, ltime,
Sergei Golubchik's avatar
Sergei Golubchik committed
5162
                                      fuzzy_date, 0 /* TODO */))
5163 5164 5165
      goto null;
    return 0;
  case DYN_COL_STRING:
Sergei Golubchik's avatar
Sergei Golubchik committed
5166
    if (str_to_datetime_with_warn(&my_charset_numeric,
Sergei Golubchik's avatar
Sergei Golubchik committed
5167
                                  val.x.string.value.str,
5168
                                  val.x.string.value.length,
5169
                                  ltime, fuzzy_date))
5170 5171 5172 5173 5174
      goto null;
    return 0;
  case DYN_COL_DATETIME:
  case DYN_COL_DATE:
  case DYN_COL_TIME:
5175
    *ltime= val.x.time_value;
5176 5177 5178 5179 5180 5181 5182 5183 5184 5185
    return 0;
  }

null:
  null_value= TRUE;
  return 1;
}

void Item_dyncol_get::print(String *str, enum_query_type query_type)
{
5186 5187 5188 5189 5190 5191 5192 5193 5194 5195
  /*
    Parent cast doesn't exist yet, only print dynamic column name. This happens
    when called from create_func_cast() / wrong_precision_error().
  */
  if (!str->length())
  {
    args[1]->print(str, query_type);
    return;
  }

unknown's avatar
unknown committed
5196 5197 5198 5199 5200
  /* see create_func_dyncol_get */
  DBUG_ASSERT(str->length() >= 5);
  DBUG_ASSERT(strncmp(str->ptr() + str->length() - 5, "cast(", 5) == 0);

  str->length(str->length() - 5);    // removing "cast("
5201 5202 5203 5204
  str->append(STRING_WITH_LEN("column_get("));
  args[0]->print(str, query_type);
  str->append(',');
  args[1]->print(str, query_type);
unknown's avatar
unknown committed
5205
  /* let the parent cast item add " as <type>)" */
5206 5207 5208 5209 5210 5211 5212
}


String *Item_func_dyncol_list::val_str(String *str)
{
  uint i;
  enum enum_dyncol_func_result rc;
5213 5214
  LEX_STRING *names= 0;
  uint count;
5215 5216 5217 5218 5219 5220 5221 5222
  DYNAMIC_COLUMN col;
  String *res= args[0]->val_str(str);

  if (args[0]->null_value)
    goto null;
  col.length= res->length();
  /* We do not change the string, so could do this trick */
  col.str= (char *)res->ptr();
5223
  if ((rc= mariadb_dyncol_list_named(&col, &count, &names)))
5224
  {
5225
    bzero(&col, sizeof(col));
5226 5227 5228
    dynamic_column_error_message(rc);
    goto null;
  }
5229
  bzero(&col, sizeof(col));
5230 5231

  /*
5232
    We estimate average name length as 10
5233
  */
5234
  if (str->alloc(count * 13))
5235 5236 5237
    goto null;

  str->length(0);
5238
  str->set_charset(&my_charset_utf8_general_ci);
5239
  for (i= 0; i < count; i++)
5240
  {
5241 5242
    append_identifier(current_thd, str, names[i].str, names[i].length);
    if (i < count - 1)
5243 5244
      str->qs_append(',');
  }
5245
  null_value= FALSE;
5246 5247
  if (names)
    my_free(names);
5248 5249 5250 5251
  return str;

null:
  null_value= TRUE;
5252 5253
  if (names)
    my_free(names);
5254 5255
  return NULL;
}
5256