1 #ifndef SQL_ITEM_INCLUDED
2 #define SQL_ITEM_INCLUDED
3
4 /* Copyright (c) 2000, 2017, Oracle and/or its affiliates.
5 Copyright (c) 2009, 2021, MariaDB Corporation.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 of the License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
19
20
21 #ifdef USE_PRAGMA_INTERFACE
22 #pragma interface /* gcc class implementation */
23 #endif
24
25 #include "sql_priv.h" /* STRING_BUFFER_USUAL_SIZE */
26 #include "unireg.h"
27 #include "sql_const.h" /* RAND_TABLE_BIT, MAX_FIELD_NAME */
28 #include "field.h" /* Derivation */
29 #include "sql_type.h"
30 #include "sql_time.h"
31 #include "mem_root_array.h"
32
33 C_MODE_START
34 #include <ma_dyncol.h>
35
36 /*
37 A prototype for a C-compatible structure to store a value of any data type.
38 Currently it has to stay in /sql, as it depends on String and my_decimal.
39 We'll do the following changes:
40 1. add pure C "struct st_string" and "struct st_my_decimal"
41 2. change type of m_string to struct st_string and move inside the union
42 3. change type of m_decmal to struct st_my_decimal and move inside the union
43 4. move the definition to some file in /include
44 */
45 struct st_value
46 {
47 enum enum_dynamic_column_type m_type;
48 union
49 {
50 longlong m_longlong;
51 double m_double;
52 MYSQL_TIME m_time;
53 } value;
54 String m_string;
55 my_decimal m_decimal;
56 };
57
58 C_MODE_END
59
60
61 class Value: public st_value
62 {
63 public:
is_null()64 bool is_null() const { return m_type == DYN_COL_NULL; }
is_longlong()65 bool is_longlong() const
66 {
67 return m_type == DYN_COL_UINT || m_type == DYN_COL_INT;
68 }
is_double()69 bool is_double() const { return m_type == DYN_COL_DOUBLE; }
is_temporal()70 bool is_temporal() const { return m_type == DYN_COL_DATETIME; }
is_string()71 bool is_string() const { return m_type == DYN_COL_STRING; }
is_decimal()72 bool is_decimal() const { return m_type == DYN_COL_DECIMAL; }
73 };
74
75
76 template<size_t buffer_size>
77 class ValueBuffer: public Value
78 {
79 char buffer[buffer_size];
reset_buffer()80 void reset_buffer()
81 {
82 m_string.set(buffer, buffer_size, &my_charset_bin);
83 }
84 public:
ValueBuffer()85 ValueBuffer()
86 {
87 reset_buffer();
88 }
89 };
90
91
92 #ifdef DBUG_OFF
dbug_print_item(Item * item)93 static inline const char *dbug_print_item(Item *item) { return NULL; }
94 #else
95 const char *dbug_print_item(Item *item);
96 #endif
97
98 class Virtual_tmp_table;
99 class sp_head;
100 class Protocol;
101 struct TABLE_LIST;
102 void item_init(void); /* Init item functions */
103 class Item_basic_value;
104 class Item_result_field;
105 class Item_field;
106 class Item_ref;
107 class Item_param;
108 class user_var_entry;
109 class JOIN;
110 struct KEY_FIELD;
111 struct SARGABLE_PARAM;
112 class RANGE_OPT_PARAM;
113 class SEL_TREE;
114 class With_sum_func_cache;
115
116 enum precedence {
117 LOWEST_PRECEDENCE,
118 ASSIGN_PRECEDENCE, // :=
119 OR_PRECEDENCE, // OR, || (unless PIPES_AS_CONCAT)
120 XOR_PRECEDENCE, // XOR
121 AND_PRECEDENCE, // AND, &&
122 NOT_PRECEDENCE, // NOT (unless HIGH_NOT_PRECEDENCE)
123 CMP_PRECEDENCE, // =, <=>, >=, >, <=, <, <>, !=, IS
124 BETWEEN_PRECEDENCE, // BETWEEN
125 IN_PRECEDENCE, // IN, LIKE, REGEXP
126 BITOR_PRECEDENCE, // |
127 BITAND_PRECEDENCE, // &
128 SHIFT_PRECEDENCE, // <<, >>
129 INTERVAL_PRECEDENCE, // first argument in +INTERVAL
130 ADD_PRECEDENCE, // +, -
131 MUL_PRECEDENCE, // *, /, DIV, %, MOD
132 BITXOR_PRECEDENCE, // ^
133 PIPES_PRECEDENCE, // || (if PIPES_AS_CONCAT)
134 NEG_PRECEDENCE, // unary -, ~, !, NOT (if HIGH_NOT_PRECEDENCE)
135 COLLATE_PRECEDENCE, // BINARY, COLLATE
136 DEFAULT_PRECEDENCE,
137 HIGHEST_PRECEDENCE
138 };
139
140 bool mark_unsupported_function(const char *where, void *store, uint result);
141
142 /* convenience helper for mark_unsupported_function() above */
143 bool mark_unsupported_function(const char *w1, const char *w2,
144 void *store, uint result);
145
146 /* Bits for the split_sum_func() function */
147 #define SPLIT_SUM_SKIP_REGISTERED 1 /* Skip registered funcs */
148 #define SPLIT_SUM_SELECT 2 /* SELECT item; Split all parts */
149
150
151 #define NO_EXTRACTION_FL (1 << 6)
152 #define FULL_EXTRACTION_FL (1 << 7)
153 #define DELETION_FL (1 << 8)
154 #define IMMUTABLE_FL (1 << 9)
155 #define SUBSTITUTION_FL (1 << 10)
156 #define EXTRACTION_MASK \
157 (NO_EXTRACTION_FL | FULL_EXTRACTION_FL | DELETION_FL | IMMUTABLE_FL)
158
159 extern const char *item_empty_name;
160
161 void dummy_error_processor(THD *thd, void *data);
162
163 void view_error_processor(THD *thd, void *data);
164
165 /*
166 Instances of Name_resolution_context store the information necessary for
167 name resolution of Items and other context analysis of a query made in
168 fix_fields().
169
170 This structure is a part of SELECT_LEX, a pointer to this structure is
171 assigned when an item is created (which happens mostly during parsing
172 (sql_yacc.yy)), but the structure itself will be initialized after parsing
173 is complete
174
175 TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
176 separate SELECT_LEX which allow to remove tricks of changing this
177 structure before and after INSERT/CREATE and its SELECT to make correct
178 field name resolution.
179 */
180 struct Name_resolution_context: Sql_alloc
181 {
182 /*
183 The name resolution context to search in when an Item cannot be
184 resolved in this context (the context of an outer select)
185 */
186 Name_resolution_context *outer_context;
187
188 /*
189 List of tables used to resolve the items of this context. Usually these
190 are tables from the FROM clause of SELECT statement. The exceptions are
191 INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
192 subquery is not moved to a separate SELECT_LEX. For these types of
193 statements we have to change this member dynamically to ensure correct
194 name resolution of different parts of the statement.
195 */
196 TABLE_LIST *table_list;
197 /*
198 In most cases the two table references below replace 'table_list' above
199 for the purpose of name resolution. The first and last name resolution
200 table references allow us to search only in a sub-tree of the nested
201 join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
202 and JOIN ... ON.
203 */
204 TABLE_LIST *first_name_resolution_table;
205 /*
206 Last table to search in the list of leaf table references that begins
207 with first_name_resolution_table.
208 */
209 TABLE_LIST *last_name_resolution_table;
210
211 /* Cache first_name_resolution_table in setup_natural_join_row_types */
212 TABLE_LIST *natural_join_first_table;
213 /*
214 SELECT_LEX item belong to, in case of merged VIEW it can differ from
215 SELECT_LEX where item was created, so we can't use table_list/field_list
216 from there
217 */
218 st_select_lex *select_lex;
219
220 /*
221 Processor of errors caused during Item name resolving, now used only to
222 hide underlying tables in errors about views (i.e. it substitute some
223 errors for views)
224 */
225 void (*error_processor)(THD *, void *);
226 void *error_processor_data;
227
228 /*
229 When TRUE items are resolved in this context both against the
230 SELECT list and this->table_list. If FALSE, items are resolved
231 only against this->table_list.
232 */
233 bool resolve_in_select_list;
234
235 /*
236 Security context of this name resolution context. It's used for views
237 and is non-zero only if the view is defined with SQL SECURITY DEFINER.
238 */
239 Security_context *security_ctx;
240
Name_resolution_contextName_resolution_context241 Name_resolution_context()
242 :outer_context(0), table_list(0), select_lex(0),
243 error_processor_data(0),
244 security_ctx(0)
245 {}
246
initName_resolution_context247 void init()
248 {
249 resolve_in_select_list= FALSE;
250 error_processor= &dummy_error_processor;
251 first_name_resolution_table= NULL;
252 last_name_resolution_table= NULL;
253 }
254
resolve_in_table_list_onlyName_resolution_context255 void resolve_in_table_list_only(TABLE_LIST *tables)
256 {
257 table_list= first_name_resolution_table= tables;
258 resolve_in_select_list= FALSE;
259 }
260
process_errorName_resolution_context261 void process_error(THD *thd)
262 {
263 (*error_processor)(thd, error_processor_data);
264 }
outer_selectName_resolution_context265 st_select_lex *outer_select()
266 {
267 return (outer_context ?
268 outer_context->select_lex :
269 NULL);
270 }
271 };
272
273
274 /*
275 Store and restore the current state of a name resolution context.
276 */
277
278 class Name_resolution_context_state
279 {
280 private:
281 TABLE_LIST *save_table_list;
282 TABLE_LIST *save_first_name_resolution_table;
283 TABLE_LIST *save_next_name_resolution_table;
284 bool save_resolve_in_select_list;
285 TABLE_LIST *save_next_local;
286
287 public:
Name_resolution_context_state()288 Name_resolution_context_state() {} /* Remove gcc warning */
289
290 public:
291 /* Save the state of a name resolution context. */
save_state(Name_resolution_context * context,TABLE_LIST * table_list)292 void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
293 {
294 save_table_list= context->table_list;
295 save_first_name_resolution_table= context->first_name_resolution_table;
296 save_resolve_in_select_list= context->resolve_in_select_list;
297 save_next_local= table_list->next_local;
298 save_next_name_resolution_table= table_list->next_name_resolution_table;
299 }
300
301 /* Restore a name resolution context from saved state. */
restore_state(Name_resolution_context * context,TABLE_LIST * table_list)302 void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
303 {
304 table_list->next_local= save_next_local;
305 table_list->next_name_resolution_table= save_next_name_resolution_table;
306 context->table_list= save_table_list;
307 context->first_name_resolution_table= save_first_name_resolution_table;
308 context->resolve_in_select_list= save_resolve_in_select_list;
309 }
310
get_first_name_resolution_table()311 TABLE_LIST *get_first_name_resolution_table()
312 {
313 return save_first_name_resolution_table;
314 }
315 };
316
317 class Name_resolution_context_backup
318 {
319 Name_resolution_context &ctx;
320 TABLE_LIST &table_list;
321 table_map save_map;
322 Name_resolution_context_state ctx_state;
323
324 public:
Name_resolution_context_backup(Name_resolution_context & _ctx,TABLE_LIST & _table_list)325 Name_resolution_context_backup(Name_resolution_context &_ctx, TABLE_LIST &_table_list)
326 : ctx(_ctx), table_list(_table_list), save_map(_table_list.map)
327 {
328 ctx_state.save_state(&ctx, &table_list);
329 ctx.table_list= &table_list;
330 ctx.first_name_resolution_table= &table_list;
331 }
~Name_resolution_context_backup()332 ~Name_resolution_context_backup()
333 {
334 ctx_state.restore_state(&ctx, &table_list);
335 table_list.map= save_map;
336 }
337 };
338
339
340 /*
341 This enum is used to report information about monotonicity of function
342 represented by Item* tree.
343 Monotonicity is defined only for Item* trees that represent table
344 partitioning expressions (i.e. have no subselects/user vars/PS parameters
345 etc etc). An Item* tree is assumed to have the same monotonicity properties
346 as its corresponding function F:
347
348 [signed] longlong F(field1, field2, ...) {
349 put values of field_i into table record buffer;
350 return item->val_int();
351 }
352
353 NOTE
354 At the moment function monotonicity is not well defined (and so may be
355 incorrect) for Item trees with parameters/return types that are different
356 from INT_RESULT, may be NULL, or are unsigned.
357 It will be possible to address this issue once the related partitioning bugs
358 (BUG#16002, BUG#15447, BUG#13436) are fixed.
359
360 The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
361 NULL which puts those rows into the NULL partition, but
362 '2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
363 for this (see Bug#20577).
364 */
365
366 typedef enum monotonicity_info
367 {
368 NON_MONOTONIC, /* none of the below holds */
369 MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */
370 MONOTONIC_INCREASING_NOT_NULL, /* But only for valid/real x and y */
371 MONOTONIC_STRICT_INCREASING,/* F() is unary and (x < y) => (F(x) < F(y)) */
372 MONOTONIC_STRICT_INCREASING_NOT_NULL /* But only for valid/real x and y */
373 } enum_monotonicity_info;
374
375 /*************************************************************************/
376
377 class sp_rcontext;
378
379 /**
380 A helper class to collect different behavior of various kinds of SP variables:
381 - local SP variables and SP parameters
382 - PACKAGE BODY routine variables
383 - (there will be more kinds in the future)
384 */
385
386 class Sp_rcontext_handler
387 {
388 public:
~Sp_rcontext_handler()389 virtual ~Sp_rcontext_handler() {}
390 /**
391 A prefix used for SP variable names in queries:
392 - EXPLAIN EXTENDED
393 - SHOW PROCEDURE CODE
394 Local variables and SP parameters have empty prefixes.
395 Package body variables are marked with a special prefix.
396 This improves readability of the output of these queries,
397 especially when a local variable or a parameter has the same
398 name with a package body variable.
399 */
400 virtual const LEX_CSTRING *get_name_prefix() const= 0;
401 /**
402 At execution time THD->spcont points to the run-time context (sp_rcontext)
403 of the currently executed routine.
404 Local variables store their data in the sp_rcontext pointed by thd->spcont.
405 Package body variables store data in separate sp_rcontext that belongs
406 to the package.
407 This method provides access to the proper sp_rcontext structure,
408 depending on the SP variable kind.
409 */
410 virtual sp_rcontext *get_rcontext(sp_rcontext *ctx) const= 0;
411 };
412
413
414 class Sp_rcontext_handler_local: public Sp_rcontext_handler
415 {
416 public:
417 const LEX_CSTRING *get_name_prefix() const;
418 sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
419 };
420
421
422 class Sp_rcontext_handler_package_body: public Sp_rcontext_handler
423 {
424 public:
425 const LEX_CSTRING *get_name_prefix() const;
426 sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
427 };
428
429
430 extern MYSQL_PLUGIN_IMPORT
431 Sp_rcontext_handler_local sp_rcontext_handler_local;
432
433
434 extern MYSQL_PLUGIN_IMPORT
435 Sp_rcontext_handler_package_body sp_rcontext_handler_package_body;
436
437
438
439 class Item_equal;
440
441 struct st_join_table* const NO_PARTICULAR_TAB= (struct st_join_table*)0x1;
442
443 typedef struct replace_equal_field_arg
444 {
445 Item_equal *item_equal;
446 struct st_join_table *context_tab;
447 } REPLACE_EQUAL_FIELD_ARG;
448
449 class Settable_routine_parameter
450 {
451 public:
452 /*
453 Set required privileges for accessing the parameter.
454
455 SYNOPSIS
456 set_required_privilege()
457 rw if 'rw' is true then we are going to read and set the
458 parameter, so SELECT and UPDATE privileges might be
459 required, otherwise we only reading it and SELECT
460 privilege might be required.
461 */
Settable_routine_parameter()462 Settable_routine_parameter() {}
~Settable_routine_parameter()463 virtual ~Settable_routine_parameter() {}
set_required_privilege(bool rw)464 virtual void set_required_privilege(bool rw) {};
465
466 /*
467 Set parameter value.
468
469 SYNOPSIS
470 set_value()
471 thd thread handle
472 ctx context to which parameter belongs (if it is local
473 variable).
474 it item which represents new value
475
476 RETURN
477 FALSE if parameter value has been set,
478 TRUE if error has occurred.
479 */
480 virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0;
481
set_out_param_info(Send_field * info)482 virtual void set_out_param_info(Send_field *info) {}
483
get_out_param_info()484 virtual const Send_field *get_out_param_info() const
485 { return NULL; }
486
get_item_param()487 virtual Item_param *get_item_param() { return 0; }
488 };
489
490
491 /*
492 A helper class to calculate offset and length of a query fragment
493 - outside of SP
494 - inside an SP
495 - inside a compound block
496 */
497 class Query_fragment
498 {
499 uint m_pos;
500 uint m_length;
set(size_t pos,size_t length)501 void set(size_t pos, size_t length)
502 {
503 DBUG_ASSERT(pos < UINT_MAX32);
504 DBUG_ASSERT(length < UINT_MAX32);
505 m_pos= (uint) pos;
506 m_length= (uint) length;
507 }
508 public:
509 Query_fragment(THD *thd, sp_head *sphead, const char *start, const char *end);
pos()510 uint pos() const { return m_pos; }
length()511 uint length() const { return m_length; }
512 };
513
514
515 /**
516 This is used for items in the query that needs to be rewritten
517 before binlogging
518
519 At the moment this applies to Item_param and Item_splocal
520 */
521 class Rewritable_query_parameter
522 {
523 public:
524 /*
525 Offset inside the query text.
526 Value of 0 means that this object doesn't have to be replaced
527 (for example SP variables in control statements)
528 */
529 my_ptrdiff_t pos_in_query;
530
531 /*
532 Byte length of parameter name in the statement. This is not
533 Item::name.length because name.length contains byte length of UTF8-encoded
534 name, but the query string is in the client charset.
535 */
536 uint len_in_query;
537
538 bool limit_clause_param;
539
540 Rewritable_query_parameter(uint pos_in_q= 0, uint len_in_q= 0)
pos_in_query(pos_in_q)541 : pos_in_query(pos_in_q), len_in_query(len_in_q),
542 limit_clause_param(false)
543 { }
544
~Rewritable_query_parameter()545 virtual ~Rewritable_query_parameter() { }
546
547 virtual bool append_for_log(THD *thd, String *str) = 0;
548 };
549
550 class Copy_query_with_rewrite
551 {
552 THD *thd;
553 const char *src;
554 size_t src_len, from;
555 String *dst;
556
copy_up_to(size_t bytes)557 bool copy_up_to(size_t bytes)
558 {
559 DBUG_ASSERT(bytes >= from);
560 return dst->append(src + from, uint32(bytes - from));
561 }
562
563 public:
564
Copy_query_with_rewrite(THD * t,const char * s,size_t l,String * d)565 Copy_query_with_rewrite(THD *t, const char *s, size_t l, String *d)
566 :thd(t), src(s), src_len(l), from(0), dst(d) { }
567
append(Rewritable_query_parameter * p)568 bool append(Rewritable_query_parameter *p)
569 {
570 if (copy_up_to(p->pos_in_query) || p->append_for_log(thd, dst))
571 return true;
572 from= p->pos_in_query + p->len_in_query;
573 return false;
574 }
575
finalize()576 bool finalize()
577 { return copy_up_to(src_len); }
578 };
579
580 struct st_dyncall_create_def
581 {
582 Item *key, *value;
583 CHARSET_INFO *cs;
584 uint len, frac;
585 DYNAMIC_COLUMN_TYPE type;
586 };
587
588 typedef struct st_dyncall_create_def DYNCALL_CREATE_DEF;
589
590
591 typedef bool (Item::*Item_processor) (void *arg);
592 /*
593 Analyzer function
594 SYNOPSIS
595 argp in/out IN: Analysis parameter
596 OUT: Parameter to be passed to the transformer
597
598 RETURN
599 TRUE Invoke the transformer
600 FALSE Don't do it
601
602 */
603 typedef bool (Item::*Item_analyzer) (uchar **argp);
604 typedef Item* (Item::*Item_transformer) (THD *thd, uchar *arg);
605 typedef void (*Cond_traverser) (const Item *item, void *arg);
606 typedef bool (Item::*Pushdown_checker) (uchar *arg);
607
608 struct st_cond_statistic;
609
610 struct find_selective_predicates_list_processor_data
611 {
612 TABLE *table;
613 List<st_cond_statistic> list;
614 };
615
616 class MY_LOCALE;
617
618 class Item_equal;
619 class COND_EQUAL;
620
621 class st_select_lex_unit;
622
623 class Item_func_not;
624 class Item_splocal;
625
626 /* Item::common_flags */
627 /* Indicates that name of this Item autogenerated or set by user */
628 #define IS_AUTO_GENERATED_NAME 1
629 /* Indicates that this item is in CYCLE clause of WITH */
630 #define IS_IN_WITH_CYCLE 2
631
632
633 /**
634 String_copier that sends Item specific warnings.
635 */
636 class String_copier_for_item: public String_copier
637 {
638 THD *m_thd;
639 public:
640 bool copy_with_warn(CHARSET_INFO *dstcs, String *dst,
641 CHARSET_INFO *srccs, const char *src,
642 uint32 src_length, uint32 nchars);
String_copier_for_item(THD * thd)643 String_copier_for_item(THD *thd): m_thd(thd) { }
644 };
645
646
647 /**
648 A helper class describing what kind of Item created a temporary field.
649 - If m_field is set, then the temporary field was created from Field
650 (e.g. when the Item was Item_field, or Item_ref pointing to Item_field)
651 - If m_default_field is set, then there is a usable DEFAULT value.
652 (e.g. when the Item is Item_field)
653 - If m_item_result_field is set, then the temporary field was created
654 from certain sub-types of Item_result_field (e.g. Item_func)
655 See create_tmp_field() in sql_select.cc for details.
656 */
657
658 class Tmp_field_src
659 {
660 Field *m_field;
661 Field *m_default_field;
662 Item_result_field *m_item_result_field;
663 public:
Tmp_field_src()664 Tmp_field_src()
665 :m_field(0),
666 m_default_field(0),
667 m_item_result_field(0)
668 { }
field()669 Field *field() const { return m_field; }
default_field()670 Field *default_field() const { return m_default_field; }
item_result_field()671 Item_result_field *item_result_field() const { return m_item_result_field; }
set_field(Field * field)672 void set_field(Field *field) { m_field= field; }
set_default_field(Field * field)673 void set_default_field(Field *field) { m_default_field= field; }
set_item_result_field(Item_result_field * item)674 void set_item_result_field(Item_result_field *item)
675 { m_item_result_field= item; }
676 };
677
678
679 /**
680 Parameters for create_tmp_field_ex().
681 See create_tmp_field() in sql_select.cc for details.
682 */
683
684 class Tmp_field_param
685 {
686 bool m_group;
687 bool m_modify_item;
688 bool m_table_cant_handle_bit_fields;
689 bool m_make_copy_field;
690 public:
Tmp_field_param(bool group,bool modify_item,bool table_cant_handle_bit_fields,bool make_copy_field)691 Tmp_field_param(bool group,
692 bool modify_item,
693 bool table_cant_handle_bit_fields,
694 bool make_copy_field)
695 :m_group(group),
696 m_modify_item(modify_item),
697 m_table_cant_handle_bit_fields(table_cant_handle_bit_fields),
698 m_make_copy_field(make_copy_field)
699 { }
group()700 bool group() const { return m_group; }
modify_item()701 bool modify_item() const { return m_modify_item; }
table_cant_handle_bit_fields()702 bool table_cant_handle_bit_fields() const
703 { return m_table_cant_handle_bit_fields; }
make_copy_field()704 bool make_copy_field() const { return m_make_copy_field; }
set_modify_item(bool to)705 void set_modify_item(bool to) { m_modify_item= to; }
706 };
707
708
709 class Item_const
710 {
711 public:
~Item_const()712 virtual ~Item_const() {}
713 virtual const Type_all_attributes *get_type_all_attributes_from_const() const= 0;
const_is_null()714 virtual bool const_is_null() const { return false; }
const_ptr_longlong()715 virtual const longlong *const_ptr_longlong() const { return NULL; }
const_ptr_double()716 virtual const double *const_ptr_double() const { return NULL; }
const_ptr_my_decimal()717 virtual const my_decimal *const_ptr_my_decimal() const { return NULL; }
const_ptr_mysql_time()718 virtual const MYSQL_TIME *const_ptr_mysql_time() const { return NULL; }
const_ptr_string()719 virtual const String *const_ptr_string() const { return NULL; }
720 };
721
722
723 /****************************************************************************/
724
725 #define STOP_PTR ((void *) 1)
726
727 class Item: public Value_source,
728 public Type_all_attributes
729 {
730 /**
731 The index in the JOIN::join_tab array of the JOIN_TAB this Item is attached
732 to. Items are attached (or 'pushed') to JOIN_TABs during optimization by the
733 make_cond_for_table procedure. During query execution, this item is
734 evaluated when the join loop reaches the corresponding JOIN_TAB.
735
736 If the value of join_tab_idx >= MAX_TABLES, this means that there is no
737 corresponding JOIN_TAB.
738 */
739 uint join_tab_idx;
740
741 static void *operator new(size_t size);
742
743 public:
new(size_t size,MEM_ROOT * mem_root)744 static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
745 { return alloc_root(mem_root, size); }
delete(void * ptr,size_t size)746 static void operator delete(void *ptr,size_t size) { TRASH_FREE(ptr, size); }
delete(void * ptr,MEM_ROOT * mem_root)747 static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
748
749 enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM,
750 WINDOW_FUNC_ITEM,
751 /*
752 NOT NULL literal-alike constants, which do not change their
753 value during an SQL statement execution, but can optionally
754 change their value between statements:
755 - Item_literal - real NOT NULL constants
756 - Item_param - can change between statements
757 - Item_splocal - can change between statements
758 - Item_user_var_as_out_param - hack
759 Note, Item_user_var_as_out_param actually abuses the type code.
760 It should be moved out of the Item tree eventually.
761 */
762 CONST_ITEM,
763 NULL_ITEM, // Item_null or Item_param bound to NULL
764 COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
765 CONTEXTUALLY_TYPED_VALUE_ITEM,
766 PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
767 FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
768 SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
769 PARAM_ITEM, TRIGGER_FIELD_ITEM,
770 EXPR_CACHE_ITEM};
771
772 enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
773
774 enum traverse_order { POSTFIX, PREFIX };
775
776 /* Cache of the result of is_expensive(). */
777 int8 is_expensive_cache;
778
779 /* Reuse size, only used by SP local variable assignment, otherwise 0 */
780 uint rsize;
781
782 protected:
783 /*
784 str_values's main purpose is to be used to cache the value in
785 save_in_field
786 */
787 String str_value;
788
789 SEL_TREE *get_mm_tree_for_const(RANGE_OPT_PARAM *param);
790
791 /**
792 Create a field based on the exact data type handler.
793 */
create_table_field_from_handler(MEM_ROOT * root,TABLE * table)794 Field *create_table_field_from_handler(MEM_ROOT *root, TABLE *table)
795 {
796 const Type_handler *h= type_handler();
797 return h->make_and_init_table_field(root, &name,
798 Record_addr(maybe_null),
799 *this, table);
800 }
801 /**
802 Create a field based on field_type of argument.
803 This is used to create a field for
804 - IFNULL(x,something)
805 - time functions
806 - prepared statement placeholders
807 - SP variables with data type references: DECLARE a TYPE OF t1.a;
808 @retval NULL error
809 @retval !NULL on success
810 */
tmp_table_field_from_field_type(MEM_ROOT * root,TABLE * table)811 Field *tmp_table_field_from_field_type(MEM_ROOT *root, TABLE *table)
812 {
813 DBUG_ASSERT(is_fixed());
814 const Type_handler *h= type_handler()->type_handler_for_tmp_table(this);
815 return h->make_and_init_table_field(root, &name,
816 Record_addr(maybe_null),
817 *this, table);
818 }
819 /**
820 Create a temporary field for a simple Item, which does not
821 need any special action after the field creation:
822 - is not an Item_field descendant (and not a reference to Item_field)
823 - is not an Item_result_field descendant
824 - does not need to copy any DEFAULT value to the result Field
825 - does not need to set Field::is_created_from_null_item for the result
826 See create_tmp_field_ex() for details on parameters and return values.
827 */
create_tmp_field_ex_simple(MEM_ROOT * root,TABLE * table,Tmp_field_src * src,const Tmp_field_param * param)828 Field *create_tmp_field_ex_simple(MEM_ROOT *root,
829 TABLE *table,
830 Tmp_field_src *src,
831 const Tmp_field_param *param)
832 {
833 DBUG_ASSERT(!param->make_copy_field());
834 DBUG_ASSERT(!is_result_field());
835 DBUG_ASSERT(type() != NULL_ITEM);
836 return tmp_table_field_from_field_type(root, table);
837 }
838 Field *create_tmp_field_int(MEM_ROOT *root, TABLE *table,
839 uint convert_int_length);
840 Field *tmp_table_field_from_field_type_maybe_null(MEM_ROOT *root,
841 TABLE *table,
842 Tmp_field_src *src,
843 const Tmp_field_param *param,
844 bool is_explicit_null);
845
846 void raise_error_not_evaluable();
847 void push_note_converted_to_negative_complement(THD *thd);
848 void push_note_converted_to_positive_complement(THD *thd);
849
850 /* Helper methods, to get an Item value from another Item */
val_real_from_item(Item * item)851 double val_real_from_item(Item *item)
852 {
853 DBUG_ASSERT(is_fixed());
854 double value= item->val_real();
855 null_value= item->null_value;
856 return value;
857 }
val_int_from_item(Item * item)858 longlong val_int_from_item(Item *item)
859 {
860 DBUG_ASSERT(is_fixed());
861 longlong value= item->val_int();
862 null_value= item->null_value;
863 return value;
864 }
val_str_from_item(Item * item,String * str)865 String *val_str_from_item(Item *item, String *str)
866 {
867 DBUG_ASSERT(is_fixed());
868 String *res= item->val_str(str);
869 if (res)
870 res->set_charset(collation.collation);
871 if ((null_value= item->null_value))
872 res= NULL;
873 return res;
874 }
val_native_from_item(THD * thd,Item * item,Native * to)875 bool val_native_from_item(THD *thd, Item *item, Native *to)
876 {
877 DBUG_ASSERT(is_fixed());
878 null_value= item->val_native(thd, to);
879 DBUG_ASSERT(null_value == item->null_value);
880 return null_value;
881 }
val_native_from_field(Field * field,Native * to)882 bool val_native_from_field(Field *field, Native *to)
883 {
884 if ((null_value= field->is_null()))
885 return true;
886 return (null_value= field->val_native(to));
887 }
val_native_with_conversion_from_item(THD * thd,Item * item,Native * to,const Type_handler * handler)888 bool val_native_with_conversion_from_item(THD *thd, Item *item, Native *to,
889 const Type_handler *handler)
890 {
891 DBUG_ASSERT(is_fixed());
892 return null_value= item->val_native_with_conversion(thd, to, handler);
893 }
val_decimal_from_item(Item * item,my_decimal * decimal_value)894 my_decimal *val_decimal_from_item(Item *item, my_decimal *decimal_value)
895 {
896 DBUG_ASSERT(is_fixed());
897 my_decimal *value= item->val_decimal(decimal_value);
898 if ((null_value= item->null_value))
899 value= NULL;
900 return value;
901 }
get_date_from_item(THD * thd,Item * item,MYSQL_TIME * ltime,date_mode_t fuzzydate)902 bool get_date_from_item(THD *thd, Item *item,
903 MYSQL_TIME *ltime, date_mode_t fuzzydate)
904 {
905 bool rc= item->get_date(thd, ltime, fuzzydate);
906 null_value= MY_TEST(rc || item->null_value);
907 return rc;
908 }
909 public:
910
911 /*
912 Cache val_str() into the own buffer, e.g. to evaluate constant
913 expressions with subqueries in the ORDER/GROUP clauses.
914 */
val_str()915 String *val_str() { return val_str(&str_value); }
get_item_func()916 virtual Item_func *get_item_func() { return NULL; }
917
918 const MY_LOCALE *locale_from_val_str();
919
920 LEX_CSTRING name; /* Name of item */
921 /* Original item name (if it was renamed)*/
922 const char *orig_name;
923 /**
924 Intrusive list pointer for free list. If not null, points to the next
925 Item on some Query_arena's free list. For instance, stored procedures
926 have their own Query_arena's.
927
928 @see Query_arena::free_list
929 */
930 Item *next;
931 int marker;
932 bool maybe_null; /* If item may be null */
933 bool in_rollup; /* If used in GROUP BY list
934 of a query with ROLLUP */
935 bool null_value; /* if item is null */
936 bool with_param; /* True if contains an SP parameter */
937 bool with_window_func; /* True if item contains a window func */
938 /**
939 True if any item except Item_sum contains a field. Set during parsing.
940 */
941 bool with_field;
942 uint8 common_flags;
is_autogenerated_name()943 bool is_autogenerated_name()
944 { return (common_flags & IS_AUTO_GENERATED_NAME); }
945 // alloc & destruct is done as start of select on THD::mem_root
946 Item(THD *thd);
947 /*
948 Constructor used by Item_field, Item_ref & aggregate (sum) functions.
949 Used for duplicating lists in processing queries with temporary
950 tables
951 Also it used for Item_cond_and/Item_cond_or for creating
952 top AND/OR structure of WHERE clause to protect it of
953 optimisation changes in prepared statements
954 */
955 Item(THD *thd, Item *item);
~Item()956 virtual ~Item()
957 {
958 #ifdef EXTRA_DEBUG
959 name.str= 0;
960 name.length= 0;
961 #endif
962 } /*lint -e1509 */
963 void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs);
set_name(THD * thd,String * str)964 void set_name(THD *thd, String *str)
965 {
966 set_name(thd, str->ptr(), str->length(), str->charset());
967 }
968 void set_name(THD *thd, const LEX_CSTRING &str,
969 CHARSET_INFO *cs= system_charset_info)
970 {
971 set_name(thd, str.str, str.length, cs);
972 }
973 void set_name_no_truncate(THD *thd, const char *str, uint length,
974 CHARSET_INFO *cs);
975 void init_make_send_field(Send_field *tmp_field, const Type_handler *h);
share_name_with(const Item * item)976 void share_name_with(const Item *item)
977 {
978 name= item->name;
979 common_flags= static_cast<uint8>
980 ((common_flags & ~IS_AUTO_GENERATED_NAME) |
981 (item->common_flags & IS_AUTO_GENERATED_NAME));
982 }
983 virtual void cleanup();
984 virtual void make_send_field(THD *thd, Send_field *field);
985
fix_fields_if_needed(THD * thd,Item ** ref)986 bool fix_fields_if_needed(THD *thd, Item **ref)
987 {
988 return is_fixed() ? false : fix_fields(thd, ref);
989 }
fix_fields_if_needed_for_scalar(THD * thd,Item ** ref)990 bool fix_fields_if_needed_for_scalar(THD *thd, Item **ref)
991 {
992 return fix_fields_if_needed(thd, ref) || check_cols(1);
993 }
fix_fields_if_needed_for_bool(THD * thd,Item ** ref)994 bool fix_fields_if_needed_for_bool(THD *thd, Item **ref)
995 {
996 return fix_fields_if_needed_for_scalar(thd, ref);
997 }
fix_fields_if_needed_for_order_by(THD * thd,Item ** ref)998 bool fix_fields_if_needed_for_order_by(THD *thd, Item **ref)
999 {
1000 return fix_fields_if_needed_for_scalar(thd, ref);
1001 }
1002 /*
1003 By default we assume that an Item is fixed by the contstructor.
1004 */
fix_fields(THD *,Item **)1005 virtual bool fix_fields(THD *, Item **)
1006 {
1007 /*
1008 This should not normally be called, because usually before
1009 fix_fields() we check is_fixed() to be false.
1010 But historically we allow fix_fields() to be called for Items
1011 who return basic_const_item()==true.
1012 */
1013 DBUG_ASSERT(is_fixed());
1014 DBUG_ASSERT(basic_const_item());
1015 return false;
1016 }
is_fixed()1017 virtual bool is_fixed() const { return true; }
unfix_fields()1018 virtual void unfix_fields()
1019 {
1020 DBUG_ASSERT(0);
1021 }
1022
1023 /*
1024 Fix after some tables has been pulled out. Basically re-calculate all
1025 attributes that are dependent on the tables.
1026 */
fix_after_pullout(st_select_lex * new_parent,Item ** ref,bool merge)1027 virtual void fix_after_pullout(st_select_lex *new_parent, Item **ref,
1028 bool merge)
1029 {};
1030
1031 /*
1032 This method should be used in case where we are sure that we do not need
1033 complete fix_fields() procedure.
1034 Usually this method is used by the optimizer when it has to create a new
1035 item out of other already fixed items. For example, if the optimizer has
1036 to create a new Item_func for an inferred equality whose left and right
1037 parts are already fixed items. In some cases the optimizer cannot use
1038 directly fixed items as the arguments of the created functional item,
1039 but rather uses intermediate type conversion items. Then the method is
1040 supposed to be applied recursively.
1041 */
quick_fix_field()1042 virtual void quick_fix_field()
1043 {
1044 DBUG_ASSERT(0);
1045 }
1046
save_in_value(THD * thd,struct st_value * value)1047 bool save_in_value(THD *thd, struct st_value *value)
1048 {
1049 return type_handler()->Item_save_in_value(thd, this, value);
1050 }
1051
1052 /* Function returns 1 on overflow and -1 on fatal errors */
1053 int save_in_field_no_warnings(Field *field, bool no_conversions);
1054 virtual int save_in_field(Field *field, bool no_conversions);
1055 virtual bool save_in_param(THD *thd, Item_param *param);
save_org_in_field(Field * field,fast_field_copier data)1056 virtual void save_org_in_field(Field *field,
1057 fast_field_copier data
1058 __attribute__ ((__unused__)))
1059 { (void) save_in_field(field, 1); }
setup_fast_field_copier(Field * field)1060 virtual fast_field_copier setup_fast_field_copier(Field *field)
1061 { return NULL; }
save_safe_in_field(Field * field)1062 virtual int save_safe_in_field(Field *field)
1063 { return save_in_field(field, 1); }
send(Protocol * protocol,st_value * buffer)1064 virtual bool send(Protocol *protocol, st_value *buffer)
1065 {
1066 return type_handler()->Item_send(this, protocol, buffer);
1067 }
1068 virtual bool eq(const Item *, bool binary_cmp) const;
field_type()1069 enum_field_types field_type() const
1070 {
1071 return type_handler()->field_type();
1072 }
1073 virtual const Type_handler *type_handler() const= 0;
1074 /**
1075 Detects if an Item has a fixed data type which is known
1076 even before fix_fields().
1077 Currently it's important only to find Items with a fixed boolean
1078 data type. More item types can be marked in the future as having
1079 a fixed data type (e.g. all literals, all fixed type functions, etc).
1080
1081 @retval NULL if the Item type is not known before fix_fields()
1082 @retval the pointer to the data type handler, if the data type
1083 is known before fix_fields().
1084 */
fixed_type_handler()1085 virtual const Type_handler *fixed_type_handler() const
1086 {
1087 return NULL;
1088 }
type_handler_for_comparison()1089 const Type_handler *type_handler_for_comparison() const
1090 {
1091 return type_handler()->type_handler_for_comparison();
1092 }
real_type_handler()1093 virtual const Type_handler *real_type_handler() const
1094 {
1095 return type_handler();
1096 }
cast_to_int_type_handler()1097 const Type_handler *cast_to_int_type_handler() const
1098 {
1099 return real_type_handler()->cast_to_int_type_handler();
1100 }
1101 /* result_type() of an item specifies how the value should be returned */
result_type()1102 Item_result result_type() const
1103 {
1104 return type_handler()->result_type();
1105 }
1106 /* ... while cmp_type() specifies how it should be compared */
cmp_type()1107 Item_result cmp_type() const
1108 {
1109 return type_handler()->cmp_type();
1110 }
string_type_handler()1111 const Type_handler *string_type_handler() const
1112 {
1113 return Type_handler::string_type_handler(max_length);
1114 }
1115 /*
1116 Calculate the maximum length of an expression.
1117 This method is used in data type aggregation for UNION, e.g.:
1118 SELECT 'b' UNION SELECT COALESCE(double_10_3_field) FROM t1;
1119
1120 The result is usually equal to max_length, except for some numeric types.
1121 In case of the INT, FLOAT, DOUBLE data types Item::max_length and
1122 Item::decimals are ignored, so the returned value depends only on the
1123 data type itself. E.g. for an expression of the DOUBLE(10,3) data type,
1124 the result is always 53 (length 10 and precision 3 do not matter).
1125
1126 max_length is ignored for these numeric data types because the length limit
1127 means only "expected maximum length", it is not a hard limit, so it does
1128 not impose any data truncation. E.g. a column of the type INT(4) can
1129 normally store big values up to 2147483647 without truncation. When we're
1130 aggregating such column for UNION it's important to create a long enough
1131 result column, not to lose any data.
1132
1133 For detailed behaviour of various data types see implementations of
1134 the corresponding Type_handler_xxx::max_display_length().
1135
1136 Note, Item_field::max_display_length() overrides this to get
1137 max_display_length() from the underlying field.
1138 */
max_display_length()1139 virtual uint32 max_display_length() const
1140 {
1141 return type_handler()->max_display_length(this);
1142 }
get_typelib()1143 const TYPELIB *get_typelib() const { return NULL; }
set_maybe_null(bool maybe_null_arg)1144 void set_maybe_null(bool maybe_null_arg) { maybe_null= maybe_null_arg; }
set_typelib(const TYPELIB * typelib)1145 void set_typelib(const TYPELIB *typelib)
1146 {
1147 // Non-field Items (e.g. hybrid functions) never have ENUM/SET types yet.
1148 DBUG_ASSERT(0);
1149 }
get_cache(THD * thd)1150 Item_cache* get_cache(THD *thd) const
1151 {
1152 return type_handler()->Item_get_cache(thd, this);
1153 }
1154 virtual enum Type type() const =0;
is_of_type(Type t,Item_result cmp)1155 bool is_of_type(Type t, Item_result cmp) const
1156 {
1157 return type() == t && cmp_type() == cmp;
1158 }
1159 /*
1160 real_type() is the type of base item. This is same as type() for
1161 most items, except Item_ref() and Item_cache_wrapper() where it
1162 shows the type for the underlying item.
1163 */
real_type()1164 virtual enum Type real_type() const { return type(); }
1165
1166 /*
1167 Return information about function monotonicity. See comment for
1168 enum_monotonicity_info for details. This function can only be called
1169 after fix_fields() call.
1170 */
get_monotonicity_info()1171 virtual enum_monotonicity_info get_monotonicity_info() const
1172 { return NON_MONOTONIC; }
1173
1174 /*
1175 Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
1176
1177 SYNOPSIS
1178 val_int_endpoint()
1179 left_endp FALSE <=> The interval is "x < const" or "x <= const"
1180 TRUE <=> The interval is "x > const" or "x >= const"
1181
1182 incl_endp IN FALSE <=> the comparison is '<' or '>'
1183 TRUE <=> the comparison is '<=' or '>='
1184 OUT The same but for the "F(x) $CMP$ F(const)" comparison
1185
1186 DESCRIPTION
1187 This function is defined only for unary monotonic functions. The caller
1188 supplies the source half-interval
1189
1190 x $CMP$ const
1191
1192 The value of const is supplied implicitly as the value this item's
1193 argument, the form of $CMP$ comparison is specified through the
1194 function's arguments. The calle returns the result interval
1195
1196 F(x) $CMP2$ F(const)
1197
1198 passing back F(const) as the return value, and the form of $CMP2$
1199 through the out parameter. NULL values are assumed to be comparable and
1200 be less than any non-NULL values.
1201
1202 RETURN
1203 The output range bound, which equal to the value of val_int()
1204 - If the value of the function is NULL then the bound is the
1205 smallest possible value of LONGLONG_MIN
1206 */
val_int_endpoint(bool left_endp,bool * incl_endp)1207 virtual longlong val_int_endpoint(bool left_endp, bool *incl_endp)
1208 { DBUG_ASSERT(0); return 0; }
1209
1210
1211 /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
1212 /*
1213 Return double precision floating point representation of item.
1214
1215 SYNOPSIS
1216 val_real()
1217
1218 RETURN
1219 In case of NULL value return 0.0 and set null_value flag to TRUE.
1220 If value is not null null_value flag will be reset to FALSE.
1221 */
1222 virtual double val_real()=0;
to_double_null()1223 Double_null to_double_null()
1224 {
1225 // val_real() must be caleed on a separate line. See to_longlong_null()
1226 double nr= val_real();
1227 return Double_null(nr, null_value);
1228 }
1229 /*
1230 Return integer representation of item.
1231
1232 SYNOPSIS
1233 val_int()
1234
1235 RETURN
1236 In case of NULL value return 0 and set null_value flag to TRUE.
1237 If value is not null null_value flag will be reset to FALSE.
1238 */
1239 virtual longlong val_int()=0;
to_longlong_hybrid()1240 Longlong_hybrid to_longlong_hybrid()
1241 {
1242 return Longlong_hybrid(val_int(), unsigned_flag);
1243 }
to_longlong_null()1244 Longlong_null to_longlong_null()
1245 {
1246 longlong nr= val_int();
1247 /*
1248 C++ does not guarantee the order of parameter evaluation,
1249 so to make sure "null_value" is passed to the constructor
1250 after the val_int() call, val_int() is caled on a separate line.
1251 */
1252 return Longlong_null(nr, null_value);
1253 }
to_longlong_hybrid_null()1254 Longlong_hybrid_null to_longlong_hybrid_null()
1255 {
1256 return Longlong_hybrid_null(to_longlong_null(), unsigned_flag);
1257 }
1258 /**
1259 Get a value for CAST(x AS SIGNED).
1260 Too large positive unsigned integer values are converted
1261 to negative complements.
1262 Values of non-integer data types are adjusted to the SIGNED range.
1263 */
val_int_signed_typecast()1264 virtual longlong val_int_signed_typecast()
1265 {
1266 return cast_to_int_type_handler()->Item_val_int_signed_typecast(this);
1267 }
1268 longlong val_int_signed_typecast_from_str();
1269 /**
1270 Get a value for CAST(x AS UNSIGNED).
1271 Negative signed integer values are converted
1272 to positive complements.
1273 Values of non-integer data types are adjusted to the UNSIGNED range.
1274 */
val_int_unsigned_typecast()1275 virtual longlong val_int_unsigned_typecast()
1276 {
1277 return cast_to_int_type_handler()->Item_val_int_unsigned_typecast(this);
1278 }
1279 longlong val_int_unsigned_typecast_from_int();
1280 longlong val_int_unsigned_typecast_from_str();
1281 longlong val_int_unsigned_typecast_from_real();
1282
1283 /**
1284 Get a value for CAST(x AS UNSIGNED).
1285 Huge positive unsigned values are converted to negative complements.
1286 */
1287 longlong val_int_signed_typecast_from_int();
1288 longlong val_int_signed_typecast_from_real();
1289
1290 /*
1291 This is just a shortcut to avoid the cast. You should still use
1292 unsigned_flag to check the sign of the item.
1293 */
val_uint()1294 inline ulonglong val_uint() { return (ulonglong) val_int(); }
1295
1296 /*
1297 Return string representation of this item object.
1298
1299 SYNOPSIS
1300 val_str()
1301 str an allocated buffer this or any nested Item object can use to
1302 store return value of this method.
1303
1304 NOTE
1305 The caller can modify the returned String, if it's not marked
1306 "const" (with the String::mark_as_const() method). That means that
1307 if the item returns its own internal buffer (e.g. tmp_value), it
1308 *must* be marked "const" [1]. So normally it's preferable to
1309 return the result value in the String, that was passed as an
1310 argument. But, for example, SUBSTR() returns a String that simply
1311 points into the buffer of SUBSTR()'s args[0]->val_str(). Such a
1312 String is always "const", so it's ok to use tmp_value for that and
1313 avoid reallocating/copying of the argument String.
1314
1315 [1] consider SELECT CONCAT(f, ":", f) FROM (SELECT func() AS f);
1316 here the return value of f() is used twice in the top-level
1317 select, and if they share the same tmp_value buffer, modifying the
1318 first one will implicitly modify the second too.
1319
1320 RETURN
1321 In case of NULL value return 0 (NULL pointer) and set null_value flag
1322 to TRUE.
1323 If value is not null null_value flag will be reset to FALSE.
1324 */
1325 virtual String *val_str(String *str)=0;
1326
1327
val_native_with_conversion(THD * thd,Native * to,const Type_handler * th)1328 bool val_native_with_conversion(THD *thd, Native *to, const Type_handler *th)
1329 {
1330 return th->Item_val_native_with_conversion(thd, this, to);
1331 }
val_native_with_conversion_result(THD * thd,Native * to,const Type_handler * th)1332 bool val_native_with_conversion_result(THD *thd, Native *to,
1333 const Type_handler *th)
1334 {
1335 return th->Item_val_native_with_conversion_result(thd, this, to);
1336 }
1337
val_native(THD * thd,Native * to)1338 virtual bool val_native(THD *thd, Native *to)
1339 {
1340 /*
1341 The default implementation for the Items that do not need native format:
1342 - Item_basic_value (default implementation)
1343 - Item_copy
1344 - Item_exists_subselect
1345 - Item_sum_field
1346 - Item_sum_or_func (default implementation)
1347 - Item_proc
1348 - Item_type_holder (as val_xxx() are never called for it);
1349
1350 These hybrid Item types override val_native():
1351 - Item_field
1352 - Item_param
1353 - Item_sp_variable
1354 - Item_ref
1355 - Item_cache_wrapper
1356 - Item_direct_ref
1357 - Item_direct_view_ref
1358 - Item_ref_null_helper
1359 - Item_name_const
1360 - Item_time_literal
1361 - Item_sum_or_func
1362 Note, these hybrid type Item_sum_or_func descendants
1363 override the default implementation:
1364 * Item_sum_hybrid
1365 * Item_func_hybrid_field_type
1366 * Item_func_min_max
1367 * Item_func_sp
1368 * Item_func_last_value
1369 * Item_func_rollup_const
1370 */
1371 DBUG_ASSERT(0);
1372 return null_value= true;
1373 }
val_native_result(THD * thd,Native * to)1374 virtual bool val_native_result(THD *thd, Native *to)
1375 {
1376 return val_native(thd, to);
1377 }
1378
1379 /*
1380 Returns string representation of this item in ASCII format.
1381
1382 SYNOPSIS
1383 val_str_ascii()
1384 str - similar to val_str();
1385
1386 NOTE
1387 This method is introduced for performance optimization purposes.
1388
1389 1. val_str() result of some Items in string context
1390 depends on @@character_set_results.
1391 @@character_set_results can be set to a "real multibyte" character
1392 set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
1393 below for convenience.)
1394
1395 So the default string result of such functions
1396 in these circumstances is real multi-byte character set, like UTF32.
1397
1398 For example, all numbers in string context
1399 return result in @@character_set_results:
1400
1401 SELECT CONCAT(20010101); -> UTF32
1402
1403 We do sprintf() first (to get ASCII representation)
1404 and then convert to UTF32;
1405
1406 So these kind "data sources" can use ASCII representation
1407 internally, but return multi-byte data only because
1408 @@character_set_results wants so.
1409 Therefore, conversion from ASCII to UTF32 is applied internally.
1410
1411
1412 2. Some other functions need in fact ASCII input.
1413
1414 For example,
1415 inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
1416
1417 Similar, fields of certain type, like DATE, TIME,
1418 when you insert string data into them, expect in fact ASCII input.
1419 If they get non-ASCII input, for example UTF32, they
1420 convert input from UTF32 to ASCII, and then use ASCII
1421 representation to do further processing.
1422
1423
1424 3. Now imagine we pass result of a data source of the first type
1425 to a data destination of the second type.
1426
1427 What happens:
1428 a. data source converts data from ASCII to UTF32, because
1429 @@character_set_results wants so and passes the result to
1430 data destination.
1431 b. data destination gets UTF32 string.
1432 c. data destination converts UTF32 string to ASCII,
1433 because it needs ASCII representation to be able to handle data
1434 correctly.
1435
1436 As a result we get two steps of unnecessary conversion:
1437 From ASCII to UTF32, then from UTF32 to ASCII.
1438
1439 A better way to handle these situations is to pass ASCII
1440 representation directly from the source to the destination.
1441
1442 This is why val_str_ascii() introduced.
1443
1444 RETURN
1445 Similar to val_str()
1446 */
1447 virtual String *val_str_ascii(String *str);
1448
1449 /*
1450 Returns the result of val_str_ascii(), translating NULLs back
1451 to empty strings (if MODE_EMPTY_STRING_IS_NULL is set).
1452 */
1453 String *val_str_ascii_revert_empty_string_is_null(THD *thd, String *str);
1454
1455 /*
1456 Returns the val_str() value converted to the given character set.
1457 */
1458 String *val_str(String *str, String *converter, CHARSET_INFO *to);
1459
val_json(String * str)1460 virtual String *val_json(String *str) { return val_str(str); }
1461 /*
1462 Return decimal representation of item with fixed point.
1463
1464 SYNOPSIS
1465 val_decimal()
1466 decimal_buffer buffer which can be used by Item for returning value
1467 (but can be not)
1468
1469 NOTE
1470 Returned value should not be changed if it is not the same which was
1471 passed via argument.
1472
1473 RETURN
1474 Return pointer on my_decimal (it can be other then passed via argument)
1475 if value is not NULL (null_value flag will be reset to FALSE).
1476 In case of NULL value it return 0 pointer and set null_value flag
1477 to TRUE.
1478 */
1479 virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
1480 /*
1481 Return boolean value of item.
1482
1483 RETURN
1484 FALSE value is false or NULL
1485 TRUE value is true (not equal to 0)
1486 */
val_bool()1487 virtual bool val_bool()
1488 {
1489 return type_handler()->Item_val_bool(this);
1490 }
1491
eval_const_cond()1492 bool eval_const_cond()
1493 {
1494 DBUG_ASSERT(const_item());
1495 DBUG_ASSERT(!is_expensive());
1496 return val_bool();
1497 }
1498
1499 /*
1500 save_val() is method of val_* family which stores value in the given
1501 field.
1502 */
save_val(Field * to)1503 virtual void save_val(Field *to) { save_org_in_field(to, NULL); }
1504 /*
1505 save_result() is method of val*result() family which stores value in
1506 the given field.
1507 */
save_result(Field * to)1508 virtual void save_result(Field *to) { save_val(to); }
1509 /* Helper functions, see item_sum.cc */
1510 String *val_string_from_real(String *str);
1511 String *val_string_from_int(String *str);
1512 my_decimal *val_decimal_from_real(my_decimal *decimal_value);
1513 my_decimal *val_decimal_from_int(my_decimal *decimal_value);
1514 my_decimal *val_decimal_from_string(my_decimal *decimal_value);
val_int_from_real()1515 longlong val_int_from_real()
1516 {
1517 DBUG_ASSERT(is_fixed());
1518 return Converter_double_to_longlong_with_warn(val_real(), false).result();
1519 }
1520 longlong val_int_from_str(int *error);
1521
1522 /*
1523 Returns true if this item can be calculated during
1524 value_depends_on_sql_mode()
1525 */
value_depends_on_sql_mode_const_item()1526 bool value_depends_on_sql_mode_const_item()
1527 {
1528 /*
1529 Currently we use value_depends_on_sql_mode() only for virtual
1530 column expressions. They should not contain any expensive items.
1531 If we ever get a crash on the assert below, it means
1532 check_vcol_func_processor() is badly implemented for this item.
1533 */
1534 DBUG_ASSERT(!is_expensive());
1535 /*
1536 It should return const_item() actually.
1537 But for some reasons Item_field::const_item() returns true
1538 at value_depends_on_sql_mode() call time.
1539 This should be checked and fixed.
1540 */
1541 return basic_const_item();
1542 }
value_depends_on_sql_mode()1543 virtual Sql_mode_dependency value_depends_on_sql_mode() const
1544 {
1545 return Sql_mode_dependency();
1546 }
1547
1548 int save_time_in_field(Field *field, bool no_conversions);
1549 int save_date_in_field(Field *field, bool no_conversions);
1550 int save_str_in_field(Field *field, bool no_conversions);
1551 int save_real_in_field(Field *field, bool no_conversions);
1552 int save_int_in_field(Field *field, bool no_conversions);
1553 int save_decimal_in_field(Field *field, bool no_conversions);
1554
1555 int save_str_value_in_field(Field *field, String *result);
1556
get_tmp_table_field()1557 virtual Field *get_tmp_table_field() { return 0; }
1558 virtual Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table);
full_name()1559 virtual const char *full_name() const { return name.str ? name.str : "???"; }
field_name_or_null()1560 const char *field_name_or_null()
1561 { return real_item()->type() == Item::FIELD_ITEM ? name.str : NULL; }
1562 const TABLE_SHARE *field_table_or_null();
1563
1564 /*
1565 *result* family of methods is analog of *val* family (see above) but
1566 return value of result_field of item if it is present. If Item have not
1567 result field, it return val(). This methods set null_value flag in same
1568 way as *val* methods do it.
1569 */
val_result()1570 virtual double val_result() { return val_real(); }
val_int_result()1571 virtual longlong val_int_result() { return val_int(); }
str_result(String * tmp)1572 virtual String *str_result(String* tmp) { return val_str(tmp); }
val_decimal_result(my_decimal * val)1573 virtual my_decimal *val_decimal_result(my_decimal *val)
1574 { return val_decimal(val); }
val_bool_result()1575 virtual bool val_bool_result() { return val_bool(); }
is_null_result()1576 virtual bool is_null_result() { return is_null(); }
1577 /*
1578 Returns 1 if result type and collation for val_str() can change between
1579 calls
1580 */
dynamic_result()1581 virtual bool dynamic_result() { return 0; }
1582 /*
1583 Bitmap of tables used by item
1584 (note: if you need to check dependencies on individual columns, check out
1585 class Field_enumerator)
1586 */
used_tables()1587 virtual table_map used_tables() const { return (table_map) 0L; }
all_used_tables()1588 virtual table_map all_used_tables() const { return used_tables(); }
1589 /*
1590 Return table map of tables that can't be NULL tables (tables that are
1591 used in a context where if they would contain a NULL row generated
1592 by a LEFT or RIGHT join, the item would not be true).
1593 This expression is used on WHERE item to determinate if a LEFT JOIN can be
1594 converted to a normal join.
1595 Generally this function should return used_tables() if the function
1596 would return null if any of the arguments are null
1597 As this is only used in the beginning of optimization, the value don't
1598 have to be updated in update_used_tables()
1599 */
not_null_tables()1600 virtual table_map not_null_tables() const { return used_tables(); }
1601 /*
1602 Returns true if this is a simple constant item like an integer, not
1603 a constant expression. Used in the optimizer to propagate basic constants.
1604 */
basic_const_item()1605 virtual bool basic_const_item() const { return 0; }
1606 /**
1607 Determines if the expression is allowed as
1608 a virtual column assignment source:
1609 INSERT INTO t1 (vcol) VALUES (10) -> error
1610 INSERT INTO t1 (vcol) VALUES (NULL) -> ok
1611 */
vcol_assignment_allowed_value()1612 virtual bool vcol_assignment_allowed_value() const { return false; }
1613 /**
1614 Test if "this" is an ORDER position (rather than an expression).
1615 Notes:
1616 - can be called before fix_fields().
1617 - local SP variables (even of integer types) are always expressions, not
1618 positions. (And they can't be used before fix_fields is called for them).
1619 */
is_order_clause_position()1620 virtual bool is_order_clause_position() const { return false; }
1621 /*
1622 Determines if the Item is an evaluable expression, that is
1623 it can return a value, so we can call methods val_xxx(), get_date(), etc.
1624 Most items are evaluable expressions.
1625 Examples of non-evaluable expressions:
1626 - Item_contextually_typed_value_specification (handling DEFAULT and IGNORE)
1627 - Item_type_param bound to DEFAULT and IGNORE
1628 We cannot call the mentioned methods for these Items,
1629 their method implementations typically have DBUG_ASSERT(0).
1630 */
is_evaluable_expression()1631 virtual bool is_evaluable_expression() const { return true; }
check_is_evaluable_expression_or_error()1632 bool check_is_evaluable_expression_or_error()
1633 {
1634 if (is_evaluable_expression())
1635 return false; // Ok
1636 raise_error_not_evaluable();
1637 return true; // Error
1638 }
1639 /* cloning of constant items (0 if it is not const) */
clone_item(THD * thd)1640 virtual Item *clone_item(THD *thd) { return 0; }
1641 /* deep copy item */
build_clone(THD * thd)1642 virtual Item* build_clone(THD *thd) { return get_copy(thd); }
eq_cmp_result()1643 virtual cond_result eq_cmp_result() const { return COND_OK; }
float_length(uint decimals_par)1644 inline uint float_length(uint decimals_par) const
1645 { return decimals < FLOATING_POINT_DECIMALS ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
1646 /* Returns total number of decimal digits */
decimal_precision()1647 virtual uint decimal_precision() const
1648 {
1649 return type_handler()->Item_decimal_precision(this);
1650 }
1651 /* Returns the number of integer part digits only */
decimal_int_part()1652 inline int decimal_int_part() const
1653 { return my_decimal_int_part(decimal_precision(), decimals); }
1654 /*
1655 Returns the number of fractional digits only.
1656 NOT_FIXED_DEC is replaced to the maximum possible number
1657 of fractional digits, taking into account the data type.
1658 */
decimal_scale()1659 uint decimal_scale() const
1660 {
1661 return type_handler()->Item_decimal_scale(this);
1662 }
1663 /*
1664 Returns how many digits a divisor adds into a division result.
1665 This is important when the integer part of the divisor can be 0.
1666 In this example:
1667 SELECT 1 / 0.000001; -> 1000000.0000
1668 the divisor adds 5 digits into the result precision.
1669
1670 Currently this method only replaces NOT_FIXED_DEC to
1671 TIME_SECOND_PART_DIGITS for temporal data types.
1672 This method can be made virtual, to create more efficient (smaller)
1673 data types for division results.
1674 For example, in
1675 SELECT 1/1.000001;
1676 the divisor could provide no additional precision into the result,
1677 so could any other items that are know to return a result
1678 with non-zero integer part.
1679 */
divisor_precision_increment()1680 uint divisor_precision_increment() const
1681 {
1682 return type_handler()->Item_divisor_precision_increment(this);
1683 }
1684 /**
1685 TIME or DATETIME precision of the item: 0..6
1686 */
time_precision(THD * thd)1687 uint time_precision(THD *thd)
1688 {
1689 return const_item() ? type_handler()->Item_time_precision(thd, this) :
1690 MY_MIN(decimals, TIME_SECOND_PART_DIGITS);
1691 }
datetime_precision(THD * thd)1692 uint datetime_precision(THD *thd)
1693 {
1694 return const_item() ? type_handler()->Item_datetime_precision(thd, this) :
1695 MY_MIN(decimals, TIME_SECOND_PART_DIGITS);
1696 }
val_int_min()1697 virtual longlong val_int_min() const
1698 {
1699 return LONGLONG_MIN;
1700 }
1701 /*
1702 Returns true if this is constant (during query execution, i.e. its value
1703 will not change until next fix_fields) and its value is known.
1704 */
const_item()1705 virtual bool const_item() const { return used_tables() == 0; }
1706 /*
1707 Returns true if this is constant but its value may be not known yet.
1708 (Can be used for parameters of prep. stmts or of stored procedures.)
1709 */
const_during_execution()1710 virtual bool const_during_execution() const
1711 { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
1712
1713 /**
1714 This method is used for to:
1715 - to generate a view definition query (SELECT-statement);
1716 - to generate a SQL-query for EXPLAIN EXTENDED;
1717 - to generate a SQL-query to be shown in INFORMATION_SCHEMA;
1718 - debug.
1719
1720 For more information about view definition query, INFORMATION_SCHEMA
1721 query and why they should be generated from the Item-tree, @see
1722 mysql_register_view().
1723 */
precedence()1724 virtual enum precedence precedence() const { return DEFAULT_PRECEDENCE; }
higher_precedence()1725 enum precedence higher_precedence() const
1726 { return (enum precedence)(precedence() + 1); }
1727 void print_parenthesised(String *str, enum_query_type query_type,
1728 enum precedence parent_prec);
1729 /**
1730 This helper is used to print expressions as a part of a table definition,
1731 in particular for
1732 - generated columns
1733 - check constraints
1734 - default value expressions
1735 - partitioning expressions
1736 */
print_for_table_def(String * str)1737 void print_for_table_def(String *str)
1738 {
1739 print_parenthesised(str,
1740 (enum_query_type)(QT_ITEM_ORIGINAL_FUNC_NULLIF |
1741 QT_ITEM_IDENT_SKIP_DB_NAMES |
1742 QT_ITEM_IDENT_SKIP_TABLE_NAMES |
1743 QT_NO_DATA_EXPANSION |
1744 QT_TO_SYSTEM_CHARSET),
1745 LOWEST_PRECEDENCE);
1746 }
1747 virtual void print(String *str, enum_query_type query_type);
1748
1749 class Print: public String
1750 {
1751 public:
Print(Item * item,enum_query_type type)1752 Print(Item *item, enum_query_type type)
1753 {
1754 item->print(this, type);
1755 }
1756 };
1757
1758 void print_item_w_name(String *str, enum_query_type query_type);
1759 void print_value(String *str);
1760
update_used_tables()1761 virtual void update_used_tables() {}
build_equal_items(THD * thd,COND_EQUAL * inheited,bool link_item_fields,COND_EQUAL ** cond_equal_ref)1762 virtual COND *build_equal_items(THD *thd, COND_EQUAL *inheited,
1763 bool link_item_fields,
1764 COND_EQUAL **cond_equal_ref)
1765 {
1766 update_used_tables();
1767 DBUG_ASSERT(!cond_equal_ref || !cond_equal_ref[0]);
1768 return this;
1769 }
1770 virtual COND *remove_eq_conds(THD *thd, Item::cond_result *cond_value,
1771 bool top_level);
add_key_fields(JOIN * join,KEY_FIELD ** key_fields,uint * and_level,table_map usable_tables,SARGABLE_PARAM ** sargables)1772 virtual void add_key_fields(JOIN *join, KEY_FIELD **key_fields,
1773 uint *and_level,
1774 table_map usable_tables,
1775 SARGABLE_PARAM **sargables)
1776 {
1777 return;
1778 }
1779 /*
1780 Make a select tree for all keys in a condition or a condition part
1781 @param param Context
1782 @param cond_ptr[OUT] Store a replacement item here if the condition
1783 can be simplified, e.g.:
1784 WHERE part1 OR part2 OR part3
1785 with one of the partN evaluating to SEL_TREE::ALWAYS.
1786 */
1787 virtual SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr);
1788 /*
1789 Checks whether the item is:
1790 - a simple equality (field=field_item or field=constant_item), or
1791 - a row equality
1792 and form multiple equality predicates.
1793 */
check_equality(THD * thd,COND_EQUAL * cond,List<Item> * eq_list)1794 virtual bool check_equality(THD *thd, COND_EQUAL *cond, List<Item> *eq_list)
1795 {
1796 return false;
1797 }
split_sum_func(THD * thd,Ref_ptr_array ref_pointer_array,List<Item> & fields,uint flags)1798 virtual void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
1799 List<Item> &fields, uint flags) {}
1800 /* Called for items that really have to be split */
1801 void split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array,
1802 List<Item> &fields,
1803 Item **ref, uint flags);
1804 virtual bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)= 0;
1805 bool get_date_from_int(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate);
1806 bool get_date_from_real(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate);
1807 bool get_date_from_string(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate);
get_time(THD * thd,MYSQL_TIME * ltime)1808 bool get_time(THD *thd, MYSQL_TIME *ltime)
1809 { return get_date(thd, ltime, Time::Options(thd)); }
1810 // Get a DATE or DATETIME value in numeric packed format for comparison
val_datetime_packed(THD * thd)1811 virtual longlong val_datetime_packed(THD *thd)
1812 {
1813 return Datetime(thd, this, Datetime::Options_cmp(thd)).to_packed();
1814 }
1815 // Get a TIME value in numeric packed format for comparison
val_time_packed(THD * thd)1816 virtual longlong val_time_packed(THD *thd)
1817 {
1818 return Time(thd, this, Time::Options_cmp(thd)).to_packed();
1819 }
1820 longlong val_datetime_packed_result(THD *thd);
1821 longlong val_time_packed_result(THD *thd);
1822
get_date_result(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)1823 virtual bool get_date_result(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
1824 { return get_date(thd, ltime,fuzzydate); }
1825
1826 /*
1827 The method allows to determine nullness of a complex expression
1828 without fully evaluating it, instead of calling val/result*() then
1829 checking null_value. Used in Item_func_isnull/Item_func_isnotnull
1830 and Item_sum_count.
1831 Any new item which can be NULL must implement this method.
1832 */
is_null()1833 virtual bool is_null() { return 0; }
1834
1835 /*
1836 Make sure the null_value member has a correct value.
1837 */
update_null_value()1838 virtual void update_null_value ()
1839 {
1840 return type_handler()->Item_update_null_value(this);
1841 }
1842
1843 /*
1844 Inform the item that there will be no distinction between its result
1845 being FALSE or NULL.
1846
1847 NOTE
1848 This function will be called for eg. Items that are top-level AND-parts
1849 of the WHERE clause. Items implementing this function (currently
1850 Item_cond_and and subquery-related item) enable special optimizations
1851 when they are "top level".
1852 */
top_level_item()1853 virtual void top_level_item() {}
1854 /*
1855 Return TRUE if it is item of top WHERE level (AND/OR) and it is
1856 important, return FALSE if it not important (we can not use to simplify
1857 calculations) or not top level
1858 */
is_top_level_item()1859 virtual bool is_top_level_item() const
1860 { return FALSE; /* not important */}
1861 /*
1862 return IN/ALL/ANY subquery or NULL
1863 */
get_IN_subquery()1864 virtual Item_in_subselect* get_IN_subquery()
1865 { return NULL; /* in is not IN/ALL/ANY */ }
1866 /*
1867 set field of temporary table for Item which can be switched on temporary
1868 table during query processing (grouping and so on)
1869 */
is_result_field()1870 virtual bool is_result_field() { return 0; }
is_bool_literal()1871 virtual bool is_bool_literal() const { return false; }
1872 /* This is to handle printing of default values */
need_parentheses_in_default()1873 virtual bool need_parentheses_in_default() { return false; }
save_in_result_field(bool no_conversions)1874 virtual void save_in_result_field(bool no_conversions) {}
1875 /*
1876 Data type format implied by the CHECK CONSTRAINT,
1877 to be sent to the client in the result set metadata.
1878 */
set_format_by_check_constraint(Send_field_extended_metadata *)1879 virtual bool set_format_by_check_constraint(Send_field_extended_metadata *)
1880 const
1881 {
1882 return false;
1883 }
1884 /*
1885 set value of aggregate function in case of no rows for grouping were found
1886 */
no_rows_in_result()1887 virtual void no_rows_in_result() {}
restore_to_before_no_rows_in_result()1888 virtual void restore_to_before_no_rows_in_result() {}
copy_or_same(THD * thd)1889 virtual Item *copy_or_same(THD *thd) { return this; }
copy_andor_structure(THD * thd)1890 virtual Item *copy_andor_structure(THD *thd) { return this; }
real_item()1891 virtual Item *real_item() { return this; }
get_tmp_table_item(THD * thd)1892 virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
make_odbc_literal(THD * thd,const LEX_CSTRING * typestr)1893 virtual Item *make_odbc_literal(THD *thd, const LEX_CSTRING *typestr)
1894 {
1895 return this;
1896 }
1897
1898 static CHARSET_INFO *default_charset();
1899
charset_for_protocol(void)1900 CHARSET_INFO *charset_for_protocol(void) const
1901 {
1902 return type_handler()->charset_for_protocol(this);
1903 };
1904
walk(Item_processor processor,bool walk_subquery,void * arg)1905 virtual bool walk(Item_processor processor, bool walk_subquery, void *arg)
1906 {
1907 return (this->*processor)(arg);
1908 }
1909
1910 virtual Item* transform(THD *thd, Item_transformer transformer, uchar *arg);
1911
1912 /*
1913 This function performs a generic "compilation" of the Item tree.
1914 The process of compilation is assumed to go as follows:
1915
1916 compile()
1917 {
1918 if (this->*some_analyzer(...))
1919 {
1920 compile children if any;
1921 this->*some_transformer(...);
1922 }
1923 }
1924
1925 i.e. analysis is performed top-down while transformation is done
1926 bottom-up.
1927 */
compile(THD * thd,Item_analyzer analyzer,uchar ** arg_p,Item_transformer transformer,uchar * arg_t)1928 virtual Item* compile(THD *thd, Item_analyzer analyzer, uchar **arg_p,
1929 Item_transformer transformer, uchar *arg_t)
1930 {
1931 if ((this->*analyzer) (arg_p))
1932 return ((this->*transformer) (thd, arg_t));
1933 return 0;
1934 }
1935
traverse_cond(Cond_traverser traverser,void * arg,traverse_order order)1936 virtual void traverse_cond(Cond_traverser traverser,
1937 void *arg, traverse_order order)
1938 {
1939 (*traverser)(this, arg);
1940 }
1941
1942 /*========= Item processors, to be used with Item::walk() ========*/
remove_dependence_processor(void * arg)1943 virtual bool remove_dependence_processor(void *arg) { return 0; }
1944 virtual bool cleanup_processor(void *arg);
cleanup_excluding_fields_processor(void * arg)1945 virtual bool cleanup_excluding_fields_processor (void *arg)
1946 { return cleanup_processor(arg); }
1947 bool cleanup_excluding_immutables_processor (void *arg);
cleanup_excluding_const_fields_processor(void * arg)1948 virtual bool cleanup_excluding_const_fields_processor (void *arg)
1949 { return cleanup_processor(arg); }
collect_item_field_processor(void * arg)1950 virtual bool collect_item_field_processor(void *arg) { return 0; }
unknown_splocal_processor(void * arg)1951 virtual bool unknown_splocal_processor(void *arg) { return 0; }
collect_outer_ref_processor(void * arg)1952 virtual bool collect_outer_ref_processor(void *arg) {return 0; }
check_inner_refs_processor(void * arg)1953 virtual bool check_inner_refs_processor(void *arg) { return 0; }
find_item_in_field_list_processor(void * arg)1954 virtual bool find_item_in_field_list_processor(void *arg) { return 0; }
1955 virtual bool find_item_processor(void *arg);
change_context_processor(void * arg)1956 virtual bool change_context_processor(void *arg) { return 0; }
reset_query_id_processor(void * arg)1957 virtual bool reset_query_id_processor(void *arg) { return 0; }
is_expensive_processor(void * arg)1958 virtual bool is_expensive_processor(void *arg) { return 0; }
1959
1960 // FIXME reduce the number of "add field to bitmap" processors
add_field_to_set_processor(void * arg)1961 virtual bool add_field_to_set_processor(void *arg) { return 0; }
register_field_in_read_map(void * arg)1962 virtual bool register_field_in_read_map(void *arg) { return 0; }
register_field_in_write_map(void * arg)1963 virtual bool register_field_in_write_map(void *arg) { return 0; }
register_field_in_bitmap(void * arg)1964 virtual bool register_field_in_bitmap(void *arg) { return 0; }
update_table_bitmaps_processor(void * arg)1965 virtual bool update_table_bitmaps_processor(void *arg) { return 0; }
1966
enumerate_field_refs_processor(void * arg)1967 virtual bool enumerate_field_refs_processor(void *arg) { return 0; }
mark_as_eliminated_processor(void * arg)1968 virtual bool mark_as_eliminated_processor(void *arg) { return 0; }
eliminate_subselect_processor(void * arg)1969 virtual bool eliminate_subselect_processor(void *arg) { return 0; }
set_fake_select_as_master_processor(void * arg)1970 virtual bool set_fake_select_as_master_processor(void *arg) { return 0; }
view_used_tables_processor(void * arg)1971 virtual bool view_used_tables_processor(void *arg) { return 0; }
eval_not_null_tables(void * arg)1972 virtual bool eval_not_null_tables(void *arg) { return 0; }
is_subquery_processor(void * arg)1973 virtual bool is_subquery_processor(void *arg) { return 0; }
count_sargable_conds(void * arg)1974 virtual bool count_sargable_conds(void *arg) { return 0; }
limit_index_condition_pushdown_processor(void * arg)1975 virtual bool limit_index_condition_pushdown_processor(void *arg) { return 0; }
exists2in_processor(void * arg)1976 virtual bool exists2in_processor(void *arg) { return 0; }
find_selective_predicates_list_processor(void * arg)1977 virtual bool find_selective_predicates_list_processor(void *arg) { return 0; }
cleanup_is_expensive_cache_processor(void * arg)1978 bool cleanup_is_expensive_cache_processor(void *arg)
1979 {
1980 is_expensive_cache= (int8)(-1);
1981 return 0;
1982 }
1983
1984 /**
1985 Check db/table_name if they defined in item and match arg values
1986
1987 @param arg Pointer to Check_table_name_prm structure
1988
1989 @retval true Match failed
1990 @retval false Match succeeded
1991 */
check_table_name_processor(void * arg)1992 virtual bool check_table_name_processor(void *arg) { return false; }
1993 /*
1994 TRUE if the expression depends only on the table indicated by tab_map
1995 or can be converted to such an exression using equalities.
1996 Not to be used for AND/OR formulas.
1997 */
excl_dep_on_table(table_map tab_map)1998 virtual bool excl_dep_on_table(table_map tab_map) { return false; }
1999 /*
2000 TRUE if the expression depends only on grouping fields of sel
2001 or can be converted to such an expression using equalities.
2002 It also checks if the expression doesn't contain stored procedures,
2003 subqueries or randomly generated elements.
2004 Not to be used for AND/OR formulas.
2005 */
excl_dep_on_grouping_fields(st_select_lex * sel)2006 virtual bool excl_dep_on_grouping_fields(st_select_lex *sel)
2007 { return false; }
2008 /*
2009 TRUE if the expression depends only on fields from the left part of
2010 IN subquery or can be converted to such an expression using equalities.
2011 Not to be used for AND/OR formulas.
2012 */
excl_dep_on_in_subq_left_part(Item_in_subselect * subq_pred)2013 virtual bool excl_dep_on_in_subq_left_part(Item_in_subselect *subq_pred)
2014 { return false; }
2015
switch_to_nullable_fields_processor(void * arg)2016 virtual bool switch_to_nullable_fields_processor(void *arg) { return 0; }
find_function_processor(void * arg)2017 virtual bool find_function_processor (void *arg) { return 0; }
2018 /*
2019 Check if a partition function is allowed
2020 SYNOPSIS
2021 check_partition_func_processor()
2022 int_arg Ignored
2023 RETURN VALUE
2024 TRUE Partition function not accepted
2025 FALSE Partition function accepted
2026
2027 DESCRIPTION
2028 check_partition_func_processor is used to check if a partition function
2029 uses an allowed function. An allowed function will always ensure that
2030 X=Y guarantees that also part_function(X)=part_function(Y) where X is
2031 a set of partition fields and so is Y. The problems comes mainly from
2032 character sets where two equal strings can be quite unequal. E.g. the
2033 german character for double s is equal to 2 s.
2034
2035 The default is that an item is not allowed
2036 in a partition function. Allowed functions
2037 can never depend on server version, they cannot depend on anything
2038 related to the environment. They can also only depend on a set of
2039 fields in the table itself. They cannot depend on other tables and
2040 cannot contain any queries and cannot contain udf's or similar.
2041 If a new Item class is defined and it inherits from a class that is
2042 allowed in a partition function then it is very important to consider
2043 whether this should be inherited to the new class. If not the function
2044 below should be defined in the new Item class.
2045
2046 The general behaviour is that most integer functions are allowed.
2047 If the partition function contains any multi-byte collations then
2048 the function check_part_func_fields will report an error on the
2049 partition function independent of what functions are used. So the
2050 only character sets allowed are single character collation and
2051 even for those only a limited set of functions are allowed. The
2052 problem with multi-byte collations is that almost every string
2053 function has the ability to change things such that two strings
2054 that are equal will not be equal after manipulated by a string
2055 function. E.g. two strings one contains a double s, there is a
2056 special german character that is equal to two s. Now assume a
2057 string function removes one character at this place, then in
2058 one the double s will be removed and in the other there will
2059 still be one s remaining and the strings are no longer equal
2060 and thus the partition function will not sort equal strings into
2061 the same partitions.
2062
2063 So the check if a partition function is valid is two steps. First
2064 check that the field types are valid, next check that the partition
2065 function is valid. The current set of partition functions valid
2066 assumes that there are no multi-byte collations amongst the partition
2067 fields.
2068 */
check_partition_func_processor(void * arg)2069 virtual bool check_partition_func_processor(void *arg) { return 1;}
post_fix_fields_part_expr_processor(void * arg)2070 virtual bool post_fix_fields_part_expr_processor(void *arg) { return 0; }
rename_fields_processor(void * arg)2071 virtual bool rename_fields_processor(void *arg) { return 0; }
2072 /*
2073 TRUE if the function is knowingly TRUE or FALSE.
2074 Not to be used for AND/OR formulas.
2075 */
is_simplified_cond_processor(void * arg)2076 virtual bool is_simplified_cond_processor(void *arg) { return false; }
2077
2078 /** Processor used to check acceptability of an item in the defining
2079 expression for a virtual column
2080
2081 @param arg always ignored
2082
2083 @retval 0 the item is accepted in the definition of a virtual column
2084 @retval 1 otherwise
2085 */
2086 struct vcol_func_processor_result
2087 {
2088 uint errors; /* Bits of possible errors */
2089 const char *name; /* Not supported function */
2090 Alter_info *alter_info;
vcol_func_processor_resultvcol_func_processor_result2091 vcol_func_processor_result() :
2092 errors(0), name(NULL), alter_info(NULL) {}
2093 };
2094 struct func_processor_rename
2095 {
2096 LEX_CSTRING db_name;
2097 LEX_CSTRING table_name;
2098 List<Create_field> fields;
2099 };
check_vcol_func_processor(void * arg)2100 virtual bool check_vcol_func_processor(void *arg)
2101 {
2102 return mark_unsupported_function(full_name(), arg, VCOL_IMPOSSIBLE);
2103 }
check_field_expression_processor(void * arg)2104 virtual bool check_field_expression_processor(void *arg) { return 0; }
check_func_default_processor(void * arg)2105 virtual bool check_func_default_processor(void *arg) { return 0; }
2106 /*
2107 Check if an expression value has allowed arguments, like DATE/DATETIME
2108 for date functions. Also used by partitioning code to reject
2109 timezone-dependent expressions in a (sub)partitioning function.
2110 */
check_valid_arguments_processor(void * arg)2111 virtual bool check_valid_arguments_processor(void *arg) { return 0; }
update_vcol_processor(void * arg)2112 virtual bool update_vcol_processor(void *arg) { return 0; }
set_fields_as_dependent_processor(void * arg)2113 virtual bool set_fields_as_dependent_processor(void *arg) { return 0; }
2114 /*
2115 Find if some of the key parts of table keys (the reference on table is
2116 passed as an argument) participate in the expression.
2117 If there is some, sets a bit for this key in the proper key map.
2118 */
check_index_dependence(void * arg)2119 virtual bool check_index_dependence(void *arg) { return 0; }
2120 /*============== End of Item processor list ======================*/
2121
2122 /*
2123 Given a condition P from the WHERE clause or from an ON expression of
2124 the processed SELECT S and a set of join tables from S marked in the
2125 parameter 'allowed'={T} a call of P->find_not_null_fields({T}) has to
2126 find the set fields {F} of the tables from 'allowed' such that:
2127 - each field from {F} is declared as nullable
2128 - each record of table t from {T} that contains NULL as the value for at
2129 at least one field from {F} can be ignored when building the result set
2130 for S
2131 It is assumed here that the condition P is conjunctive and all its column
2132 references belong to T.
2133
2134 Examples:
2135 CREATE TABLE t1 (a int, b int);
2136 CREATE TABLE t2 (a int, b int);
2137
2138 SELECT * FROM t1,t2 WHERE t1.a=t2.a and t1.b > 5;
2139 A call of find_not_null_fields() for the whole WHERE condition and {t1,t2}
2140 should find {t1.a,t1.b,t2.a}
2141
2142 SELECT * FROM t1 LEFT JOIN ON (t1.a=t2.a and t2.a > t2.b);
2143 A call of find_not_null_fields() for the ON expression and {t2}
2144 should find {t2.a,t2.b}
2145
2146 The function returns TRUE if it succeeds to prove that all records of
2147 a table from {T} can be ignored. Otherwise it always returns FALSE.
2148
2149 Example:
2150 SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t2.a IS NULL;
2151 A call of find_not_null_fields() for the WHERE condition and {t1,t2}
2152 will return TRUE.
2153
2154 It is assumed that the implementation of this virtual function saves
2155 the info on the found set of fields in the structures associates with
2156 tables from {T}.
2157 */
find_not_null_fields(table_map allowed)2158 virtual bool find_not_null_fields(table_map allowed) { return false; }
2159
2160 /*
2161 Does not guarantee deep copy (depends on copy ctor).
2162 See build_clone() for deep copy.
2163 */
2164 virtual Item *get_copy(THD *thd)=0;
2165
2166 bool cache_const_expr_analyzer(uchar **arg);
2167 Item* cache_const_expr_transformer(THD *thd, uchar *arg);
2168
propagate_equal_fields(THD *,const Context &,COND_EQUAL *)2169 virtual Item* propagate_equal_fields(THD*, const Context &, COND_EQUAL *)
2170 {
2171 return this;
2172 };
2173
2174 Item* propagate_equal_fields_and_change_item_tree(THD *thd,
2175 const Context &ctx,
2176 COND_EQUAL *cond,
2177 Item **place);
2178
2179 /* arg points to REPLACE_EQUAL_FIELD_ARG object */
replace_equal_field(THD * thd,uchar * arg)2180 virtual Item *replace_equal_field(THD *thd, uchar *arg) { return this; }
2181
2182 struct Collect_deps_prm
2183 {
2184 List<Item> *parameters;
2185 /* unit from which we count nest_level */
2186 st_select_lex_unit *nest_level_base;
2187 uint count;
2188 int nest_level;
2189 bool collect;
2190 };
2191
2192 struct Check_table_name_prm
2193 {
2194 LEX_CSTRING db;
2195 LEX_CSTRING table_name;
2196 String field;
Check_table_name_prmCheck_table_name_prm2197 Check_table_name_prm(LEX_CSTRING _db, LEX_CSTRING _table_name) :
2198 db(_db), table_name(_table_name) {}
2199 };
2200
2201 /*
2202 For SP local variable returns pointer to Item representing its
2203 current value and pointer to current Item otherwise.
2204 */
this_item()2205 virtual Item *this_item() { return this; }
this_item()2206 virtual const Item *this_item() const { return this; }
2207
2208 /*
2209 For SP local variable returns address of pointer to Item representing its
2210 current value and pointer passed via parameter otherwise.
2211 */
this_item_addr(THD * thd,Item ** addr_arg)2212 virtual Item **this_item_addr(THD *thd, Item **addr_arg) { return addr_arg; }
2213
2214 // Row emulation
cols()2215 virtual uint cols() const { return 1; }
element_index(uint i)2216 virtual Item* element_index(uint i) { return this; }
addr(uint i)2217 virtual Item** addr(uint i) { return 0; }
2218 virtual bool check_cols(uint c);
2219 bool check_type_traditional_scalar(const char *opname) const;
2220 bool check_type_scalar(const char *opname) const;
2221 bool check_type_or_binary(const char *opname, const Type_handler *handler) const;
2222 bool check_type_general_purpose_string(const char *opname) const;
2223 bool check_type_can_return_int(const char *opname) const;
2224 bool check_type_can_return_decimal(const char *opname) const;
2225 bool check_type_can_return_real(const char *opname) const;
2226 bool check_type_can_return_str(const char *opname) const;
2227 bool check_type_can_return_text(const char *opname) const;
2228 bool check_type_can_return_date(const char *opname) const;
2229 bool check_type_can_return_time(const char *opname) const;
2230 // It is not row => null inside is impossible
null_inside()2231 virtual bool null_inside() { return 0; }
2232 // used in row subselects to get value of elements
bring_value()2233 virtual void bring_value() {}
2234
type_handler_long_or_longlong()2235 const Type_handler *type_handler_long_or_longlong() const
2236 {
2237 return Type_handler::type_handler_long_or_longlong(max_char_length(),
2238 unsigned_flag);
2239 }
2240
2241 /**
2242 Create field for temporary table.
2243 @param table Temporary table
2244 @param [OUT] src Who created the fields
2245 @param param Create parameters
2246 @retval NULL (on error)
2247 @retval a pointer to a newly create Field (on success)
2248 */
2249 virtual Field *create_tmp_field_ex(MEM_ROOT *root,
2250 TABLE *table,
2251 Tmp_field_src *src,
2252 const Tmp_field_param *param)= 0;
field_for_view_update()2253 virtual Item_field *field_for_view_update() { return 0; }
2254
neg_transformer(THD * thd)2255 virtual Item *neg_transformer(THD *thd) { return NULL; }
update_value_transformer(THD * thd,uchar * select_arg)2256 virtual Item *update_value_transformer(THD *thd, uchar *select_arg)
2257 { return this; }
expr_cache_insert_transformer(THD * thd,uchar * unused)2258 virtual Item *expr_cache_insert_transformer(THD *thd, uchar *unused)
2259 { return this; }
derived_field_transformer_for_having(THD * thd,uchar * arg)2260 virtual Item *derived_field_transformer_for_having(THD *thd, uchar *arg)
2261 { return this; }
derived_field_transformer_for_where(THD * thd,uchar * arg)2262 virtual Item *derived_field_transformer_for_where(THD *thd, uchar *arg)
2263 { return this; }
grouping_field_transformer_for_where(THD * thd,uchar * arg)2264 virtual Item *grouping_field_transformer_for_where(THD *thd, uchar *arg)
2265 { return this; }
2266 /* Now is not used. */
in_subq_field_transformer_for_where(THD * thd,uchar * arg)2267 virtual Item *in_subq_field_transformer_for_where(THD *thd, uchar *arg)
2268 { return this; }
in_subq_field_transformer_for_having(THD * thd,uchar * arg)2269 virtual Item *in_subq_field_transformer_for_having(THD *thd, uchar *arg)
2270 { return this; }
in_predicate_to_in_subs_transformer(THD * thd,uchar * arg)2271 virtual Item *in_predicate_to_in_subs_transformer(THD *thd, uchar *arg)
2272 { return this; }
field_transformer_for_having_pushdown(THD * thd,uchar * arg)2273 virtual Item *field_transformer_for_having_pushdown(THD *thd, uchar *arg)
2274 { return this; }
multiple_equality_transformer(THD * thd,uchar * arg)2275 virtual Item *multiple_equality_transformer(THD *thd, uchar *arg)
2276 { return this; }
expr_cache_is_needed(THD *)2277 virtual bool expr_cache_is_needed(THD *) { return FALSE; }
2278 virtual Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
needs_charset_converter(uint32 length,CHARSET_INFO * tocs)2279 bool needs_charset_converter(uint32 length, CHARSET_INFO *tocs) const
2280 {
2281 /*
2282 This will return "true" if conversion happens:
2283 - between two non-binary different character sets
2284 - from "binary" to "unsafe" character set
2285 (those that can have non-well-formed string)
2286 - from "binary" to UCS2-alike character set with mbminlen>1,
2287 when prefix left-padding is needed for an incomplete character:
2288 binary 0xFF -> ucs2 0x00FF)
2289 */
2290 if (!String::needs_conversion_on_storage(length,
2291 collation.collation, tocs))
2292 return false;
2293 /*
2294 No needs to add converter if an "arg" is NUMERIC or DATETIME
2295 value (which is pure ASCII) and at the same time target DTCollation
2296 is ASCII-compatible. For example, no needs to rewrite:
2297 SELECT * FROM t1 WHERE datetime_field = '2010-01-01';
2298 to
2299 SELECT * FROM t1 WHERE CONVERT(datetime_field USING cs) = '2010-01-01';
2300
2301 TODO: avoid conversion of any values with
2302 repertoire ASCII and 7bit-ASCII-compatible,
2303 not only numeric/datetime origin.
2304 */
2305 if (collation.derivation == DERIVATION_NUMERIC &&
2306 collation.repertoire == MY_REPERTOIRE_ASCII &&
2307 !(collation.collation->state & MY_CS_NONASCII) &&
2308 !(tocs->state & MY_CS_NONASCII))
2309 return false;
2310 return true;
2311 }
needs_charset_converter(CHARSET_INFO * tocs)2312 bool needs_charset_converter(CHARSET_INFO *tocs)
2313 {
2314 // Pass 1 as length to force conversion if tocs->mbminlen>1.
2315 return needs_charset_converter(1, tocs);
2316 }
2317 Item *const_charset_converter(THD *thd, CHARSET_INFO *tocs, bool lossless,
2318 const char *func_name);
const_charset_converter(THD * thd,CHARSET_INFO * tocs,bool lossless)2319 Item *const_charset_converter(THD *thd, CHARSET_INFO *tocs, bool lossless)
2320 { return const_charset_converter(thd, tocs, lossless, NULL); }
delete_self()2321 void delete_self()
2322 {
2323 cleanup();
2324 delete this;
2325 }
2326
get_item_const()2327 virtual const Item_const *get_item_const() const { return NULL; }
get_item_splocal()2328 virtual Item_splocal *get_item_splocal() { return 0; }
get_rewritable_query_parameter()2329 virtual Rewritable_query_parameter *get_rewritable_query_parameter()
2330 { return 0; }
2331
2332 /*
2333 Return Settable_routine_parameter interface of the Item. Return 0
2334 if this Item is not Settable_routine_parameter.
2335 */
get_settable_routine_parameter()2336 virtual Settable_routine_parameter *get_settable_routine_parameter()
2337 {
2338 return 0;
2339 }
2340
get_load_data_outvar()2341 virtual Load_data_outvar *get_load_data_outvar()
2342 {
2343 return 0;
2344 }
get_load_data_outvar_or_error()2345 Load_data_outvar *get_load_data_outvar_or_error()
2346 {
2347 Load_data_outvar *dst= get_load_data_outvar();
2348 if (dst)
2349 return dst;
2350 my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), name.str);
2351 return NULL;
2352 }
2353
2354 /**
2355 Test whether an expression is expensive to compute. Used during
2356 optimization to avoid computing expensive expressions during this
2357 phase. Also used to force temp tables when sorting on expensive
2358 functions.
2359 @todo
2360 Normally we should have a method:
2361 cost Item::execution_cost(),
2362 where 'cost' is either 'double' or some structure of various cost
2363 parameters.
2364
2365 @note
2366 This function is now used to prevent evaluation of expensive subquery
2367 predicates during the optimization phase. It also prevents evaluation
2368 of predicates that are not computable at this moment.
2369 */
is_expensive()2370 virtual bool is_expensive()
2371 {
2372 if (is_expensive_cache < 0)
2373 is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
2374 return MY_TEST(is_expensive_cache);
2375 }
2376 String *check_well_formed_result(String *str, bool send_error= 0);
2377 bool eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs);
too_big_for_varchar()2378 bool too_big_for_varchar() const
2379 { return max_char_length() > CONVERT_IF_BIGGER_TO_BLOB; }
fix_length_and_charset(uint32 max_char_length_arg,CHARSET_INFO * cs)2380 void fix_length_and_charset(uint32 max_char_length_arg, CHARSET_INFO *cs)
2381 {
2382 max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
2383 collation.collation= cs;
2384 }
fix_char_length(size_t max_char_length_arg)2385 void fix_char_length(size_t max_char_length_arg)
2386 {
2387 max_length= char_to_byte_length_safe(max_char_length_arg,
2388 collation.collation->mbmaxlen);
2389 }
2390 /*
2391 Return TRUE if the item points to a column of an outer-joined table.
2392 */
is_outer_field()2393 virtual bool is_outer_field() const { DBUG_ASSERT(is_fixed()); return FALSE; }
2394
2395 /**
2396 Checks if this item or any of its descendents contains a subquery.
2397 This is a replacement of the former Item::has_subquery() and
2398 Item::with_subselect.
2399 */
with_subquery()2400 virtual bool with_subquery() const { DBUG_ASSERT(is_fixed()); return false; }
2401
with_sum_func()2402 virtual bool with_sum_func() const { return false; }
get_with_sum_func_cache()2403 virtual With_sum_func_cache* get_with_sum_func_cache() { return NULL; }
2404
2405 Item* set_expr_cache(THD *thd);
2406
get_item_equal()2407 virtual Item_equal *get_item_equal() { return NULL; }
set_item_equal(Item_equal * item_eq)2408 virtual void set_item_equal(Item_equal *item_eq) {};
find_item_equal(COND_EQUAL * cond_equal)2409 virtual Item_equal *find_item_equal(COND_EQUAL *cond_equal) { return NULL; }
2410 /**
2411 Set the join tab index to the minimal (left-most) JOIN_TAB to which this
2412 Item is attached. The number is an index is depth_first_tab() traversal
2413 order.
2414 */
set_join_tab_idx(uint join_tab_idx_arg)2415 virtual void set_join_tab_idx(uint join_tab_idx_arg)
2416 {
2417 if (join_tab_idx_arg < join_tab_idx)
2418 join_tab_idx= join_tab_idx_arg;
2419 }
get_join_tab_idx()2420 uint get_join_tab_idx() const { return join_tab_idx; }
2421
view_used_tables(TABLE_LIST * view)2422 table_map view_used_tables(TABLE_LIST *view)
2423 {
2424 view->view_used_tables= 0;
2425 walk(&Item::view_used_tables_processor, 0, view);
2426 return view->view_used_tables;
2427 }
2428
2429 /**
2430 Collect and add to the list cache parameters for this Item.
2431
2432 @note Now implemented only for subqueries and in_optimizer,
2433 if we need it for general function then this method should
2434 be defined for Item_func.
2435 */
get_cache_parameters(List<Item> & parameters)2436 virtual void get_cache_parameters(List<Item> ¶meters) { };
2437
mark_as_condition_AND_part(TABLE_LIST * embedding)2438 virtual void mark_as_condition_AND_part(TABLE_LIST *embedding) {};
2439
2440 /* how much position should be reserved for Exists2In transformation */
exists2in_reserved_items()2441 virtual uint exists2in_reserved_items() { return 0; };
2442
2443 virtual Item *neg(THD *thd);
2444
2445 /**
2446 Inform the item that it is located under a NOT, which is a top-level item.
2447 */
under_not(Item_func_not * upper)2448 virtual void under_not(Item_func_not * upper
2449 __attribute__((unused))) {};
2450 /*
2451 If Item_field is wrapped in Item_direct_wrep remove this Item_direct_ref
2452 wrapper.
2453 */
remove_item_direct_ref()2454 virtual Item *remove_item_direct_ref() { return this; }
2455
2456
2457 void register_in(THD *thd);
2458
depends_only_on(table_map view_map)2459 bool depends_only_on(table_map view_map)
2460 { return marker & FULL_EXTRACTION_FL; }
get_extraction_flag()2461 int get_extraction_flag()
2462 { return marker & EXTRACTION_MASK; }
set_extraction_flag(int flags)2463 void set_extraction_flag(int flags)
2464 {
2465 marker &= ~EXTRACTION_MASK;
2466 marker|= flags;
2467 }
clear_extraction_flag()2468 void clear_extraction_flag()
2469 {
2470 marker &= ~EXTRACTION_MASK;
2471 }
2472 void check_pushable_cond(Pushdown_checker excl_dep_func, uchar *arg);
pushable_cond_checker_for_derived(uchar * arg)2473 bool pushable_cond_checker_for_derived(uchar *arg)
2474 {
2475 return excl_dep_on_table(*((table_map *)arg));
2476 }
pushable_cond_checker_for_subquery(uchar * arg)2477 bool pushable_cond_checker_for_subquery(uchar *arg)
2478 {
2479 DBUG_ASSERT(((Item*) arg)->get_IN_subquery());
2480 return excl_dep_on_in_subq_left_part(((Item*)arg)->get_IN_subquery());
2481 }
2482 Item *build_pushable_cond(THD *thd,
2483 Pushdown_checker checker,
2484 uchar *arg);
2485 /*
2486 Checks if this item depends only on the arg table
2487 */
pushable_equality_checker_for_derived(uchar * arg)2488 bool pushable_equality_checker_for_derived(uchar *arg)
2489 {
2490 return (used_tables() == *((table_map *)arg));
2491 }
2492 /*
2493 Checks if this item consists in the left part of arg IN subquery predicate
2494 */
2495 bool pushable_equality_checker_for_subquery(uchar *arg);
2496 };
2497
2498 MEM_ROOT *get_thd_memroot(THD *thd);
2499
2500 template <class T>
get_item_copy(THD * thd,T * item)2501 inline Item* get_item_copy (THD *thd, T* item)
2502 {
2503 Item *copy= new (get_thd_memroot(thd)) T(*item);
2504 if (likely(copy))
2505 copy->register_in(thd);
2506 return copy;
2507 }
2508
2509
2510 #ifndef DBUG_OFF
2511 /**
2512 A helper class to print the data type and the value for an Item
2513 in debug builds.
2514 */
2515 class DbugStringItemTypeValue: public StringBuffer<128>
2516 {
2517 public:
DbugStringItemTypeValue(THD * thd,const Item * item)2518 DbugStringItemTypeValue(THD *thd, const Item *item)
2519 {
2520 append('(');
2521 append(item->type_handler()->name().ptr());
2522 append(')');
2523 const_cast<Item*>(item)->print(this, QT_EXPLAIN);
2524 /* Append end \0 to allow usage of c_ptr() */
2525 append('\0');
2526 str_length--;
2527 }
2528 };
2529 #endif
2530
2531 class With_sum_func_cache
2532 {
2533 protected:
2534 bool m_with_sum_func; // True if the owner item contains a sum func
2535 public:
With_sum_func_cache()2536 With_sum_func_cache()
2537 :m_with_sum_func(false)
2538 { }
With_sum_func_cache(const Item * a)2539 With_sum_func_cache(const Item *a)
2540 :m_with_sum_func(a->with_sum_func())
2541 { }
With_sum_func_cache(const Item * a,const Item * b)2542 With_sum_func_cache(const Item *a, const Item *b)
2543 :m_with_sum_func(a->with_sum_func() || b->with_sum_func())
2544 { }
With_sum_func_cache(const Item * a,const Item * b,const Item * c)2545 With_sum_func_cache(const Item *a, const Item *b, const Item *c)
2546 :m_with_sum_func(a->with_sum_func() || b->with_sum_func() ||
2547 c->with_sum_func())
2548 { }
With_sum_func_cache(const Item * a,const Item * b,const Item * c,const Item * d)2549 With_sum_func_cache(const Item *a, const Item *b, const Item *c,
2550 const Item *d)
2551 :m_with_sum_func(a->with_sum_func() || b->with_sum_func() ||
2552 c->with_sum_func() || d->with_sum_func())
2553 { }
With_sum_func_cache(const Item * a,const Item * b,const Item * c,const Item * d,const Item * e)2554 With_sum_func_cache(const Item *a, const Item *b, const Item *c,
2555 const Item *d, const Item *e)
2556 :m_with_sum_func(a->with_sum_func() || b->with_sum_func() ||
2557 c->with_sum_func() || d->with_sum_func() ||
2558 e->with_sum_func())
2559 { }
set_with_sum_func()2560 void set_with_sum_func() { m_with_sum_func= true; }
reset_with_sum_func()2561 void reset_with_sum_func() { m_with_sum_func= false; }
copy_with_sum_func(const Item * item)2562 void copy_with_sum_func(const Item *item)
2563 {
2564 m_with_sum_func= item->with_sum_func();
2565 }
join_with_sum_func(const Item * item)2566 void join_with_sum_func(const Item *item)
2567 {
2568 m_with_sum_func|= item->with_sum_func();
2569 }
2570 };
2571
2572
2573 /*
2574 This class is a replacement for the former member Item::with_subselect.
2575 Determines if the descendant Item is a subselect or some of
2576 its arguments is or contains a subselect.
2577 */
2578 class With_subquery_cache
2579 {
2580 protected:
2581 bool m_with_subquery;
2582 public:
With_subquery_cache()2583 With_subquery_cache(): m_with_subquery(false) { }
join(const Item * item)2584 void join(const Item *item) { m_with_subquery|= item->with_subquery(); }
2585 };
2586
2587
2588 /**
2589 Compare two Items for List<Item>::add_unique()
2590 */
2591
2592 bool cmp_items(Item *a, Item *b);
2593
2594
2595 /**
2596 Array of items, e.g. function or aggerate function arguments.
2597 */
2598 class Item_args
2599 {
2600 protected:
2601 Item **args, *tmp_arg[2];
2602 uint arg_count;
2603 void set_arguments(THD *thd, List<Item> &list);
walk_args(Item_processor processor,bool walk_subquery,void * arg)2604 bool walk_args(Item_processor processor, bool walk_subquery, void *arg)
2605 {
2606 for (uint i= 0; i < arg_count; i++)
2607 {
2608 if (args[i]->walk(processor, walk_subquery, arg))
2609 return true;
2610 }
2611 return false;
2612 }
2613 bool transform_args(THD *thd, Item_transformer transformer, uchar *arg);
2614 void propagate_equal_fields(THD *, const Item::Context &, COND_EQUAL *);
excl_dep_on_table(table_map tab_map)2615 bool excl_dep_on_table(table_map tab_map)
2616 {
2617 for (uint i= 0; i < arg_count; i++)
2618 {
2619 if (args[i]->const_item())
2620 continue;
2621 if (!args[i]->excl_dep_on_table(tab_map))
2622 return false;
2623 }
2624 return true;
2625 }
2626 bool excl_dep_on_grouping_fields(st_select_lex *sel);
eq(const Item_args * other,bool binary_cmp)2627 bool eq(const Item_args *other, bool binary_cmp) const
2628 {
2629 for (uint i= 0; i < arg_count ; i++)
2630 {
2631 if (!args[i]->eq(other->args[i], binary_cmp))
2632 return false;
2633 }
2634 return true;
2635 }
excl_dep_on_in_subq_left_part(Item_in_subselect * subq_pred)2636 bool excl_dep_on_in_subq_left_part(Item_in_subselect *subq_pred)
2637 {
2638 for (uint i= 0; i < arg_count; i++)
2639 {
2640 if (args[i]->const_item())
2641 continue;
2642 if (!args[i]->excl_dep_on_in_subq_left_part(subq_pred))
2643 return false;
2644 }
2645 return true;
2646 }
2647 public:
Item_args(void)2648 Item_args(void)
2649 :args(NULL), arg_count(0)
2650 { }
Item_args(Item * a)2651 Item_args(Item *a)
2652 :args(tmp_arg), arg_count(1)
2653 {
2654 args[0]= a;
2655 }
Item_args(Item * a,Item * b)2656 Item_args(Item *a, Item *b)
2657 :args(tmp_arg), arg_count(2)
2658 {
2659 args[0]= a; args[1]= b;
2660 }
Item_args(THD * thd,Item * a,Item * b,Item * c)2661 Item_args(THD *thd, Item *a, Item *b, Item *c)
2662 {
2663 arg_count= 0;
2664 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 3))))
2665 {
2666 arg_count= 3;
2667 args[0]= a; args[1]= b; args[2]= c;
2668 }
2669 }
Item_args(THD * thd,Item * a,Item * b,Item * c,Item * d)2670 Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d)
2671 {
2672 arg_count= 0;
2673 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 4))))
2674 {
2675 arg_count= 4;
2676 args[0]= a; args[1]= b; args[2]= c; args[3]= d;
2677 }
2678 }
Item_args(THD * thd,Item * a,Item * b,Item * c,Item * d,Item * e)2679 Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e)
2680 {
2681 arg_count= 5;
2682 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 5))))
2683 {
2684 arg_count= 5;
2685 args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
2686 }
2687 }
Item_args(THD * thd,List<Item> & list)2688 Item_args(THD *thd, List<Item> &list)
2689 {
2690 set_arguments(thd, list);
2691 }
2692 Item_args(THD *thd, const Item_args *other);
2693 bool alloc_arguments(THD *thd, uint count);
add_argument(Item * item)2694 void add_argument(Item *item)
2695 {
2696 args[arg_count++]= item;
2697 }
2698 /**
2699 Extract row elements from the given position.
2700 For example, for this input: (1,2),(3,4),(5,6)
2701 pos=0 will extract (1,3,5)
2702 pos=1 will extract (2,4,6)
2703 @param thd - current thread, to allocate memory on its mem_root
2704 @param rows - an array of compatible ROW-type items
2705 @param pos - the element position to extract
2706 */
alloc_and_extract_row_elements(THD * thd,const Item_args * rows,uint pos)2707 bool alloc_and_extract_row_elements(THD *thd, const Item_args *rows, uint pos)
2708 {
2709 DBUG_ASSERT(rows->argument_count() > 0);
2710 DBUG_ASSERT(rows->arguments()[0]->cols() > pos);
2711 if (alloc_arguments(thd, rows->argument_count()))
2712 return true;
2713 for (uint i= 0; i < rows->argument_count(); i++)
2714 {
2715 DBUG_ASSERT(rows->arguments()[0]->cols() == rows->arguments()[i]->cols());
2716 Item *arg= rows->arguments()[i]->element_index(pos);
2717 add_argument(arg);
2718 }
2719 DBUG_ASSERT(argument_count() == rows->argument_count());
2720 return false;
2721 }
arguments()2722 inline Item **arguments() const { return args; }
argument_count()2723 inline uint argument_count() const { return arg_count; }
remove_arguments()2724 inline void remove_arguments() { arg_count=0; }
2725 Sql_mode_dependency value_depends_on_sql_mode_bit_or() const;
2726 };
2727
2728
2729 /*
2730 Class to be used to enumerate all field references in an item tree. This
2731 includes references to outside but not fields of the tables within a
2732 subquery.
2733 Suggested usage:
2734
2735 class My_enumerator : public Field_enumerator
2736 {
2737 virtual void visit_field() { ... your actions ...}
2738 }
2739
2740 My_enumerator enumerator;
2741 item->walk(Item::enumerate_field_refs_processor, ...,&enumerator);
2742
2743 This is similar to Visitor pattern.
2744 */
2745
2746 class Field_enumerator
2747 {
2748 public:
2749 virtual void visit_field(Item_field *field)= 0;
~Field_enumerator()2750 virtual ~Field_enumerator() {}; /* purecov: inspected */
Field_enumerator()2751 Field_enumerator() {} /* Remove gcc warning */
2752 };
2753
2754 class Item_string;
2755
2756
2757 class Item_fixed_hybrid: public Item
2758 {
2759 public:
2760 bool fixed; // If item was fixed with fix_fields
2761 public:
Item_fixed_hybrid(THD * thd)2762 Item_fixed_hybrid(THD *thd): Item(thd), fixed(false)
2763 { }
Item_fixed_hybrid(THD * thd,Item_fixed_hybrid * item)2764 Item_fixed_hybrid(THD *thd, Item_fixed_hybrid *item)
2765 :Item(thd, item), fixed(item->fixed)
2766 { }
fix_fields(THD * thd,Item ** ref)2767 bool fix_fields(THD *thd, Item **ref)
2768 {
2769 DBUG_ASSERT(!fixed);
2770 fixed= true;
2771 return false;
2772 }
cleanup()2773 void cleanup()
2774 {
2775 Item::cleanup();
2776 fixed= false;
2777 }
quick_fix_field()2778 void quick_fix_field() { fixed= true; }
unfix_fields()2779 void unfix_fields() { fixed= false; }
is_fixed()2780 bool is_fixed() const { return fixed; }
2781 };
2782
2783
2784 /**
2785 A common class for Item_basic_constant and Item_param
2786 */
2787 class Item_basic_value :public Item,
2788 public Item_const
2789 {
2790 protected:
2791 // Value metadata, e.g. to make string processing easier
2792 class Metadata: private MY_STRING_METADATA
2793 {
2794 public:
Metadata(const String * str)2795 Metadata(const String *str)
2796 {
2797 my_string_metadata_get(this, str->charset(), str->ptr(), str->length());
2798 }
Metadata(const String * str,my_repertoire_t repertoire_arg)2799 Metadata(const String *str, my_repertoire_t repertoire_arg)
2800 {
2801 MY_STRING_METADATA::repertoire= repertoire_arg;
2802 MY_STRING_METADATA::char_length= str->numchars();
2803 }
repertoire()2804 my_repertoire_t repertoire() const
2805 {
2806 return MY_STRING_METADATA::repertoire;
2807 }
char_length()2808 size_t char_length() const { return MY_STRING_METADATA::char_length; }
2809 };
fix_charset_and_length(CHARSET_INFO * cs,Derivation dv,Metadata metadata)2810 void fix_charset_and_length(CHARSET_INFO *cs,
2811 Derivation dv, Metadata metadata)
2812 {
2813 /*
2814 We have to have a different max_length than 'length' here to
2815 ensure that we get the right length if we do use the item
2816 to create a new table. In this case max_length must be the maximum
2817 number of chars for a string of this type because we in Create_field::
2818 divide the max_length with mbmaxlen).
2819 */
2820 collation.set(cs, dv, metadata.repertoire());
2821 fix_char_length(metadata.char_length());
2822 decimals= NOT_FIXED_DEC;
2823 }
fix_charset_and_length_from_str_value(const String & str,Derivation dv)2824 void fix_charset_and_length_from_str_value(const String &str, Derivation dv)
2825 {
2826 fix_charset_and_length(str.charset(), dv, Metadata(&str));
2827 }
Item_basic_value(THD * thd)2828 Item_basic_value(THD *thd): Item(thd) {}
2829 public:
create_tmp_field_ex(MEM_ROOT * root,TABLE * table,Tmp_field_src * src,const Tmp_field_param * param)2830 Field *create_tmp_field_ex(MEM_ROOT *root,
2831 TABLE *table, Tmp_field_src *src,
2832 const Tmp_field_param *param) override
2833 {
2834
2835 /*
2836 create_tmp_field_ex() for this type of Items is called for:
2837 - CREATE TABLE ... SELECT
2838 - In ORDER BY: SELECT max(a) FROM t1 GROUP BY a ORDER BY 'const';
2839 - In CURSORS:
2840 DECLARE c CURSOR FOR SELECT 'test';
2841 OPEN c;
2842 */
2843 return tmp_table_field_from_field_type_maybe_null(root,
2844 table, src, param,
2845 type() == Item::NULL_ITEM);
2846 }
2847 bool eq(const Item *item, bool binary_cmp) const override;
get_type_all_attributes_from_const()2848 const Type_all_attributes *get_type_all_attributes_from_const() const
2849 override
2850 { return this; }
2851 };
2852
2853
2854 class Item_basic_constant :public Item_basic_value
2855 {
2856 public:
Item_basic_constant(THD * thd)2857 Item_basic_constant(THD *thd): Item_basic_value(thd) {};
check_vcol_func_processor(void * arg)2858 bool check_vcol_func_processor(void *arg) { return false; }
get_item_const()2859 const Item_const *get_item_const() const { return this; }
make_string_literal_concat(THD * thd,const LEX_CSTRING *)2860 virtual Item_basic_constant *make_string_literal_concat(THD *thd,
2861 const LEX_CSTRING *)
2862 {
2863 DBUG_ASSERT(0);
2864 return this;
2865 }
2866 };
2867
2868
2869 /*****************************************************************************
2870 The class is a base class for representation of stored routine variables in
2871 the Item-hierarchy. There are the following kinds of SP-vars:
2872 - local variables (Item_splocal);
2873 - CASE expression (Item_case_expr);
2874 *****************************************************************************/
2875
2876 class Item_sp_variable :public Item_fixed_hybrid
2877 {
2878 protected:
2879 /*
2880 THD, which is stored in fix_fields() and is used in this_item() to avoid
2881 current_thd use.
2882 */
2883 THD *m_thd;
2884
2885 bool fix_fields_from_item(THD *thd, Item **, const Item *);
2886 public:
2887 LEX_CSTRING m_name;
2888
2889 public:
2890 #ifdef DBUG_ASSERT_EXISTS
2891 /*
2892 Routine to which this Item_splocal belongs. Used for checking if correct
2893 runtime context is used for variable handling.
2894 */
2895 const sp_head *m_sp;
2896 #endif
2897
2898 public:
2899 Item_sp_variable(THD *thd, const LEX_CSTRING *sp_var_name);
2900
2901 public:
2902 bool fix_fields(THD *thd, Item **) override= 0;
2903
2904 double val_real() override;
2905 longlong val_int() override;
2906 String *val_str(String *sp) override;
2907 my_decimal *val_decimal(my_decimal *decimal_value) override;
2908 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override;
2909 bool val_native(THD *thd, Native *to) override;
2910 bool is_null() override;
2911
2912 public:
2913 void make_send_field(THD *thd, Send_field *field) override;
2914
const_item()2915 bool const_item() const override { return true; }
2916
create_tmp_field_ex(MEM_ROOT * root,TABLE * table,Tmp_field_src * src,const Tmp_field_param * param)2917 Field *create_tmp_field_ex(MEM_ROOT *root,
2918 TABLE *table, Tmp_field_src *src,
2919 const Tmp_field_param *param) override
2920 {
2921 return create_tmp_field_ex_simple(root, table, src, param);
2922 }
2923 inline int save_in_field(Field *field, bool no_conversions) override;
2924 inline bool send(Protocol *protocol, st_value *buffer) override;
check_vcol_func_processor(void * arg)2925 bool check_vcol_func_processor(void *arg) override
2926 {
2927 return mark_unsupported_function(m_name.str, arg, VCOL_IMPOSSIBLE);
2928 }
2929 };
2930
2931 /*****************************************************************************
2932 Item_sp_variable inline implementation.
2933 *****************************************************************************/
2934
save_in_field(Field * field,bool no_conversions)2935 inline int Item_sp_variable::save_in_field(Field *field, bool no_conversions)
2936 {
2937 return this_item()->save_in_field(field, no_conversions);
2938 }
2939
send(Protocol * protocol,st_value * buffer)2940 inline bool Item_sp_variable::send(Protocol *protocol, st_value *buffer)
2941 {
2942 return this_item()->send(protocol, buffer);
2943 }
2944
2945
2946 /*****************************************************************************
2947 A reference to local SP variable (incl. reference to SP parameter), used in
2948 runtime.
2949 *****************************************************************************/
2950
2951 class Item_splocal :public Item_sp_variable,
2952 private Settable_routine_parameter,
2953 public Rewritable_query_parameter,
2954 public Type_handler_hybrid_field_type
2955 {
2956 protected:
2957 const Sp_rcontext_handler *m_rcontext_handler;
2958
2959 uint m_var_idx;
2960
2961 Type m_type;
2962
2963 bool append_value_for_log(THD *thd, String *str);
2964
2965 sp_rcontext *get_rcontext(sp_rcontext *local_ctx) const;
2966 Item_field *get_variable(sp_rcontext *ctx) const;
2967
2968 public:
2969 Item_splocal(THD *thd, const Sp_rcontext_handler *rh,
2970 const LEX_CSTRING *sp_var_name, uint sp_var_idx,
2971 const Type_handler *handler,
2972 uint pos_in_q= 0, uint len_in_q= 0);
2973
2974 bool fix_fields(THD *, Item **) override;
2975 Item *this_item() override;
2976 const Item *this_item() const override;
2977 Item **this_item_addr(THD *thd, Item **) override;
2978
2979 void print(String *str, enum_query_type query_type) override;
2980
2981 public:
2982 inline const LEX_CSTRING *my_name() const;
2983
2984 inline uint get_var_idx() const;
2985
type()2986 Type type() const override { return m_type; }
type_handler()2987 const Type_handler *type_handler() const override
2988 { return Type_handler_hybrid_field_type::type_handler(); }
cols()2989 uint cols() const override { return this_item()->cols(); }
element_index(uint i)2990 Item* element_index(uint i) override
2991 { return this_item()->element_index(i); }
addr(uint i)2992 Item** addr(uint i) override { return this_item()->addr(i); }
2993 bool check_cols(uint c) override;
2994
2995 private:
2996 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
2997
2998 public:
get_item_splocal()2999 Item_splocal *get_item_splocal() override { return this; }
3000
get_rewritable_query_parameter()3001 Rewritable_query_parameter *get_rewritable_query_parameter() override
3002 { return this; }
3003
get_settable_routine_parameter()3004 Settable_routine_parameter *get_settable_routine_parameter() override
3005 { return this; }
3006
3007 bool append_for_log(THD *thd, String *str) override;
3008
get_copy(THD *)3009 Item *get_copy(THD *) override { return nullptr; }
3010
3011 /*
3012 Override the inherited create_field_for_create_select(),
3013 because we want to preserve the exact data type for:
3014 DECLARE a1 INT;
3015 DECLARE a2 TYPE OF t1.a2;
3016 CREATE TABLE t1 AS SELECT a1, a2;
3017 The inherited implementation would create a column
3018 based on result_type(), which is less exact.
3019 */
create_field_for_create_select(MEM_ROOT * root,TABLE * table)3020 Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override
3021 { return create_table_field_from_handler(root, table); }
3022
is_valid_limit_clause_variable_with_error()3023 bool is_valid_limit_clause_variable_with_error() const
3024 {
3025 /*
3026 In case if the variable has an anchored data type, e.g.:
3027 DECLARE a TYPE OF t1.a;
3028 type_handler() is set to &type_handler_null and this
3029 function detects such variable as not valid in LIMIT.
3030 */
3031 if (type_handler()->is_limit_clause_valid_type())
3032 return true;
3033 my_error(ER_WRONG_SPVAR_TYPE_IN_LIMIT, MYF(0));
3034 return false;
3035 }
3036 };
3037
3038
3039 /**
3040 An Item_splocal variant whose data type becomes known only at
3041 sp_rcontext creation time, e.g. "DECLARE var1 t1.col1%TYPE".
3042 */
3043 class Item_splocal_with_delayed_data_type: public Item_splocal
3044 {
3045 public:
Item_splocal_with_delayed_data_type(THD * thd,const Sp_rcontext_handler * rh,const LEX_CSTRING * sp_var_name,uint sp_var_idx,uint pos_in_q,uint len_in_q)3046 Item_splocal_with_delayed_data_type(THD *thd,
3047 const Sp_rcontext_handler *rh,
3048 const LEX_CSTRING *sp_var_name,
3049 uint sp_var_idx,
3050 uint pos_in_q, uint len_in_q)
3051 :Item_splocal(thd, rh, sp_var_name, sp_var_idx, &type_handler_null,
3052 pos_in_q, len_in_q)
3053 { }
3054 };
3055
3056
3057 /**
3058 SP variables that are fields of a ROW.
3059 DELCARE r ROW(a INT,b INT);
3060 SELECT r.a; -- This is handled by Item_splocal_row_field
3061 */
3062 class Item_splocal_row_field :public Item_splocal
3063 {
3064 protected:
3065 LEX_CSTRING m_field_name;
3066 uint m_field_idx;
3067 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3068 public:
3069 Item_splocal_row_field(THD *thd,
3070 const Sp_rcontext_handler *rh,
3071 const LEX_CSTRING *sp_var_name,
3072 const LEX_CSTRING *sp_field_name,
3073 uint sp_var_idx, uint sp_field_idx,
3074 const Type_handler *handler,
3075 uint pos_in_q= 0, uint len_in_q= 0)
Item_splocal(thd,rh,sp_var_name,sp_var_idx,handler,pos_in_q,len_in_q)3076 :Item_splocal(thd, rh, sp_var_name, sp_var_idx, handler, pos_in_q, len_in_q),
3077 m_field_name(*sp_field_name),
3078 m_field_idx(sp_field_idx)
3079 { }
3080 bool fix_fields(THD *thd, Item **) override;
3081 Item *this_item() override;
3082 const Item *this_item() const override;
3083 Item **this_item_addr(THD *thd, Item **) override;
3084 bool append_for_log(THD *thd, String *str) override;
3085 void print(String *str, enum_query_type query_type) override;
3086 };
3087
3088
3089 class Item_splocal_row_field_by_name :public Item_splocal_row_field
3090 {
3091 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3092 public:
3093 Item_splocal_row_field_by_name(THD *thd,
3094 const Sp_rcontext_handler *rh,
3095 const LEX_CSTRING *sp_var_name,
3096 const LEX_CSTRING *sp_field_name,
3097 uint sp_var_idx,
3098 const Type_handler *handler,
3099 uint pos_in_q= 0, uint len_in_q= 0)
3100 :Item_splocal_row_field(thd, rh, sp_var_name, sp_field_name,
3101 sp_var_idx, 0 /* field index will be set later */,
3102 handler, pos_in_q, len_in_q)
3103 { }
3104 bool fix_fields(THD *thd, Item **it) override;
3105 void print(String *str, enum_query_type query_type) override;
3106 };
3107
3108
3109 /*****************************************************************************
3110 Item_splocal inline implementation.
3111 *****************************************************************************/
3112
my_name()3113 inline const LEX_CSTRING *Item_splocal::my_name() const
3114 {
3115 return &m_name;
3116 }
3117
get_var_idx()3118 inline uint Item_splocal::get_var_idx() const
3119 {
3120 return m_var_idx;
3121 }
3122
3123 /*****************************************************************************
3124 A reference to case expression in SP, used in runtime.
3125 *****************************************************************************/
3126
3127 class Item_case_expr :public Item_sp_variable
3128 {
3129 public:
3130 Item_case_expr(THD *thd, uint case_expr_id);
3131
3132 public:
3133 bool fix_fields(THD *thd, Item **) override;
3134 Item *this_item() override;
3135 const Item *this_item() const override;
3136 Item **this_item_addr(THD *thd, Item **) override;
3137
3138 Type type() const override;
type_handler()3139 const Type_handler *type_handler() const override
3140 { return this_item()->type_handler(); }
3141
3142 public:
3143 /*
3144 NOTE: print() is intended to be used from views and for debug.
3145 Item_case_expr can not occur in views, so here it is only for debug
3146 purposes.
3147 */
3148 void print(String *str, enum_query_type query_type) override;
get_copy(THD *)3149 Item *get_copy(THD *) override { return nullptr; }
3150
3151 private:
3152 uint m_case_expr_id;
3153 };
3154
3155 /*****************************************************************************
3156 Item_case_expr inline implementation.
3157 *****************************************************************************/
3158
type()3159 inline enum Item::Type Item_case_expr::type() const
3160 {
3161 return this_item()->type();
3162 }
3163
3164 /*
3165 NAME_CONST(given_name, const_value).
3166 This 'function' has all properties of the supplied const_value (which is
3167 assumed to be a literal constant), and the name given_name.
3168
3169 This is used to replace references to SP variables when we write PROCEDURE
3170 statements into the binary log.
3171
3172 TODO
3173 Together with Item_splocal and Item::this_item() we can actually extract
3174 common a base of this class and Item_splocal. Maybe it is possible to
3175 extract a common base with class Item_ref, too.
3176 */
3177
3178 class Item_name_const : public Item_fixed_hybrid
3179 {
3180 Item *value_item;
3181 Item *name_item;
3182 public:
3183 Item_name_const(THD *thd, Item *name_arg, Item *val);
3184
3185 bool fix_fields(THD *, Item **) override;
3186
3187 Type type() const override;
3188 double val_real() override;
3189 longlong val_int() override;
3190 String *val_str(String *sp) override;
3191 my_decimal *val_decimal(my_decimal *) override;
3192 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override;
3193 bool val_native(THD *thd, Native *to) override;
3194 bool is_null() override;
3195 void print(String *str, enum_query_type query_type) override;
3196
type_handler()3197 const Type_handler *type_handler() const override
3198 {
3199 return value_item->type_handler();
3200 }
3201
const_item()3202 bool const_item() const override { return true; }
3203
create_tmp_field_ex(MEM_ROOT * root,TABLE * table,Tmp_field_src * src,const Tmp_field_param * param)3204 Field *create_tmp_field_ex(MEM_ROOT *root,
3205 TABLE *table, Tmp_field_src *src,
3206 const Tmp_field_param *param) override
3207 {
3208 /*
3209 We can get to here when using a CURSOR for a query with NAME_CONST():
3210 DECLARE c CURSOR FOR SELECT NAME_CONST('x','y') FROM t1;
3211 OPEN c;
3212 */
3213 return tmp_table_field_from_field_type_maybe_null(root, table, src, param,
3214 type() == Item::NULL_ITEM);
3215 }
save_in_field(Field * field,bool no_conversions)3216 int save_in_field(Field *field, bool no_conversions) override
3217 {
3218 return value_item->save_in_field(field, no_conversions);
3219 }
3220
send(Protocol * protocol,st_value * buffer)3221 bool send(Protocol *protocol, st_value *buffer) override
3222 {
3223 return value_item->send(protocol, buffer);
3224 }
check_vcol_func_processor(void * arg)3225 bool check_vcol_func_processor(void *arg) override
3226 {
3227 return mark_unsupported_function("name_const()", arg, VCOL_IMPOSSIBLE);
3228 }
get_copy(THD * thd)3229 Item *get_copy(THD *thd) override
3230 { return get_item_copy<Item_name_const>(thd, this); }
3231 };
3232
3233
3234 class Item_literal: public Item_basic_constant
3235 {
3236 public:
Item_literal(THD * thd)3237 Item_literal(THD *thd): Item_basic_constant(thd)
3238 { }
type()3239 Type type() const override { return CONST_ITEM; }
check_partition_func_processor(void *)3240 bool check_partition_func_processor(void *) override { return false;}
const_item()3241 bool const_item() const override { return true; }
basic_const_item()3242 bool basic_const_item() const override { return true; }
3243 };
3244
3245
3246 class Item_num: public Item_literal
3247 {
3248 public:
Item_num(THD * thd)3249 Item_num(THD *thd): Item_literal(thd) { collation= DTCollation_numeric(); }
3250 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs) override;
get_date(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)3251 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
3252 {
3253 return type_handler()->Item_get_date_with_warn(thd, this, ltime, fuzzydate);
3254 }
3255 };
3256
3257 #define NO_CACHED_FIELD_INDEX ((uint)(-1))
3258
3259 class st_select_lex;
3260
3261
3262 class Item_result_field :public Item_fixed_hybrid /* Item with result field */
3263 {
3264 protected:
3265 Field *create_tmp_field_ex_from_handler(MEM_ROOT *root, TABLE *table,
3266 Tmp_field_src *src,
3267 const Tmp_field_param *param,
3268 const Type_handler *h);
3269 public:
3270 Field *result_field; /* Save result here */
Item_result_field(THD * thd)3271 Item_result_field(THD *thd): Item_fixed_hybrid(thd), result_field(0) {}
3272 // Constructor used for Item_sum/Item_cond_and/or (see Item comment)
Item_result_field(THD * thd,Item_result_field * item)3273 Item_result_field(THD *thd, Item_result_field *item):
3274 Item_fixed_hybrid(thd, item), result_field(item->result_field)
3275 {}
~Item_result_field()3276 ~Item_result_field() {} /* Required with gcc 2.95 */
get_tmp_table_field()3277 Field *get_tmp_table_field() override { return result_field; }
create_tmp_field_ex(MEM_ROOT * root,TABLE * table,Tmp_field_src * src,const Tmp_field_param * param)3278 Field *create_tmp_field_ex(MEM_ROOT *root, TABLE *table, Tmp_field_src *src,
3279 const Tmp_field_param *param) override
3280 {
3281 DBUG_ASSERT(fixed);
3282 const Type_handler *h= type_handler()->type_handler_for_tmp_table(this);
3283 return create_tmp_field_ex_from_handler(root, table, src, param, h);
3284 }
3285 void get_tmp_field_src(Tmp_field_src *src, const Tmp_field_param *param);
3286 /*
3287 This implementation of used_tables() used by Item_avg_field and
3288 Item_variance_field which work when only temporary table left, so theu
3289 return table map of the temporary table.
3290 */
used_tables()3291 table_map used_tables() const override { return 1; }
is_result_field()3292 bool is_result_field() override { return true; }
save_in_result_field(bool no_conversions)3293 void save_in_result_field(bool no_conversions) override
3294 {
3295 save_in_field(result_field, no_conversions);
3296 }
3297 void cleanup() override;
check_vcol_func_processor(void *)3298 bool check_vcol_func_processor(void *) override { return false; }
3299 };
3300
3301
3302 class Item_ident :public Item_result_field
3303 {
3304 protected:
3305 /*
3306 We have to store initial values of db_name, table_name and field_name
3307 to be able to restore them during cleanup() because they can be
3308 updated during fix_fields() to values from Field object and life-time
3309 of those is shorter than life-time of Item_field.
3310 */
3311 LEX_CSTRING orig_db_name;
3312 LEX_CSTRING orig_table_name;
3313 LEX_CSTRING orig_field_name;
3314
3315 void undeclared_spvar_error() const;
3316
3317 public:
3318 Name_resolution_context *context;
3319 LEX_CSTRING db_name;
3320 LEX_CSTRING table_name;
3321 LEX_CSTRING field_name;
3322 bool alias_name_used; /* true if item was resolved against alias */
3323 /*
3324 Cached value of index for this field in table->field array, used by prep.
3325 stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX
3326 if index value is not known.
3327 */
3328 uint cached_field_index;
3329 /*
3330 Cached pointer to table which contains this field, used for the same reason
3331 by prep. stmt. too in case then we have not-fully qualified field.
3332 0 - means no cached value.
3333 */
3334 TABLE_LIST *cached_table;
3335 st_select_lex *depended_from;
3336 /*
3337 Some Items resolved in another select should not be marked as dependency
3338 of the subquery where they are. During normal name resolution, we check
3339 this. Stored procedures and prepared statements first try to resolve an
3340 ident item using a cached table reference and field position from the
3341 previous query execution (cached_table/cached_field_index). If the
3342 tables were not changed, the ident matches the table/field, and we have
3343 faster resolution of the ident without looking through all tables and
3344 fields in the query. But in this case, we can not check all conditions
3345 about this ident item dependency, so we should cache the condition in
3346 this variable.
3347 */
3348 bool can_be_depended;
3349 Item_ident(THD *thd, Name_resolution_context *context_arg,
3350 const LEX_CSTRING &db_name_arg, const LEX_CSTRING &table_name_arg,
3351 const LEX_CSTRING &field_name_arg);
3352 Item_ident(THD *thd, Item_ident *item);
3353 Item_ident(THD *thd, TABLE_LIST *view_arg, const LEX_CSTRING &field_name_arg);
3354 const char *full_name() const override;
3355 void cleanup() override;
3356 st_select_lex *get_depended_from() const;
3357 bool remove_dependence_processor(void * arg) override;
3358 void print(String *str, enum_query_type query_type) override;
change_context_processor(void * cntx)3359 bool change_context_processor(void *cntx) override
3360 { context= (Name_resolution_context *)cntx; return FALSE; }
3361 /**
3362 Collect outer references
3363 */
3364 bool collect_outer_ref_processor(void *arg) override;
3365 friend bool insert_fields(THD *thd, Name_resolution_context *context,
3366 const char *db_name,
3367 const char *table_name, List_iterator<Item> *it,
3368 bool any_privileges, bool returning_field);
3369 };
3370
3371
3372 class Item_field :public Item_ident,
3373 public Load_data_outvar
3374 {
3375 protected:
3376 void set_field(Field *field);
3377 public:
3378 Field *field;
3379 Item_equal *item_equal;
3380 /*
3381 if any_privileges set to TRUE then here real effective privileges will
3382 be stored
3383 */
3384 privilege_t have_privileges;
3385 /* field need any privileges (for VIEW creation) */
3386 bool any_privileges;
3387 Item_field(THD *thd, Name_resolution_context *context_arg,
3388 const LEX_CSTRING &db_arg, const LEX_CSTRING &table_name_arg,
3389 const LEX_CSTRING &field_name_arg);
Item_field(THD * thd,Name_resolution_context * context_arg,const LEX_CSTRING & field_name_arg)3390 Item_field(THD *thd, Name_resolution_context *context_arg,
3391 const LEX_CSTRING &field_name_arg)
3392 :Item_field(thd, context_arg, null_clex_str, null_clex_str, field_name_arg)
3393 { }
Item_field(THD * thd,Name_resolution_context * context_arg)3394 Item_field(THD *thd, Name_resolution_context *context_arg)
3395 :Item_field(thd, context_arg, null_clex_str, null_clex_str, null_clex_str)
3396 { }
3397 /*
3398 Constructor needed to process subselect with temporary tables (see Item)
3399 */
3400 Item_field(THD *thd, Item_field *item);
3401 /*
3402 Constructor used inside setup_wild(), ensures that field, table,
3403 and database names will live as long as Item_field (this is important
3404 in prepared statements).
3405 */
3406 Item_field(THD *thd, Name_resolution_context *context_arg, Field *field);
3407 /*
3408 If this constructor is used, fix_fields() won't work, because
3409 db_name, table_name and column_name are unknown. It's necessary to call
3410 reset_field() before fix_fields() for all fields created this way.
3411 */
3412 Item_field(THD *thd, Field *field);
type()3413 Type type() const override { return FIELD_ITEM; }
3414 bool eq(const Item *item, bool binary_cmp) const override;
3415 double val_real() override;
3416 longlong val_int() override;
3417 my_decimal *val_decimal(my_decimal *) override;
3418 String *val_str(String*) override;
3419 void save_result(Field *to) override;
3420 double val_result() override;
3421 longlong val_int_result() override;
3422 bool val_native(THD *thd, Native *to) override;
3423 bool val_native_result(THD *thd, Native *to) override;
3424 String *str_result(String* tmp) override;
3425 my_decimal *val_decimal_result(my_decimal *) override;
3426 bool val_bool_result() override;
3427 bool is_null_result() override;
3428 bool send(Protocol *protocol, st_value *buffer) override;
get_load_data_outvar()3429 Load_data_outvar *get_load_data_outvar() override { return this; }
load_data_set_null(THD * thd,const Load_data_param * param)3430 bool load_data_set_null(THD *thd, const Load_data_param *param) override
3431 {
3432 return field->load_data_set_null(thd);
3433 }
load_data_set_value(THD * thd,const char * pos,uint length,const Load_data_param * param)3434 bool load_data_set_value(THD *thd, const char *pos, uint length,
3435 const Load_data_param *param) override
3436 {
3437 field->load_data_set_value(pos, length, param->charset());
3438 return false;
3439 }
3440 bool load_data_set_no_data(THD *thd, const Load_data_param *param) override;
3441 void load_data_print_for_log_event(THD *thd, String *to) const override;
load_data_add_outvar(THD * thd,Load_data_param * param)3442 bool load_data_add_outvar(THD *thd, Load_data_param *param) const override
3443 {
3444 return param->add_outvar_field(thd, field);
3445 }
load_data_fixed_length()3446 uint load_data_fixed_length() const override
3447 {
3448 return field->field_length;
3449 }
3450 void reset_field(Field *f);
3451 bool fix_fields(THD *, Item **) override;
3452 void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge)
3453 override;
3454 void make_send_field(THD *thd, Send_field *tmp_field) override;
3455 int save_in_field(Field *field,bool no_conversions) override;
3456 void save_org_in_field(Field *field, fast_field_copier optimizer_data)
3457 override;
3458 fast_field_copier setup_fast_field_copier(Field *field) override;
3459 table_map used_tables() const override;
3460 table_map all_used_tables() const override;
type_handler()3461 const Type_handler *type_handler() const override
3462 {
3463 const Type_handler *handler= field->type_handler();
3464 return handler->type_handler_for_item_field();
3465 }
real_type_handler()3466 const Type_handler *real_type_handler() const override
3467 {
3468 if (field->is_created_from_null_item)
3469 return &type_handler_null;
3470 return field->type_handler();
3471 }
3472 Field *create_tmp_field_from_item_field(MEM_ROOT *root, TABLE *new_table,
3473 Item_ref *orig_item,
3474 const Tmp_field_param *param);
3475 Field *create_tmp_field_ex(MEM_ROOT *root,
3476 TABLE *table, Tmp_field_src *src,
3477 const Tmp_field_param *param) override;
get_typelib()3478 const TYPELIB *get_typelib() const override { return field->get_typelib(); }
get_monotonicity_info()3479 enum_monotonicity_info get_monotonicity_info() const override
3480 {
3481 return MONOTONIC_STRICT_INCREASING;
3482 }
value_depends_on_sql_mode()3483 Sql_mode_dependency value_depends_on_sql_mode() const override
3484 {
3485 return Sql_mode_dependency(0, field->value_depends_on_sql_mode());
3486 }
3487 longlong val_int_endpoint(bool left_endp, bool *incl_endp) override;
3488 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override;
3489 bool get_date_result(THD *thd, MYSQL_TIME *ltime,date_mode_t fuzzydate)
3490 override;
3491 longlong val_datetime_packed(THD *thd) override;
3492 longlong val_time_packed(THD *thd) override;
is_null()3493 bool is_null() override { return field->is_null(); }
3494 void update_null_value() override;
update_table_bitmaps()3495 void update_table_bitmaps()
3496 {
3497 if (field && field->table)
3498 {
3499 TABLE *tab= field->table;
3500 tab->covering_keys.intersect(field->part_of_key);
3501 if (tab->read_set)
3502 tab->mark_column_with_deps(field);
3503 }
3504 }
update_used_tables()3505 void update_used_tables() override
3506 {
3507 update_table_bitmaps();
3508 }
build_equal_items(THD * thd,COND_EQUAL * inherited,bool link_item_fields,COND_EQUAL ** cond_equal_ref)3509 COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
3510 bool link_item_fields,
3511 COND_EQUAL **cond_equal_ref) override
3512 {
3513 /*
3514 normilize_cond() replaced all conditions of type
3515 WHERE/HAVING field
3516 to:
3517 WHERE/HAVING field<>0
3518 By the time of a build_equal_items() call, all such conditions should
3519 already be replaced. No Item_field are possible.
3520 Note, some Item_field derivants are still possible.
3521 Item_insert_value:
3522 SELECT * FROM t1 WHERE VALUES(a);
3523 Item_default_value:
3524 SELECT * FROM t1 WHERE DEFAULT(a);
3525 */
3526 DBUG_ASSERT(type() != FIELD_ITEM);
3527 return Item_ident::build_equal_items(thd, inherited, link_item_fields,
3528 cond_equal_ref);
3529 }
is_result_field()3530 bool is_result_field() override { return false; }
3531 void save_in_result_field(bool no_conversions) override;
3532 Item *get_tmp_table_item(THD *thd) override;
3533 bool find_not_null_fields(table_map allowed) override;
3534 bool collect_item_field_processor(void * arg) override;
3535 bool unknown_splocal_processor(void *arg) override;
3536 bool add_field_to_set_processor(void * arg) override;
3537 bool find_item_in_field_list_processor(void *arg) override;
3538 bool register_field_in_read_map(void *arg) override;
3539 bool register_field_in_write_map(void *arg) override;
3540 bool register_field_in_bitmap(void *arg) override;
check_partition_func_processor(void *)3541 bool check_partition_func_processor(void *) override {return false;}
3542 bool post_fix_fields_part_expr_processor(void *bool_arg) override;
3543 bool check_valid_arguments_processor(void *bool_arg) override;
3544 bool check_field_expression_processor(void *arg) override;
3545 bool enumerate_field_refs_processor(void *arg) override;
3546 bool update_table_bitmaps_processor(void *arg) override;
3547 bool switch_to_nullable_fields_processor(void *arg) override;
3548 bool update_vcol_processor(void *arg) override;
3549 bool rename_fields_processor(void *arg) override;
3550 bool check_vcol_func_processor(void *arg) override;
set_fields_as_dependent_processor(void * arg)3551 bool set_fields_as_dependent_processor(void *arg) override
3552 {
3553 if (!(used_tables() & OUTER_REF_TABLE_BIT))
3554 {
3555 depended_from= (st_select_lex *) arg;
3556 item_equal= NULL;
3557 }
3558 return 0;
3559 }
check_table_name_processor(void * arg)3560 bool check_table_name_processor(void *arg) override
3561 {
3562 Check_table_name_prm &p= *static_cast<Check_table_name_prm*>(arg);
3563 if (!field && p.table_name.length && table_name.length)
3564 {
3565 DBUG_ASSERT(p.db.length);
3566 if ((db_name.length &&
3567 my_strcasecmp(table_alias_charset, p.db.str, db_name.str)) ||
3568 my_strcasecmp(table_alias_charset, p.table_name.str, table_name.str))
3569 {
3570 print(&p.field, (enum_query_type) (QT_ITEM_ORIGINAL_FUNC_NULLIF |
3571 QT_NO_DATA_EXPANSION |
3572 QT_TO_SYSTEM_CHARSET));
3573 return true;
3574 }
3575 }
3576 return false;
3577 }
3578 void cleanup() override;
get_item_equal()3579 Item_equal *get_item_equal() override { return item_equal; }
set_item_equal(Item_equal * item_eq)3580 void set_item_equal(Item_equal *item_eq) override { item_equal= item_eq; }
3581 Item_equal *find_item_equal(COND_EQUAL *cond_equal) override;
3582 Item* propagate_equal_fields(THD *, const Context &, COND_EQUAL *) override;
3583 Item *replace_equal_field(THD *thd, uchar *arg) override;
max_display_length()3584 uint32 max_display_length() const override
3585 { return field->max_display_length(); }
field_for_view_update()3586 Item_field *field_for_view_update() override { return this; }
3587 int fix_outer_field(THD *thd, Field **field, Item **reference);
3588 Item *update_value_transformer(THD *thd, uchar *select_arg) override;
3589 Item *derived_field_transformer_for_having(THD *thd, uchar *arg) override;
3590 Item *derived_field_transformer_for_where(THD *thd, uchar *arg) override;
3591 Item *grouping_field_transformer_for_where(THD *thd, uchar *arg) override;
3592 Item *in_subq_field_transformer_for_where(THD *thd, uchar *arg) override;
3593 Item *in_subq_field_transformer_for_having(THD *thd, uchar *arg) override;
3594 void print(String *str, enum_query_type query_type) override;
3595 bool excl_dep_on_table(table_map tab_map) override;
3596 bool excl_dep_on_grouping_fields(st_select_lex *sel) override;
3597 bool excl_dep_on_in_subq_left_part(Item_in_subselect *subq_pred) override;
cleanup_excluding_fields_processor(void * arg)3598 bool cleanup_excluding_fields_processor(void *arg) override
3599 { return field ? 0 : cleanup_processor(arg); }
cleanup_excluding_const_fields_processor(void * arg)3600 bool cleanup_excluding_const_fields_processor(void *arg) override
3601 { return field && const_item() ? 0 : cleanup_processor(arg); }
3602
get_copy(THD * thd)3603 Item *get_copy(THD *thd) override
3604 { return get_item_copy<Item_field>(thd, this); }
is_outer_field()3605 bool is_outer_field() const override
3606 {
3607 DBUG_ASSERT(fixed);
3608 return field->table->pos_in_table_list->outer_join;
3609 }
3610 bool check_index_dependence(void *arg) override;
3611 friend class Item_default_value;
3612 friend class Item_insert_value;
3613 friend class st_select_lex_unit;
3614 };
3615
3616
3617 /**
3618 Item_field for the ROW data type
3619 */
3620 class Item_field_row: public Item_field,
3621 public Item_args
3622 {
3623 public:
Item_field_row(THD * thd,Field * field)3624 Item_field_row(THD *thd, Field *field)
3625 :Item_field(thd, field),
3626 Item_args()
3627 { }
get_copy(THD * thd)3628 Item *get_copy(THD *thd) override
3629 { return get_item_copy<Item_field_row>(thd, this); }
3630
type_handler()3631 const Type_handler *type_handler() const override
3632 { return &type_handler_row; }
cols()3633 uint cols() const override { return arg_count; }
element_index(uint i)3634 Item* element_index(uint i) override { return arg_count ? args[i] : this; }
addr(uint i)3635 Item** addr(uint i) override { return arg_count ? args + i : NULL; }
check_cols(uint c)3636 bool check_cols(uint c) override
3637 {
3638 if (cols() != c)
3639 {
3640 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
3641 return true;
3642 }
3643 return false;
3644 }
3645 bool row_create_items(THD *thd, List<Spvar_definition> *list);
3646 };
3647
3648
3649 /*
3650 @brief
3651 Item_temptable_field is the same as Item_field, except that print()
3652 continues to work even if the table has been dropped.
3653
3654 @detail
3655
3656 We need this item for "ANALYZE statement" feature. Query execution has
3657 these steps:
3658
3659 1. Run the query.
3660 2. Cleanup starts. Temporary tables are destroyed
3661 3. print "ANALYZE statement" output, if needed
3662 4. Call close_thread_table() for regular tables.
3663
3664 Step #4 is done after step #3, so "ANALYZE stmt" has no problem printing
3665 Item_field objects that refer to regular tables.
3666
3667 However, Step #3 is done after Step #2. Attempt to print Item_field objects
3668 that refer to temporary tables will cause access to freed memory.
3669
3670 To resolve this, we use Item_temptable_field to refer to items in temporary
3671 (work) tables.
3672 */
3673
3674 class Item_temptable_field :public Item_field
3675 {
3676 public:
Item_temptable_field(THD * thd,Name_resolution_context * context_arg,Field * field)3677 Item_temptable_field(THD *thd, Name_resolution_context *context_arg, Field *field)
3678 : Item_field(thd, context_arg, field) {}
3679
Item_temptable_field(THD * thd,Field * field)3680 Item_temptable_field(THD *thd, Field *field)
3681 : Item_field(thd, field) {}
3682
Item_temptable_field(THD * thd,Item_field * item)3683 Item_temptable_field(THD *thd, Item_field *item) : Item_field(thd, item) {};
3684
3685 void print(String *str, enum_query_type query_type) override;
3686 };
3687
3688
3689 class Item_null :public Item_basic_constant
3690 {
3691 public:
3692 Item_null(THD *thd, const char *name_par=0, CHARSET_INFO *cs= &my_charset_bin):
Item_basic_constant(thd)3693 Item_basic_constant(thd)
3694 {
3695 maybe_null= null_value= TRUE;
3696 max_length= 0;
3697 name.str= name_par ? name_par : "NULL";
3698 name.length= strlen(name.str);
3699 collation.set(cs, DERIVATION_IGNORABLE, MY_REPERTOIRE_ASCII);
3700 }
type()3701 Type type() const override { return NULL_ITEM; }
vcol_assignment_allowed_value()3702 bool vcol_assignment_allowed_value() const override { return true; }
3703 double val_real() override;
3704 longlong val_int() override;
3705 String *val_str(String *str) override;
3706 my_decimal *val_decimal(my_decimal *) override;
3707 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override;
3708 longlong val_datetime_packed(THD *) override;
3709 longlong val_time_packed(THD *) override;
3710 int save_in_field(Field *field, bool no_conversions) override;
3711 int save_safe_in_field(Field *field) override;
3712 bool send(Protocol *protocol, st_value *buffer) override;
type_handler()3713 const Type_handler *type_handler() const override
3714 { return &type_handler_null; }
basic_const_item()3715 bool basic_const_item() const override { return true; }
3716 Item *clone_item(THD *thd) override;
const_is_null()3717 bool const_is_null() const override { return true; }
is_null()3718 bool is_null() override { return true; }
3719
print(String * str,enum_query_type)3720 void print(String *str, enum_query_type) override
3721 {
3722 str->append(STRING_WITH_LEN("NULL"));
3723 }
3724
3725 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs) override;
check_partition_func_processor(void *)3726 bool check_partition_func_processor(void *) override { return false; }
3727 Item_basic_constant *make_string_literal_concat(THD *thd,
3728 const LEX_CSTRING *)
3729 override;
get_copy(THD * thd)3730 Item *get_copy(THD *thd) override
3731 { return get_item_copy<Item_null>(thd, this); }
3732 };
3733
3734 class Item_null_result :public Item_null
3735 {
3736 public:
3737 Field *result_field;
Item_null_result(THD * thd)3738 Item_null_result(THD *thd): Item_null(thd), result_field(0) {}
is_result_field()3739 bool is_result_field() override { return result_field != 0; }
type_handler()3740 const Type_handler *type_handler() const override
3741 {
3742 if (result_field)
3743 return result_field->type_handler();
3744 return &type_handler_null;
3745 }
create_tmp_field_ex(MEM_ROOT *,TABLE *,Tmp_field_src *,const Tmp_field_param *)3746 Field *create_tmp_field_ex(MEM_ROOT *, TABLE *, Tmp_field_src *,
3747 const Tmp_field_param *) override
3748 {
3749 DBUG_ASSERT(0);
3750 return NULL;
3751 }
save_in_result_field(bool no_conversions)3752 void save_in_result_field(bool no_conversions) override
3753 {
3754 save_in_field(result_field, no_conversions);
3755 }
check_partition_func_processor(void *)3756 bool check_partition_func_processor(void *) override { return true; }
check_vcol_func_processor(void * arg)3757 bool check_vcol_func_processor(void *arg) override
3758 {
3759 return mark_unsupported_function(full_name(), arg, VCOL_IMPOSSIBLE);
3760 }
3761 };
3762
3763 /*
3764 Item represents one placeholder ('?') of prepared statement
3765
3766 Notes:
3767 Item_param::field_type() is used when this item is in a temporary table.
3768 This is NOT placeholder metadata sent to client, as this value
3769 is assigned after sending metadata (in setup_one_conversion_function).
3770 For example in case of 'SELECT ?' you'll get MYSQL_TYPE_STRING both
3771 in result set and placeholders metadata, no matter what type you will
3772 supply for this placeholder in mysql_stmt_execute.
3773
3774 Item_param has two Type_handler pointers,
3775 which can point to different handlers:
3776
3777 1. In the Type_handler_hybrid_field_type member
3778 It's initialized in:
3779 - Item_param::setup_conversion(), for client-server PS protocol,
3780 according to the bind type.
3781 - Item_param::set_from_item(), for EXECUTE and EXECUTE IMMEDIATE,
3782 according to the actual parameter data type.
3783
3784 2. In the "value" member.
3785 It's initialized in:
3786 - Item_param::set_param_func(), for client-server PS protocol.
3787 - Item_param::set_from_item(), for EXECUTE and EXECUTE IMMEDIATE.
3788 */
3789
3790 class Item_param :public Item_basic_value,
3791 private Settable_routine_parameter,
3792 public Rewritable_query_parameter,
3793 private Type_handler_hybrid_field_type
3794 {
3795 /*
3796 NO_VALUE is a special value meaning that the parameter has not been
3797 assigned yet. Item_param::state is assigned to NO_VALUE in constructor
3798 and is used at prepare time.
3799
3800 1. At prepare time
3801 Item_param::fix_fields() sets "fixed" to true,
3802 but as Item_param::state is still NO_VALUE,
3803 Item_param::basic_const_item() returns false. This prevents various
3804 optimizations to happen at prepare time fix_fields().
3805 For example, in this query:
3806 PREPARE stmt FROM 'SELECT FORMAT(10000,2,?)';
3807 Item_param::basic_const_item() is tested from
3808 Item_func_format::fix_length_and_dec().
3809
3810 2. At execute time:
3811 When Item_param gets a value
3812 (or a pseudo-value like DEFAULT_VALUE or IGNORE_VALUE):
3813 - Item_param::state changes from NO_VALUE to something else
3814 - Item_param::fixed is changed to true
3815 All Item_param::set_xxx() make sure to do so.
3816 In the state with an assigned value:
3817 - Item_param::basic_const_item() returns true
3818 - Item::type() returns NULL_ITEM or CONST_ITEM,
3819 depending on the value assigned.
3820 So in this state Item_param behaves in many cases like a literal.
3821
3822 When Item_param::cleanup() is called:
3823 - Item_param::state does not change
3824 - Item_param::fixed changes to false
3825 Note, this puts Item_param into an inconsistent state:
3826 - Item_param::basic_const_item() still returns "true"
3827 - Item_param::type() still pretends to be a basic constant Item
3828 Both are not expected in combination with fixed==false.
3829 However, these methods are not really called in this state,
3830 see asserts in Item_param::basic_const_item() and Item_param::type().
3831
3832 When Item_param::reset() is called:
3833 - Item_param::state changes to NO_VALUE
3834 - Item_param::fixed changes to false
3835 */
3836 enum enum_item_param_state
3837 {
3838 NO_VALUE, NULL_VALUE, SHORT_DATA_VALUE, LONG_DATA_VALUE,
3839 DEFAULT_VALUE, IGNORE_VALUE
3840 } state;
3841
3842 void fix_temporal(uint32 max_length_arg, uint decimals_arg);
3843
3844 struct CONVERSION_INFO
3845 {
3846 /*
3847 Character sets conversion info for string values.
3848 Character sets of client and connection defined at bind time are used
3849 for all conversions, even if one of them is later changed (i.e.
3850 between subsequent calls to mysql_stmt_execute).
3851 */
3852 CHARSET_INFO *character_set_client;
3853 CHARSET_INFO *character_set_of_placeholder;
3854 /*
3855 This points at character set of connection if conversion
3856 to it is required (i. e. if placeholder typecode is not BLOB).
3857 Otherwise it's equal to character_set_client (to simplify
3858 check in convert_str_value()).
3859 */
3860 CHARSET_INFO *final_character_set_of_str_value;
3861 private:
needs_conversionCONVERSION_INFO3862 bool needs_conversion() const
3863 {
3864 return final_character_set_of_str_value !=
3865 character_set_of_placeholder;
3866 }
3867 bool convert(THD *thd, String *str);
3868 public:
3869 void set(THD *thd, CHARSET_INFO *cs);
convert_if_neededCONVERSION_INFO3870 bool convert_if_needed(THD *thd, String *str)
3871 {
3872 /*
3873 Check is so simple because all charsets were set up properly
3874 in setup_one_conversion_function, where typecode of
3875 placeholder was also taken into account: the variables are different
3876 here only if conversion is really necessary.
3877 */
3878 if (needs_conversion())
3879 return convert(thd, str);
3880 str->set_charset(final_character_set_of_str_value);
3881 return false;
3882 }
3883 };
3884
3885 bool m_empty_string_is_null;
3886
3887 class PValue_simple
3888 {
3889 public:
3890 union
3891 {
3892 longlong integer;
3893 double real;
3894 CONVERSION_INFO cs_info;
3895 MYSQL_TIME time;
3896 };
swap(PValue_simple & other)3897 void swap(PValue_simple &other)
3898 {
3899 swap_variables(PValue_simple, *this, other);
3900 }
3901 };
3902
3903 class PValue: public Type_handler_hybrid_field_type,
3904 public PValue_simple,
3905 public Value_source
3906 {
3907 public:
PValue()3908 PValue(): Type_handler_hybrid_field_type(&type_handler_null) {}
3909 my_decimal m_decimal;
3910 String m_string;
3911 /*
3912 A buffer for string and long data values. Historically all allocated
3913 values returned from val_str() were treated as eligible to
3914 modification. I. e. in some cases Item_func_concat can append it's
3915 second argument to return value of the first one. Because of that we
3916 can't return the original buffer holding string data from val_str(),
3917 and have to have one buffer for data and another just pointing to
3918 the data. This is the latter one and it's returned from val_str().
3919 Can not be declared inside the union as it's not a POD type.
3920 */
3921 String m_string_ptr;
3922
swap(PValue & other)3923 void swap(PValue &other)
3924 {
3925 Type_handler_hybrid_field_type::swap(other);
3926 PValue_simple::swap(other);
3927 m_decimal.swap(other.m_decimal);
3928 m_string.swap(other.m_string);
3929 m_string_ptr.swap(other.m_string_ptr);
3930 }
3931 double val_real(const Type_std_attributes *attr) const;
3932 longlong val_int(const Type_std_attributes *attr) const;
3933 my_decimal *val_decimal(my_decimal *dec, const Type_std_attributes *attr);
3934 String *val_str(String *str, const Type_std_attributes *attr);
3935 };
3936
3937 PValue value;
3938
3939 const String *value_query_val_str(THD *thd, String* str) const;
3940 Item *value_clone_item(THD *thd);
3941 bool is_evaluable_expression() const override;
3942 bool can_return_value() const;
3943
3944 public:
3945 /*
3946 Used for bulk protocol only.
3947 */
3948 enum enum_indicator_type indicator;
3949
type_handler()3950 const Type_handler *type_handler() const override
3951 { return Type_handler_hybrid_field_type::type_handler(); }
3952
vcol_assignment_allowed_value()3953 bool vcol_assignment_allowed_value() const override
3954 {
3955 switch (state) {
3956 case NULL_VALUE:
3957 case DEFAULT_VALUE:
3958 case IGNORE_VALUE:
3959 return true;
3960 case NO_VALUE:
3961 case SHORT_DATA_VALUE:
3962 case LONG_DATA_VALUE:
3963 break;
3964 }
3965 return false;
3966 }
3967
3968 Item_param(THD *thd, const LEX_CSTRING *name_arg,
3969 uint pos_in_query_arg, uint len_in_query_arg);
3970
type()3971 Type type() const override
3972 {
3973 // Don't pretend to be a constant unless value for this item is set.
3974 switch (state) {
3975 case NO_VALUE: return PARAM_ITEM;
3976 case NULL_VALUE: return NULL_ITEM;
3977 case SHORT_DATA_VALUE: return CONST_ITEM;
3978 case LONG_DATA_VALUE: return CONST_ITEM;
3979 case DEFAULT_VALUE: return PARAM_ITEM;
3980 case IGNORE_VALUE: return PARAM_ITEM;
3981 }
3982 DBUG_ASSERT(0);
3983 return PARAM_ITEM;
3984 }
3985
is_order_clause_position()3986 bool is_order_clause_position() const override
3987 {
3988 return state == SHORT_DATA_VALUE &&
3989 type_handler()->is_order_clause_position_type();
3990 }
3991
get_item_const()3992 const Item_const *get_item_const() const override
3993 {
3994 switch (state) {
3995 case SHORT_DATA_VALUE:
3996 case LONG_DATA_VALUE:
3997 case NULL_VALUE:
3998 return this;
3999 case IGNORE_VALUE:
4000 case DEFAULT_VALUE:
4001 case NO_VALUE:
4002 break;
4003 }
4004 return NULL;
4005 }
4006
const_is_null()4007 bool const_is_null() const override { return state == NULL_VALUE; }
can_return_const_value(Item_result type)4008 bool can_return_const_value(Item_result type) const
4009 {
4010 return can_return_value() &&
4011 value.type_handler()->cmp_type() == type &&
4012 type_handler()->cmp_type() == type;
4013 }
const_ptr_longlong()4014 const longlong *const_ptr_longlong() const override
4015 { return can_return_const_value(INT_RESULT) ? &value.integer : NULL; }
const_ptr_double()4016 const double *const_ptr_double() const override
4017 { return can_return_const_value(REAL_RESULT) ? &value.real : NULL; }
const_ptr_my_decimal()4018 const my_decimal *const_ptr_my_decimal() const override
4019 { return can_return_const_value(DECIMAL_RESULT) ? &value.m_decimal : NULL; }
const_ptr_mysql_time()4020 const MYSQL_TIME *const_ptr_mysql_time() const override
4021 { return can_return_const_value(TIME_RESULT) ? &value.time : NULL; }
const_ptr_string()4022 const String *const_ptr_string() const override
4023 { return can_return_const_value(STRING_RESULT) ? &value.m_string : NULL; }
4024
val_real()4025 double val_real() override
4026 {
4027 return can_return_value() ? value.val_real(this) : 0e0;
4028 }
val_int()4029 longlong val_int() override
4030 {
4031 return can_return_value() ? value.val_int(this) : 0;
4032 }
val_decimal(my_decimal * dec)4033 my_decimal *val_decimal(my_decimal *dec) override
4034 {
4035 return can_return_value() ? value.val_decimal(dec, this) : NULL;
4036 }
val_str(String * str)4037 String *val_str(String *str) override
4038 {
4039 return can_return_value() ? value.val_str(str, this) : NULL;
4040 }
4041 bool get_date(THD *thd, MYSQL_TIME *tm, date_mode_t fuzzydate) override;
val_native(THD * thd,Native * to)4042 bool val_native(THD *thd, Native *to) override
4043 {
4044 return Item_param::type_handler()->Item_param_val_native(thd, this, to);
4045 }
4046
4047 int save_in_field(Field *field, bool no_conversions) override;
4048
4049 void set_default();
4050 void set_ignore();
4051 void set_null();
4052 void set_int(longlong i, uint32 max_length_arg);
4053 void set_double(double i);
4054 void set_decimal(const char *str, ulong length);
4055 void set_decimal(const my_decimal *dv, bool unsigned_arg);
4056 bool set_str(const char *str, ulong length,
4057 CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
4058 bool set_longdata(const char *str, ulong length);
4059 void set_time(MYSQL_TIME *tm, timestamp_type type, uint32 max_length_arg);
4060 void set_time(const MYSQL_TIME *tm, uint32 max_length_arg, uint decimals_arg);
4061 bool set_from_item(THD *thd, Item *item);
4062 void reset();
4063
4064 void set_param_tiny(uchar **pos, ulong len);
4065 void set_param_short(uchar **pos, ulong len);
4066 void set_param_int32(uchar **pos, ulong len);
4067 void set_param_int64(uchar **pos, ulong len);
4068 void set_param_float(uchar **pos, ulong len);
4069 void set_param_double(uchar **pos, ulong len);
4070 void set_param_decimal(uchar **pos, ulong len);
4071 void set_param_time(uchar **pos, ulong len);
4072 void set_param_datetime(uchar **pos, ulong len);
4073 void set_param_date(uchar **pos, ulong len);
4074 void set_param_str(uchar **pos, ulong len);
4075
4076 void setup_conversion(THD *thd, uchar param_type);
4077 void setup_conversion_blob(THD *thd);
4078 void setup_conversion_string(THD *thd, CHARSET_INFO *fromcs);
4079
4080 /*
4081 Assign placeholder value from bind data.
4082 Note, that 'len' has different semantics in embedded library (as we
4083 don't need to check that packet is not broken there). See
4084 sql_prepare.cc for details.
4085 */
set_param_func(uchar ** pos,ulong len)4086 void set_param_func(uchar **pos, ulong len)
4087 {
4088 /*
4089 To avoid Item_param::set_xxx() asserting on data type mismatch,
4090 we set the value type handler here:
4091 - It can not be initialized yet after Item_param::setup_conversion().
4092 - Also, for LIMIT clause parameters, the value type handler might have
4093 changed from the real type handler to type_handler_longlong.
4094 So here we'll restore it.
4095 */
4096 const Type_handler *h= Item_param::type_handler();
4097 value.set_handler(h);
4098 h->Item_param_set_param_func(this, pos, len);
4099 }
4100
set_value(THD * thd,const Type_all_attributes * attr,const st_value * val,const Type_handler * h)4101 bool set_value(THD *thd, const Type_all_attributes *attr,
4102 const st_value *val, const Type_handler *h)
4103 {
4104 value.set_handler(h); // See comments in set_param_func()
4105 return h->Item_param_set_from_value(thd, this, attr, val);
4106 }
4107
set_limit_clause_param(longlong nr)4108 bool set_limit_clause_param(longlong nr)
4109 {
4110 value.set_handler(&type_handler_slonglong);
4111 set_int(nr, MY_INT64_NUM_DECIMAL_DIGITS);
4112 return !unsigned_flag && value.integer < 0;
4113 }
4114 const String *query_val_str(THD *thd, String *str) const;
4115
4116 bool convert_str_value(THD *thd);
4117
4118 /*
4119 If value for parameter was not set we treat it as non-const
4120 so no one will use parameters value in fix_fields still
4121 parameter is constant during execution.
4122 */
const_item()4123 bool const_item() const override
4124 {
4125 return state != NO_VALUE;
4126 }
used_tables()4127 table_map used_tables() const override
4128 {
4129 return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT;
4130 }
4131 void print(String *str, enum_query_type query_type) override;
is_null()4132 bool is_null() override
4133 { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; }
4134 bool basic_const_item() const override;
has_no_value()4135 bool has_no_value() const
4136 {
4137 return state == NO_VALUE;
4138 }
has_long_data_value()4139 bool has_long_data_value() const
4140 {
4141 return state == LONG_DATA_VALUE;
4142 }
has_int_value()4143 bool has_int_value() const
4144 {
4145 return state == SHORT_DATA_VALUE &&
4146 value.type_handler()->cmp_type() == INT_RESULT;
4147 }
4148 /*
4149 This method is used to make a copy of a basic constant item when
4150 propagating constants in the optimizer. The reason to create a new
4151 item and not use the existing one is not precisely known (2005/04/16).
4152 Probably we are trying to preserve tree structure of items, in other
4153 words, avoid pointing at one item from two different nodes of the tree.
4154 Return a new basic constant item if parameter value is a basic
4155 constant, assert otherwise. This method is called only if
4156 basic_const_item returned TRUE.
4157 */
4158 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs) override;
4159 Item *clone_item(THD *thd) override;
4160 void set_param_type_and_swap_value(Item_param *from);
4161
get_rewritable_query_parameter()4162 Rewritable_query_parameter *get_rewritable_query_parameter() override
4163 { return this; }
get_settable_routine_parameter()4164 Settable_routine_parameter *get_settable_routine_parameter() override
4165 { return m_is_settable_routine_parameter ? this : nullptr; }
4166
4167 bool append_for_log(THD *thd, String *str) override;
check_vcol_func_processor(void *)4168 bool check_vcol_func_processor(void *) override { return false; }
get_copy(THD *)4169 Item *get_copy(THD *) override { return nullptr; }
4170
4171 bool add_as_clone(THD *thd);
4172 void sync_clones();
register_clone(Item_param * i)4173 bool register_clone(Item_param *i) { return m_clones.push_back(i); }
4174
4175 private:
4176 void invalid_default_param() const;
4177
4178 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
4179
4180 void set_out_param_info(Send_field *info) override;
4181
4182 public:
4183 const Send_field *get_out_param_info() const override;
4184
get_item_param()4185 Item_param *get_item_param() override { return this; }
4186
4187 void make_send_field(THD *thd, Send_field *field) override;
4188
4189 private:
4190 Send_field *m_out_param_info;
4191 bool m_is_settable_routine_parameter;
4192 /*
4193 Array of all references of this parameter marker used in a CTE to its clones
4194 created for copies of this marker used the CTE's copies. It's used to
4195 synchronize the actual value of the parameter with the values of the clones.
4196 */
4197 Mem_root_array<Item_param *, true> m_clones;
4198 };
4199
4200
4201 class Item_int :public Item_num
4202 {
4203 public:
4204 longlong value;
4205 Item_int(THD *thd, int32 i,size_t length= MY_INT32_NUM_DECIMAL_DIGITS):
Item_num(thd)4206 Item_num(thd), value((longlong) i)
4207 { max_length=(uint32)length; }
4208 Item_int(THD *thd, longlong i,size_t length= MY_INT64_NUM_DECIMAL_DIGITS):
Item_num(thd)4209 Item_num(thd), value(i)
4210 { max_length=(uint32)length; }
4211 Item_int(THD *thd, ulonglong i, size_t length= MY_INT64_NUM_DECIMAL_DIGITS):
Item_num(thd)4212 Item_num(thd), value((longlong)i)
4213 { max_length=(uint32)length; unsigned_flag= 1; }
Item_int(THD * thd,const char * str_arg,longlong i,size_t length)4214 Item_int(THD *thd, const char *str_arg,longlong i,size_t length):
4215 Item_num(thd), value(i)
4216 {
4217 max_length=(uint32)length;
4218 name.str= str_arg; name.length= safe_strlen(name.str);
4219 }
Item_int(THD * thd,const char * str_arg,longlong i,size_t length,bool flag)4220 Item_int(THD *thd, const char *str_arg,longlong i,size_t length, bool flag):
4221 Item_num(thd), value(i)
4222 {
4223 max_length=(uint32)length;
4224 name.str= str_arg; name.length= safe_strlen(name.str);
4225 unsigned_flag= flag;
4226 }
4227 Item_int(THD *thd, const char *str_arg, size_t length=64);
type_handler()4228 const Type_handler *type_handler() const override
4229 { return type_handler_long_or_longlong(); }
create_field_for_create_select(MEM_ROOT * root,TABLE * table)4230 Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override
4231 { return tmp_table_field_from_field_type(root, table); }
const_ptr_longlong()4232 const longlong *const_ptr_longlong() const override { return &value; }
val_int()4233 longlong val_int() override { return value; }
val_int_min()4234 longlong val_int_min() const override { return value; }
val_real()4235 double val_real() override { return (double) value; }
4236 my_decimal *val_decimal(my_decimal *) override;
4237 String *val_str(String*) override;
4238 int save_in_field(Field *field, bool no_conversions) override;
is_order_clause_position()4239 bool is_order_clause_position() const override { return true; }
4240 Item *clone_item(THD *thd) override;
4241 void print(String *str, enum_query_type query_type) override;
4242 Item *neg(THD *thd) override;
decimal_precision()4243 uint decimal_precision() const override
4244 { return (uint) (max_length - MY_TEST(value < 0)); }
get_copy(THD * thd)4245 Item *get_copy(THD *thd) override
4246 { return get_item_copy<Item_int>(thd, this); }
4247 };
4248
4249
4250 /*
4251 We sometimes need to distinguish a number from a boolean:
4252 a[1] and a[true] are different things in XPath.
4253 Also in JSON boolean values should be treated differently.
4254 */
4255 class Item_bool :public Item_int
4256 {
4257 public:
Item_bool(THD * thd,const char * str_arg,longlong i)4258 Item_bool(THD *thd, const char *str_arg, longlong i):
4259 Item_int(thd, str_arg, i, 1) {}
Item_bool(THD * thd,bool i)4260 Item_bool(THD *thd, bool i) :Item_int(thd, (longlong) i, 1) { }
is_bool_literal()4261 bool is_bool_literal() const override { return true; }
4262 Item *neg_transformer(THD *thd) override;
type_handler()4263 const Type_handler *type_handler() const override
4264 { return &type_handler_bool; }
fixed_type_handler()4265 const Type_handler *fixed_type_handler() const override
4266 { return &type_handler_bool; }
quick_fix_field()4267 void quick_fix_field() override
4268 {
4269 /*
4270 We can get here when Item_bool is created instead of a constant
4271 predicate at various condition optimization stages in sql_select.
4272 */
4273 }
4274 };
4275
4276
4277 class Item_uint :public Item_int
4278 {
4279 public:
4280 Item_uint(THD *thd, const char *str_arg, size_t length);
Item_uint(THD * thd,ulonglong i)4281 Item_uint(THD *thd, ulonglong i): Item_int(thd, i, 10) {}
4282 Item_uint(THD *thd, const char *str_arg, longlong i, uint length);
val_real()4283 double val_real() { return ulonglong2double((ulonglong)value); }
4284 Item *clone_item(THD *thd);
4285 Item *neg(THD *thd);
decimal_precision()4286 uint decimal_precision() const { return max_length; }
get_copy(THD * thd)4287 Item *get_copy(THD *thd)
4288 { return get_item_copy<Item_uint>(thd, this); }
4289 };
4290
4291
4292 class Item_datetime :public Item_int
4293 {
4294 protected:
4295 MYSQL_TIME ltime;
4296 public:
Item_datetime(THD * thd)4297 Item_datetime(THD *thd): Item_int(thd, 0) { unsigned_flag=0; }
4298 int save_in_field(Field *field, bool no_conversions) override;
4299 longlong val_int() override;
val_real()4300 double val_real() override { return (double)val_int(); }
4301 void set(longlong packed, enum_mysql_timestamp_type ts_type);
get_date(THD * thd,MYSQL_TIME * to,date_mode_t fuzzydate)4302 bool get_date(THD *thd, MYSQL_TIME *to, date_mode_t fuzzydate) override
4303 {
4304 *to= ltime;
4305 return false;
4306 }
4307 };
4308
4309
4310 /* decimal (fixed point) constant */
4311 class Item_decimal :public Item_num
4312 {
4313 protected:
4314 my_decimal decimal_value;
4315 public:
4316 Item_decimal(THD *thd, const char *str_arg, size_t length,
4317 CHARSET_INFO *charset);
4318 Item_decimal(THD *thd, const char *str, const my_decimal *val_arg,
4319 uint decimal_par, uint length);
4320 Item_decimal(THD *thd, const my_decimal *value_par);
4321 Item_decimal(THD *thd, longlong val, bool unsig);
4322 Item_decimal(THD *thd, double val, int precision, int scale);
4323 Item_decimal(THD *thd, const uchar *bin, int precision, int scale);
4324
type_handler()4325 const Type_handler *type_handler() const override
4326 { return &type_handler_newdecimal; }
val_int()4327 longlong val_int() override
4328 { return decimal_value.to_longlong(unsigned_flag); }
val_real()4329 double val_real() override { return decimal_value.to_double(); }
val_str(String * to)4330 String *val_str(String *to) override { return decimal_value.to_string(to); }
val_decimal(my_decimal * val)4331