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
102
String *Item_func_reverse::val_str(String *str)
431
104
assert(fixed == 1);