1 #ifndef ITEM_STRFUNC_INCLUDED
2 #define ITEM_STRFUNC_INCLUDED
3 
4 /*
5    Copyright (c) 2000, 2011, Oracle and/or its affiliates.
6    Copyright (c) 2009, 2019, MariaDB
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; version 2 of the License.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */
20 
21 
22 /* This file defines all string functions */
23 
24 #ifdef USE_PRAGMA_INTERFACE
25 #pragma interface			/* gcc class implementation */
26 #endif
27 
28 extern size_t username_char_length;
29 
30 class Item_str_func :public Item_func
31 {
32 protected:
33   /**
34      Sets the result value of the function an empty string, using the current
35      character set. No memory is allocated.
36      @retval A pointer to the str_value member.
37    */
38   virtual String *make_empty_result()
39   {
40     /*
41       Reset string length to an empty string. We don't use str_value.set() as
42       we don't want to free and potentially have to reallocate the buffer
43       for each call.
44     */
45     str_value.length(0);
46     str_value.set_charset(collation.collation);
47     return &str_value;
48   }
49 public:
50   Item_str_func(THD *thd): Item_func(thd) { decimals=NOT_FIXED_DEC; }
51   Item_str_func(THD *thd, Item *a): Item_func(thd, a) {decimals=NOT_FIXED_DEC; }
52   Item_str_func(THD *thd, Item *a, Item *b):
53     Item_func(thd, a, b) { decimals=NOT_FIXED_DEC; }
54   Item_str_func(THD *thd, Item *a, Item *b, Item *c):
55     Item_func(thd, a, b, c) { decimals=NOT_FIXED_DEC; }
56   Item_str_func(THD *thd, Item *a, Item *b, Item *c, Item *d):
57     Item_func(thd, a, b, c, d) { decimals=NOT_FIXED_DEC; }
58   Item_str_func(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e):
59     Item_func(thd, a, b, c, d, e) { decimals=NOT_FIXED_DEC; }
60   Item_str_func(THD *thd, List<Item> &list):
61     Item_func(thd, list) { decimals=NOT_FIXED_DEC; }
62   longlong val_int();
63   double val_real();
64   my_decimal *val_decimal(my_decimal *);
65   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
66   { return get_date_from_string(ltime, fuzzydate); }
67   const Type_handler *type_handler() const { return string_type_handler(); }
68   void left_right_max_length();
69   bool fix_fields(THD *thd, Item **ref);
70   void update_null_value()
71   {
72     StringBuffer<MAX_FIELD_WIDTH> tmp;
73     (void) val_str(&tmp);
74   }
75 };
76 
77 
78 
79 /*
80   Functions that return values with ASCII repertoire
81 */
82 class Item_str_ascii_func :public Item_str_func
83 {
84   String ascii_buf;
85 public:
86   Item_str_ascii_func(THD *thd): Item_str_func(thd) {}
87   Item_str_ascii_func(THD *thd, Item *a): Item_str_func(thd, a) {}
88   Item_str_ascii_func(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
89   Item_str_ascii_func(THD *thd, Item *a, Item *b, Item *c):
90     Item_str_func(thd, a, b, c) {}
91   String *val_str(String *str)
92   {
93     return val_str_from_val_str_ascii(str, &ascii_buf);
94   }
95   String *val_str_ascii(String *)= 0;
96 };
97 
98 
99 /**
100   Functions that return a checksum or a hash of the argument,
101   or somehow else encode or decode the argument,
102   returning an ASCII-repertoire string.
103 */
104 class Item_str_ascii_checksum_func: public Item_str_ascii_func
105 {
106 public:
107   Item_str_ascii_checksum_func(THD *thd, Item *a)
108    :Item_str_ascii_func(thd, a) { }
109   Item_str_ascii_checksum_func(THD *thd, Item *a, Item *b)
110    :Item_str_ascii_func(thd, a, b) { }
111   bool eq(const Item *item, bool binary_cmp) const
112   {
113     // Always use binary argument comparison: MD5('x') != MD5('X')
114     return Item_func::eq(item, true);
115   }
116 };
117 
118 
119 /**
120   Functions that return a checksum or a hash of the argument,
121   or somehow else encode or decode the argument,
122   returning a binary string.
123 */
124 class Item_str_binary_checksum_func: public Item_str_func
125 {
126 public:
127   Item_str_binary_checksum_func(THD *thd, Item *a)
128    :Item_str_func(thd, a) { }
129   Item_str_binary_checksum_func(THD *thd, Item *a, Item *b)
130    :Item_str_func(thd, a, b) { }
131   bool eq(const Item *item, bool binary_cmp) const
132   {
133     /*
134       Always use binary argument comparison:
135         FROM_BASE64('test') != FROM_BASE64('TEST')
136     */
137     return Item_func::eq(item, true);
138   }
139 };
140 
141 
142 class Item_func_md5 :public Item_str_ascii_checksum_func
143 {
144 public:
145   Item_func_md5(THD *thd, Item *a): Item_str_ascii_checksum_func(thd, a) {}
146   String *val_str_ascii(String *);
147   bool fix_length_and_dec()
148   {
149     fix_length_and_charset(32, default_charset());
150     return FALSE;
151   }
152   const char *func_name() const { return "md5"; }
153   Item *get_copy(THD *thd)
154   { return get_item_copy<Item_func_md5>(thd, this); }
155 };
156 
157 
158 class Item_func_sha :public Item_str_ascii_checksum_func
159 {
160 public:
161   Item_func_sha(THD *thd, Item *a): Item_str_ascii_checksum_func(thd, a) {}
162   String *val_str_ascii(String *);
163   bool fix_length_and_dec();
164   const char *func_name() const { return "sha"; }
165   Item *get_copy(THD *thd)
166   { return get_item_copy<Item_func_sha>(thd, this); }
167 };
168 
169 class Item_func_sha2 :public Item_str_ascii_checksum_func
170 {
171 public:
172   Item_func_sha2(THD *thd, Item *a, Item *b)
173    :Item_str_ascii_checksum_func(thd, a, b) {}
174   String *val_str_ascii(String *);
175   bool fix_length_and_dec();
176   const char *func_name() const { return "sha2"; }
177   Item *get_copy(THD *thd)
178   { return get_item_copy<Item_func_sha2>(thd, this); }
179 };
180 
181 class Item_func_to_base64 :public Item_str_ascii_checksum_func
182 {
183   String tmp_value;
184 public:
185   Item_func_to_base64(THD *thd, Item *a)
186    :Item_str_ascii_checksum_func(thd, a) {}
187   String *val_str_ascii(String *);
188   bool fix_length_and_dec();
189   const char *func_name() const { return "to_base64"; }
190   Item *get_copy(THD *thd)
191   { return get_item_copy<Item_func_to_base64>(thd, this); }
192 };
193 
194 class Item_func_from_base64 :public Item_str_binary_checksum_func
195 {
196   String tmp_value;
197 public:
198   Item_func_from_base64(THD *thd, Item *a)
199    :Item_str_binary_checksum_func(thd, a) { }
200   String *val_str(String *);
201   bool fix_length_and_dec();
202   const char *func_name() const { return "from_base64"; }
203   Item *get_copy(THD *thd)
204   { return get_item_copy<Item_func_from_base64>(thd, this); }
205 };
206 
207 #include <my_crypt.h>
208 
209 class Item_aes_crypt :public Item_str_binary_checksum_func
210 {
211   enum { AES_KEY_LENGTH = 128 };
212   void create_key(String *user_key, uchar* key);
213 
214 protected:
215   int what;
216   String tmp_value;
217 public:
218   Item_aes_crypt(THD *thd, Item *a, Item *b)
219    :Item_str_binary_checksum_func(thd, a, b) {}
220   String *val_str(String *);
221 };
222 
223 class Item_func_aes_encrypt :public Item_aes_crypt
224 {
225 public:
226   Item_func_aes_encrypt(THD *thd, Item *a, Item *b)
227    :Item_aes_crypt(thd, a, b) {}
228   bool fix_length_and_dec();
229   const char *func_name() const { return "aes_encrypt"; }
230   Item *get_copy(THD *thd)
231   { return get_item_copy<Item_func_aes_encrypt>(thd, this); }
232 };
233 
234 class Item_func_aes_decrypt :public Item_aes_crypt
235 {
236 public:
237   Item_func_aes_decrypt(THD *thd, Item *a, Item *b):
238     Item_aes_crypt(thd, a, b) {}
239   bool fix_length_and_dec();
240   const char *func_name() const { return "aes_decrypt"; }
241   Item *get_copy(THD *thd)
242   { return get_item_copy<Item_func_aes_decrypt>(thd, this); }
243 };
244 
245 
246 class Item_func_concat :public Item_str_func
247 {
248 protected:
249   String tmp_value;
250   /*
251     Append a non-NULL value to the result.
252     @param [IN]     thd          - The current thread.
253     @param [IN/OUT] res          - The current val_str() return value.
254     @param [IN]     app          - The value to be appended.
255     @retval                      - false on success, true on error
256   */
257   bool append_value(THD *thd, String *res, const String *app);
258   bool realloc_result(String *str, uint length) const;
259 public:
260   Item_func_concat(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
261   Item_func_concat(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
262   String *val_str(String *);
263   bool fix_length_and_dec();
264   const char *func_name() const { return "concat"; }
265   Item *get_copy(THD *thd)
266   { return get_item_copy<Item_func_concat>(thd, this); }
267 };
268 
269 
270 /*
271   This class handles the || operator in sql_mode=ORACLE.
272   Unlike the traditional MariaDB concat(), it treats NULL arguments as ''.
273 */
274 class Item_func_concat_operator_oracle :public Item_func_concat
275 {
276 public:
277   Item_func_concat_operator_oracle(THD *thd, List<Item> &list)
278    :Item_func_concat(thd, list)
279   { }
280   Item_func_concat_operator_oracle(THD *thd, Item *a, Item *b)
281    :Item_func_concat(thd, a, b)
282   { }
283   String *val_str(String *);
284   const char *func_name() const { return "concat_operator_oracle"; }
285   Item *get_copy(THD *thd)
286   {
287     return get_item_copy<Item_func_concat_operator_oracle>(thd, this);
288   }
289 };
290 
291 
292 class Item_func_decode_histogram :public Item_str_func
293 {
294 public:
295   Item_func_decode_histogram(THD *thd, Item *a, Item *b):
296     Item_str_func(thd, a, b) {}
297   String *val_str(String *);
298   bool fix_length_and_dec()
299   {
300     collation.set(system_charset_info);
301     max_length= MAX_BLOB_WIDTH;
302     maybe_null= 1;
303     return FALSE;
304   }
305   const char *func_name() const { return "decode_histogram"; }
306   Item *get_copy(THD *thd)
307   { return get_item_copy<Item_func_decode_histogram>(thd, this); }
308 };
309 
310 class Item_func_concat_ws :public Item_str_func
311 {
312   String tmp_value;
313 public:
314   Item_func_concat_ws(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
315   String *val_str(String *);
316   bool fix_length_and_dec();
317   const char *func_name() const { return "concat_ws"; }
318   table_map not_null_tables() const { return 0; }
319   Item *get_copy(THD *thd)
320   { return get_item_copy<Item_func_concat_ws>(thd, this); }
321 };
322 
323 class Item_func_reverse :public Item_str_func
324 {
325   String tmp_value;
326 public:
327   Item_func_reverse(THD *thd, Item *a): Item_str_func(thd, a) {}
328   String *val_str(String *);
329   bool fix_length_and_dec();
330   const char *func_name() const { return "reverse"; }
331   Item *get_copy(THD *thd)
332   { return get_item_copy<Item_func_reverse>(thd, this); }
333 };
334 
335 
336 class Item_func_replace :public Item_str_func
337 {
338   String tmp_value,tmp_value2;
339 public:
340   Item_func_replace(THD *thd, Item *org, Item *find, Item *replace):
341     Item_str_func(thd, org, find, replace) {}
342   String *val_str(String *to) { return val_str_internal(to, NULL); };
343   bool fix_length_and_dec();
344   String *val_str_internal(String *str, String *empty_string_for_null);
345   const char *func_name() const { return "replace"; }
346   Item *get_copy(THD *thd)
347   { return get_item_copy<Item_func_replace>(thd, this); }
348 };
349 
350 
351 class Item_func_replace_oracle :public Item_func_replace
352 {
353   String tmp_emtpystr;
354 public:
355   Item_func_replace_oracle(THD *thd, Item *org, Item *find, Item *replace):
356     Item_func_replace(thd, org, find, replace) {}
357   String *val_str(String *to) { return val_str_internal(to, &tmp_emtpystr); };
358   const char *func_name() const { return "replace_oracle"; }
359   Item *get_copy(THD *thd)
360   { return get_item_copy<Item_func_replace_oracle>(thd, this); }
361 };
362 
363 
364 class Item_func_regexp_replace :public Item_str_func
365 {
366   Regexp_processor_pcre re;
367   bool append_replacement(String *str,
368                           const LEX_CSTRING *source,
369                           const LEX_CSTRING *replace);
370 public:
371   Item_func_regexp_replace(THD *thd, Item *a, Item *b, Item *c):
372     Item_str_func(thd, a, b, c)
373     {}
374   void cleanup()
375   {
376     DBUG_ENTER("Item_func_regex::cleanup");
377     Item_str_func::cleanup();
378     re.cleanup();
379     DBUG_VOID_RETURN;
380   }
381   String *val_str(String *str);
382   bool fix_fields(THD *thd, Item **ref);
383   bool fix_length_and_dec();
384   const char *func_name() const { return "regexp_replace"; }
385   Item *get_copy(THD *thd) { return 0;}
386 };
387 
388 
389 class Item_func_regexp_substr :public Item_str_func
390 {
391   Regexp_processor_pcre re;
392 public:
393   Item_func_regexp_substr(THD *thd, Item *a, Item *b):
394     Item_str_func(thd, a, b)
395     {}
396   void cleanup()
397   {
398     DBUG_ENTER("Item_func_regex::cleanup");
399     Item_str_func::cleanup();
400     re.cleanup();
401     DBUG_VOID_RETURN;
402   }
403   String *val_str(String *str);
404   bool fix_fields(THD *thd, Item **ref);
405   bool fix_length_and_dec();
406   const char *func_name() const { return "regexp_substr"; }
407   Item *get_copy(THD *thd) { return 0; }
408 };
409 
410 
411 class Item_func_insert :public Item_str_func
412 {
413   String tmp_value;
414 public:
415   Item_func_insert(THD *thd, Item *org, Item *start, Item *length,
416                    Item *new_str):
417     Item_str_func(thd, org, start, length, new_str) {}
418   String *val_str(String *);
419   bool fix_length_and_dec();
420   const char *func_name() const { return "insert"; }
421   Item *get_copy(THD *thd)
422   { return get_item_copy<Item_func_insert>(thd, this); }
423 };
424 
425 
426 class Item_str_conv :public Item_str_func
427 {
428 protected:
429   uint multiply;
430   my_charset_conv_case converter;
431   String tmp_value;
432 public:
433   Item_str_conv(THD *thd, Item *item): Item_str_func(thd, item) {}
434   String *val_str(String *);
435 };
436 
437 
438 class Item_func_lcase :public Item_str_conv
439 {
440 public:
441   Item_func_lcase(THD *thd, Item *item): Item_str_conv(thd, item) {}
442   const char *func_name() const { return "lcase"; }
443   bool fix_length_and_dec();
444   Item *get_copy(THD *thd)
445   { return get_item_copy<Item_func_lcase>(thd, this); }
446 };
447 
448 class Item_func_ucase :public Item_str_conv
449 {
450 public:
451   Item_func_ucase(THD *thd, Item *item): Item_str_conv(thd, item) {}
452   const char *func_name() const { return "ucase"; }
453   bool fix_length_and_dec();
454   Item *get_copy(THD *thd)
455   { return get_item_copy<Item_func_ucase>(thd, this); }
456 };
457 
458 
459 class Item_func_left :public Item_str_func
460 {
461   String tmp_value;
462 public:
463   Item_func_left(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
464   String *val_str(String *);
465   bool fix_length_and_dec();
466   const char *func_name() const { return "left"; }
467   Item *get_copy(THD *thd)
468   { return get_item_copy<Item_func_left>(thd, this); }
469 };
470 
471 
472 class Item_func_right :public Item_str_func
473 {
474   String tmp_value;
475 public:
476   Item_func_right(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
477   String *val_str(String *);
478   bool fix_length_and_dec();
479   const char *func_name() const { return "right"; }
480   Item *get_copy(THD *thd)
481   { return get_item_copy<Item_func_right>(thd, this); }
482 };
483 
484 
485 class Item_func_substr :public Item_str_func
486 {
487   String tmp_value;
488 protected:
489   virtual longlong get_position() { return args[1]->val_int(); }
490 public:
491   Item_func_substr(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
492   Item_func_substr(THD *thd, Item *a, Item *b, Item *c):
493     Item_str_func(thd, a, b, c) {}
494   String *val_str(String *);
495   bool fix_length_and_dec();
496   const char *func_name() const { return "substr"; }
497   Item *get_copy(THD *thd)
498   { return get_item_copy<Item_func_substr>(thd, this); }
499 };
500 
501 class Item_func_substr_oracle :public Item_func_substr
502 {
503 protected:
504   longlong get_position()
505   { longlong pos= args[1]->val_int(); return pos == 0 ? 1 : pos; }
506   String *make_empty_result()
507   { null_value= 1; return NULL; }
508 public:
509   Item_func_substr_oracle(THD *thd, Item *a, Item *b):
510     Item_func_substr(thd, a, b) {}
511   Item_func_substr_oracle(THD *thd, Item *a, Item *b, Item *c):
512     Item_func_substr(thd, a, b, c) {}
513   bool fix_length_and_dec()
514   {
515     bool res= Item_func_substr::fix_length_and_dec();
516     maybe_null= true;
517     return res;
518   }
519   const char *func_name() const { return "substr_oracle"; }
520   Item *get_copy(THD *thd)
521   { return get_item_copy<Item_func_substr_oracle>(thd, this); }
522 };
523 
524 class Item_func_substr_index :public Item_str_func
525 {
526   String tmp_value;
527 public:
528   Item_func_substr_index(THD *thd, Item *a,Item *b,Item *c):
529     Item_str_func(thd, a, b, c) {}
530   String *val_str(String *);
531   bool fix_length_and_dec();
532   const char *func_name() const { return "substring_index"; }
533   Item *get_copy(THD *thd)
534   { return get_item_copy<Item_func_substr_index>(thd, this); }
535 
536 };
537 
538 
539 class Item_func_trim :public Item_str_func
540 {
541 protected:
542   String tmp_value;
543   String remove;
544   String *trimmed_value(String *res, uint32 offset, uint32 length)
545   {
546     if (length == 0)
547       return make_empty_result();
548 
549     tmp_value.set(*res, offset, length);
550     /*
551       Make sure to return correct charset and collation:
552       TRIM(0x000000 FROM _ucs2 0x0061)
553       should set charset to "binary" rather than to "ucs2".
554     */
555     tmp_value.set_charset(collation.collation);
556     return &tmp_value;
557   }
558   String *non_trimmed_value(String *res)
559   {
560     return trimmed_value(res, 0, res->length());
561   }
562   virtual const char *func_name_ext() const { return ""; }
563 public:
564   Item_func_trim(THD *thd, Item *a, Item *b): Item_str_func(thd, a, b) {}
565   Item_func_trim(THD *thd, Item *a): Item_str_func(thd, a) {}
566   Sql_mode_dependency value_depends_on_sql_mode() const;
567   String *val_str(String *);
568   bool fix_length_and_dec();
569   const char *func_name() const { return "trim"; }
570   void print(String *str, enum_query_type query_type);
571   virtual const char *mode_name() const { return "both"; }
572   Item *get_copy(THD *thd)
573   { return get_item_copy<Item_func_trim>(thd, this); }
574 };
575 
576 
577 class Item_func_trim_oracle :public Item_func_trim
578 {
579 protected:
580   String *make_empty_result()
581   { null_value= 1; return NULL; }
582   const char *func_name_ext() const { return "_oracle"; }
583 public:
584   Item_func_trim_oracle(THD *thd, Item *a, Item *b):
585     Item_func_trim(thd, a, b) {}
586   Item_func_trim_oracle(THD *thd, Item *a): Item_func_trim(thd, a) {}
587   const char *func_name() const { return "trim_oracle"; }
588   bool fix_length_and_dec()
589   {
590     bool res= Item_func_trim::fix_length_and_dec();
591     maybe_null= true;
592     return res;
593   }
594   Item *get_copy(THD *thd)
595   { return get_item_copy<Item_func_trim_oracle>(thd, this); }
596 };
597 
598 
599 class Item_func_ltrim :public Item_func_trim
600 {
601 public:
602   Item_func_ltrim(THD *thd, Item *a, Item *b): Item_func_trim(thd, a, b) {}
603   Item_func_ltrim(THD *thd, Item *a): Item_func_trim(thd, a) {}
604   Sql_mode_dependency value_depends_on_sql_mode() const
605   {
606     return Item_func::value_depends_on_sql_mode();
607   }
608   String *val_str(String *);
609   const char *func_name() const { return "ltrim"; }
610   const char *mode_name() const { return "leading"; }
611   Item *get_copy(THD *thd)
612   { return get_item_copy<Item_func_ltrim>(thd, this); }
613 };
614 
615 
616 class Item_func_ltrim_oracle :public Item_func_ltrim
617 {
618 protected:
619   String *make_empty_result()
620   { null_value= 1; return NULL; }
621   const char *func_name_ext() const { return "_oracle"; }
622 public:
623   Item_func_ltrim_oracle(THD *thd, Item *a, Item *b):
624     Item_func_ltrim(thd, a, b) {}
625   Item_func_ltrim_oracle(THD *thd, Item *a): Item_func_ltrim(thd, a) {}
626   const char *func_name() const { return "ltrim_oracle"; }
627   bool fix_length_and_dec()
628   {
629     bool res= Item_func_ltrim::fix_length_and_dec();
630     maybe_null= true;
631     return res;
632   }
633   Item *get_copy(THD *thd)
634   { return get_item_copy<Item_func_ltrim_oracle>(thd, this); }
635 };
636 
637 
638 class Item_func_rtrim :public Item_func_trim
639 {
640 public:
641   Item_func_rtrim(THD *thd, Item *a, Item *b): Item_func_trim(thd, a, b) {}
642   Item_func_rtrim(THD *thd, Item *a): Item_func_trim(thd, a) {}
643   String *val_str(String *);
644   const char *func_name() const { return "rtrim"; }
645   const char *mode_name() const { return "trailing"; }
646   Item *get_copy(THD *thd)
647   { return get_item_copy<Item_func_rtrim>(thd, this); }
648 };
649 
650 
651 class Item_func_rtrim_oracle :public Item_func_rtrim
652 {
653 protected:
654   String *make_empty_result()
655   { null_value= 1; return NULL; }
656   const char *func_name_ext() const { return "_oracle"; }
657 public:
658   Item_func_rtrim_oracle(THD *thd, Item *a, Item *b):
659     Item_func_rtrim(thd, a, b) {}
660   Item_func_rtrim_oracle(THD *thd, Item *a): Item_func_rtrim(thd, a) {}
661   const char *func_name() const { return "rtrim_oracle"; }
662   bool fix_length_and_dec()
663   {
664     bool res= Item_func_rtrim::fix_length_and_dec();
665     maybe_null= true;
666     return res;
667   }
668   Item *get_copy(THD *thd)
669   { return get_item_copy<Item_func_rtrim_oracle>(thd, this); }
670 };
671 
672 /*
673   Item_func_password -- new (4.1.1) PASSWORD() function implementation.
674   Returns strcat('*', octet2hex(sha1(sha1(password)))). '*' stands for new
675   password format, sha1(sha1(password) is so-called hash_stage2 value.
676   Length of returned string is always 41 byte. To find out how entire
677   authentication procedure works, see comments in password.c.
678 */
679 
680 class Item_func_password :public Item_str_ascii_checksum_func
681 {
682 public:
683   enum PW_Alg {OLD, NEW};
684 private:
685   char tmp_value[SCRAMBLED_PASSWORD_CHAR_LENGTH+1];
686   enum PW_Alg alg;
687   bool deflt;
688 public:
689   Item_func_password(THD *thd, Item *a):
690     Item_str_ascii_checksum_func(thd, a), alg(NEW), deflt(1) {}
691   Item_func_password(THD *thd, Item *a, PW_Alg al):
692     Item_str_ascii_checksum_func(thd, a), alg(al), deflt(0) {}
693   String *val_str_ascii(String *str);
694   bool fix_fields(THD *thd, Item **ref);
695   bool fix_length_and_dec()
696   {
697     fix_length_and_charset((alg == 1 ?
698                             SCRAMBLED_PASSWORD_CHAR_LENGTH :
699                             SCRAMBLED_PASSWORD_CHAR_LENGTH_323),
700                            default_charset());
701     return FALSE;
702   }
703   const char *func_name() const { return ((deflt || alg == 1) ?
704                                           "password" : "old_password"); }
705   static char *alloc(THD *thd, const char *password, size_t pass_len,
706                      enum PW_Alg al);
707   Item *get_copy(THD *thd)
708   { return get_item_copy<Item_func_password>(thd, this); }
709 };
710 
711 
712 
713 class Item_func_des_encrypt :public Item_str_binary_checksum_func
714 {
715   String tmp_value,tmp_arg;
716 public:
717   Item_func_des_encrypt(THD *thd, Item *a)
718    :Item_str_binary_checksum_func(thd, a) {}
719   Item_func_des_encrypt(THD *thd, Item *a, Item *b)
720    :Item_str_binary_checksum_func(thd, a, b) {}
721   String *val_str(String *);
722   bool fix_length_and_dec()
723   {
724     maybe_null=1;
725     /* 9 = MAX ((8- (arg_len % 8)) + 1) */
726     max_length = args[0]->max_length + 9;
727     return FALSE;
728   }
729   const char *func_name() const { return "des_encrypt"; }
730   Item *get_copy(THD *thd)
731   { return get_item_copy<Item_func_des_encrypt>(thd, this); }
732 };
733 
734 class Item_func_des_decrypt :public Item_str_binary_checksum_func
735 {
736   String tmp_value;
737 public:
738   Item_func_des_decrypt(THD *thd, Item *a)
739    :Item_str_binary_checksum_func(thd, a) {}
740   Item_func_des_decrypt(THD *thd, Item *a, Item *b)
741    :Item_str_binary_checksum_func(thd, a, b) {}
742   String *val_str(String *);
743   bool fix_length_and_dec()
744   {
745     maybe_null=1;
746     /* 9 = MAX ((8- (arg_len % 8)) + 1) */
747     max_length= args[0]->max_length;
748     if (max_length >= 9U)
749       max_length-= 9U;
750     return FALSE;
751   }
752   const char *func_name() const { return "des_decrypt"; }
753   Item *get_copy(THD *thd)
754   { return get_item_copy<Item_func_des_decrypt>(thd, this); }
755 };
756 
757 
758 /**
759   QQ: Item_func_encrypt should derive from Item_str_ascii_checksum_func.
760   However, it should be fixed to handle UCS2, UTF16, UTF32 properly first,
761   as the underlying crypt() call expects a null-terminated input string.
762 */
763 class Item_func_encrypt :public Item_str_binary_checksum_func
764 {
765   String tmp_value;
766 
767   /* Encapsulate common constructor actions */
768   void constructor_helper()
769   {
770     collation.set(&my_charset_bin);
771   }
772 public:
773   Item_func_encrypt(THD *thd, Item *a): Item_str_binary_checksum_func(thd, a)
774   {
775     constructor_helper();
776   }
777   Item_func_encrypt(THD *thd, Item *a, Item *b)
778    :Item_str_binary_checksum_func(thd, a, b)
779   {
780     constructor_helper();
781   }
782   String *val_str(String *);
783   bool fix_length_and_dec() { maybe_null=1; max_length = 13; return FALSE; }
784   const char *func_name() const { return "encrypt"; }
785   bool check_vcol_func_processor(void *arg)
786   {
787     return FALSE;
788   }
789   Item *get_copy(THD *thd)
790   { return get_item_copy<Item_func_encrypt>(thd, this); }
791 };
792 
793 #include "sql_crypt.h"
794 
795 
796 class Item_func_encode :public Item_str_binary_checksum_func
797 {
798 private:
799   /** Whether the PRNG has already been seeded. */
800   bool seeded;
801 protected:
802   SQL_CRYPT sql_crypt;
803 public:
804   Item_func_encode(THD *thd, Item *a, Item *seed_arg):
805     Item_str_binary_checksum_func(thd, a, seed_arg) {}
806   String *val_str(String *);
807   bool fix_length_and_dec();
808   const char *func_name() const { return "encode"; }
809   Item *get_copy(THD *thd)
810   { return get_item_copy<Item_func_encode>(thd, this); }
811 protected:
812   virtual void crypto_transform(String *);
813 private:
814   /** Provide a seed for the PRNG sequence. */
815   bool seed();
816 };
817 
818 
819 class Item_func_decode :public Item_func_encode
820 {
821 public:
822   Item_func_decode(THD *thd, Item *a, Item *seed_arg): Item_func_encode(thd, a, seed_arg) {}
823   const char *func_name() const { return "decode"; }
824   Item *get_copy(THD *thd)
825   { return get_item_copy<Item_func_decode>(thd, this); }
826 protected:
827   void crypto_transform(String *);
828 };
829 
830 
831 class Item_func_sysconst :public Item_str_func
832 {
833 public:
834   Item_func_sysconst(THD *thd): Item_str_func(thd)
835   { collation.set(system_charset_info,DERIVATION_SYSCONST); }
836   Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
837   /*
838     Used to create correct Item name in new converted item in
839     safe_charset_converter, return string representation of this function
840     call
841   */
842   virtual const char *fully_qualified_func_name() const = 0;
843   bool check_vcol_func_processor(void *arg)
844   {
845     return mark_unsupported_function(fully_qualified_func_name(), arg,
846                                      VCOL_SESSION_FUNC);
847   }
848   bool const_item() const;
849 };
850 
851 
852 class Item_func_database :public Item_func_sysconst
853 {
854 public:
855   Item_func_database(THD *thd): Item_func_sysconst(thd) {}
856   String *val_str(String *);
857   bool fix_length_and_dec()
858   {
859     max_length= NAME_CHAR_LEN * system_charset_info->mbmaxlen;
860     maybe_null=1;
861     return FALSE;
862   }
863   const char *func_name() const { return "database"; }
864   const char *fully_qualified_func_name() const { return "database()"; }
865   Item *get_copy(THD *thd)
866   { return get_item_copy<Item_func_database>(thd, this); }
867 };
868 
869 
870 class Item_func_sqlerrm :public Item_func_sysconst
871 {
872 public:
873   Item_func_sqlerrm(THD *thd): Item_func_sysconst(thd) {}
874   String *val_str(String *);
875   const char *func_name() const { return "SQLERRM"; }
876   const char *fully_qualified_func_name() const { return "SQLERRM"; }
877   void print(String *str, enum_query_type query_type)
878   {
879     str->append(func_name());
880   }
881   bool fix_length_and_dec()
882   {
883     max_length= 512 * system_charset_info->mbmaxlen;
884     null_value= maybe_null= false;
885     return FALSE;
886   }
887   Item *get_copy(THD *thd)
888   { return get_item_copy<Item_func_sqlerrm>(thd, this); }
889 };
890 
891 
892 class Item_func_user :public Item_func_sysconst
893 {
894 protected:
895   bool init (const char *user, const char *host);
896 
897 public:
898   Item_func_user(THD *thd): Item_func_sysconst(thd)
899   {
900     str_value.set("", 0, system_charset_info);
901   }
902   String *val_str(String *)
903   {
904     DBUG_ASSERT(fixed == 1);
905     return (null_value ? 0 : &str_value);
906   }
907   bool fix_fields(THD *thd, Item **ref);
908   bool fix_length_and_dec()
909   {
910     max_length= (uint32) (username_char_length +
911                  HOSTNAME_LENGTH + 1) * SYSTEM_CHARSET_MBMAXLEN;
912     return FALSE;
913   }
914   const char *func_name() const { return "user"; }
915   const char *fully_qualified_func_name() const { return "user()"; }
916   int save_in_field(Field *field, bool no_conversions)
917   {
918     return save_str_value_in_field(field, &str_value);
919   }
920   Item *get_copy(THD *thd)
921   { return get_item_copy<Item_func_user>(thd, this); }
922 };
923 
924 
925 class Item_func_current_user :public Item_func_user
926 {
927   Name_resolution_context *context;
928 
929 public:
930   Item_func_current_user(THD *thd, Name_resolution_context *context_arg):
931     Item_func_user(thd), context(context_arg) {}
932   bool fix_fields(THD *thd, Item **ref);
933   const char *func_name() const { return "current_user"; }
934   const char *fully_qualified_func_name() const { return "current_user()"; }
935   bool check_vcol_func_processor(void *arg)
936   {
937     context= 0;
938     return mark_unsupported_function(fully_qualified_func_name(), arg,
939                                      VCOL_SESSION_FUNC);
940   }
941 };
942 
943 
944 class Item_func_current_role :public Item_func_sysconst
945 {
946   Name_resolution_context *context;
947 
948 public:
949   Item_func_current_role(THD *thd, Name_resolution_context *context_arg):
950     Item_func_sysconst(thd), context(context_arg) {}
951   bool fix_fields(THD *thd, Item **ref);
952   bool fix_length_and_dec()
953   {
954     max_length= (uint32) username_char_length * SYSTEM_CHARSET_MBMAXLEN;
955     return FALSE;
956   }
957   int save_in_field(Field *field, bool no_conversions)
958   { return save_str_value_in_field(field, &str_value); }
959   const char *func_name() const { return "current_role"; }
960   const char *fully_qualified_func_name() const { return "current_role()"; }
961   String *val_str(String *)
962   {
963     DBUG_ASSERT(fixed == 1);
964     return null_value ? NULL : &str_value;
965   }
966   bool check_vcol_func_processor(void *arg)
967   {
968 
969     context= 0;
970     return mark_unsupported_function(fully_qualified_func_name(), arg,
971                                      VCOL_SESSION_FUNC);
972   }
973   Item *get_copy(THD *thd)
974   { return get_item_copy<Item_func_current_role>(thd, this); }
975 };
976 
977 
978 class Item_func_soundex :public Item_str_func
979 {
980   String tmp_value;
981 public:
982   Item_func_soundex(THD *thd, Item *a): Item_str_func(thd, a) {}
983   String *val_str(String *);
984   bool fix_length_and_dec();
985   const char *func_name() const { return "soundex"; }
986   Item *get_copy(THD *thd)
987   { return get_item_copy<Item_func_soundex>(thd, this); }
988 };
989 
990 
991 class Item_func_elt :public Item_str_func
992 {
993 public:
994   Item_func_elt(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
995   double val_real();
996   longlong val_int();
997   String *val_str(String *str);
998   bool fix_length_and_dec();
999   const char *func_name() const { return "elt"; }
1000   Item *get_copy(THD *thd)
1001   { return get_item_copy<Item_func_elt>(thd, this); }
1002 };
1003 
1004 
1005 class Item_func_make_set :public Item_str_func
1006 {
1007   String tmp_str;
1008 
1009 public:
1010   Item_func_make_set(THD *thd, List<Item> &list): Item_str_func(thd, list) {}
1011   String *val_str(String *str);
1012   bool fix_length_and_dec();
1013   const char *func_name() const { return "make_set"; }
1014   Item *get_copy(THD *thd)
1015   { return get_item_copy<Item_func_make_set>(thd, this); }
1016 };
1017 
1018 
1019 class Item_func_format :public Item_str_ascii_func
1020 {
1021   const MY_LOCALE *locale;
1022 public:
1023   Item_func_format(THD *thd, Item *org, Item *dec):
1024     Item_str_ascii_func(thd, org, dec) {}
1025   Item_func_format(THD *thd, Item *org, Item *dec, Item *lang):
1026     Item_str_ascii_func(thd, org, dec, lang) {}
1027 
1028   String *val_str_ascii(String *);
1029   bool fix_length_and_dec();
1030   const char *func_name() const { return "format"; }
1031   Item *get_copy(THD *thd)
1032   { return get_item_copy<Item_func_format>(thd, this); }
1033 };
1034 
1035 
1036 class Item_func_char :public Item_str_func
1037 {
1038 public:
1039   Item_func_char(THD *thd, List<Item> &list): Item_str_func(thd, list)
1040   { collation.set(&my_charset_bin); }
1041   Item_func_char(THD *thd, List<Item> &list, CHARSET_INFO *cs):
1042     Item_str_func(thd, list)
1043   { collation.set(cs); }
1044   Item_func_char(THD *thd, Item *arg1, CHARSET_INFO *cs):
1045     Item_str_func(thd, arg1)
1046   { collation.set(cs); }
1047   String *val_str(String *);
1048   void append_char(String * str, int32 num);
1049   bool fix_length_and_dec()
1050   {
1051     max_length= arg_count * 4;
1052     return FALSE;
1053   }
1054   const char *func_name() const { return "char"; }
1055   void print(String *str, enum_query_type query_type);
1056   Item *get_copy(THD *thd)
1057   { return get_item_copy<Item_func_char>(thd, this); }
1058 };
1059 
1060 class Item_func_chr :public Item_func_char
1061 {
1062 public:
1063   Item_func_chr(THD *thd, Item *arg1, CHARSET_INFO *cs):
1064     Item_func_char(thd, arg1, cs) {}
1065   String *val_str(String *);
1066   bool fix_length_and_dec()
1067   {
1068     max_length= 4;
1069     return FALSE;
1070   }
1071   const char *func_name() const { return "chr"; }
1072   Item *get_copy(THD *thd)
1073   { return get_item_copy<Item_func_chr>(thd, this); }
1074 };
1075 
1076 class Item_func_repeat :public Item_str_func
1077 {
1078   String tmp_value;
1079 public:
1080   Item_func_repeat(THD *thd, Item *arg1, Item *arg2):
1081     Item_str_func(thd, arg1, arg2) {}
1082   String *val_str(String *);
1083   bool fix_length_and_dec();
1084   const char *func_name() const { return "repeat"; }
1085   Item *get_copy(THD *thd)
1086   { return get_item_copy<Item_func_repeat>(thd, this); }
1087 };
1088 
1089 
1090 class Item_func_space :public Item_str_func
1091 {
1092 public:
1093   Item_func_space(THD *thd, Item *arg1): Item_str_func(thd, arg1) {}
1094   String *val_str(String *);
1095   bool fix_length_and_dec();
1096   const char *func_name() const { return "space"; }
1097   Item *get_copy(THD *thd)
1098   { return get_item_copy<Item_func_space>(thd, this); }
1099 };
1100 
1101 
1102 class Item_func_binlog_gtid_pos :public Item_str_func
1103 {
1104 public:
1105   Item_func_binlog_gtid_pos(THD *thd, Item *arg1, Item *arg2):
1106     Item_str_func(thd, arg1, arg2) {}
1107   String *val_str(String *);
1108   bool fix_length_and_dec();
1109   const char *func_name() const { return "binlog_gtid_pos"; }
1110   bool check_vcol_func_processor(void *arg)
1111   {
1112     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
1113   }
1114   Item *get_copy(THD *thd)
1115   { return get_item_copy<Item_func_binlog_gtid_pos>(thd, this); }
1116 };
1117 
1118 
1119 class Item_func_pad: public Item_str_func
1120 {
1121 protected:
1122   String tmp_value, pad_str;
1123 public:
1124   Item_func_pad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1125     Item_str_func(thd, arg1, arg2, arg3) {}
1126   Item_func_pad(THD *thd, Item *arg1, Item *arg2):
1127     Item_str_func(thd, arg1, arg2) {}
1128   bool fix_length_and_dec();
1129 };
1130 
1131 
1132 class Item_func_rpad :public Item_func_pad
1133 {
1134 public:
1135   Item_func_rpad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1136     Item_func_pad(thd, arg1, arg2, arg3) {}
1137   Item_func_rpad(THD *thd, Item *arg1, Item *arg2):
1138     Item_func_pad(thd, arg1, arg2) {}
1139   String *val_str(String *);
1140   const char *func_name() const { return "rpad"; }
1141   Sql_mode_dependency value_depends_on_sql_mode() const;
1142   Item *get_copy(THD *thd)
1143   { return get_item_copy<Item_func_rpad>(thd, this); }
1144 };
1145 
1146 
1147 class Item_func_rpad_oracle :public Item_func_rpad
1148 {
1149   String *make_empty_result()
1150   { null_value= 1; return NULL; }
1151 public:
1152   Item_func_rpad_oracle(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1153     Item_func_rpad(thd, arg1, arg2, arg3) {}
1154   Item_func_rpad_oracle(THD *thd, Item *arg1, Item *arg2):
1155     Item_func_rpad(thd, arg1, arg2) {}
1156   bool fix_length_and_dec()
1157   {
1158     bool res= Item_func_rpad::fix_length_and_dec();
1159     maybe_null= true;
1160     return res;
1161   }
1162   const char *func_name() const { return "rpad_oracle"; }
1163   Item *get_copy(THD *thd)
1164   { return get_item_copy<Item_func_rpad_oracle>(thd, this); }
1165 };
1166 
1167 
1168 class Item_func_lpad :public Item_func_pad
1169 {
1170 public:
1171   Item_func_lpad(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1172     Item_func_pad(thd, arg1, arg2, arg3) {}
1173   Item_func_lpad(THD *thd, Item *arg1, Item *arg2):
1174     Item_func_pad(thd, arg1, arg2) {}
1175   String *val_str(String *);
1176   const char *func_name() const { return "lpad"; }
1177   Item *get_copy(THD *thd)
1178   { return get_item_copy<Item_func_lpad>(thd, this); }
1179 };
1180 
1181 
1182 class Item_func_lpad_oracle :public Item_func_lpad
1183 {
1184   String *make_empty_result()
1185   { null_value= 1; return NULL; }
1186 public:
1187   Item_func_lpad_oracle(THD *thd, Item *arg1, Item *arg2, Item *arg3):
1188     Item_func_lpad(thd, arg1, arg2, arg3) {}
1189   Item_func_lpad_oracle(THD *thd, Item *arg1, Item *arg2):
1190     Item_func_lpad(thd, arg1, arg2) {}
1191   bool fix_length_and_dec()
1192   {
1193     bool res= Item_func_lpad::fix_length_and_dec();
1194     maybe_null= true;
1195     return res;
1196   }
1197   const char *func_name() const { return "lpad_oracle"; }
1198   Item *get_copy(THD *thd)
1199   { return get_item_copy<Item_func_lpad_oracle>(thd, this); }
1200 };
1201 
1202 
1203 class Item_func_conv :public Item_str_func
1204 {
1205 public:
1206   Item_func_conv(THD *thd, Item *a, Item *b, Item *c):
1207     Item_str_func(thd, a, b, c) {}
1208   const char *func_name() const { return "conv"; }
1209   String *val_str(String *);
1210   bool fix_length_and_dec()
1211   {
1212     collation.set(default_charset());
1213     max_length=64;
1214     maybe_null= 1;
1215     return FALSE;
1216   }
1217   Item *get_copy(THD *thd)
1218   { return get_item_copy<Item_func_conv>(thd, this); }
1219 };
1220 
1221 
1222 class Item_func_hex :public Item_str_ascii_checksum_func
1223 {
1224 protected:
1225   String tmp_value;
1226   /*
1227     Calling arg[0]->type_handler() can be expensive on every row.
1228     It's a virtual method, and in case if args[0] is a complex Item,
1229     its type_handler() can call more virtual methods.
1230     So let's cache it during fix_length_and_dec().
1231   */
1232   const Type_handler *m_arg0_type_handler;
1233 public:
1234   Item_func_hex(THD *thd, Item *a):
1235     Item_str_ascii_checksum_func(thd, a), m_arg0_type_handler(NULL) {}
1236   const char *func_name() const { return "hex"; }
1237   String *val_str_ascii_from_val_int(String *str);
1238   String *val_str_ascii_from_val_real(String *str);
1239   String *val_str_ascii_from_val_str(String *str);
1240   String *val_str_ascii(String *str)
1241   {
1242     DBUG_ASSERT(fixed);
1243     return m_arg0_type_handler->Item_func_hex_val_str_ascii(this, str);
1244   }
1245   bool fix_length_and_dec()
1246   {
1247     collation.set(default_charset(), DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
1248     decimals=0;
1249     fix_char_length(args[0]->max_length * 2);
1250     m_arg0_type_handler= args[0]->type_handler();
1251     return FALSE;
1252   }
1253   Item *get_copy(THD *thd)
1254   { return get_item_copy<Item_func_hex>(thd, this); }
1255 };
1256 
1257 class Item_func_unhex :public Item_str_func
1258 {
1259   String tmp_value;
1260 public:
1261   Item_func_unhex(THD *thd, Item *a): Item_str_func(thd, a)
1262   {
1263     /* there can be bad hex strings */
1264     maybe_null= 1;
1265   }
1266   const char *func_name() const { return "unhex"; }
1267   String *val_str(String *);
1268   bool fix_length_and_dec()
1269   {
1270     collation.set(&my_charset_bin);
1271     decimals=0;
1272     max_length=(1+args[0]->max_length)/2;
1273     return FALSE;
1274   }
1275   Item *get_copy(THD *thd)
1276   { return get_item_copy<Item_func_unhex>(thd, this); }
1277 };
1278 
1279 
1280 #ifndef DBUG_OFF
1281 class Item_func_like_range :public Item_str_func
1282 {
1283 protected:
1284   String min_str;
1285   String max_str;
1286   const bool is_min;
1287 public:
1288   Item_func_like_range(THD *thd, Item *a, Item *b, bool is_min_arg):
1289     Item_str_func(thd, a, b), is_min(is_min_arg)
1290   { maybe_null= 1; }
1291   String *val_str(String *);
1292   bool fix_length_and_dec()
1293   {
1294     collation.set(args[0]->collation);
1295     decimals=0;
1296     max_length= MAX_BLOB_WIDTH;
1297     return FALSE;
1298   }
1299 };
1300 
1301 
1302 class Item_func_like_range_min :public Item_func_like_range
1303 {
1304 public:
1305   Item_func_like_range_min(THD *thd, Item *a, Item *b):
1306     Item_func_like_range(thd, a, b, true) { }
1307   const char *func_name() const { return "like_range_min"; }
1308   Item *get_copy(THD *thd)
1309   { return get_item_copy<Item_func_like_range_min>(thd, this); }
1310 };
1311 
1312 
1313 class Item_func_like_range_max :public Item_func_like_range
1314 {
1315 public:
1316   Item_func_like_range_max(THD *thd, Item *a, Item *b):
1317     Item_func_like_range(thd, a, b, false) { }
1318   const char *func_name() const { return "like_range_max"; }
1319   Item *get_copy(THD *thd)
1320   { return get_item_copy<Item_func_like_range_max>(thd, this); }
1321 };
1322 #endif
1323 
1324 
1325 class Item_func_binary :public Item_str_func
1326 {
1327 public:
1328   Item_func_binary(THD *thd, Item *a): Item_str_func(thd, a) {}
1329   String *val_str(String *a)
1330   {
1331     DBUG_ASSERT(fixed == 1);
1332     String *tmp=args[0]->val_str(a);
1333     null_value=args[0]->null_value;
1334     if (tmp)
1335       tmp->set_charset(&my_charset_bin);
1336     return tmp;
1337   }
1338   bool fix_length_and_dec()
1339   {
1340     collation.set(&my_charset_bin);
1341     max_length=args[0]->max_length;
1342     return FALSE;
1343   }
1344   void print(String *str, enum_query_type query_type);
1345   const char *func_name() const { return "cast_as_binary"; }
1346   bool need_parentheses_in_default() { return true; }
1347   Item *get_copy(THD *thd)
1348   { return get_item_copy<Item_func_binary>(thd, this); }
1349 };
1350 
1351 
1352 class Item_load_file :public Item_str_func
1353 {
1354   String tmp_value;
1355 public:
1356   Item_load_file(THD *thd, Item *a): Item_str_func(thd, a) {}
1357   String *val_str(String *);
1358   const char *func_name() const { return "load_file"; }
1359   bool fix_length_and_dec()
1360   {
1361     collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
1362     maybe_null=1;
1363     max_length=MAX_BLOB_WIDTH;
1364     return FALSE;
1365   }
1366   bool check_vcol_func_processor(void *arg)
1367   {
1368     return mark_unsupported_function(func_name(), "()", arg, VCOL_IMPOSSIBLE);
1369   }
1370   Item *get_copy(THD *thd)
1371   { return get_item_copy<Item_load_file>(thd, this); }
1372 };
1373 
1374 
1375 class Item_func_export_set: public Item_str_func
1376 {
1377  public:
1378   Item_func_export_set(THD *thd, Item *a, Item *b, Item* c):
1379     Item_str_func(thd, a, b, c) {}
1380   Item_func_export_set(THD *thd, Item *a, Item *b, Item* c, Item* d):
1381     Item_str_func(thd, a, b, c, d) {}
1382   Item_func_export_set(THD *thd, Item *a, Item *b, Item* c, Item* d, Item* e):
1383     Item_str_func(thd, a, b, c, d, e) {}
1384   String  *val_str(String *str);
1385   bool fix_length_and_dec();
1386   const char *func_name() const { return "export_set"; }
1387   Item *get_copy(THD *thd)
1388   { return get_item_copy<Item_func_export_set>(thd, this); }
1389 };
1390 
1391 
1392 class Item_func_quote :public Item_str_func
1393 {
1394   String tmp_value;
1395 public:
1396   Item_func_quote(THD *thd, Item *a): Item_str_func(thd, a) {}
1397   const char *func_name() const { return "quote"; }
1398   String *val_str(String *);
1399   bool fix_length_and_dec()
1400   {
1401     collation.set(args[0]->collation);
1402     ulonglong max_result_length= (ulonglong) args[0]->max_length * 2 +
1403                                   2 * collation.collation->mbmaxlen;
1404     max_length= (uint32) MY_MIN(max_result_length, MAX_BLOB_WIDTH);
1405     return FALSE;
1406   }
1407   Item *get_copy(THD *thd)
1408   { return get_item_copy<Item_func_quote>(thd, this); }
1409 };
1410 
1411 class Item_func_conv_charset :public Item_str_func
1412 {
1413   bool use_cached_value;
1414   String tmp_value;
1415 public:
1416   bool safe;
1417   Item_func_conv_charset(THD *thd, Item *a, CHARSET_INFO *cs):
1418     Item_str_func(thd, a)
1419   {
1420     collation.set(cs, DERIVATION_IMPLICIT);
1421     use_cached_value= 0; safe= 0;
1422   }
1423   Item_func_conv_charset(THD *thd, Item *a, CHARSET_INFO *cs, bool cache_if_const):
1424     Item_str_func(thd, a)
1425   {
1426     collation.set(cs, DERIVATION_IMPLICIT);
1427     if (cache_if_const && args[0]->const_item() && !args[0]->is_expensive())
1428     {
1429       uint errors= 0;
1430       String tmp, *str= args[0]->val_str(&tmp);
1431       if (!str || str_value.copy(str->ptr(), str->length(),
1432                                  str->charset(), cs, &errors))
1433         null_value= 1;
1434       use_cached_value= 1;
1435       str_value.mark_as_const();
1436       safe= (errors == 0);
1437     }
1438     else
1439     {
1440       use_cached_value= 0;
1441       /*
1442         Conversion from and to "binary" is safe.
1443         Conversion to Unicode is safe.
1444         Conversion from an expression with the ASCII repertoire
1445         to any character set that can store characters U+0000..U+007F
1446         is safe:
1447         - All supported multibyte character sets can store U+0000..U+007F
1448         - All supported 7bit character sets can store U+0000..U+007F
1449           except those marked with MY_CS_NONASCII (e.g. swe7).
1450         Other kind of conversions are potentially lossy.
1451       */
1452       safe= (args[0]->collation.collation == &my_charset_bin ||
1453              cs == &my_charset_bin ||
1454              (cs->state & MY_CS_UNICODE) ||
1455              (args[0]->collation.repertoire == MY_REPERTOIRE_ASCII &&
1456               (cs->mbmaxlen > 1 || !(cs->state & MY_CS_NONASCII))));
1457     }
1458   }
1459   bool is_json_type() { return args[0]->is_json_type(); }
1460   String *val_str(String *);
1461   longlong val_int()
1462   {
1463     if (args[0]->result_type() == STRING_RESULT)
1464       return Item_str_func::val_int();
1465     longlong res= args[0]->val_int();
1466     if ((null_value= args[0]->null_value))
1467       return 0;
1468     return res;
1469   }
1470   double val_real()
1471   {
1472     if (args[0]->result_type() == STRING_RESULT)
1473       return Item_str_func::val_real();
1474     double res= args[0]->val_real();
1475     if ((null_value= args[0]->null_value))
1476       return 0;
1477     return res;
1478   }
1479   my_decimal *val_decimal(my_decimal *d)
1480   {
1481     if (args[0]->result_type() == STRING_RESULT)
1482       return Item_str_func::val_decimal(d);
1483     my_decimal *res= args[0]->val_decimal(d);
1484     if ((null_value= args[0]->null_value))
1485       return NULL;
1486     return res;
1487   }
1488   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
1489   {
1490     if (args[0]->result_type() == STRING_RESULT)
1491       return Item_str_func::get_date(ltime, fuzzydate);
1492     bool res= args[0]->get_date(ltime, fuzzydate);
1493     if ((null_value= args[0]->null_value))
1494       return 1;
1495     return res;
1496   }
1497   bool fix_length_and_dec();
1498   const char *func_name() const { return "convert"; }
1499   void print(String *str, enum_query_type query_type);
1500   Item *get_copy(THD *thd)
1501   { return get_item_copy<Item_func_conv_charset>(thd, this); }
1502 };
1503 
1504 class Item_func_set_collation :public Item_str_func
1505 {
1506   CHARSET_INFO *m_set_collation;
1507 public:
1508   Item_func_set_collation(THD *thd, Item *a, CHARSET_INFO *set_collation):
1509     Item_str_func(thd, a), m_set_collation(set_collation) {}
1510   String *val_str(String *);
1511   bool fix_length_and_dec();
1512   bool eq(const Item *item, bool binary_cmp) const;
1513   const char *func_name() const { return "collate"; }
1514   enum precedence precedence() const { return COLLATE_PRECEDENCE; }
1515   enum Functype functype() const { return COLLATE_FUNC; }
1516   void print(String *str, enum_query_type query_type);
1517   Item_field *field_for_view_update()
1518   {
1519     /* this function is transparent for view updating */
1520     return args[0]->field_for_view_update();
1521   }
1522   bool need_parentheses_in_default() { return true; }
1523   Item *get_copy(THD *thd)
1524   { return get_item_copy<Item_func_set_collation>(thd, this); }
1525 };
1526 
1527 
1528 class Item_func_expr_str_metadata :public Item_str_func
1529 {
1530 public:
1531   Item_func_expr_str_metadata(THD *thd, Item *a): Item_str_func(thd, a) { }
1532   bool fix_length_and_dec()
1533   {
1534      collation.set(system_charset_info);
1535      max_length= 64 * collation.collation->mbmaxlen; // should be enough
1536      maybe_null= 0;
1537      return FALSE;
1538   };
1539   table_map not_null_tables() const { return 0; }
1540   Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
1541   { return this; }
1542   bool const_item() const { return true; }
1543 };
1544 
1545 
1546 class Item_func_charset :public Item_func_expr_str_metadata
1547 {
1548 public:
1549   Item_func_charset(THD *thd, Item *a)
1550     :Item_func_expr_str_metadata(thd, a) { }
1551   String *val_str(String *);
1552   const char *func_name() const { return "charset"; }
1553   Item *get_copy(THD *thd)
1554   { return get_item_copy<Item_func_charset>(thd, this); }
1555 };
1556 
1557 
1558 class Item_func_collation :public Item_func_expr_str_metadata
1559 {
1560 public:
1561   Item_func_collation(THD *thd, Item *a)
1562     :Item_func_expr_str_metadata(thd, a) {}
1563   String *val_str(String *);
1564   const char *func_name() const { return "collation"; }
1565   Item *get_copy(THD *thd)
1566   { return get_item_copy<Item_func_collation>(thd, this); }
1567 };
1568 
1569 
1570 class Item_func_weight_string :public Item_str_func
1571 {
1572   String tmp_value;
1573   uint flags;
1574   uint nweights;
1575   uint result_length;
1576 public:
1577   Item_func_weight_string(THD *thd, Item *a, uint result_length_arg,
1578                           uint nweights_arg, uint flags_arg):
1579     Item_str_func(thd, a)
1580   {
1581     nweights= nweights_arg;
1582     flags= flags_arg;
1583     result_length= result_length_arg;
1584   }
1585   const char *func_name() const { return "weight_string"; }
1586   String *val_str(String *);
1587   bool fix_length_and_dec();
1588   bool eq(const Item *item, bool binary_cmp) const
1589   {
1590     if (!Item_str_func::eq(item, binary_cmp))
1591       return false;
1592     Item_func_weight_string *that= (Item_func_weight_string *)item;
1593     return this->flags == that->flags &&
1594            this->nweights == that->nweights &&
1595            this->result_length == that->result_length;
1596   }
1597   Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond)
1598   { return this; }
1599   void print(String *str, enum_query_type query_type);
1600   Item *get_copy(THD *thd)
1601   { return get_item_copy<Item_func_weight_string>(thd, this); }
1602 };
1603 
1604 class Item_func_crc32 :public Item_long_func
1605 {
1606   bool check_arguments() const
1607   { return args[0]->check_type_can_return_str(func_name()); }
1608   String value;
1609 public:
1610   Item_func_crc32(THD *thd, Item *a): Item_long_func(thd, a)
1611   { unsigned_flag= 1; }
1612   const char *func_name() const { return "crc32"; }
1613   bool fix_length_and_dec() { max_length=10; return FALSE; }
1614   longlong val_int();
1615   Item *get_copy(THD *thd)
1616   { return get_item_copy<Item_func_crc32>(thd, this); }
1617 };
1618 
1619 class Item_func_uncompressed_length : public Item_long_func_length
1620 {
1621   String value;
1622 public:
1623   Item_func_uncompressed_length(THD *thd, Item *a)
1624    :Item_long_func_length(thd, a) {}
1625   const char *func_name() const{return "uncompressed_length";}
1626   bool fix_length_and_dec() { max_length=10; maybe_null= true; return FALSE; }
1627   longlong val_int();
1628   Item *get_copy(THD *thd)
1629   { return get_item_copy<Item_func_uncompressed_length>(thd, this); }
1630 };
1631 
1632 #ifdef HAVE_COMPRESS
1633 #define ZLIB_DEPENDED_FUNCTION ;
1634 #else
1635 #define ZLIB_DEPENDED_FUNCTION { null_value=1; return 0; }
1636 #endif
1637 
1638 class Item_func_compress: public Item_str_binary_checksum_func
1639 {
1640   String tmp_value;
1641 public:
1642   Item_func_compress(THD *thd, Item *a)
1643    :Item_str_binary_checksum_func(thd, a) {}
1644   bool fix_length_and_dec()
1645   {
1646     max_length= (args[0]->max_length * 120) / 100 + 12;
1647     return FALSE;
1648   }
1649   const char *func_name() const{return "compress";}
1650   String *val_str(String *) ZLIB_DEPENDED_FUNCTION
1651   Item *get_copy(THD *thd)
1652   { return get_item_copy<Item_func_compress>(thd, this); }
1653 };
1654 
1655 class Item_func_uncompress: public Item_str_binary_checksum_func
1656 {
1657   String tmp_value;
1658 public:
1659   Item_func_uncompress(THD *thd, Item *a)
1660    :Item_str_binary_checksum_func(thd, a) {}
1661   bool fix_length_and_dec()
1662   {
1663     maybe_null= 1; max_length= MAX_BLOB_WIDTH;
1664     return FALSE;
1665   }
1666   const char *func_name() const{return "uncompress";}
1667   String *val_str(String *) ZLIB_DEPENDED_FUNCTION
1668   Item *get_copy(THD *thd)
1669   { return get_item_copy<Item_func_uncompress>(thd, this); }
1670 };
1671 
1672 
1673 class Item_func_uuid: public Item_str_func
1674 {
1675 public:
1676   Item_func_uuid(THD *thd): Item_str_func(thd) {}
1677   bool fix_length_and_dec()
1678   {
1679     collation.set(system_charset_info,
1680                   DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII);
1681     fix_char_length(MY_UUID_STRING_LENGTH);
1682     return FALSE;
1683   }
1684   bool const_item() const { return false; }
1685   table_map used_tables() const { return RAND_TABLE_BIT; }
1686   const char *func_name() const{ return "uuid"; }
1687   String *val_str(String *);
1688   bool check_vcol_func_processor(void *arg)
1689   {
1690     return mark_unsupported_function(func_name(), "()", arg, VCOL_NON_DETERMINISTIC);
1691   }
1692   Item *get_copy(THD *thd)
1693   { return get_item_copy<Item_func_uuid>(thd, this); }
1694 };
1695 
1696 
1697 class Item_func_dyncol_create: public Item_str_func
1698 {
1699 protected:
1700   DYNCALL_CREATE_DEF *defs;
1701   DYNAMIC_COLUMN_VALUE *vals;
1702   uint *keys_num;
1703   LEX_STRING *keys_str;
1704   bool names, force_names;
1705   bool prepare_arguments(THD *thd, bool force_names);
1706   void print_arguments(String *str, enum_query_type query_type);
1707 public:
1708   Item_func_dyncol_create(THD *thd, List<Item> &args, DYNCALL_CREATE_DEF *dfs);
1709   bool fix_fields(THD *thd, Item **ref);
1710   bool fix_length_and_dec();
1711   const char *func_name() const{ return "column_create"; }
1712   String *val_str(String *);
1713   void print(String *str, enum_query_type query_type);
1714   enum Functype functype() const   { return DYNCOL_FUNC; }
1715   Item *get_copy(THD *thd)
1716   { return get_item_copy<Item_func_dyncol_create>(thd, this); }
1717 };
1718 
1719 
1720 class Item_func_dyncol_add: public Item_func_dyncol_create
1721 {
1722 public:
1723   Item_func_dyncol_add(THD *thd, List<Item> &args_arg, DYNCALL_CREATE_DEF *dfs):
1724     Item_func_dyncol_create(thd, args_arg, dfs)
1725   {}
1726   const char *func_name() const{ return "column_add"; }
1727   String *val_str(String *);
1728   void print(String *str, enum_query_type query_type);
1729   Item *get_copy(THD *thd)
1730   { return get_item_copy<Item_func_dyncol_add>(thd, this); }
1731 };
1732 
1733 class Item_func_dyncol_json: public Item_str_func
1734 {
1735 public:
1736   Item_func_dyncol_json(THD *thd, Item *str): Item_str_func(thd, str)
1737     {collation.set(DYNCOL_UTF);}
1738   const char *func_name() const{ return "column_json"; }
1739   String *val_str(String *);
1740   bool fix_length_and_dec()
1741   {
1742     max_length= MAX_BLOB_WIDTH;
1743     maybe_null= 1;
1744     decimals= 0;
1745     return FALSE;
1746   }
1747   Item *get_copy(THD *thd)
1748   { return get_item_copy<Item_func_dyncol_json>(thd, this); }
1749 };
1750 
1751 /*
1752   The following functions is always called from an Item_cast function
1753 */
1754 
1755 class Item_dyncol_get: public Item_str_func
1756 {
1757 public:
1758   Item_dyncol_get(THD *thd, Item *str, Item *num): Item_str_func(thd, str, num)
1759   {}
1760   bool fix_length_and_dec()
1761   { maybe_null= 1;; max_length= MAX_BLOB_WIDTH; return FALSE; }
1762   /* Mark that collation can change between calls */
1763   bool dynamic_result() { return 1; }
1764 
1765   const char *func_name() const { return "column_get"; }
1766   String *val_str(String *);
1767   longlong val_int();
1768   longlong val_int_signed_typecast()
1769   {
1770     unsigned_flag= false;   // Mark that we want to have a signed value
1771     longlong value= val_int(); // val_int() can change unsigned_flag
1772     if (!null_value && unsigned_flag && value < 0)
1773       push_note_converted_to_negative_complement(current_thd);
1774     return value;
1775   }
1776   longlong val_int_unsigned_typecast()
1777   {
1778     unsigned_flag= true; // Mark that we want to have an unsigned value
1779     longlong value= val_int(); // val_int() can change unsigned_flag
1780     if (!null_value && unsigned_flag == 0 && value < 0)
1781       push_note_converted_to_positive_complement(current_thd);
1782     return value;
1783   }
1784   double val_real();
1785   my_decimal *val_decimal(my_decimal *);
1786   bool get_dyn_value(THD *thd, DYNAMIC_COLUMN_VALUE *val, String *tmp);
1787   bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
1788   void print(String *str, enum_query_type query_type);
1789   Item *get_copy(THD *thd)
1790   { return get_item_copy<Item_dyncol_get>(thd, this); }
1791 };
1792 
1793 
1794 class Item_func_dyncol_list: public Item_str_func
1795 {
1796 public:
1797   Item_func_dyncol_list(THD *thd, Item *str): Item_str_func(thd, str)
1798     {collation.set(DYNCOL_UTF);}
1799   bool fix_length_and_dec()
1800   { maybe_null= 1; max_length= MAX_BLOB_WIDTH; return FALSE; };
1801   const char *func_name() const{ return "column_list"; }
1802   String *val_str(String *);
1803   Item *get_copy(THD *thd)
1804   { return get_item_copy<Item_func_dyncol_list>(thd, this); }
1805 };
1806 
1807 /*
1808   this is used by JOIN_TAB::keep_current_rowid
1809   and stores handler::position().
1810   It has nothing to do with _rowid pseudo-column, that the parser supports.
1811 */
1812 class Item_temptable_rowid :public Item_str_func
1813 {
1814 public:
1815   TABLE *table;
1816   Item_temptable_rowid(TABLE *table_arg);
1817   const Type_handler *type_handler() const { return &type_handler_string; }
1818   Field *create_tmp_field(bool group, TABLE *table)
1819   { return create_table_field_from_handler(table); }
1820   String *val_str(String *str);
1821   enum Functype functype() const { return  TEMPTABLE_ROWID; }
1822   const char *func_name() const { return "<rowid>"; }
1823   bool fix_length_and_dec();
1824   Item *get_copy(THD *thd)
1825   { return get_item_copy<Item_temptable_rowid>(thd, this); }
1826 };
1827 
1828 #endif /* ITEM_STRFUNC_INCLUDED */
1829