46
46
String my_empty_string("",default_charset_info);
51
bool Item_str_func::fix_fields(Session *session, Item **ref)
53
bool res= Item_func::fix_fields(session, ref);
55
In Item_str_func::check_well_formed_result() we may set null_value
56
flag on the same condition as in test() below.
58
maybe_null= (maybe_null || true);
63
my_decimal *Item_str_func::val_decimal(my_decimal *decimal_value)
67
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
71
(void)str2my_decimal(E_DEC_FATAL_ERROR, (char*) res->ptr(),
72
res->length(), res->charset(), decimal_value);
77
double Item_str_func::val_real()
81
char *end_not_used, buff[64];
82
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
84
return res ? my_strntod(res->charset(), (char*) res->ptr(), res->length(),
85
&end_not_used, &err_not_used) : 0.0;
89
int64_t Item_str_func::val_int()
94
String *res, tmp(buff,sizeof(buff), &my_charset_bin);
97
my_strntoll(res->charset(), res->ptr(), res->length(), 10, NULL,
103
Concatenate args with the following premises:
104
If only one arg (which is ok), return value of arg;
105
Don't reallocate val_str() if not absolute necessary.
108
String *Item_func_concat::val_str(String *str)
111
String *res,*res2,*use_as_buff;
116
if (!(res=args[0]->val_str(str)))
118
use_as_buff= &tmp_value;
119
/* Item_subselect in --ps-protocol mode will state it as a non-const */
120
is_const= args[0]->const_item() || !args[0]->used_tables();
121
for (i=1 ; i < arg_count ; i++)
123
if (res->length() == 0)
125
if (!(res=args[i]->val_str(str)))
130
if (!(res2=args[i]->val_str(use_as_buff)))
132
if (res2->length() == 0)
134
if (res->length()+res2->length() >
135
current_session->variables.max_allowed_packet)
137
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
138
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
139
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
140
current_session->variables.max_allowed_packet);
143
if (!is_const && res->alloced_length() >= res->length()+res2->length())
147
else if (str->alloced_length() >= res->length()+res2->length())
150
str->replace(0,0,*res);
157
use_as_buff= &tmp_value;
159
else if (res == &tmp_value)
161
if (res->append(*res2)) // Must be a blob
164
else if (res2 == &tmp_value)
165
{ // This can happend only 1 time
166
if (tmp_value.replace(0,0,*res))
169
use_as_buff=str; // Put next arg here
171
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
172
res2->ptr() <= tmp_value.ptr() + tmp_value.alloced_length())
175
This happens really seldom:
176
In this case res2 is sub string of tmp_value. We will
177
now work in place in tmp_value to set it to res | res2
179
/* Chop the last characters in tmp_value that isn't in res2 */
180
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
182
/* Place res2 at start of tmp_value, remove chars before res2 */
183
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
187
use_as_buff=str; // Put next arg here
190
{ // Two big const strings
192
NOTE: We should be prudent in the initial allocation unit -- the
193
size of the arguments is a function of data distribution, which
194
can be any. Instead of overcommitting at the first row, we grow
195
the allocated amount by the factor of 2. This ensures that no
196
more than 25% of memory will be overcommitted on average.
199
uint32_t concat_len= res->length() + res2->length();
201
if (tmp_value.alloced_length() < concat_len)
203
if (tmp_value.alloced_length() == 0)
205
if (tmp_value.alloc(concat_len))
210
uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
212
if (tmp_value.realloc(new_len))
217
if (tmp_value.copy(*res) || tmp_value.append(*res2))
226
res->set_charset(collation.collation);
235
void Item_func_concat::fix_length_and_dec()
237
uint64_t max_result_length= 0;
239
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
242
for (uint32_t i=0 ; i < arg_count ; i++)
244
if (args[i]->collation.collation->mbmaxlen != collation.collation->mbmaxlen)
245
max_result_length+= (args[i]->max_length /
246
args[i]->collation.collation->mbmaxlen) *
247
collation.collation->mbmaxlen;
249
max_result_length+= args[i]->max_length;
252
if (max_result_length >= MAX_BLOB_WIDTH)
254
max_result_length= MAX_BLOB_WIDTH;
257
max_length= (ulong) max_result_length;
262
concat with separator. First arg is the separator
263
concat_ws takes at least two arguments.
266
String *Item_func_concat_ws::val_str(String *str)
269
char tmp_str_buff[10];
270
String tmp_sep_str(tmp_str_buff, sizeof(tmp_str_buff),default_charset_info),
271
*sep_str, *res, *res2,*use_as_buff;
275
if (!(sep_str= args[0]->val_str(&tmp_sep_str)))
278
use_as_buff= &tmp_value;
279
str->length(0); // QQ; Should be removed
282
// Skip until non-null argument is found.
283
// If not, return the empty string
284
for (i=1; i < arg_count; i++)
285
if ((res= args[i]->val_str(str)))
288
return &my_empty_string;
290
for (i++; i < arg_count ; i++)
292
if (!(res2= args[i]->val_str(use_as_buff)))
293
continue; // Skip NULL
295
if (res->length() + sep_str->length() + res2->length() >
296
current_session->variables.max_allowed_packet)
298
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
299
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
300
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
301
current_session->variables.max_allowed_packet);
304
if (res->alloced_length() >=
305
res->length() + sep_str->length() + res2->length())
307
res->append(*sep_str); // res->length() > 0 always
310
else if (str->alloced_length() >=
311
res->length() + sep_str->length() + res2->length())
313
/* We have room in str; We can't get any errors here */
315
{ // This is quote uncommon!
316
str->replace(0,0,*sep_str);
317
str->replace(0,0,*res);
322
str->append(*sep_str);
326
use_as_buff= &tmp_value;
328
else if (res == &tmp_value)
330
if (res->append(*sep_str) || res->append(*res2))
331
goto null; // Must be a blob
333
else if (res2 == &tmp_value)
334
{ // This can happend only 1 time
335
if (tmp_value.replace(0,0,*sep_str) || tmp_value.replace(0,0,*res))
338
use_as_buff=str; // Put next arg here
340
else if (tmp_value.is_alloced() && res2->ptr() >= tmp_value.ptr() &&
341
res2->ptr() < tmp_value.ptr() + tmp_value.alloced_length())
344
This happens really seldom:
345
In this case res2 is sub string of tmp_value. We will
346
now work in place in tmp_value to set it to res | sep_str | res2
348
/* Chop the last characters in tmp_value that isn't in res2 */
349
tmp_value.length((uint32_t) (res2->ptr() - tmp_value.ptr()) +
351
/* Place res2 at start of tmp_value, remove chars before res2 */
352
if (tmp_value.replace(0,(uint32_t) (res2->ptr() - tmp_value.ptr()),
354
tmp_value.replace(res->length(),0, *sep_str))
357
use_as_buff=str; // Put next arg here
360
{ // Two big const strings
362
NOTE: We should be prudent in the initial allocation unit -- the
363
size of the arguments is a function of data distribution, which can
364
be any. Instead of overcommitting at the first row, we grow the
365
allocated amount by the factor of 2. This ensures that no more than
366
25% of memory will be overcommitted on average.
369
uint32_t concat_len= res->length() + sep_str->length() + res2->length();
371
if (tmp_value.alloced_length() < concat_len)
373
if (tmp_value.alloced_length() == 0)
375
if (tmp_value.alloc(concat_len))
380
uint32_t new_len = cmax(tmp_value.alloced_length() * 2, concat_len);
382
if (tmp_value.realloc(new_len))
387
if (tmp_value.copy(*res) ||
388
tmp_value.append(*sep_str) ||
389
tmp_value.append(*res2))
395
res->set_charset(collation.collation);
404
void Item_func_concat_ws::fix_length_and_dec()
406
uint64_t max_result_length;
408
if (agg_arg_charsets(collation, args, arg_count, MY_COLL_ALLOW_CONV, 1))
412
arg_count cannot be less than 2,
413
it is done on parser level in sql_yacc.yy
414
so, (arg_count - 2) is safe here.
416
max_result_length= (uint64_t) args[0]->max_length * (arg_count - 2);
417
for (uint32_t i=1 ; i < arg_count ; i++)
418
max_result_length+=args[i]->max_length;
420
if (max_result_length >= MAX_BLOB_WIDTH)
422
max_result_length= MAX_BLOB_WIDTH;
425
max_length= (ulong) max_result_length;
429
String *Item_func_reverse::val_str(String *str)
432
String *res = args[0]->val_str(str);
433
char *ptr, *end, *tmp;
435
if ((null_value=args[0]->null_value))
437
/* An empty string is a special case as the string pointer may be null */
439
return &my_empty_string;
440
if (tmp_value.alloced_length() < res->length() &&
441
tmp_value.realloc(res->length()))
446
tmp_value.length(res->length());
447
tmp_value.set_charset(res->charset());
448
ptr= (char *) res->ptr();
449
end= ptr + res->length();
450
tmp= (char *) tmp_value.ptr() + tmp_value.length();
452
if (use_mb(res->charset()))
457
if ((l= my_ismbchar(res->charset(),ptr,end)))
477
void Item_func_reverse::fix_length_and_dec()
479
collation.set(args[0]->collation);
480
max_length = args[0]->max_length;
484
Replace all occurences of string2 in string1 with string3.
486
Don't reallocate val_str() if not needed.
489
Fix that this works with binary strings when using USE_MB
492
String *Item_func_replace::val_str(String *str)
495
String *res,*res2,*res3;
497
uint32_t from_length,to_length;
500
const char *ptr,*end,*strend,*search,*search_end;
506
res=args[0]->val_str(str);
507
if (args[0]->null_value)
509
res2=args[1]->val_str(&tmp_value);
510
if (args[1]->null_value)
513
res->set_charset(collation.collation);
516
binary_cmp = ((res->charset()->state & MY_CS_BINSORT) || !use_mb(res->charset()));
519
if (res2->length() == 0)
522
if ((offset=res->strstr(*res2)) < 0)
526
if (binary_cmp && (offset=res->strstr(*res2)) < 0)
529
if (!(res3=args[2]->val_str(&tmp_value2)))
531
from_length= res2->length();
532
to_length= res3->length();
538
search_end=search+from_length;
540
ptr=res->ptr()+offset;
541
strend=res->ptr()+res->length();
542
end=strend-from_length+1;
548
i=(char*) ptr+1; j=(char*) search+1;
549
while (j != search_end)
550
if (*i++ != *j++) goto skip;
551
offset= (int) (ptr-res->ptr());
552
if (res->length()-from_length + to_length >
553
current_session->variables.max_allowed_packet)
555
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
556
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
557
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
559
current_session->variables.max_allowed_packet);
566
res=copy_if_not_alloced(str,res,res->length()+to_length);
568
res->replace((uint) offset,from_length,*res3);
569
offset+=(int) to_length;
573
if ((l=my_ismbchar(res->charset(), ptr,strend))) ptr+=l;
581
if (res->length()-from_length + to_length >
582
current_session->variables.max_allowed_packet)
584
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
585
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
586
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED), func_name(),
587
current_session->variables.max_allowed_packet);
593
res=copy_if_not_alloced(str,res,res->length()+to_length);
595
res->replace((uint) offset,from_length,*res3);
596
offset+=(int) to_length;
598
while ((offset=res->strstr(*res2,(uint) offset)) >= 0);
607
void Item_func_replace::fix_length_and_dec()
609
uint64_t max_result_length= args[0]->max_length;
610
int diff=(int) (args[2]->max_length - args[1]->max_length);
611
if (diff > 0 && args[1]->max_length)
612
{ // Calculate of maxreplaces
613
uint64_t max_substrs= max_result_length/args[1]->max_length;
614
max_result_length+= max_substrs * (uint) diff;
616
if (max_result_length >= MAX_BLOB_WIDTH)
618
max_result_length= MAX_BLOB_WIDTH;
621
max_length= (ulong) max_result_length;
623
if (agg_arg_charsets(collation, args, 3, MY_COLL_CMP_CONV, 1))
628
String *Item_func_insert::val_str(String *str)
632
int64_t start, length; /* must be int64_t to avoid truncation */
635
res=args[0]->val_str(str);
636
res2=args[3]->val_str(&tmp_value);
637
start= args[1]->val_int() - 1;
638
length= args[2]->val_int();
640
if (args[0]->null_value || args[1]->null_value || args[2]->null_value ||
642
goto null; /* purecov: inspected */
644
if ((start < 0) || (start > res->length()))
645
return res; // Wrong param; skip insert
646
if ((length < 0) || (length > res->length()))
647
length= res->length();
649
/* start and length are now sufficiently valid to pass to charpos function */
650
start= res->charpos((int) start);
651
length= res->charpos((int) length, (uint32_t) start);
653
/* Re-testing with corrected params */
654
if (start > res->length())
655
return res; /* purecov: inspected */ // Wrong param; skip insert
656
if (length > res->length() - start)
657
length= res->length() - start;
659
if ((uint64_t) (res->length() - length + res2->length()) >
660
(uint64_t) current_session->variables.max_allowed_packet)
662
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
663
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
664
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
665
func_name(), current_session->variables.max_allowed_packet);
668
res=copy_if_not_alloced(str,res,res->length());
669
res->replace((uint32_t) start,(uint32_t) length,*res2);
677
void Item_func_insert::fix_length_and_dec()
679
uint64_t max_result_length;
681
// Handle character set for args[0] and args[3].
682
if (agg_arg_charsets(collation, &args[0], 2, MY_COLL_ALLOW_CONV, 3))
684
max_result_length= ((uint64_t) args[0]->max_length+
685
(uint64_t) args[3]->max_length);
686
if (max_result_length >= MAX_BLOB_WIDTH)
688
max_result_length= MAX_BLOB_WIDTH;
691
max_length= (ulong) max_result_length;
695
String *Item_str_conv::val_str(String *str)
699
if (!(res=args[0]->val_str(str)))
701
null_value=1; /* purecov: inspected */
702
return 0; /* purecov: inspected */
708
res= copy_if_not_alloced(str,res,res->length());
709
len= converter(collation.collation, (char*) res->ptr(), res->length(),
710
(char*) res->ptr(), res->length());
711
assert(len <= res->length());
716
uint32_t len= res->length() * multiply;
717
tmp_value.alloc(len);
718
tmp_value.set_charset(collation.collation);
719
len= converter(collation.collation, (char*) res->ptr(), res->length(),
720
(char*) tmp_value.ptr(), len);
721
tmp_value.length(len);
728
void Item_func_lcase::fix_length_and_dec()
730
collation.set(args[0]->collation);
731
multiply= collation.collation->casedn_multiply;
732
converter= collation.collation->cset->casedn;
733
max_length= args[0]->max_length * multiply;
736
void Item_func_ucase::fix_length_and_dec()
738
collation.set(args[0]->collation);
739
multiply= collation.collation->caseup_multiply;
740
converter= collation.collation->cset->caseup;
741
max_length= args[0]->max_length * multiply;
745
String *Item_func_left::val_str(String *str)
748
String *res= args[0]->val_str(str);
750
/* must be int64_t to avoid truncation */
751
int64_t length= args[1]->val_int();
754
if ((null_value=(args[0]->null_value || args[1]->null_value)))
757
/* if "unsigned_flag" is set, we have a *huge* positive number. */
758
if ((length <= 0) && (!args[1]->unsigned_flag))
759
return &my_empty_string;
761
if ((res->length() <= (uint64_t) length) ||
762
(res->length() <= (char_pos= res->charpos((int) length))))
765
tmp_value.set(*res, 0, char_pos);
770
void Item_str_func::left_right_max_length()
772
max_length=args[0]->max_length;
773
if (args[1]->const_item())
775
int length=(int) args[1]->val_int()*collation.collation->mbmaxlen;
779
set_if_smaller(max_length,(uint) length);
784
void Item_func_left::fix_length_and_dec()
786
collation.set(args[0]->collation);
787
left_right_max_length();
791
String *Item_func_right::val_str(String *str)
794
String *res= args[0]->val_str(str);
795
/* must be int64_t to avoid truncation */
796
int64_t length= args[1]->val_int();
798
if ((null_value=(args[0]->null_value || args[1]->null_value)))
799
return 0; /* purecov: inspected */
801
/* if "unsigned_flag" is set, we have a *huge* positive number. */
802
if ((length <= 0) && (!args[1]->unsigned_flag))
803
return &my_empty_string; /* purecov: inspected */
805
if (res->length() <= (uint64_t) length)
806
return res; /* purecov: inspected */
808
uint32_t start=res->numchars();
809
if (start <= (uint) length)
811
start=res->charpos(start - (uint) length);
812
tmp_value.set(*res,start,res->length()-start);
817
void Item_func_right::fix_length_and_dec()
819
collation.set(args[0]->collation);
820
left_right_max_length();
824
48
String *Item_func_substr::val_str(String *str)
826
50
assert(fixed == 1);