MySQL 8.0.29
Source Code Documentation
item.h
Go to the documentation of this file.
1#ifndef ITEM_INCLUDED
2#define ITEM_INCLUDED
3
4/* Copyright (c) 2000, 2021, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <sys/types.h>
27
28#include <cfloat>
29#include <climits>
30#include <cmath>
31#include <cstdio>
32#include <cstring>
33#include <memory>
34#include <new>
35#include <string>
36#include <type_traits>
37#include <vector>
38
39#include "decimal.h"
40#include "field_types.h" // enum_field_types
41#include "lex_string.h"
42#include "m_ctype.h"
43#include "m_string.h"
44#include "memory_debugging.h"
45#include "my_alloc.h"
46#include "my_bitmap.h"
47#include "my_compiler.h"
48#include "my_dbug.h"
49#include "my_double2ulonglong.h"
50#include "my_inttypes.h"
51#include "my_sys.h"
52#include "my_table_map.h"
53#include "my_time.h"
55#include "mysql_com.h"
56#include "mysql_time.h"
57#include "mysqld_error.h"
58#include "sql/enum_query_type.h"
59#include "sql/field.h" // Derivation
60#include "sql/mem_root_array.h"
61#include "sql/my_decimal.h" // my_decimal
62#include "sql/parse_location.h" // POS
63#include "sql/parse_tree_node_base.h" // Parse_tree_node
64#include "sql/sql_array.h" // Bounds_checked_array
65#include "sql/sql_const.h"
66#include "sql/sql_list.h"
67#include "sql/table.h"
68#include "sql/table_trigger_field_support.h" // Table_trigger_field_support
69#include "sql/thr_malloc.h"
70#include "sql/trigger_def.h" // enum_trigger_variable_type
71#include "sql_string.h"
72#include "template_utils.h"
73
74class Item;
75class Item_field;
77class Item_sum;
78class Json_wrapper;
79class Protocol;
80class Query_block;
82class THD;
83class user_var_entry;
84struct TYPELIB;
85
87
88void item_init(void); /* Init item functions */
89
90/**
91 Default condition filtering (selectivity) values used by
92 get_filtering_effect() and friends when better estimates
93 (statistics) are not available for a predicate.
94*/
95/**
96 For predicates that are always satisfied. Must be 1.0 or the filter
97 calculation logic will break down.
98*/
99#define COND_FILTER_ALLPASS 1.0f
100/// Filtering effect for equalities: col1 = col2
101#define COND_FILTER_EQUALITY 0.1f
102/// Filtering effect for inequalities: col1 > col2
103#define COND_FILTER_INEQUALITY 0.3333f
104/// Filtering effect for between: col1 BETWEEN a AND b
105#define COND_FILTER_BETWEEN 0.1111f
106/**
107 Value is out-of-date, will need recalculation.
108 This is used by post-greedy-search logic which changes the access method and
109 thus makes obsolete the filtering value calculated by best_access_path(). For
110 example, test_if_skip_sort_order().
111*/
112#define COND_FILTER_STALE -1.0f
113/**
114 A special subcase of the above:
115 - if this is table/index/range scan, and
116 - rows_fetched is how many rows we will examine, and
117 - rows_fetched is less than the number of rows in the table (as determined
118 by test_if_cheaper_ordering() and test_if_skip_sort_order()).
119 Unlike the ordinary case where rows_fetched:
120 - is set by calculate_scan_cost(), and
121 - is how many rows pass the constant condition (so, less than we will
122 examine), and
123 - the actual rows_fetched to show in EXPLAIN is the number of rows in the
124 table (== rows which we will examine), and
125 - the constant condition's effect has to be moved to filter_effect for
126 EXPLAIN.
127*/
128#define COND_FILTER_STALE_NO_CONST -2.0f
129
130static inline uint32 char_to_byte_length_safe(uint32 char_length_arg,
131 uint32 mbmaxlen_arg) {
132 ulonglong tmp = ((ulonglong)char_length_arg) * mbmaxlen_arg;
133 return (tmp > UINT_MAX32) ? (uint32)UINT_MAX32 : (uint32)tmp;
134}
135
137 Item_result result_type,
138 uint8 decimals) {
139 if (is_temporal_type(real_type_to_type(data_type)))
140 return decimals ? DECIMAL_RESULT : INT_RESULT;
141 if (result_type == STRING_RESULT) return REAL_RESULT;
142 return result_type;
143}
144
145/*
146 "Declared Type Collation"
147 A combination of collation and its derivation.
148
149 Flags for collation aggregation modes:
150 MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
151 MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
152 (i.e. constant).
153 MY_COLL_ALLOW_CONV - allow any kind of conversion
154 (combination of the above two)
155 MY_COLL_ALLOW_NUMERIC_CONV - if all items were numbers, convert to
156 @@character_set_connection
157 MY_COLL_DISALLOW_NONE - don't allow return DERIVATION_NONE
158 (e.g. when aggregating for comparison)
159 MY_COLL_CMP_CONV - combination of MY_COLL_ALLOW_CONV
160 and MY_COLL_DISALLOW_NONE
161*/
162
163#define MY_COLL_ALLOW_SUPERSET_CONV 1
164#define MY_COLL_ALLOW_COERCIBLE_CONV 2
165#define MY_COLL_DISALLOW_NONE 4
166#define MY_COLL_ALLOW_NUMERIC_CONV 8
167
168#define MY_COLL_ALLOW_CONV \
169 (MY_COLL_ALLOW_SUPERSET_CONV | MY_COLL_ALLOW_COERCIBLE_CONV)
170#define MY_COLL_CMP_CONV (MY_COLL_ALLOW_CONV | MY_COLL_DISALLOW_NONE)
171
173 public:
177
181 }
186 }
187 DTCollation(const CHARSET_INFO *collation_arg, Derivation derivation_arg) {
188 collation = collation_arg;
189 derivation = derivation_arg;
190 set_repertoire_from_charset(collation_arg);
191 }
192 void set(const DTCollation &dt) {
193 collation = dt.collation;
196 }
197 void set(const CHARSET_INFO *collation_arg, Derivation derivation_arg) {
198 collation = collation_arg;
199 derivation = derivation_arg;
200 set_repertoire_from_charset(collation_arg);
201 }
202 void set(const CHARSET_INFO *collation_arg, Derivation derivation_arg,
203 uint repertoire_arg) {
204 collation = collation_arg;
205 derivation = derivation_arg;
206 repertoire = repertoire_arg;
207 }
208 void set_numeric() {
212 }
213 void set(const CHARSET_INFO *collation_arg) {
214 collation = collation_arg;
215 set_repertoire_from_charset(collation_arg);
216 }
217 void set(Derivation derivation_arg) { derivation = derivation_arg; }
218 void set_repertoire(uint repertoire_arg) { repertoire = repertoire_arg; }
219 bool aggregate(DTCollation &dt, uint flags = 0);
220 bool set(DTCollation &dt1, DTCollation &dt2, uint flags = 0) {
221 set(dt1);
222 return aggregate(dt2, flags);
223 }
224 const char *derivation_name() const {
225 switch (derivation) {
227 return "NUMERIC";
229 return "IGNORABLE";
231 return "COERCIBLE";
233 return "IMPLICIT";
235 return "SYSCONST";
237 return "EXPLICIT";
238 case DERIVATION_NONE:
239 return "NONE";
240 default:
241 return "UNKNOWN";
242 }
243 }
244};
245
246/**
247 Class used as argument to Item::walk() together with mark_field_in_map()
248*/
250 public:
253
254 /**
255 If == NULL, update map of any table.
256 If <> NULL, update map of only this table.
257 */
258 TABLE *const table;
259 /// How to mark the map.
261};
262
263/**
264 Class used as argument to Item::walk() together with used_tables_for_level()
265*/
267 public:
269
270 Query_block *const select; ///< Level for which data is accumulated
271 table_map used_tables; ///< Accumulated used tables data
272};
273
274/*************************************************************************/
275
276/**
277 Storage for name strings.
278 Enpowers Simple_cstring with allocation routines from the sql_strmake family.
279
280 This class must stay as small as possible as we often
281 pass it into functions using call-by-value evaluation.
282
283 Don't add new members or virual methods into this class!
284*/
286 private:
287 void set_or_copy(const char *str, size_t length, bool is_null_terminated) {
288 if (is_null_terminated)
289 set(str, length);
290 else
291 copy(str, length);
292 }
293
294 public:
296 /*
297 Please do NOT add constructor Name_string(const char *str) !
298 It will involve hidden strlen() call, which can affect
299 performance negatively. Use Name_string(str, len) instead.
300 */
301 Name_string(const char *str, size_t length) : Simple_cstring(str, length) {}
304 Name_string(const char *str, size_t length, bool is_null_terminated)
305 : Simple_cstring() {
306 set_or_copy(str, length, is_null_terminated);
307 }
308 Name_string(const LEX_STRING str, bool is_null_terminated)
309 : Simple_cstring() {
310 set_or_copy(str.str, str.length, is_null_terminated);
311 }
312 /**
313 Allocate space using sql_strmake() or sql_strmake_with_convert().
314 */
315 void copy(const char *str, size_t length, const CHARSET_INFO *cs);
316 /**
317 Variants for copy(), for various argument combinations.
318 */
319 void copy(const char *str, size_t length) {
321 }
322 void copy(const char *str) {
323 copy(str, (str ? strlen(str) : 0), system_charset_info);
324 }
325 void copy(const LEX_STRING lex) { copy(lex.str, lex.length); }
326 void copy(const LEX_STRING *lex) { copy(lex->str, lex->length); }
327 void copy(const Name_string str) { copy(str.ptr(), str.length()); }
328 /**
329 Compare name to another name in C string, case insensitively.
330 */
331 bool eq(const char *str) const {
332 assert(str && ptr());
333 return my_strcasecmp(system_charset_info, ptr(), str) == 0;
334 }
335 bool eq_safe(const char *str) const { return is_set() && str && eq(str); }
336 /**
337 Compare name to another name in Name_string, case insensitively.
338 */
339 bool eq(const Name_string name) const { return eq(name.ptr()); }
340 bool eq_safe(const Name_string name) const {
341 return is_set() && name.is_set() && eq(name);
342 }
343};
344
345#define NAME_STRING(x) Name_string(STRING_WITH_LEN(x))
346
347extern const Name_string null_name_string;
348
349/**
350 Storage for Item names.
351 Adds "autogenerated" flag and warning functionality to Name_string.
352*/
354 private:
355 bool m_is_autogenerated; /* indicates if name of this Item
356 was autogenerated or set by user */
357 public:
361 /**
362 Set m_is_autogenerated flag to the given value.
363 */
366 }
367 /**
368 Return the auto-generated flag.
369 */
370 bool is_autogenerated() const { return m_is_autogenerated; }
371 using Name_string::copy;
372 /**
373 Copy name together with autogenerated flag.
374 Produce a warning if name was cut.
375 */
376 void copy(const char *str_arg, size_t length_arg, const CHARSET_INFO *cs_arg,
377 bool is_autogenerated_arg);
378};
379
380/*
381 Instances of Name_resolution_context store the information necesary for
382 name resolution of Items and other context analysis of a query made in
383 fix_fields().
384
385 This structure is a part of Query_block, a pointer to this structure is
386 assigned when an item is created (which happens mostly during parsing
387 (sql_yacc.yy)), but the structure itself will be initialized after parsing
388 is complete
389
390 TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
391 separate Query_block which allow to remove tricks of changing this
392 structure before and after INSERT/CREATE and its SELECT to make correct
393 field name resolution.
394*/
396 /*
397 The name resolution context to search in when an Item cannot be
398 resolved in this context (the context of an outer select)
399 */
401 /// Link to next name res context with the same query block as the base
403
404 /*
405 List of tables used to resolve the items of this context. Usually these
406 are tables from the FROM clause of SELECT statement. The exceptions are
407 INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
408 subquery is not moved to a separate Query_block. For these types of
409 statements we have to change this member dynamically to ensure correct
410 name resolution of different parts of the statement.
411 */
413 /*
414 In most cases the two table references below replace 'table_list' above
415 for the purpose of name resolution. The first and last name resolution
416 table references allow us to search only in a sub-tree of the nested
417 join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
418 and JOIN ... ON.
419 */
421 /*
422 Last table to search in the list of leaf table references that begins
423 with first_name_resolution_table.
424 */
426
427 /*
428 Query_block item belong to, in case of merged VIEW it can differ from
429 Query_block where item was created, so we can't use table_list/field_list
430 from there
431 */
433
434 /*
435 Processor of errors caused during Item name resolving, now used only to
436 hide underlying tables in errors about views (i.e. it substitute some
437 errors for views)
438 */
441
442 /**
443 When true, items are resolved in this context against
444 Query_block::item_list, SELECT_lex::group_list and
445 this->table_list. If false, items are resolved only against
446 this->table_list.
447
448 @see Query_block::item_list, Query_block::group_list
449 */
451
452 /*
453 Security context of this name resolution context. It's used for views
454 and is non-zero only if the view is defined with SQL SECURITY DEFINER.
455 */
457
465 DBUG_PRINT("outer_field", ("creating ctx %p", this));
466 }
467
468 void init() {
470 view_error_handler = false;
473 }
474
478 }
479};
480
481/**
482 Struct used to pass around arguments to/from
483 check_function_as_value_generator
484*/
487 int default_error_code, Value_generator_source val_gen_src)
488 : err_code(default_error_code), source(val_gen_src) {}
489 /// the order of the column in table
490 int col_index{-1};
491 /// the error code found during check(if any)
493 /*
494 If it is a generated column, default expression or check constraint
495 expresion value generator.
496 */
498 /// the name of the function which is not allowed
499 const char *banned_function_name{nullptr};
500
501 /// Return the correct error code, based on whether or not if we are checking
502 /// for disallowed functions in generated column expressions, in default
503 /// value expressions or in check constraint expression.
505 return ((source == VGS_GENERATED_COLUMN)
506 ? ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
508 ? ER_DEFAULT_VAL_GENERATED_FUNCTION_IS_NOT_ALLOWED
509 : ER_CHECK_CONSTRAINT_FUNCTION_IS_NOT_ALLOWED);
510 }
511};
512/*
513 Store and restore the current state of a name resolution context.
514*/
515
517 private:
523
524 public:
525 /* Save the state of a name resolution context. */
526 void save_state(Name_resolution_context *context, TABLE_LIST *table_list) {
527 save_table_list = context->table_list;
530 save_next_local = table_list->next_local;
532 }
533
534 /* Restore a name resolution context from saved state. */
536 table_list->next_local = save_next_local;
538 context->table_list = save_table_list;
541 }
542
543 void update_next_local(TABLE_LIST *table_list) {
544 save_next_local = table_list;
545 }
546
549 }
550};
551
552/*
553 This enum is used to report information about monotonicity of function
554 represented by Item* tree.
555 Monotonicity is defined only for Item* trees that represent table
556 partitioning expressions (i.e. have no subqueries/user vars/dynamic parameters
557 etc etc). An Item* tree is assumed to have the same monotonicity properties
558 as its corresponding function F:
559
560 [signed] longlong F(field1, field2, ...) {
561 put values of field_i into table record buffer;
562 return item->val_int();
563 }
564
565 NOTE
566 At the moment function monotonicity is not well defined (and so may be
567 incorrect) for Item trees with parameters/return types that are different
568 from INT_RESULT, may be NULL, or are unsigned.
569 It will be possible to address this issue once the related partitioning bugs
570 (BUG#16002, BUG#15447, BUG#13436) are fixed.
571
572 The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
573 NULL which puts those rows into the NULL partition, but
574 '2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
575 for this (see Bug#20577).
576*/
577
578typedef enum monotonicity_info {
579 NON_MONOTONIC, /* none of the below holds */
580 MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */
581 MONOTONIC_INCREASING_NOT_NULL, /* But only for valid/real x and y */
582 MONOTONIC_STRICT_INCREASING, /* F() is unary and (x < y) => (F(x) < F(y)) */
583 MONOTONIC_STRICT_INCREASING_NOT_NULL /* But only for valid/real x and y */
585
586/**
587 A type for SQL-like 3-valued Booleans: true/false/unknown.
588*/
589class Bool3 {
590 public:
591 /// @returns an instance set to "FALSE"
592 static const Bool3 false3() { return Bool3(v_FALSE); }
593 /// @returns an instance set to "UNKNOWN"
594 static const Bool3 unknown3() { return Bool3(v_UNKNOWN); }
595 /// @returns an instance set to "TRUE"
596 static const Bool3 true3() { return Bool3(v_TRUE); }
597
598 bool is_true() const { return m_val == v_TRUE; }
599 bool is_unknown() const { return m_val == v_UNKNOWN; }
600 bool is_false() const { return m_val == v_FALSE; }
601
602 private:
604 /// This is private; instead, use false3()/etc.
605 Bool3(value v) : m_val(v) {}
606
608 /*
609 No operator to convert Bool3 to bool (or int) - intentionally: how
610 would you map unknown3 to true/false?
611 It is because we want to block such conversions that Bool3 is a class
612 instead of a plain enum.
613 */
614};
615
616/**
617 Type properties, used to collect type information for later assignment
618 to an Item object. The object stores attributes signedness, max length
619 and collation. However, precision and scale (for decimal numbers) and
620 fractional second precision (for time and datetime) are not stored,
621 since any type derived from this object will have default values for these
622 attributes.
623*/
625 public:
626 /// Constructor for any signed numeric type or date type
627 /// Defaults are provided for attributes like signedness and max length
629 : m_type(type_arg),
630 m_unsigned_flag(false),
631 m_max_length(0),
633 assert(type_arg != MYSQL_TYPE_VARCHAR && type_arg != MYSQL_TYPE_JSON);
634 }
635 /// Constructor for any numeric type, with explicit signedness
636 Type_properties(enum_field_types type_arg, bool unsigned_arg)
637 : m_type(type_arg),
638 m_unsigned_flag(unsigned_arg),
639 m_max_length(0),
641 assert(is_numeric_type(type_arg) || type_arg == MYSQL_TYPE_BIT ||
642 type_arg == MYSQL_TYPE_YEAR);
643 }
644 /// Constructor for character type, with explicit character set.
645 /// Default length/max length is provided.
647 : m_type(type_arg),
648 m_unsigned_flag(false),
649 m_max_length(0),
651 /// Constructor for Item
652 Type_properties(Item &item);
654 const bool m_unsigned_flag;
657};
658
659/*************************************************************************/
660
661class sp_rcontext;
662
664 public:
666 virtual ~Settable_routine_parameter() = default;
667 /**
668 Set required privileges for accessing the parameter.
669
670 @param privilege The required privileges for this field, with the
671 following alternatives:
672 MODE_IN - SELECT_ACL
673 MODE_OUT - UPDATE_ACL
674 MODE_INOUT - SELECT_ACL | UPDATE_ACL
675 */
676 virtual void set_required_privilege(ulong privilege [[maybe_unused]]) {}
677
678 /*
679 Set parameter value.
680
681 SYNOPSIS
682 set_value()
683 thd thread handle
684 ctx context to which parameter belongs (if it is local
685 variable).
686 it item which represents new value
687
688 RETURN
689 false if parameter value has been set,
690 true if error has occurred.
691 */
692 virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it) = 0;
693
694 virtual void set_out_param_info(Send_field *info [[maybe_unused]]) {}
695
696 virtual const Send_field *get_out_param_info() const { return nullptr; }
697};
698
699/*
700 Analyzer function
701 SYNOPSIS
702 argp in/out IN: Analysis parameter
703 OUT: Parameter to be passed to the transformer
704
705 RETURN
706 true Invoke the transformer
707 false Don't do it
708
709*/
710typedef bool (Item::*Item_analyzer)(uchar **argp);
711
712/**
713 Type for transformers used by Item::transform and Item::compile
714 @param arg Argument used by the transformer. Really a typeless pointer
715 in spite of the uchar type (historical reasons). The
716 transformer needs to cast this to the desired pointer type
717 @returns The transformed item
718*/
719typedef Item *(Item::*Item_transformer)(uchar *arg);
720typedef void (*Cond_traverser)(const Item *item, void *arg);
721
722/**
723 Utility mixin class to be able to walk() only parts of item trees.
724
725 Used with PREFIX+POSTFIX walk: in the prefix call of the Item
726 processor, we process the item X, may decide that its children should not
727 be processed (just like if they didn't exist): processor calls stop_at(X)
728 for that. Then walk() goes to a child Y; the processor tests is_stopped(Y)
729 which returns true, so processor sees that it must not do any processing
730 and returns immediately. Finally, the postfix call to the processor on X
731 tests is_stopped(X) which returns "true" and understands that the
732 not-to-be-processed children have been skipped so calls restart(). Thus,
733 any sibling of X, any part of the Item tree not under X, can then be
734 processed.
735*/
737 protected:
742
743 /// Stops walking children of this item
744 void stop_at(const Item *i) {
745 assert(!stopped_at_item);
746 stopped_at_item = i;
747 }
748
749 /**
750 @returns if we are stopped. If item 'i' is where we stopped, restarts the
751 walk for next items.
752 */
753 bool is_stopped(const Item *i) {
754 if (stopped_at_item) {
755 /*
756 Walking was disabled for a tree part rooted a one ancestor of 'i' or
757 rooted at 'i'.
758 */
759 if (stopped_at_item == i) {
760 /*
761 Walking was disabled for the tree part rooted at 'i'; we have now just
762 returned back to this root (POSTFIX call), left the tree part:
763 enable the walk again, for other tree parts.
764 */
765 stopped_at_item = nullptr;
766 }
767 // No further processing to do for this item:
768 return true;
769 }
770 return false;
771 }
772
773 private:
775};
776
777/**
778 Base class that is used to represent any kind of expression in a
779 relational query. The class provides subclasses for simple components, like
780 literal (constant) values, column references and variable references,
781 as well as more complex expressions like comparison predicates,
782 arithmetic and string functions, row objects, function references and
783 subqueries.
784
785 The lifetime of an Item class object is often the same as a relational
786 statement, which may be used for several executions, but in some cases
787 it may also be generated for an optimized statement and thus be valid
788 only for one execution.
789
790 For Item objects with longer lifespan than one execution, we must take
791 special precautions when referencing objects with shorter lifespan.
792 For example, TABLE and Field objects against most tables are valid only for
793 one execution. For such objects, Item classes should rather reference
794 TABLE_LIST and Item_field objects instead of TABLE and Field, because
795 these classes support dynamic rebinding of objects before each execution.
796 See Item::bind_fields() which binds new objects per execution and
797 Item::cleanup() that deletes references to such objects.
798
799 These mechanisms can also be used to handle other objects with shorter
800 lifespan, such as function references and variable references.
801*/
802class Item : public Parse_tree_node {
804
805 friend class udf_handler;
806 virtual bool is_expensive_processor(uchar *) { return false; }
807
808 protected:
809 /**
810 Sets the result value of the function an empty string, using the current
811 character set. No memory is allocated.
812 @retval A pointer to the str_value member.
813 */
816 return &str_value;
817 }
818
819 public:
820 Item(const Item &) = delete;
821 void operator=(Item &) = delete;
822 static void *operator new(size_t size) noexcept {
823 return (*THR_MALLOC)->Alloc(size);
824 }
825 static void *operator new(size_t size, MEM_ROOT *mem_root,
826 const std::nothrow_t &arg
827 [[maybe_unused]] = std::nothrow) noexcept {
828 return mem_root->Alloc(size);
829 }
830
831 static void operator delete(void *ptr [[maybe_unused]],
832 size_t size [[maybe_unused]]) {
833 TRASH(ptr, size);
834 }
835 static void operator delete(void *, MEM_ROOT *,
836 const std::nothrow_t &) noexcept {}
837
838 enum Type {
869 };
870
872
874
875 /// How to cache constant JSON data
877 /// Don't cache
879 /// Source data is a JSON string, parse and cache result
881 /// Source data is SQL scalar, convert and cache result
883 };
884
885 enum Bool_test ///< Modifier for result transformation
886 { BOOL_IS_TRUE = 0x00,
896 };
897
898 // Return the default data type for a given result type
900 switch (result) {
901 case INT_RESULT:
902 return MYSQL_TYPE_LONGLONG;
903 case DECIMAL_RESULT:
905 case REAL_RESULT:
906 return MYSQL_TYPE_DOUBLE;
907 case STRING_RESULT:
908 return MYSQL_TYPE_VARCHAR;
909 case INVALID_RESULT:
910 return MYSQL_TYPE_INVALID;
911 case ROW_RESULT:
912 default:
913 assert(false);
914 }
915 return MYSQL_TYPE_INVALID;
916 }
917
918 // Return the default result type for a given data type
920 switch (type) {
921 case MYSQL_TYPE_TINY:
922 case MYSQL_TYPE_SHORT:
923 case MYSQL_TYPE_INT24:
924 case MYSQL_TYPE_LONG:
926 case MYSQL_TYPE_BOOL:
927 case MYSQL_TYPE_BIT:
928 case MYSQL_TYPE_YEAR:
929 return INT_RESULT;
932 return DECIMAL_RESULT;
933 case MYSQL_TYPE_FLOAT:
935 return REAL_RESULT;
942 case MYSQL_TYPE_BLOB:
944 case MYSQL_TYPE_JSON:
945 case MYSQL_TYPE_ENUM:
946 case MYSQL_TYPE_SET:
947 return STRING_RESULT;
949 case MYSQL_TYPE_DATE:
950 case MYSQL_TYPE_TIME:
955 case MYSQL_TYPE_TIME2:
956 return STRING_RESULT;
958 return INVALID_RESULT;
959 case MYSQL_TYPE_NULL:
961 break;
962 }
963 assert(false);
964 return INVALID_RESULT;
965 }
966
967 /**
968 Provide data type for a user or system variable, based on the type of
969 the item that is assigned to the variable.
970
971 @note MYSQL_TYPE_VARCHAR is returned for all string types, but must be
972 further adjusted based on maximum string length by the caller.
973
974 @param src_type Source type that variable's type is derived from
975 */
977 switch (src_type) {
978 case MYSQL_TYPE_BOOL:
979 case MYSQL_TYPE_TINY:
980 case MYSQL_TYPE_SHORT:
981 case MYSQL_TYPE_INT24:
982 case MYSQL_TYPE_LONG:
984 case MYSQL_TYPE_BIT:
985 return MYSQL_TYPE_LONGLONG;
989 case MYSQL_TYPE_FLOAT:
991 return MYSQL_TYPE_DOUBLE;
995 return MYSQL_TYPE_VARCHAR;
996 case MYSQL_TYPE_YEAR:
997 return MYSQL_TYPE_LONGLONG;
999 case MYSQL_TYPE_DATE:
1000 case MYSQL_TYPE_TIME:
1002 case MYSQL_TYPE_NEWDATE:
1005 case MYSQL_TYPE_TIME2:
1006 case MYSQL_TYPE_JSON:
1007 case MYSQL_TYPE_ENUM:
1008 case MYSQL_TYPE_SET:
1010 case MYSQL_TYPE_NULL:
1012 case MYSQL_TYPE_BLOB:
1015 return MYSQL_TYPE_VARCHAR;
1016 case MYSQL_TYPE_INVALID:
1018 return MYSQL_TYPE_INVALID;
1019 }
1020 assert(false);
1021 return MYSQL_TYPE_NULL;
1022 }
1023
1024 /// Item constructor for general use.
1025 Item();
1026
1027 /**
1028 Constructor used by Item_field, Item_ref & aggregate functions.
1029 Used for duplicating lists in processing queries with temporary tables.
1030
1031 Also used for Item_cond_and/Item_cond_or for creating top AND/OR structure
1032 of WHERE clause to protect it of optimisation changes in prepared statements
1033 */
1034 Item(THD *thd, const Item *item);
1035
1036 /**
1037 Parse-time context-independent constructor.
1038
1039 This constructor and caller constructors of child classes must not
1040 access/change thd->lex (including thd->lex->current_query_block(),
1041 thd->m_parser_state etc structures).
1042
1043 If we need to finalize the construction of the object, then we move
1044 all context-sensitive code to the itemize() virtual function.
1045
1046 The POS parameter marks this constructor and other context-independent
1047 constructors of child classes for easy recognition/separation from other
1048 (context-dependent) constructors.
1049 */
1050 explicit Item(const POS &);
1051
1052#ifdef EXTRA_DEBUG
1053 ~Item() override { item_name.set(0); }
1054#else
1055 ~Item() override = default;
1056#endif
1057
1058 private:
1059 /*
1060 Hide the contextualize*() functions: call/override the itemize()
1061 in Item class tree instead.
1062 */
1063 bool contextualize(Parse_context *) override {
1064 assert(0);
1065 return true;
1066 }
1067
1068 protected:
1069 /**
1070 Helper function to skip itemize() for grammar-allocated items
1071
1072 @param [out] res pointer to "this"
1073
1074 @retval true can skip itemize()
1075 @retval false can't skip: the item is allocated directly by the parser
1076 */
1077 bool skip_itemize(Item **res) {
1078 *res = this;
1079 return !is_parser_item;
1080 }
1081
1082 /*
1083 Checks if the function should return binary result based on the items
1084 provided as parameter.
1085 Function should only be used by Item_bit_func*
1086
1087 @param a item to check
1088 @param b item to check, may be nullptr
1089
1090 @returns true if binary result.
1091 */
1092 static bool bit_func_returns_binary(const Item *a, const Item *b);
1093
1094 public:
1095 /**
1096 The same as contextualize() but with additional parameter
1097
1098 This function finalize the construction of Item objects (see the Item(POS)
1099 constructor): we can access/change parser contexts from the itemize()
1100 function.
1101
1102 @param pc current parse context
1103 @param [out] res pointer to "this" or to a newly allocated
1104 replacement object to use in the Item tree instead
1105
1106 @retval false success
1107 @retval true syntax/OOM/etc error
1108 */
1109 virtual bool itemize(Parse_context *pc, Item **res);
1110
1111 void rename(char *new_name);
1112 void init_make_field(Send_field *tmp_field, enum enum_field_types type);
1113 /**
1114 Called for every Item after use (preparation and execution).
1115 Release all allocated resources, such as dynamic memory.
1116 Prepare for new execution by clearing cached values.
1117 Do not remove values allocated during preparation, destructor handles this.
1118 */
1119 virtual void cleanup() { marker = MARKER_NONE; }
1120 /**
1121 Called when an item has been removed, can be used to notify external
1122 objects about the removal, e.g subquery predicates that are part of
1123 the sj_candidates container.
1124 */
1125 virtual void notify_removal() {}
1126 virtual void make_field(Send_field *field);
1127 virtual Field *make_string_field(TABLE *table) const;
1128 virtual bool fix_fields(THD *, Item **);
1129 /**
1130 Fix after tables have been moved from one query_block level to the parent
1131 level, e.g by semijoin conversion.
1132 Basically re-calculate all attributes dependent on the tables.
1133
1134 @param parent_query_block query_block that tables are moved to.
1135 @param removed_query_block query_block that tables are moved away from,
1136 child of parent_query_block.
1137 */
1138 virtual void fix_after_pullout(Query_block *parent_query_block
1139 [[maybe_unused]],
1140 Query_block *removed_query_block
1141 [[maybe_unused]]) {}
1142 /*
1143 should be used in case where we are sure that we do not need
1144 complete fix_fields() procedure.
1145 */
1146 inline void quick_fix_field() { fixed = true; }
1147 virtual void set_can_use_prefix_key() {}
1148
1149 /**
1150 Propagate data type specifications into parameters and user variables.
1151 If item has descendants, propagate type recursively into these.
1152
1153 @param thd thread handler
1154 @param type Data type properties that are propagated
1155
1156 @returns false if success, true if error
1157 */
1158 virtual bool propagate_type(THD *thd [[maybe_unused]],
1159 const Type_properties &type [[maybe_unused]]) {
1160 return false;
1161 }
1162
1163 /**
1164 Wrapper for easier calling of propagate_type(const Type_properties &).
1165 @param thd thread handler
1166 @param def type to make Type_properties object
1167 @param pin if true: also mark the type as pinned
1168 @param inherit if true: also mark the type as inherited
1169
1170 @returns false if success, true if error
1171 */
1173 bool pin = false, bool inherit = false) {
1174 /*
1175 Propagate supplied type if types have not yet been assigned to expression,
1176 or type is pinned, in which case the supplied type overrides the
1177 actual type of parameters. Note we do not support "pinning" of
1178 expressions containing parameters, only standalone parameters,
1179 but this is a very minor problem.
1180 */
1181 if (data_type() != MYSQL_TYPE_INVALID && !(pin && type() == PARAM_ITEM))
1182 return false;
1183 if (propagate_type(thd,
1184 (def == MYSQL_TYPE_VARCHAR)
1186 : (def == MYSQL_TYPE_JSON)
1188 : Type_properties(def)))
1189 return true;
1190 if (pin) pin_data_type();
1191 if (inherit) set_data_type_inherited();
1192
1193 return false;
1194 }
1195
1196 /**
1197 For Items with data type JSON, mark that a string argument is treated
1198 as a scalar JSON value. Only relevant for the Item_param class.
1199 */
1200 virtual void mark_json_as_scalar() {}
1201
1202 protected:
1203 /**
1204 Helper function which does all of the work for
1205 save_in_field(Field*, bool), except some error checking common to
1206 all subclasses, which is performed by save_in_field() itself.
1207
1208 Subclasses that need to specialize the behaviour of
1209 save_in_field(), should override this function instead of
1210 save_in_field().
1211
1212 @param[in,out] field the field to save the item into
1213 @param no_conversions whether or not to allow conversions of the value
1214
1215 @return the status from saving into the field
1216 @retval TYPE_OK item saved without any errors or warnings
1217 @retval != TYPE_OK there were errors or warnings when saving the item
1218 */
1220 bool no_conversions);
1221
1222 public:
1223 /**
1224 Save the item into a field but do not emit any warnings.
1225
1226 @param field field to save the item into
1227 @param no_conversions whether or not to allow conversions of the value
1228
1229 @return the status from saving into the field
1230 @retval TYPE_OK item saved without any issues
1231 @retval != TYPE_OK there were issues saving the item
1232 */
1234 bool no_conversions);
1235 /**
1236 Save a temporal value in packed longlong format into a Field.
1237 Used in optimizer.
1238
1239 Subclasses that need to specialize this function, should override
1240 save_in_field_inner().
1241
1242 @param[in,out] field the field to save the item into
1243 @param no_conversions whether or not to allow conversions of the value
1244
1245 @return the status from saving into the field
1246 @retval TYPE_OK item saved without any errors or warnings
1247 @retval != TYPE_OK there were errors or warnings when saving the item
1248 */
1249 type_conversion_status save_in_field(Field *field, bool no_conversions);
1250
1251 /**
1252 A slightly faster value of save_in_field() that returns no error value
1253 (you will need to check thd->is_error() yourself), and does not support
1254 saving into hidden fields for functional indexes. Used by copy_funcs(),
1255 to avoid the functional call overhead and RAII setup of save_in_field().
1256 */
1257 void save_in_field_no_error_check(Field *field, bool no_conversions) {
1258 assert(!field->is_field_for_functional_index());
1259 save_in_field_inner(field, no_conversions);
1260 }
1261
1262 virtual void save_org_in_field(Field *field) { save_in_field(field, true); }
1263
1264 virtual bool send(Protocol *protocol, String *str);
1265 bool evaluate(THD *thd, String *str);
1266 virtual bool eq(const Item *, bool binary_cmp) const;
1267 virtual Item_result result_type() const { return REAL_RESULT; }
1268 /**
1269 Result type when an item appear in a numeric context.
1270 See Field::numeric_context_result_type() for more comments.
1271 */
1274 }
1275 /**
1276 Similar to result_type() but makes DATE, DATETIME, TIMESTAMP
1277 pretend to be numbers rather than strings.
1278 */
1281 : result_type();
1282 }
1283
1284 /**
1285 Set data type for item as inherited.
1286 Non-empty implementation only for dynamic parameters.
1287 */
1288 virtual void set_data_type_inherited() {}
1289
1290 /**
1291 Pin the data type for the item.
1292 Non-empty implementation only for dynamic parameters.
1293 */
1294 virtual void pin_data_type() {}
1295
1296 /// Retrieve the derived data type of the Item.
1298 return static_cast<enum_field_types>(m_data_type);
1299 }
1300
1301 /**
1302 Retrieve actual data type for an item. Equal to data_type() for
1303 all items, except parameters.
1304 */
1305 virtual enum_field_types actual_data_type() const { return data_type(); }
1306
1307 /**
1308 Get the default data (output) type for the specific item.
1309 Important for some SQL functions that may deliver multiple result types,
1310 and is used to determine data type for function's parameters that cannot
1311 be type-resolved by looking at the context.
1312 An example of such function is '+', which may return INT, DECIMAL,
1313 DOUBLE, depending on arguments.
1314 On the contrary, many other functions have a fixed output type, usually
1315 set with set_data_type_XXX(), which overrides the value of
1316 default_data_type(). For example, COS always returns DOUBLE,
1317 */
1319 // If data type has been set, the information returned here is irrelevant:
1320 assert(data_type() == MYSQL_TYPE_INVALID);
1321 return MYSQL_TYPE_VARCHAR;
1322 }
1323 /**
1324 Set the data type of the current Item. It is however recommended to
1325 use one of the type-specific setters if possible.
1326
1327 @param data_type The data type of this Item.
1328 */
1330 m_data_type = static_cast<uint8>(data_type);
1331 }
1332
1333 inline void set_data_type_bool() {
1336 max_length = 1;
1337 }
1338
1339 /**
1340 Set the data type of the Item to be longlong.
1341 Maximum display width is set to be the maximum of a 64-bit integer,
1342 but it may be adjusted later. The unsigned property is not affected.
1343 */
1347 fix_char_length(21);
1348 }
1349
1350 /**
1351 Set the data type of the Item to be decimal.
1352 The unsigned property must have been set before calling this function.
1353
1354 @param precision Number of digits of precision
1355 @param scale Number of digits after decimal point.
1356 */
1357 inline void set_data_type_decimal(uint8 precision, uint8 scale) {
1360 decimals = scale;
1362 precision, scale, unsigned_flag));
1363 }
1364
1365 /// Set the data type of the Item to be double precision floating point.
1366 inline void set_data_type_double() {
1371 }
1372
1373 /// Set the data type of the Item to be single precision floating point.
1374 inline void set_data_type_float() {
1379 }
1380
1381 /**
1382 Set the Item to be variable length string. Actual type is determined from
1383 maximum string size. Collation must have been set before calling function.
1384
1385 @param max_l Maximum number of characters in string
1386 */
1387 inline void set_data_type_string(uint32 max_l) {
1394 else
1396 }
1397
1398 /**
1399 Set the Item to be variable length string. Like function above, but with
1400 larger string length precision.
1401
1402 @param max_char_length_arg Maximum number of characters in string
1403 */
1404 inline void set_data_type_string(ulonglong max_char_length_arg) {
1405 ulonglong max_result_length =
1406 max_char_length_arg * collation.collation->mbmaxlen;
1407 if (max_result_length > MAX_BLOB_WIDTH) {
1408 max_result_length = MAX_BLOB_WIDTH;
1409 m_nullable = true;
1410 }
1412 uint32(max_result_length / collation.collation->mbmaxlen));
1413 }
1414
1415 /**
1416 Set the Item to be variable length string. Like function above, but will
1417 also set character set and collation.
1418
1419 @param max_l Maximum number of characters in string
1420 @param cs Pointer to character set and collation struct
1421 */
1422 inline void set_data_type_string(uint32 max_l, const CHARSET_INFO *cs) {
1424 set_data_type_string(max_l);
1425 }
1426
1427 /**
1428 Set the Item to be variable length string. Like function above, but will
1429 also set full collation information.
1430
1431 @param max_l Maximum number of characters in string
1432 @param coll Ref to collation data, including derivation and repertoire
1433 */
1434 inline void set_data_type_string(uint32 max_l, const DTCollation &coll) {
1435 collation.set(coll);
1436 set_data_type_string(max_l);
1437 }
1438
1439 /**
1440 Set the Item to be fixed length string. Collation must have been set
1441 before calling function.
1442
1443 @param max_l Number of characters in string
1444 */
1445 inline void set_data_type_char(uint32 max_l) {
1446 assert(max_l <= MAX_CHAR_WIDTH);
1450 }
1451
1452 /**
1453 Set the Item to be fixed length string. Like function above, but will
1454 also set character set and collation.
1455
1456 @param max_l Maximum number of characters in string
1457 @param cs Pointer to character set and collation struct
1458 */
1459 inline void set_data_type_char(uint32 max_l, const CHARSET_INFO *cs) {
1461 set_data_type_char(max_l);
1462 }
1463
1464 /**
1465 Set the Item to be of BLOB type.
1466
1467 @param max_l Maximum number of bytes in data type
1468 */
1469 inline void set_data_type_blob(uint32 max_l) {
1471 max_length = max_l;
1473 }
1474
1475 /// Set all type properties for Item of DATE type.
1476 inline void set_data_type_date() {
1479 decimals = 0;
1481 }
1482
1483 /**
1484 Set all type properties for Item of TIME type.
1485
1486 @param fsp Fractional seconds precision
1487 */
1488 inline void set_data_type_time(uint8 fsp) {
1491 decimals = fsp;
1492 max_length = MAX_TIME_WIDTH + fsp + (fsp > 0 ? 1 : 0);
1493 }
1494
1495 /**
1496 Set all properties for Item of DATETIME type.
1497
1498 @param fsp Fractional seconds precision
1499 */
1503 decimals = fsp;
1504 max_length = MAX_DATETIME_WIDTH + fsp + (fsp > 0 ? 1 : 0);
1505 }
1506
1507 /**
1508 Set all properties for Item of TIMESTAMP type.
1509
1510 @param fsp Fractional seconds precision
1511 */
1515 decimals = fsp;
1516 max_length = MAX_DATETIME_WIDTH + fsp + (fsp > 0 ? 1 : 0);
1517 }
1518
1519 /**
1520 Set the data type of the Item to be GEOMETRY.
1521 */
1527 }
1528 /**
1529 Set the data type of the Item to be JSON.
1530 */
1536 }
1537
1538 /**
1539 Set the data type of the Item to be YEAR.
1540 */
1544 fix_char_length(5); // YYYY + sign
1545 }
1546
1547 /**
1548 Set the data type of the Item to be bit.
1549 */
1553 fix_char_length(21);
1554 unsigned_flag = true;
1555 }
1556
1557 /**
1558 Set data type properties of the item from the properties of another item.
1559
1560 @param item Item to set data type properties from.
1561 */
1562 inline void set_data_type_from_item(const Item *item) {
1563 set_data_type(item->data_type());
1564 collation = item->collation;
1565 max_length = item->max_length;
1566 decimals = item->decimals;
1568 }
1569
1570 /**
1571 Determine correct string field type, based on string length
1572
1573 @param max_bytes Maximum string size, in number of bytes
1574 */
1576 if (max_bytes > Field::MAX_MEDIUM_BLOB_WIDTH)
1577 return MYSQL_TYPE_LONG_BLOB;
1578 else if (max_bytes > Field::MAX_VARCHAR_WIDTH)
1580 else
1581 return MYSQL_TYPE_VARCHAR;
1582 }
1583
1584 /// Get the typelib information for an item of type set or enum
1585 virtual TYPELIB *get_typelib() const { return nullptr; }
1586
1587 virtual Item_result cast_to_int_type() const { return result_type(); }
1588 virtual enum Type type() const = 0;
1589
1591
1592 /*
1593 Return information about function monotonicity. See comment for
1594 enum_monotonicity_info for details. This function can only be called
1595 after fix_fields() call.
1596 */
1598 return NON_MONOTONIC;
1599 }
1600
1601 /*
1602 Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$
1603 const2"
1604
1605 SYNOPSIS
1606 val_int_endpoint()
1607 left_endp false <=> The interval is "x < const" or "x <= const"
1608 true <=> The interval is "x > const" or "x >= const"
1609
1610 incl_endp IN false <=> the comparison is '<' or '>'
1611 true <=> the comparison is '<=' or '>='
1612 OUT The same but for the "F(x) $CMP$ F(const)" comparison
1613
1614 DESCRIPTION
1615 This function is defined only for unary monotonic functions. The caller
1616 supplies the source half-interval
1617
1618 x $CMP$ const
1619
1620 The value of const is supplied implicitly as the value this item's
1621 argument, the form of $CMP$ comparison is specified through the
1622 function's arguments. The calle returns the result interval
1623
1624 F(x) $CMP2$ F(const)
1625
1626 passing back F(const) as the return value, and the form of $CMP2$
1627 through the out parameter. NULL values are assumed to be comparable and
1628 be less than any non-NULL values.
1629
1630 RETURN
1631 The output range bound, which equal to the value of val_int()
1632 - If the value of the function is NULL then the bound is the
1633 smallest possible value of LLONG_MIN
1634 */
1635 virtual longlong val_int_endpoint(bool left_endp [[maybe_unused]],
1636 bool *incl_endp [[maybe_unused]]) {
1637 assert(0);
1638 return 0;
1639 }
1640
1641 /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
1642 /*
1643 Return double precision floating point representation of item.
1644
1645 SYNOPSIS
1646 val_real()
1647
1648 RETURN
1649 In case of NULL value return 0.0 and set null_value flag to true.
1650 If value is not null null_value flag will be reset to false.
1651 */
1652 virtual double val_real() = 0;
1653 /*
1654 Return integer representation of item.
1655
1656 SYNOPSIS
1657 val_int()
1658
1659 RETURN
1660 In case of NULL value return 0 and set null_value flag to true.
1661 If value is not null null_value flag will be reset to false.
1662 */
1663 virtual longlong val_int() = 0;
1664 /**
1665 Return date value of item in packed longlong format.
1666 */
1667 virtual longlong val_date_temporal();
1668 /**
1669 Return time value of item in packed longlong format.
1670 */
1671 virtual longlong val_time_temporal();
1672
1673 /**
1674 Return date or time value of item in packed longlong format,
1675 depending on item field type.
1676 */
1678 if (data_type() == MYSQL_TYPE_TIME) return val_time_temporal();
1679 assert(is_temporal_with_date());
1680 return val_date_temporal();
1681 }
1682
1683 /**
1684 Produces a key suitable for filesort. Most of the time, val_int() would
1685 suffice, but for temporal values, the packed value (as sent to the handler)
1686 is called for. It is also necessary that the value is in UTC. This function
1687 supplies just that.
1688
1689 @return A sort key value.
1690 */
1694 return val_int();
1695 }
1696
1697 /**
1698 Get date or time value in packed longlong format.
1699 Before conversion from MYSQL_TIME to packed format,
1700 the MYSQL_TIME value is rounded to "dec" fractional digits.
1701 */
1703
1704 /*
1705 This is just a shortcut to avoid the cast. You should still use
1706 unsigned_flag to check the sign of the item.
1707 */
1708 inline ulonglong val_uint() { return (ulonglong)val_int(); }
1709 /*
1710 Return string representation of this item object.
1711
1712 SYNOPSIS
1713 val_str()
1714 str an allocated buffer this or any nested Item object can use to
1715 store return value of this method.
1716
1717 NOTE
1718 Buffer passed via argument should only be used if the item itself
1719 doesn't have an own String buffer. In case when the item maintains
1720 it's own string buffer, it's preferable to return it instead to
1721 minimize number of mallocs/memcpys.
1722 The caller of this method can modify returned string, but only in case
1723 when it was allocated on heap, (is_alloced() is true). This allows
1724 the caller to efficiently use a buffer allocated by a child without
1725 having to allocate a buffer of it's own. The buffer, given to
1726 val_str() as argument, belongs to the caller and is later used by the
1727 caller at it's own choosing.
1728 A few implications from the above:
1729 - unless you return a string object which only points to your buffer
1730 but doesn't manages it you should be ready that it will be
1731 modified.
1732 - even for not allocated strings (is_alloced() == false) the caller
1733 can change charset (see Item_func_{typecast/binary}. XXX: is this
1734 a bug?
1735 - still you should try to minimize data copying and return internal
1736 object whenever possible.
1737
1738 RETURN
1739 In case of NULL value or error, return error_str() as this function will
1740 check if the return value may be null, and it will either set null_value
1741 to true and return nullptr or to false and it will return empty string.
1742 If value is not null set null_value flag to false before returning it.
1743 */
1744 virtual String *val_str(String *str) = 0;
1745
1746 /*
1747 Returns string representation of this item in ASCII format.
1748
1749 SYNOPSIS
1750 val_str_ascii()
1751 str - similar to val_str();
1752
1753 NOTE
1754 This method is introduced for performance optimization purposes.
1755
1756 1. val_str() result of some Items in string context
1757 depends on @@character_set_results.
1758 @@character_set_results can be set to a "real multibyte" character
1759 set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
1760 below for convenience.)
1761
1762 So the default string result of such functions
1763 in these circumstances is real multi-byte character set, like UTF32.
1764
1765 For example, all numbers in string context
1766 return result in @@character_set_results:
1767
1768 SELECT CONCAT(20010101); -> UTF32
1769
1770 We do sprintf() first (to get ASCII representation)
1771 and then convert to UTF32;
1772
1773 So these kind "data sources" can use ASCII representation
1774 internally, but return multi-byte data only because
1775 @@character_set_results wants so.
1776 Therefore, conversion from ASCII to UTF32 is applied internally.
1777
1778
1779 2. Some other functions need in fact ASCII input.
1780
1781 For example,
1782 inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
1783
1784 Similar, fields of certain type, like DATE, TIME,
1785 when you insert string data into them, expect in fact ASCII input.
1786 If they get non-ASCII input, for example UTF32, they
1787 convert input from UTF32 to ASCII, and then use ASCII
1788 representation to do further processing.
1789
1790
1791 3. Now imagine we pass result of a data source of the first type
1792 to a data destination of the second type.
1793
1794 What happens:
1795 a. data source converts data from ASCII to UTF32, because
1796 @@character_set_results wants so and passes the result to
1797 data destination.
1798 b. data destination gets UTF32 string.
1799 c. data destination converts UTF32 string to ASCII,
1800 because it needs ASCII representation to be able to handle data
1801 correctly.
1802
1803 As a result we get two steps of unnecessary conversion:
1804 From ASCII to UTF32, then from UTF32 to ASCII.
1805
1806 A better way to handle these situations is to pass ASCII
1807 representation directly from the source to the destination.
1808
1809 This is why val_str_ascii() introduced.
1810
1811 RETURN
1812 Similar to val_str()
1813 */
1814 virtual String *val_str_ascii(String *str);
1815
1816 /*
1817 Return decimal representation of item with fixed point.
1818
1819 SYNOPSIS
1820 val_decimal()
1821 decimal_buffer buffer which can be used by Item for returning value
1822 (but can be not)
1823
1824 NOTE
1825 Returned value should not be changed if it is not the same which was
1826 passed via argument.
1827
1828 RETURN
1829 Return pointer on my_decimal (it can be other then passed via argument)
1830 if value is not NULL (null_value flag will be reset to false).
1831 In case of NULL value it return 0 pointer and set null_value flag
1832 to true.
1833 */
1834 virtual my_decimal *val_decimal(my_decimal *decimal_buffer) = 0;
1835 /*
1836 Return boolean value of item.
1837
1838 RETURN
1839 false value is false or NULL
1840 true value is true (not equal to 0)
1841 */
1842 virtual bool val_bool();
1843
1844 /**
1845 Get a JSON value from an Item.
1846
1847 All subclasses that can return a JSON value, should override this
1848 function. The function in the base class is not expected to be
1849 called. If it is called, it most likely means that some subclass
1850 is missing an override of val_json().
1851
1852 @param[in,out] result The resulting Json_wrapper.
1853
1854 @return false if successful, true on failure
1855 */
1856 /* purecov: begin deadcode */
1857 virtual bool val_json(Json_wrapper *result [[maybe_unused]]) {
1858 assert(false);
1859 my_error(ER_NOT_SUPPORTED_YET, MYF(0), "item type for JSON");
1860 return error_json();
1861 }
1862 /* purecov: end */
1863
1864 /**
1865 Calculate the filter contribution that is relevant for table
1866 'filter_for_table' for this item.
1867
1868 @param thd Thread handler
1869 @param filter_for_table The table we are calculating filter effect for
1870 @param read_tables Tables earlier in the join sequence.
1871 Predicates for table 'filter_for_table' that
1872 rely on values from these tables can be part of
1873 the filter effect.
1874 @param fields_to_ignore Fields in 'filter_for_table' that should not
1875 be part of the filter calculation. The filtering
1876 effect of these fields is already part of the
1877 calculation somehow (e.g. because there is a
1878 predicate "col = <const>", and the optimizer
1879 has decided to do ref access on 'col').
1880 @param rows_in_table The number of rows in table 'filter_for_table'
1881
1882 @return the filtering effect (between 0 and 1) this
1883 Item contributes with.
1884 */
1885 virtual float get_filtering_effect(THD *thd [[maybe_unused]],
1886 table_map filter_for_table
1887 [[maybe_unused]],
1888 table_map read_tables [[maybe_unused]],
1889 const MY_BITMAP *fields_to_ignore
1890 [[maybe_unused]],
1891 double rows_in_table [[maybe_unused]]) {
1892 // Filtering effect cannot be calculated for a table already read.
1893 assert((read_tables & filter_for_table) == 0);
1894 return COND_FILTER_ALLPASS;
1895 }
1896
1897 /**
1898 Get the value to return from val_json() in case of errors.
1899
1900 @see Item::error_bool
1901
1902 @return The value val_json() should return, which is true.
1903 */
1904 bool error_json() {
1906 return true;
1907 }
1908
1909 /**
1910 Convert a non-temporal type to date
1911 */
1913
1914 /**
1915 Convert a non-temporal type to time
1916 */
1918
1919 protected:
1920 /* Helper functions, see item_sum.cc */
1937 double val_real_from_decimal();
1938 double val_real_from_string();
1939
1940 /**
1941 Get the value to return from val_bool() in case of errors.
1942
1943 This function is called from val_bool() when an error has occurred
1944 and we need to return something to abort evaluation of the
1945 item. The expected pattern in val_bool() is
1946
1947 if (@<error condition@>)
1948 {
1949 my_error(...)
1950 return error_bool();
1951 }
1952
1953 @return The value val_bool() should return.
1954 */
1955 bool error_bool() {
1957 return false;
1958 }
1959
1960 /**
1961 Get the value to return from val_int() in case of errors.
1962
1963 @see Item::error_bool
1964
1965 @return The value val_int() should return.
1966 */
1969 return 0;
1970 }
1971
1972 /**
1973 Get the value to return from val_real() in case of errors.
1974
1975 @see Item::error_bool
1976
1977 @return The value val_real() should return.
1978 */
1979 double error_real() {
1981 return 0.0;
1982 }
1983
1984 public:
1985 /**
1986 Get the value to return from val_decimal() in case of errors.
1987
1988 @see Item::error_decimal
1989
1990 @return The value val_decimal() should return.
1991 */
1994 if (null_value) return nullptr;
1995 my_decimal_set_zero(decimal_value);
1996 return decimal_value;
1997 }
1998
1999 /**
2000 Get the value to return from val_str() in case of errors.
2001
2002 @see Item::error_bool
2003
2004 @return The value val_str() should return.
2005 */
2009 }
2010
2011 protected:
2012 /**
2013 Gets the value to return from val_str() when returning a NULL value.
2014 @return The value val_str() should return.
2015 */
2017 assert(m_nullable);
2018 null_value = true;
2019 return nullptr;
2020 }
2021
2022 /**
2023 Convert val_str() to date in MYSQL_TIME
2024 */
2026 /**
2027 Convert val_real() to date in MYSQL_TIME
2028 */
2030 /**
2031 Convert val_decimal() to date in MYSQL_TIME
2032 */
2034 /**
2035 Convert val_int() to date in MYSQL_TIME
2036 */
2038 /**
2039 Convert get_time() from time to date in MYSQL_TIME
2040 */
2041 bool get_date_from_time(MYSQL_TIME *ltime);
2042
2043 /**
2044 Convert a numeric type to date
2045 */
2046 bool get_date_from_numeric(MYSQL_TIME *ltime, my_time_flags_t fuzzydate);
2047
2048 /**
2049 Convert val_str() to time in MYSQL_TIME
2050 */
2051 bool get_time_from_string(MYSQL_TIME *ltime);
2052 /**
2053 Convert val_real() to time in MYSQL_TIME
2054 */
2055 bool get_time_from_real(MYSQL_TIME *ltime);
2056 /**
2057 Convert val_decimal() to time in MYSQL_TIME
2058 */
2059 bool get_time_from_decimal(MYSQL_TIME *ltime);
2060 /**
2061 Convert val_int() to time in MYSQL_TIME
2062 */
2063 bool get_time_from_int(MYSQL_TIME *ltime);
2064 /**
2065 Convert date to time
2066 */
2067 bool get_time_from_date(MYSQL_TIME *ltime);
2068 /**
2069 Convert datetime to time
2070 */
2072
2073 /**
2074 Convert a numeric type to time
2075 */
2076 bool get_time_from_numeric(MYSQL_TIME *ltime);
2077
2079
2081
2082 public:
2086
2087 /**
2088 If this Item is being materialized into a temporary table, returns the
2089 field that is being materialized into. (Typically, this is the
2090 “result_field” members for items that have one.)
2091 */
2093 DBUG_TRACE;
2094 return nullptr;
2095 }
2096 /* This is also used to create fields in CREATE ... SELECT: */
2097 virtual Field *tmp_table_field(TABLE *) { return nullptr; }
2098 virtual const char *full_name() const {
2099 return item_name.is_set() ? item_name.ptr() : "???";
2100 }
2101
2102 /* bit map of tables used by item */
2103 virtual table_map used_tables() const { return (table_map)0L; }
2104
2105 /**
2106 Return table map of tables that can't be NULL tables (tables that are
2107 used in a context where if they would contain a NULL row generated
2108 by a LEFT or RIGHT join, the item would not be true).
2109 This expression is used on WHERE item to determinate if a LEFT JOIN can be
2110 converted to a normal join.
2111 Generally this function should return used_tables() if the function
2112 would return null if any of the arguments are null
2113 As this is only used in the beginning of optimization, the value don't
2114 have to be updated in update_used_tables()
2115 */
2116 virtual table_map not_null_tables() const { return used_tables(); }
2117
2118 /**
2119 Returns true if this is a simple constant item like an integer, not
2120 a constant expression. Used in the optimizer to propagate basic constants.
2121 It is assumed that val_xxx() does not modify the item's state for
2122 such items. It is also assumed that val_str() can be called with nullptr
2123 as argument as val_str() will return an internally cached const string.
2124 */
2125 virtual bool basic_const_item() const { return false; }
2126 /**
2127 @returns true when a const item may be evaluated during resolving.
2128 Only const items that are basic const items are evaluated when
2129 resolving CREATE VIEW statements. For other statements, all
2130 const items may be evaluated during resolving.
2131 */
2132 bool may_eval_const_item(const THD *thd) const;
2133 /**
2134 @return cloned item if it is constant
2135 @retval nullptr if this is not const
2136 */
2137 virtual Item *clone_item() const { return nullptr; }
2138 virtual cond_result eq_cmp_result() const { return COND_OK; }
2139 inline uint float_length(uint decimals_par) const {
2140 return decimals != DECIMAL_NOT_SPECIFIED ? (DBL_DIG + 2 + decimals_par)
2141 : DBL_DIG + 8;
2142 }
2143 virtual uint decimal_precision() const;
2144 inline int decimal_int_part() const {
2146 }
2147 /**
2148 TIME precision of the item: 0..6
2149 */
2150 virtual uint time_precision();
2151 /**
2152 DATETIME precision of the item: 0..6
2153 */
2154 virtual uint datetime_precision();
2155 /**
2156 Returns true if item is constant, regardless of query evaluation state.
2157 An expression is constant if it:
2158 - refers no tables.
2159 - refers no subqueries that refers any tables.
2160 - refers no non-deterministic functions.
2161 - refers no statement parameters.
2162 - contains no group expression under rollup
2163 */
2164 bool const_item() const { return (used_tables() == 0); }
2165 /**
2166 Returns true if item is constant during one query execution.
2167 If const_for_execution() is true but const_item() is false, value is
2168 not available before tables have been locked and parameters have been
2169 assigned values. This applies to
2170 - statement parameters
2171 - non-dependent subqueries
2172 - deterministic stored functions that contain SQL code.
2173 For items where the default implementation of used_tables() and
2174 const_item() are effective, const_item() will always return true.
2175 */
2176 bool const_for_execution() const {
2177 return !(used_tables() & ~INNER_TABLE_BIT);
2178 }
2179
2180 /**
2181 Return true if this is a const item that may be evaluated in
2182 the current phase of statement processing.
2183 - No evaluation is performed when analyzing a view, otherwise:
2184 - Items that have the const_item() property can always be evaluated.
2185 - Items that have the const_for_execution() property can be evaluated when
2186 tables are locked (ie during optimization or execution).
2187
2188 This function should be used in the following circumstances:
2189 - during preparation to check whether an item can be permanently transformed
2190 - to check that an item is constant in functions that may be used in both
2191 the preparation and optimization phases.
2192
2193 This function should not be used by code that is called during optimization
2194 and/or execution only. Use const_for_execution() in this case.
2195 */
2196 bool may_evaluate_const(const THD *thd) const;
2197
2198 /**
2199 @returns true if this item is non-deterministic, which means that a
2200 has a component that must be evaluated once per row in
2201 execution of a JOIN query.
2202 */
2204
2205 /**
2206 @returns true if this item is an outer reference, usually this means that
2207 it references a column that contained in a table located in
2208 the FROM clause of an outer query block.
2209 */
2210 bool is_outer_reference() const {
2212 }
2213
2214 /**
2215 This method is used for to:
2216 - to generate a view definition query (SELECT-statement);
2217 - to generate a SQL-query for EXPLAIN EXTENDED;
2218 - to generate a SQL-query to be shown in INFORMATION_SCHEMA;
2219 - to generate a SQL-query that looks like a prepared statement for
2220 query_rewrite
2221 - debug.
2222
2223 For more information about view definition query, INFORMATION_SCHEMA
2224 query and why they should be generated from the Item-tree, @see
2225 mysql_register_view().
2226 */
2227 virtual void print(const THD *, String *str, enum_query_type) const {
2228 str->append(full_name());
2229 }
2230
2231 void print_item_w_name(const THD *thd, String *,
2232 enum_query_type query_type) const;
2233 /**
2234 Prints the item when it's part of ORDER BY and GROUP BY.
2235 @param thd Thread handle
2236 @param str String to print to
2237 @param query_type How to format the item
2238 @param used_alias Whether item was referenced with alias.
2239 */
2240 void print_for_order(const THD *thd, String *str, enum_query_type query_type,
2241 bool used_alias) const;
2242
2243 /**
2244 Updates used tables, not null tables information and accumulates
2245 properties up the item tree, cf. used_tables_cache, not_null_tables_cache
2246 and m_accum_properties.
2247
2248 TODO(sgunders): Consider just removing these caches; it causes a lot of bugs
2249 (cache invalidation is known to be a complex problem), and the performance
2250 benefits are dubious.
2251 */
2252 virtual void update_used_tables() {}
2253
2255 }
2256 /* Called for items that really have to be split */
2257 void split_sum_func2(THD *thd, Ref_item_array ref_item_array,
2258 mem_root_deque<Item *> *fields, Item **ref,
2259 bool skip_registered);
2260 virtual bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
2261 virtual bool get_time(MYSQL_TIME *ltime) = 0;
2262 /**
2263 Get timestamp in "struct timeval" format.
2264 @retval false on success
2265 @retval true on error
2266 */
2267 virtual bool get_timeval(my_timeval *tm, int *warnings);
2268 /**
2269 The method allows to determine nullness of a complex expression
2270 without fully evaluating it, instead of calling val*() then
2271 checking null_value. Used in Item_func_isnull/Item_func_isnotnull
2272 and Item_sum_count/Item_sum_count_distinct.
2273 Any item which can be NULL must implement this method.
2274
2275 @retval false if the expression is not NULL.
2276 @retval true if the expression is NULL, or evaluation caused an error.
2277 The null_value member is set according to the return value.
2278 */
2279 virtual bool is_null() { return false; }
2280
2281 /**
2282 Make sure the null_value member has a correct value.
2283 null_value is set true also when evaluation causes error.
2284
2285 @returns false if success, true if error
2286 */
2287 bool update_null_value();
2288
2289 /**
2290 Apply the IS TRUE truth property, meaning that an UNKNOWN result and a
2291 FALSE result are treated the same.
2292
2293 This property is applied e.g to all conditions in WHERE, HAVING and ON
2294 clauses, and is recursively applied to operands of AND, OR
2295 operators. Some items (currently AND and subquery predicates) may enable
2296 special optimizations when they have this property.
2297 */
2298 virtual void apply_is_true() {}
2299 /*
2300 set field of temporary table for Item which can be switched on temporary
2301 table during query processing (grouping and so on). @see
2302 Item_result_field.
2303 */
2304 virtual void set_result_field(Field *) {}
2305 virtual bool is_result_field() const { return false; }
2306 virtual Field *get_result_field() const { return nullptr; }
2307 virtual bool is_bool_func() const { return false; }
2308 /*
2309 Set value of aggregate function in case of no rows for grouping were found.
2310 Also used for subqueries with outer references in SELECT list.
2311 */
2312 virtual void no_rows_in_result() {}
2313 virtual Item *copy_or_same(THD *) { return this; }
2314 virtual Item *copy_andor_structure(THD *) { return this; }
2315 /**
2316 @returns the "real item" underlying the owner object. Used to strip away
2317 Item_ref objects.
2318 @note remember to implement both real_item() functions in sub classes!
2319 */
2320 virtual Item *real_item() { return this; }
2321 virtual const Item *real_item() const { return this; }
2322 /**
2323 If an Item is materialized in a temporary table, a different Item may have
2324 to be used in the part of the query that runs after the materialization.
2325 For instance, if the Item was an Item_field, the new Item_field needs to
2326 point into the temporary table instead of the original one, but if, on the
2327 other hand, the Item was a literal constant, it can be reused as-is.
2328 This function encapsulates these policies for the different kinds of Items.
2329 See also get_tmp_table_field().
2330
2331 TODO: Document how aggregate functions (Item_sum) are handled.
2332 */
2333 virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
2334
2335 static const CHARSET_INFO *default_charset();
2336 virtual const CHARSET_INFO *compare_collation() const { return nullptr; }
2337
2338 /*
2339 For backward compatibility, to make numeric
2340 data types return "binary" charset in client-side metadata.
2341 */
2344 : &my_charset_bin;
2345 }
2346
2347 /**
2348 Traverses a tree of Items in prefix and/or postfix order.
2349 Optionally walks into subqueries.
2350
2351 @param processor processor function to be invoked per item
2352 returns true to abort traversal, false to continue
2353 @param walk controls how to traverse the item tree
2354 enum_walk::PREFIX: call processor before invoking
2355 children enum_walk::POSTFIX: call processor after invoking children
2356 enum_walk::SUBQUERY go down into subqueries
2357 walk values are bit-coded and may be combined.
2358 Omitting both enum_walk::PREFIX and enum_walk::POSTFIX
2359 is undefined behaviour.
2360 @param arg Optional pointer to a walk-specific object
2361
2362 @retval false walk succeeded
2363 @retval true walk aborted
2364 by agreement, an error may have been reported
2365 */
2366
2367 virtual bool walk(Item_processor processor, enum_walk walk [[maybe_unused]],
2368 uchar *arg) {
2369 return (this->*processor)(arg);
2370 }
2371
2372 /** @see WalkItem, CompileItem */
2373 template <class T>
2375 return (*reinterpret_cast<std::remove_reference_t<T> *>(arg))(this);
2376 }
2377
2378 /** See CompileItem */
2379 template <class T>
2381 return (*reinterpret_cast<std::remove_reference_t<T> *>(*arg))(this);
2382 }
2383
2384 /**
2385 Perform a generic transformation of the Item tree, by adding zero or
2386 more additional Item objects to it.
2387
2388 @param transformer Transformer function
2389 @param[in,out] arg Pointer to struct used by transformer function
2390
2391 @returns Returned item tree after transformation, NULL if error
2392
2393 Transformation is performed as follows:
2394
2395 @code
2396 transform()
2397 {
2398 transform children if any;
2399 return this->*some_transformer(...);
2400 }
2401 @endcode
2402
2403 Note that unlike Item::compile(), transform() does not support an analyzer
2404 function, ie. all children are unconditionally invoked.
2405
2406 Item::transform() should handle all transformations during preparation.
2407 Notice that all transformations are permanent; they are not rolled back.
2408
2409 Use Item::compile() to perform transformations during optimization.
2410 */
2411 virtual Item *transform(Item_transformer transformer, uchar *arg);
2412
2413 /**
2414 Perform a generic "compilation" of the Item tree, ie transform the Item tree
2415 by adding zero or more Item objects to it.
2416
2417 @param analyzer Analyzer function, see details section
2418 @param[in,out] arg_p Pointer to struct used by analyzer function
2419 @param transformer Transformer function, see details section
2420 @param[in,out] arg_t Pointer to struct used by transformer function
2421
2422 @returns Returned item tree after transformation, NULL if error
2423
2424 The process of this transformation is assumed to be as follows:
2425
2426 @code
2427 compile()
2428 {
2429 if (this->*some_analyzer(...))
2430 {
2431 compile children if any;
2432 return this->*some_transformer(...);
2433 }
2434 else
2435 return this;
2436 }
2437 @endcode
2438
2439 i.e. analysis is performed top-down while transformation is done
2440 bottom-up. If no transformation is applied, the item is returned unchanged.
2441 A transformation error is indicated by returning a NULL pointer. Notice
2442 that the analyzer function should never cause an error.
2443
2444 The function is supposed to be used during the optimization stage of
2445 query execution. All new allocations are recorded using
2446 THD::change_item_tree() so that they can be rolled back after execution.
2447
2448 @todo Pass THD to compile() function, thus no need to use current_thd.
2449 */
2450 virtual Item *compile(Item_analyzer analyzer, uchar **arg_p,
2451 Item_transformer transformer, uchar *arg_t) {
2452 if ((this->*analyzer)(arg_p)) return ((this->*transformer)(arg_t));
2453 return this;
2454 }
2455
2456 virtual void traverse_cond(Cond_traverser traverser, void *arg,
2458 (*traverser)(this, arg);
2459 }
2460
2461 /*
2462 This is used to get the most recent version of any function in
2463 an item tree. The version is the version where a MySQL function
2464 was introduced in. So any function which is added should use
2465 this function and set the int_arg to maximum of the input data
2466 and their own version info.
2467 */
2468 virtual bool intro_version(uchar *) { return false; }
2469
2470 /// cleanup() item if it is resolved ('fixed').
2472 if (fixed) cleanup();
2473 return false;
2474 }
2475
2476 virtual bool collect_item_field_processor(uchar *) { return false; }
2477 virtual bool collect_item_field_or_ref_processor(uchar *) { return false; }
2478
2480 public:
2483 : m_items(fields_or_refs) {}
2486 const Collect_item_fields_or_refs &) = delete;
2487
2488 friend class Item_sum;
2489 friend class Item_field;
2490 friend class Item_ref;
2491 };
2492
2494 public:
2498 Query_block *transformed_block)
2499 : m_item_fields_or_view_refs(fields_or_vr),
2500 m_transformed_block(transformed_block) {}
2502 delete;
2504 const Collect_item_fields_or_view_refs &) = delete;
2505
2506 friend class Item_sum;
2507 friend class Item_field;
2508 friend class Item_view_ref;
2509 };
2510
2511 /**
2512 Collects fields and view references that have the qualifying table
2513 in the specified query block.
2514 */
2516 return false;
2517 }
2518
2519 /**
2520 Item::walk function. Set bit in table->tmp_set for all fields in
2521 table 'arg' that are referred to by the Item.
2522 */
2523 virtual bool add_field_to_set_processor(uchar *) { return false; }
2524
2525 /// A processor to handle the select lex visitor framework.
2526 virtual bool visitor_processor(uchar *arg);
2527
2528 /**
2529 Item::walk function. Set bit in table->cond_set for all fields of
2530 all tables that are referred to by the Item.
2531 */
2532 virtual bool add_field_to_cond_set_processor(uchar *) { return false; }
2533
2534 /**
2535 Visitor interface for removing all column expressions (Item_field) in
2536 this expression tree from a bitmap. @see walk()
2537
2538 @param arg A MY_BITMAP* cast to unsigned char*, where the bits represent
2539 Field::field_index values.
2540 */
2541 virtual bool remove_column_from_bitmap(uchar *arg [[maybe_unused]]) {
2542 return false;
2543 }
2544 virtual bool find_item_in_field_list_processor(uchar *) { return false; }
2545 virtual bool change_context_processor(uchar *) { return false; }
2546 virtual bool find_item_processor(uchar *arg) { return this == (void *)arg; }
2548 return !basic_const_item();
2549 }
2550 /// Is this an Item_field which references the given Field argument?
2551 virtual bool find_field_processor(uchar *) { return false; }
2552 /// Wrap incompatible arguments in CAST nodes to the expected data types
2553 virtual bool cast_incompatible_args(uchar *) { return false; }
2554 /**
2555 Mark underlying field in read or write map of a table.
2556
2557 @param arg Mark_field object
2558 */
2559 virtual bool mark_field_in_map(uchar *arg [[maybe_unused]]) { return false; }
2560
2561 /// Traverse the item tree and replace fields that are outside of reach with
2562 /// fields that are within reach. This is used by hash join when it detects
2563 /// that a join condition refers to a field that is outside of reach, due to
2564 /// equality propagation. See
2565 /// Item_func::ensure_multi_equality_fields_are_available_walker for more
2566 /// details.
2568 return false;
2569 }
2570
2571 protected:
2572 /**
2573 Helper function for mark_field_in_map(uchar *arg).
2574
2575 @param mark_field Mark_field object
2576 @param field Field to be marked for read/write
2577 */
2578 static inline bool mark_field_in_map(Mark_field *mark_field, Field *field) {
2579 TABLE *table = mark_field->table;
2580 if (table != nullptr && table != field->table) return false;
2581
2582 table = field->table;
2583 table->mark_column_used(field, mark_field->mark);
2584
2585 return false;
2586 }
2587
2588 public:
2589 /**
2590 Reset execution state for such window function types
2591 as determined by arg
2592
2593 @param arg pointing to a bool which, if true, says to reset state
2594 for framing window function, else for non-framing
2595 */
2596 virtual bool reset_wf_state(uchar *arg [[maybe_unused]]) { return false; }
2597
2598 /**
2599 Return used table information for the specified query block (level).
2600 For a field that is resolved from this query block, return the table number.
2601 For a field that is resolved from a query block outer to the specified one,
2602 return OUTER_REF_TABLE_BIT
2603
2604 @param[in,out] arg pointer to an instance of class Used_tables, which is
2605 constructed with the query block as argument.
2606 The used tables information is accumulated in the field
2607 used_tables in this class.
2608
2609 @note This function is used to update used tables information after
2610 merging a query block (a subquery) with its parent.
2611 */
2612 virtual bool used_tables_for_level(uchar *arg [[maybe_unused]]) {
2613 return false;
2614 }
2615 /**
2616 Check privileges.
2617
2618 @param thd thread handle
2619 */
2620 virtual bool check_column_privileges(uchar *thd [[maybe_unused]]) {
2621 return false;
2622 }
2623 virtual bool inform_item_in_cond_of_tab(uchar *) { return false; }
2624 /**
2625 Bind objects from the current execution context to field objects in
2626 item trees. Typically used to bind Field objects from TABLEs to
2627 Item_field objects.
2628 */
2629 virtual void bind_fields() {}
2630
2631 /**
2632 Context object for (functions that override)
2633 Item::clean_up_after_removal().
2634 */
2636 public:
2638 assert(root != nullptr);
2639 }
2640
2642
2643 private:
2644 /**
2645 Pointer to Cleanup_after_removal_context containing from which
2646 select the walk started, i.e., the Query_block that contained the clause
2647 that was removed.
2648 */
2650 };
2651 /**
2652 Clean up after removing the item from the item tree.
2653
2654 param arg pointer to a Cleanup_after_removal_context object
2655 */
2656 virtual bool clean_up_after_removal(uchar *arg [[maybe_unused]]) {
2657 assert(arg != nullptr);
2658 return false;
2659 }
2660
2661 /**
2662 Propagate components that use referenced columns from derived tables.
2663 Some columns from derived tables may be determined to be unused, but
2664 may actually reference other columns that are used. This function will
2665 return true for such columns when called with Item::walk(), which then
2666 means that this column can also be marked as used.
2667 @see also Query_block::delete_unused_merged_columns().
2668 */
2670
2671 /**
2672 Called by Item::walk() to set all the referenced items' derived_used flag.
2673 */
2676 return false;
2677 }
2678
2679 /// @see Distinct_check::check_query()
2680 virtual bool aggregate_check_distinct(uchar *) { return false; }
2681 /// @see Group_check::check_query()
2682 virtual bool aggregate_check_group(uchar *) { return false; }
2683 /// @see Group_check::analyze_conjunct()
2684 virtual bool is_strong_side_column_not_in_fd(uchar *) { return false; }
2685 /// @see Group_check::is_in_fd_of_underlying()
2686 virtual bool is_column_not_in_fd(uchar *) { return false; }
2687 virtual Bool3 local_column(const Query_block *) const {
2688 return Bool3::false3();
2689 }
2690
2691 /**
2692 Minion class under Collect_scalar_subquery_info. Information about one
2693 scalar subquery being considered for transformation
2694 */
2695 struct Css_info {
2696 /// set of locations
2698 /// the scalar subquery
2701 /// Where did we find item above? Used when m_location == L_JOIN_COND,
2702 /// nullptr for other locations.
2704 /// If true, we can forego cardinality checking of the derived table
2706 /// If true, add a COALESCE around replaced subquery: used for implicitly
2707 /// grouped COUNT() in subquery select list when subquery is correlated
2708 bool m_add_coalesce{false};
2709 };
2710
2711 /**
2712 Context struct used by walk method collect_scalar_subqueries to
2713 accumulate information about scalar subqueries found.
2714
2715 In: m_location of expression walked, m_join_condition_context
2716 Out: m_list
2717 */
2719 enum Location { L_SELECT = 1, L_WHERE = 2, L_HAVING = 4, L_JOIN_COND = 8 };
2720 /// accumulated all scalar subqueries found
2721 std::vector<Css_info> m_list;
2722 /// we are currently looking at this kind of clause, cf. enum Location
2727 friend class Item_sum;
2729 };
2730
2731 virtual bool collect_scalar_subqueries(uchar *) { return false; }
2732 virtual bool collect_grouped_aggregates(uchar *) { return false; }
2733 virtual bool collect_subqueries(uchar *) { return false; }
2734 virtual bool update_depended_from(uchar *) { return false; }
2735 /**
2736 Check if an aggregate is referenced from within the GROUP BY
2737 clause of the query block in which it is aggregated. Such
2738 references will be rejected.
2739 @see Item_ref::fix_fields()
2740 @retval true if this is an aggregate which is referenced from
2741 the GROUP BY clause of the aggregating query block
2742 @retval false otherwise
2743 */
2744 virtual bool has_aggregate_ref_in_group_by(uchar *) { return false; }
2745
2746 bool visit_all_analyzer(uchar **) { return true; }
2747 virtual bool cache_const_expr_analyzer(uchar **cache_item);
2749
2750 virtual bool equality_substitution_analyzer(uchar **) { return false; }
2751
2752 virtual Item *equality_substitution_transformer(uchar *) { return this; }
2753
2754 /**
2755 Check if a partition function is allowed.
2756
2757 @return whether a partition function is not accepted
2758
2759 @details
2760 check_partition_func_processor is used to check if a partition function
2761 uses an allowed function. An allowed function will always ensure that
2762 X=Y guarantees that also part_function(X)=part_function(Y) where X is
2763 a set of partition fields and so is Y. The problems comes mainly from
2764 character sets where two equal strings can be quite unequal. E.g. the
2765 german character for double s is equal to 2 s.
2766
2767 The default is that an item is not allowed
2768 in a partition function. Allowed functions
2769 can never depend on server version, they cannot depend on anything
2770 related to the environment. They can also only depend on a set of
2771 fields in the table itself. They cannot depend on other tables and
2772 cannot contain any queries and cannot contain udf's or similar.
2773 If a new Item class is defined and it inherits from a class that is
2774 allowed in a partition function then it is very important to consider
2775 whether this should be inherited to the new class. If not the function
2776 below should be defined in the new Item class.
2777
2778 The general behaviour is that most integer functions are allowed.
2779 If the partition function contains any multi-byte collations then
2780 the function check_part_func_fields will report an error on the
2781 partition function independent of what functions are used. So the
2782 only character sets allowed are single character collation and
2783 even for those only a limited set of functions are allowed. The
2784 problem with multi-byte collations is that almost every string
2785 function has the ability to change things such that two strings
2786 that are equal will not be equal after manipulated by a string
2787 function. E.g. two strings one contains a double s, there is a
2788 special german character that is equal to two s. Now assume a
2789 string function removes one character at this place, then in
2790 one the double s will be removed and in the other there will
2791 still be one s remaining and the strings are no longer equal
2792 and thus the partition function will not sort equal strings into
2793 the same partitions.
2794
2795 So the check if a partition function is valid is two steps. First
2796 check that the field types are valid, next check that the partition
2797 function is valid. The current set of partition functions valid
2798 assumes that there are no multi-byte collations amongst the partition
2799 fields.
2800 */
2801 virtual bool check_partition_func_processor(uchar *) { return true; }
2802 virtual bool subst_argument_checker(uchar **arg) {
2803 if (*arg) *arg = nullptr;
2804 return true;
2805 }
2806 virtual bool explain_subquery_checker(uchar **) { return true; }
2807 virtual Item *explain_subquery_propagator(uchar *) { return this; }
2808
2809 virtual Item *equal_fields_propagator(uchar *) { return this; }
2810 // Mark the item to not be part of substitution.
2811 virtual bool disable_constant_propagation(uchar *) { return false; }
2812 virtual Item *replace_equal_field(uchar *) { return this; }
2813 /*
2814 Check if an expression value has allowed arguments, like DATE/DATETIME
2815 for date functions. Also used by partitioning code to reject
2816 timezone-dependent expressions in a (sub)partitioning function.
2817 */
2818 virtual bool check_valid_arguments_processor(uchar *) { return false; }
2819
2820 /**
2821 Check if this item is allowed for a virtual column or inside a
2822 default expression. Should be overridden in child classes.
2823
2824 @param[in,out] args Due to the limitation of Item::walk()
2825 it is declared as a pointer to uchar, underneath there's a actually a
2826 structure of type Check_function_as_value_generator_parameters.
2827 It is used mainly in Item_field.
2828
2829 @returns true if function is not accepted
2830 */
2831 virtual bool check_function_as_value_generator(uchar *args);
2832
2833 /**
2834 Check if a generated expression depends on DEFAULT function with
2835 specific column name as argument.
2836
2837 @param[in] args Name of column used as DEFAULT function argument.
2838
2839 @returns false if the function is not DEFAULT(args), otherwise true.
2840 */
2842 [[maybe_unused]]) {
2843 return false;
2844 }
2845 /**
2846 Check if all the columns present in this expression are from the
2847 derived table. Used in determining if a condition can be pushed
2848 down to derived table.
2849 */
2850 virtual bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) {
2851 // A generic item cannot be pushed down unless constant.
2852 return !const_item();
2853 }
2854
2855 /**
2856 Check if all the columns present in this expression are present
2857 in PARTITION clause of window functions of the derived table.
2858 Used in checking if a condition can be pushed down to derived table.
2859 */
2860 virtual bool check_column_in_window_functions(uchar *arg [[maybe_unused]]) {
2861 return false;
2862 }
2863 /**
2864 Check if all the columns present in this expression are present
2865 in GROUP BY clause of the derived table. Used in checking if
2866 a condition can be pushed down to derived table.
2867 */
2868 virtual bool check_column_in_group_by(uchar *arg [[maybe_unused]]) {
2869 return false;
2870 }
2871 /**
2872 Assuming this expression is part of a condition that would be pushed to the
2873 WHERE clause of a materialized derived table, replace, in this expression,
2874 each derived table's column with a clone of the expression lying under it
2875 in the derived table's definition. We replace with a clone, because the
2876 condition can be pushed further down in case of nested derived tables.
2877 */
2878 virtual Item *replace_with_derived_expr(uchar *arg [[maybe_unused]]) {
2879 return this;
2880 }
2881 /**
2882 Assuming this expression is part of a condition that would be pushed to the
2883 HAVING clause of a materialized derived table, replace, in this expression,
2884 each derived table's column with a reference to the expression lying under
2885 it in the derived table's definition. Unlike replace_with_derived_expr, a
2886 clone is not used because HAVING condition will not be pushed further
2887 down in case of nested derived tables.
2888 */
2889 virtual Item *replace_with_derived_expr_ref(uchar *arg [[maybe_unused]]) {
2890 return this;
2891 }
2892 /**
2893 Assuming this expression is part of a condition that would be pushed to a
2894 materialized derived table, replace, in this expression, each view reference
2895 with a clone of the expression in merged derived table's definition.
2896 We replace with a clone, because the referenced item in a view reference
2897 is shared by all the view references to that expression.
2898 */
2899 virtual Item *replace_view_refs_with_clone(uchar *arg [[maybe_unused]]) {
2900 return this;
2901 }
2902 /*
2903 For SP local variable returns pointer to Item representing its
2904 current value and pointer to current Item otherwise.
2905 */
2906 virtual Item *this_item() { return this; }
2907 virtual const Item *this_item() const { return this; }
2908
2909 /*
2910 For SP local variable returns address of pointer to Item representing its
2911 current value and pointer passed via parameter otherwise.
2912 */
2913 virtual Item **this_item_addr(THD *, Item **addr_arg) { return addr_arg; }
2914
2915 // Row emulation
2916 virtual uint cols() const { return 1; }
2917 virtual Item *element_index(uint) { return this; }
2918 virtual Item **addr(uint) { return nullptr; }
2919 virtual bool check_cols(uint c);
2920 // It is not row => null inside is impossible
2921 virtual bool null_inside() { return false; }
2922 // used in row subselects to get value of elements
2923 virtual void bring_value() {}
2924
2925 Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length) const;
2926 virtual Item_field *field_for_view_update() { return nullptr; }
2927 /**
2928 Informs an item that it is wrapped in a truth test, in case it wants to
2929 transforms itself to implement this test by itself.
2930 @param thd Thread handle
2931 @param test Truth test
2932 */
2933 virtual Item *truth_transformer(THD *thd [[maybe_unused]],
2934 Bool_test test [[maybe_unused]]) {
2935 return nullptr;
2936 }
2937 virtual Item *update_value_transformer(uchar *) { return this; }
2938
2940 Query_block *m_trans_block; ///< Transformed query block
2941 Query_block *m_curr_block; ///< Transformed query block or a contained
2942 ///< subquery. Pushed when diving into
2943 ///< subqueries.
2944 Item_replacement(Query_block *transformed_block, Query_block *current_block)
2945 : m_trans_block(transformed_block), m_curr_block(current_block) {}
2946 };
2948 Field *m_target; ///< The field to be replaced
2949 Item_field *m_item; ///< The replacement field
2951 : Item_replacement(select, select), m_target(target), m_item(item) {}
2952 };
2953
2955 Item *m_target; ///< The item identifying the view_ref to be replaced
2956 Field *m_field; ///< The replacement field
2957 ///< subquery. Pushed when diving into
2958 ///< subqueries.
2960 : Item_replacement(select, select), m_target(target), m_field(field) {}
2961 };
2962
2967 : m_target(target), m_replacement(replacement) {}
2968 };
2969
2970 /**
2971 When walking the item tree seeing an Item_singlerow_subselect matching
2972 a target, replace it with a substitute field used when transforming
2973 scalar subqueries into derived tables. Cf.
2974 Query_block::transform_scalar_subqueries_to_join_with_derived.
2975 */
2976 virtual Item *replace_scalar_subquery(uchar *) { return this; }
2977
2978 /**
2979 Transform processor used by Query_block::transform_grouped_to_derived
2980 to replace fields which used to be at the transformed query block
2981 with corresponding fields in the new derived table containing the grouping
2982 operation of the original transformed query block.
2983 */
2984 virtual Item *replace_item_field(uchar *) { return this; }
2985 virtual Item *replace_item_view_ref(uchar *) { return this; }
2986 virtual Item *replace_aggregate(uchar *) { return this; }
2987 virtual Item *replace_outer_ref(uchar *) { return this; }
2988
2993 : m_target(target), m_owner(owner) {}
2994 };
2995
2996 /**
2997 A walker processor overridden by Item_aggregate_ref, q.v.
2998 */
2999 virtual bool update_aggr_refs(uchar *) { return false; }
3000
3001 virtual Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs);
3002 /**
3003 Delete this item.
3004 Note that item must have been cleanup up by calling Item::cleanup().
3005 */
3006 void delete_self() { delete this; }
3007
3008 /** @return whether the item is local to a stored procedure */
3009 virtual bool is_splocal() const { return false; }
3010
3011 /*
3012 Return Settable_routine_parameter interface of the Item. Return 0
3013 if this Item is not Settable_routine_parameter.
3014 */
3016 return nullptr;
3017 }
3018 inline bool is_temporal_with_date() const {
3020 }
3023 }
3024 inline bool is_temporal_with_time() const {
3026 }
3027 inline bool is_temporal() const {
3029 }
3030 /**
3031 Check whether this and the given item has compatible comparison context.
3032 Used by the equality propagation. See Item_field::equal_fields_propagator.
3033
3034 @return
3035 true if the context is the same or if fields could be
3036 compared as DATETIME values by the Arg_comparator.
3037 false otherwise.
3038 */
3039 inline bool has_compatible_context(Item *item) const {
3040 /* Same context. */
3042 return true;
3043 /* DATETIME comparison context. */
3045 return item->is_temporal_with_date() ||
3046 item->cmp_context == STRING_RESULT;
3047 if (item->is_temporal_with_date())
3049 return false;
3050 }
3052 return Field::GEOM_GEOMETRY;
3053 }
3054 String *check_well_formed_result(String *str, bool send_error, bool truncate);
3055 bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO *cs);
3056
3057 /*
3058 Test whether an expression is expensive to compute. Used during
3059 optimization to avoid computing expensive expressions during this
3060 phase. Also used to force temp tables when sorting on expensive
3061 functions.
3062 TODO:
3063 Normally we should have a method:
3064 cost Item::execution_cost(),
3065 where 'cost' is either 'double' or some structure of various cost
3066 parameters.
3067 */
3068 virtual bool is_expensive() {
3069 if (is_expensive_cache < 0)
3072 return is_expensive_cache;
3073 }
3074
3075 /**
3076 @return maximum number of characters that this Item can store
3077 If Item is of string or blob type, return max string length in bytes
3078 divided by bytes per character, otherwise return max_length.
3079 @todo - check if collation for other types should have mbmaxlen = 1
3080 */
3082 /*
3083 Length of e.g. 5.5e5 in an expression such as GREATEST(5.5e5, '5') is 5
3084 (length of that string) although length of the actual value is 6.
3085 Return MAX_DOUBLE_STR_LENGTH to prevent truncation of data without having
3086 to evaluate the value of the item.
3087 */
3088 uint32 max_len =
3090 if (result_type() == STRING_RESULT)
3091 return max_len / collation.collation->mbmaxlen;
3092 return max_len;
3093 }
3094
3096 if (cs == &my_charset_bin && result_type() == STRING_RESULT) {
3097 return max_length;
3098 }
3099 return max_char_length();
3100 }
3101
3102 inline void fix_char_length(uint32 max_char_length_arg) {
3103 max_length = char_to_byte_length_safe(max_char_length_arg,
3105 }
3106
3107 /*
3108 Return true if the item points to a column of an outer-joined table.
3109 */
3110 virtual bool is_outer_field() const {
3111 assert(fixed);
3112 return false;
3113 }
3114
3115 /**
3116 Check if an item either is a blob field, or will be represented as a BLOB
3117 field if a field is created based on this item.
3118
3119 @retval true If a field based on this item will be a BLOB field,
3120 @retval false Otherwise.
3121 */
3122 bool is_blob_field() const;
3123
3124 protected:
3125 /// Set accumulated properties for an Item
3126 void set_accum_properties(const Item *item) {
3128 }
3129
3130 /// Add more accumulated properties to an Item
3131 void add_accum_properties(const Item *item) {
3133 }
3134
3135 /// Set the "has subquery" property
3137
3138 /// Set the "has stored program" property
3140
3141 public:
3142 /// @return true if this item or any of its decendents contains a subquery.
3144
3145 /// @return true if this item or any of its decendents refers a stored func.
3146 bool has_stored_program() const {
3148 }
3149
3150 /// @return true if this item or any of its decendents is an aggregated func.
3152
3153 /// Set the "has aggregation" property
3155
3156 /// Reset the "has aggregation" property
3157 void reset_aggregation() { m_accum_properties &= ~PROP_AGGREGATION; }
3158
3159 /// @return true if this item or any of its decendents is a window func.
3161
3162 /// Set the "has window function" property
3164
3165 /**
3166 @return true if this item or any of its decendents within the same query
3167 has a reference to a ROLLUP expression
3168 */
3170
3171 /// Set the property: this item (tree) contains a reference to a ROLLUP expr
3173
3174 /**
3175 @return true if this item or any of underlying items is a GROUPING function
3176 */
3177 bool has_grouping_func() const {
3179 }
3180
3181 /// Set the property: this item is a call to GROUPING
3183
3184 /// Whether this Item was created by the IN->EXISTS subquery transformation
3185 virtual bool created_by_in2exists() const { return false; }
3186
3187 // @return true if an expression in select list of derived table is used
3188 bool is_derived_used() const { return derived_used; }
3189
3191 if (has_subquery())
3193 }
3194
3195 /**
3196 Analyzer function for GC substitution. @see substitute_gc()
3197 */
3198 virtual bool gc_subst_analyzer(uchar **) { return false; }
3199 /**
3200 Transformer function for GC substitution. @see substitute_gc()
3201 */
3202 virtual Item *gc_subst_transformer(uchar *) { return this; }
3203
3204 /**
3205 A processor that replaces any Fields with a Create_field_wrapper. This
3206 will allow us to resolve functions during CREATE TABLE, where we only have
3207 Create_field available and not Field. Used for functional index
3208 implementation.
3209 */
3210 virtual bool replace_field_processor(uchar *) { return false; }
3211 /**
3212 Check if this item is of a type that is eligible for GC
3213 substitution. All items that belong to subclasses of Item_func are
3214 eligible for substitution. @see substitute_gc()
3215 Item_fields can also be eligible if they are given as an argument to
3216 a function that takes an array (the field can be substituted with a
3217 generated column that backs a multi-valued index on that field).
3218
3219 @param array true if the item is an argument to a function that takes an
3220 array, or false otherwise
3221 @return true if the expression is eligible for substitution, false otherwise
3222 */
3223 bool can_be_substituted_for_gc(bool array = false) const;
3224
3225 void aggregate_decimal_properties(Item **item, uint nitems);
3226 void aggregate_float_properties(Item **item, uint nitems);
3227 void aggregate_char_length(Item **args, uint nitems);
3228 void aggregate_temporal_properties(Item **item, uint nitems);
3229 bool aggregate_string_properties(const char *name, Item **item, uint nitems);
3230 void aggregate_num_type(Item_result result_type, Item **item, uint nitems);
3231
3232 /**
3233 This function applies only to Item_field objects referred to by an Item_ref
3234 object that has been marked as a const_item.
3235
3236 @param arg Keep track of whether an Item_ref refers to an Item_field.
3237 */
3238 virtual bool repoint_const_outer_ref(uchar *arg [[maybe_unused]]) {
3239 return false;
3240 }
3241 virtual bool strip_db_table_name_processor(uchar *) { return false; }
3242
3243 private:
3244 virtual bool subq_opt_away_processor(uchar *) { return false; }
3245
3246 // Set an expression from select list of derived table as used.
3248
3249 public: // Start of data fields
3250 /**
3251 Intrusive list pointer for free list. If not null, points to the next
3252 Item on some Query_arena's free list. For instance, stored procedures
3253 have their own Query_arena's.
3254
3255 @see Query_arena::free_list
3256 */
3258
3259 protected:
3260 /// str_values's main purpose is to cache the value in save_in_field
3262
3263 public:
3264 /**
3265 Character set and collation properties assigned for this Item.
3266 Used if Item represents a character string expression.
3267 */
3269 Item_name_string item_name; ///< Name from query
3270 Item_name_string orig_name; ///< Original item name (if it was renamed)
3271 /**
3272 Maximum length of result of evaluating this item, in number of bytes.
3273 - For character or blob data types, max char length multiplied by max
3274 character size (collation.mbmaxlen).
3275 - For decimal type, it is the precision in digits plus sign (unless
3276 unsigned) plus decimal point (unless it has zero decimals).
3277 - For other numeric types, the default or specific display length.
3278 - For date/time types, the display length (10 for DATE, 10 + optional FSP
3279 for TIME, 19 + optional fsp for datetime/timestamp).
3280 - For bit, the number of bits.
3281 - For enum, the string length of the widest enum element.
3282 - For set, the sum of the string length of each set element plus separators.
3283 - For geometry, the maximum size of a BLOB (it's underlying storage type).
3284 - For json, the maximum size of a BLOB (it's underlying storage type).
3285 */
3286 uint32 max_length; ///< Maximum length, in bytes
3287 enum item_marker ///< Values for member 'marker'
3289 /// When contextualization or itemization adds an implicit comparison '0<>'
3290 /// (see make_condition()), to record that this Item_func_ne was created for
3291 /// this purpose; this value is tested during resolution.
3293 /// When doing constant propagation (e.g. change_cond_ref_to_const(), to
3294 /// remember that we have already processed the item.
3296 /// When creating an internal temporary table: says how to store BIT fields.
3298 /// When analyzing functional dependencies for only_full_group_by (says
3299 /// whether a nullable column can be treated at not nullable).
3301 /// When we change DISTINCT to GROUP BY: used for book-keeping of
3302 /// fields.
3304 /// When pushing conditions down to derived table: it says a condition
3305 /// contains only derived table's columns.
3307 /// When pushing index conditions: it says whether a condition uses only
3308 /// indexed columns.
3310 /**
3311 This member has several successive meanings, depending on the phase we're
3312 in (@see item_marker).
3313 The important property is that a phase must have a value (or few values)
3314 which is reserved for this phase. If it wants to set "marked", it assigns
3315 the value; it it wants to test if it is marked, it tests marker !=
3316 value. If the value has been assigned and the phase wants to cancel it can
3317 set marker to MARKER_NONE, which is a magic number which no phase
3318 reserves.
3319 A phase can expect 'marker' to be MARKER_NONE at the start of execution of
3320 a normal statement, at the start of preparation of a PS, and at the start
3321 of execution of a PS.
3322 A phase should not expect marker's value to survive after the phase's
3323 end - as a following phase may change it.
3324 */
3326 Item_result cmp_context; ///< Comparison context
3327 private:
3328 const bool is_parser_item; ///< true if allocated directly by parser
3329 int8 is_expensive_cache; ///< Cache of result of is_expensive()
3330 uint8 m_data_type; ///< Data type assigned to Item
3331 public:
3332 bool fixed; ///< True if item has been resolved
3333 /**
3334 Number of decimals in result when evaluating this item
3335 - For integer type, always zero.
3336 - For decimal type, number of decimals.
3337 - For float type, it may be DECIMAL_NOT_SPECIFIED
3338 - For time, datetime and timestamp, number of decimals in fractional second
3339 - For string types, may be decimals of cast source or DECIMAL_NOT_SPECIFIED
3340 */
3342
3343 bool is_nullable() const { return m_nullable; }
3344 void set_nullable(bool nullable) { m_nullable = nullable; }
3345
3346 private:
3347 /**
3348 True if this item may hold the NULL value(if null_value may be set to true).
3349
3350 For items that represent rows, it is true if one of the columns
3351 may be null.
3352
3353 For items that represent scalar or row subqueries, it is true if
3354 one of the returned columns could be null, or if the subquery
3355 could return zero rows.
3356
3357 It is worth noting that this information is correct only until
3358 equality propagation has been run by the optimization phase.
3359 Indeed, consider:
3360 select * from t1, t2,t3 where t1.pk=t2.a and t1.pk+1...
3361 the '+' is not nullable as t1.pk is not nullable;
3362 but if the optimizer chooses plan is t2-t3-t1, then, due to equality
3363 propagation it will replace t1.pk in '+' with t2.a (as t2 is before t1
3364 in plan), making the '+' capable of returning NULL when t2.a is NULL.
3365 */
3367
3368 public:
3369 bool null_value; ///< True if item is null
3371 bool m_is_window_function; ///< True if item represents window func
3372 /**
3373 If the item is in a SELECT list (Query_block::fields) and hidden is true,
3374 the item wasn't actually in the list as given by the user (it was added
3375 by the optimizer, to e.g. make sure it was part of a given
3376 materialization), and should not be returned in the actual result.
3377
3378 If the item is not in a SELECT list, the value is irrelevant.
3379 */
3380 bool hidden{false};
3381 /**
3382 True if item is a top most element in the expression being
3383 evaluated for a check constraint.
3384 */
3386
3387 private:
3388 /**
3389 True if this is an expression from the select list of a derived table
3390 which is actually used by outer query.
3391 */
3393
3394 protected:
3395 /**
3396 Set of properties that are calculated by accumulation from underlying items.
3397 Computed by constructors and fix_fields() and updated by
3398 update_used_tables(). The properties are accumulated up to the root of the
3399 current item tree, except they are not accumulated across subqueries and
3400 functions.
3401 */
3402 static constexpr uint8 PROP_SUBQUERY = 0x01;
3403 static constexpr uint8 PROP_STORED_PROGRAM = 0x02;
3404 static constexpr uint8 PROP_AGGREGATION = 0x04;
3405 static constexpr uint8 PROP_WINDOW_FUNCTION = 0x08;
3406 /**
3407 Set if the item or one or more of the underlying items contains a
3408 ROLLUP expression. The rolled up expression itself is not so marked.
3409 */
3410 static constexpr uint8 PROP_ROLLUP_EXPR = 0x10;
3411 /**
3412 Set if the item or one or more of the underlying items is a GROUPING
3413 function.
3414 */
3415 static constexpr uint8 PROP_GROUPING_FUNC = 0x20;
3417
3418 public:
3419 /**
3420 Check if this expression can be used for partial update of a given
3421 JSON column.
3422
3423 For example, the expression `JSON_REPLACE(col, '$.foo', 'bar')`
3424 can be used to partially update the column `col`.
3425
3426 @param field the JSON column that is being updated
3427 @return true if this expression can be used for partial update,
3428 false otherwise
3429 */
3430 virtual bool supports_partial_update(const Field_json *field
3431 [[maybe_unused]]) const {
3432 return false;
3433 }
3434
3435 /**
3436 Whether the item returns array of its data type
3437 */
3438 virtual bool returns_array() const { return false; }
3439
3440 /**
3441 A helper funciton to ensure proper usage of CAST(.. AS .. ARRAY)
3442 */
3443 virtual void allow_array_cast() {}
3444};
3445
3446/**
3447 Descriptor of what and how to cache for
3448 Item::cache_const_expr_transformer/analyzer.
3449
3450*/
3451
3453 /// Path from the expression's top to the current item in item tree
3454 /// used to track parent of current item for caching JSON data
3456 /// Item to cache. Used as a binary flag, but kept as Item* for assertion
3457 Item *cache_item{nullptr};
3458 /// How to cache JSON data. @see Item::enum_const_item_cache
3460};
3461
3462/**
3463 A helper class to give in a functor to Item::walk(). Use as e.g.:
3464
3465 bool result = WalkItem(root_item, enum_walk::POSTFIX, [](Item *item) { ... });
3466
3467 TODO: Make Item::walk() just take in a functor in the first place, instead of
3468 a pointer-to-member and an opaque argument.
3469 */
3470template <class T>
3471inline bool WalkItem(Item *item, enum_walk walk, T &&functor) {
3472 return item->walk(&Item::walk_helper_thunk<T>, walk,
3473 reinterpret_cast<uchar *>(&functor));
3474}
3475
3476/**
3477 Same as WalkItem, but for Item::compile(). Use as e.g.:
3478
3479 Item *item = CompileItem(root_item,
3480 [](Item *item) { return true; }, // Analyzer.
3481 [](Item *item) { return item; }); // Transformer.
3482 */
3483template <class T, class U>
3484inline Item *CompileItem(Item *item, T &&analyzer, U &&transformer) {
3485 uchar *analyzer_ptr = reinterpret_cast<uchar *>(&analyzer);
3486 return item->compile(&Item::analyze_helper_thunk<T>, &analyzer_ptr,
3487 &Item::walk_helper_thunk<U>,
3488 reinterpret_cast<uchar *>(&transformer));
3489}
3490
3491class sp_head;
3492
3495
3496 public:
3499
3500 /// @todo add implementation of basic_const_item
3501 /// and remove from subclasses as appropriate.
3502
3504 table_map used_tables() const override { return used_table_map; }
3505 bool check_function_as_value_generator(uchar *) override { return false; }
3506 /* to prevent drop fixed flag (no need parent cleanup call) */
3507 void cleanup() override {
3508 // @todo We should ensure we never change "basic constant" nodes.
3509 // We should then be able to add this assert:
3510 // assert(marker == MARKER_NONE);
3511 // and remove the call to Item::cleanup()
3512 Item::cleanup();
3513 }
3514 bool basic_const_item() const override { return true; }
3516};
3517
3518/*****************************************************************************
3519 The class is a base class for representation of stored routine variables in
3520 the Item-hierarchy. There are the following kinds of SP-vars:
3521 - local variables (Item_splocal);
3522 - CASE expression (Item_case_expr);
3523*****************************************************************************/
3524
3525class Item_sp_variable : public Item {
3526 public:
3528
3529 public:
3530#ifndef NDEBUG
3531 /*
3532 Routine to which this Item_splocal belongs. Used for checking if correct
3533 runtime context is used for variable handling.
3534 */
3535 sp_head *m_sp{nullptr};
3536#endif
3537
3538 public:
3539 Item_sp_variable(const Name_string sp_var_name);
3540
3541 table_map used_tables() const override { return INNER_TABLE_BIT; }
3542 bool fix_fields(THD *thd, Item **) override;
3543
3544 double val_real() override;
3545 longlong val_int() override;
3546 String *val_str(String *sp) override;
3547 my_decimal *val_decimal(my_decimal *decimal_value) override;
3548 bool val_json(Json_wrapper *result) override;
3549 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
3550 bool get_time(MYSQL_TIME *ltime) override;
3551 bool is_null() override;
3552
3553 public:
3554 inline void make_field(Send_field *field) override;
3555 bool send(Protocol *protocol, String *str) override {
3556 // Need to override send() in case this_item() is an Item_field with a
3557 // ZEROFILL attribute.
3558 return this_item()->send(protocol, str);
3559 }
3560 bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) override {
3561 // It is ok to push down a condition like "column > SP_variable"
3562 return false;
3563 }
3564
3565 protected:
3567 Field *field, bool no_conversions) override;
3568};
3569
3570/*****************************************************************************
3571 Item_sp_variable inline implementation.
3572*****************************************************************************/
3573
3575 Item *it = this_item();
3577 it->make_field(field);
3578}
3579
3581 Field *field, bool no_conversions) {
3582 return this_item()->save_in_field(field, no_conversions);
3583}
3584
3585/*****************************************************************************
3586 A reference to local SP variable (incl. reference to SP parameter), used in
3587 runtime.
3588*****************************************************************************/
3589
3590class Item_splocal final : public Item_sp_variable,
3593
3596
3597 public:
3598 /*
3599 If this variable is a parameter in LIMIT clause.
3600 Used only during NAME_CONST substitution, to not append
3601 NAME_CONST to the resulting query and thus not break
3602 the slave.
3603 */
3605 /*
3606 Position of this reference to SP variable in the statement (the
3607 statement itself is in sp_instr_stmt::m_query).
3608 This is valid only for references to SP variables in statements,
3609 excluding DECLARE CURSOR statement. It is used to replace references to SP
3610 variables with NAME_CONST calls when putting statements into the binary
3611 log.
3612 Value of 0 means that this object doesn't corresponding to reference to
3613 SP variable in query text.
3614 */
3616 /*
3617 Byte length of SP variable name in the statement (see pos_in_query).
3618 The value of this field may differ from the name_length value because
3619 name_length contains byte length of UTF8-encoded item name, but
3620 the query string (see sp_instr_stmt::m_query) is currently stored with
3621 a charset from the SET NAMES statement.
3622 */
3624
3625 Item_splocal(const Name_string sp_var_name, uint sp_var_idx,
3626 enum_field_types sp_var_type, uint pos_in_q = 0,
3627 uint len_in_q = 0);
3628
3629 bool is_splocal() const override { return true; }
3630
3631 Item *this_item() override;
3632 const Item *this_item() const override;
3633 Item **this_item_addr(THD *thd, Item **) override;
3634
3635 void print(const THD *thd, String *str,
3636 enum_query_type query_type) const override;
3637
3638 public:
3639 inline uint get_var_idx() const { return m_var_idx; }
3640
3641 inline enum Type type() const override { return m_type; }
3642 inline Item_result result_type() const override { return m_result_type; }
3643 bool val_json(Json_wrapper *result) override;
3644
3645 private:
3646 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3647
3648 public:
3650 return this;
3651 }
3652};
3653
3654/*****************************************************************************
3655 A reference to case expression in SP, used in runtime.
3656*****************************************************************************/
3657
3658class Item_case_expr final : public Item_sp_variable {
3659 public:
3660 Item_case_expr(uint case_expr_id);
3661
3662 public:
3663 Item *this_item() override;
3664 const Item *this_item() const override;
3665 Item **this_item_addr(THD *thd, Item **) override;
3666
3667 Type type() const override { return this_item()->type(); }
3668 Item_result result_type() const override {
3669 return this_item()->result_type();
3670 }
3671 /*
3672 NOTE: print() is intended to be used from views and for debug.
3673 Item_case_expr can not occur in views, so here it is only for debug
3674 purposes.
3675 */
3676 void print(const THD *thd, String *str,
3677 enum_query_type query_type) const override;
3678
3679 private:
3681};
3682
3683/*
3684 NAME_CONST(given_name, const_value).
3685 This 'function' has all properties of the supplied const_value (which is
3686 assumed to be a literal constant), and the name given_name.
3687
3688 This is used to replace references to SP variables when we write PROCEDURE
3689 statements into the binary log.
3690
3691 TODO
3692 Together with Item_splocal and Item::this_item() we can actually extract
3693 common a base of this class and Item_splocal. Maybe it is possible to
3694 extract a common base with class Item_ref, too.
3695*/
3696
3697class Item_name_const final : public Item {
3698 typedef Item super;
3699
3703
3704 public:
3705 Item_name_const(const POS &pos, Item *name_arg, Item *val);
3706
3707 bool itemize(Parse_context *pc, Item **res) override;
3708 bool fix_fields(THD *, Item **) override;
3709
3710 enum Type type() const override;
3711 double val_real() override;
3712 longlong val_int() override;
3713 String *val_str(String *sp) override;
3714 my_decimal *val_decimal(my_decimal *) override;
3715 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
3716 bool get_time(MYSQL_TIME *ltime) override;
3717 bool is_null() override;
3718 void print(const THD *thd, String *str,
3719 enum_query_type query_type) const override;
3720
3721 Item_result result_type() const override { return value_item->result_type(); }
3722
3724 // Item_name_const always wraps a literal, so there is no need to cache it.
3725 return false;
3726 }
3727
3728 protected:
3730 bool no_conversions) override {
3731 return value_item->save_in_field(field, no_conversions);
3732 }
3733};
3734
3736 Item **items, uint nitems, uint flags);
3737bool agg_item_set_converter(DTCollation &coll, const char *fname, Item **args,
3738 uint nargs, uint flags, int item_sep,
3739 bool only_consts);
3740bool agg_item_charsets(DTCollation &c, const char *name, Item **items,
3741 uint nitems, uint flags, int item_sep, bool only_consts);
3743 const char *name, Item **items,
3744 uint nitems, int item_sep = 1) {
3747 return agg_item_charsets(c, name, items, nitems, flags, item_sep, false);
3748}
3750 Item **items, uint nitems,
3751 int item_sep = 1) {
3754 return agg_item_charsets(c, name, items, nitems, flags, item_sep, true);
3755}
3756
3759
3760 public:
3762 explicit Item_num(const POS &pos) : super(pos) { collation.set_numeric(); }
3763
3764 virtual Item_num *neg() = 0;
3765 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
3766 bool check_partition_func_processor(uchar *) override { return false; }
3767};
3768
3769#define NO_FIELD_INDEX ((uint16)(-1))
3770
3771class Item_ident : public Item {
3772 typedef Item super;
3773
3774 protected:
3775 /**
3776 The fields m_orig_db_name, m_orig_table_name and m_orig_field_name are
3777 maintained so that we can provide information about the origin of a
3778 column that may have been renamed within the query, e.g. as required by
3779 connectors.
3780
3781 Names the original schema of the table that is the source of the field.
3782 If field is from
3783 - a non-aliased base table, the same as db_name.
3784 - an aliased base table, the name of the schema of the base table.
3785 - an expression (including aggregation), a NULL pointer.
3786 - a derived table, the name of the schema of the underlying base table.
3787 - a view, the name of the schema of the underlying base table.
3788 - a temporary table (in optimization stage), the name of the schema of
3789 the source base table.
3790 */
3791 const char *m_orig_db_name;
3792 /**
3793 Names the original table that is the source of the field. If field is from
3794 - a non-aliased base table, the same as table_name.
3795 - an aliased base table, the name of the base table.
3796 - an expression (including aggregation), a NULL pointer.
3797 - a derived table, the name of the underlying base table.
3798 - a view, the name of the underlying base table.
3799 - a temporary table (in optimization stage), the name of the source base tbl
3800 */
3802 /**
3803 Names the field in the source base table. If field is from
3804 - an expression, a NULL pointer.
3805 - a view or base table and is not aliased, the same as field_name.
3806 - a view or base table and is aliased, the column name of the view or
3807 base table.
3808 - a derived table, the column name of the underlying base table.
3809 - a temporary table (in optimization stage), the name of the source column.
3810 */
3812 bool m_alias_of_expr; ///< if this Item's name is alias of SELECT expression
3813
3814 public:
3815 /**
3816 For regularly resolved column references, 'context' points to a name
3817 resolution context object belonging to the query block which simply
3818 contains the reference. To further clarify, in
3819 SELECT (SELECT t.a) FROM t;
3820 t.a is an Item_ident whose 'context' belongs to the subquery
3821 (context->query_block == that of the subquery).
3822 For column references that are part of a generated column expression,
3823 'context' points to a temporary name resolution context object during
3824 resolving, but is set to nullptr after resolving is done. Note that
3825 Item_ident::local_column() depends on that.
3826 */
3828 /**
3829 Schema name of the base table or view the column is part of.
3830 If an expression, a NULL pointer.
3831 If from a derived table, a NULL pointer.
3832 */
3833 const char *db_name;
3834 /**
3835 If column is from a non-aliased base table or view, name of base table or
3836 view.
3837 If column is from an aliased base table or view, the alias name.
3838 If column is from a derived table, the name of the derived table.
3839 If column is from an expression, a NULL pointer.
3840 */
3841 const char *table_name;
3842 /**
3843 If column is aliased, the column alias name.
3844 If column is from a non-aliased base table or view, the name of the
3845 column in that base table or view.
3846 If column is from an expression, a string generated from that expression.
3847
3848 Notice that a column can be aliased in two ways:
3849 1. With an explicit column alias, or @<as clause@>, or
3850 2. With only a column name specified, which differs from the table's
3851 column name due to case insensitivity.
3852 In both cases field_name will differ from m_orig_field_name.
3853 field_name is normally identical to Item::item_name.
3854 */
3855 const char *field_name;
3856
3857 /*
3858 Cached pointer to table which contains this field, used for the same reason
3859 by prep. stmt. too in case then we have not-fully qualified field.
3860 0 - means no cached value.
3861 @todo Notice that this is usually the same as Item_field::table_ref.
3862 cached_table should be replaced by table_ref ASAP.
3863 */
3866
3867 Item_ident(Name_resolution_context *context_arg, const char *db_name_arg,
3868 const char *table_name_arg, const char *field_name_arg)
3869 : m_orig_db_name(db_name_arg),
3870 m_orig_table_name(table_name_arg),
3871 m_orig_field_name(field_name_arg),
3872 m_alias_of_expr(false),
3873 context(context_arg),
3874 db_name(db_name_arg),
3875 table_name(table_name_arg),
3876 field_name(field_name_arg),
3879 item_name.set(field_name_arg);
3880 }
3881
3882 Item_ident(const POS &pos, const char *db_name_arg,
3883 const char *table_name_arg, const char *field_name_arg)
3884 : super(pos),
3885 m_orig_db_name(db_name_arg),
3886 m_orig_table_name(table_name_arg),
3887 m_orig_field_name(field_name_arg),
3888 m_alias_of_expr(false),
3889 db_name(db_name_arg),
3890 table_name(table_name_arg),
3891 field_name(field_name_arg),
3894 item_name.set(field_name_arg);
3895 }
3896
3897 /// Constructor used by Item_field & Item_*_ref (see Item comment)
3898
3900 : Item(thd, item),
3905 context(item->context),
3906 db_name(item->db_name),
3907 table_name(item->table_name),
3908 field_name(item->field_name),
3911
3912 bool itemize(Parse_context *pc, Item **res) override;
3913
3914 const char *full_name() const override;
3915 void set_orignal_db_name(const char *name_arg) { m_orig_db_name = name_arg; }
3916 void set_original_table_name(const char *name_arg) {
3917 m_orig_table_name = name_arg;
3918 }
3919 void set_original_field_name(const char *name_arg) {
3920 m_orig_field_name = name_arg;
3921 }
3922 const char *original_db_name() const { return m_orig_db_name; }
3923 const char *original_table_name() const { return m_orig_table_name; }
3924 const char *original_field_name() const { return m_orig_field_name; }
3925 void fix_after_pullout(Query_block *parent_query_block,
3926 Query_block *removed_query_block) override;
3927 bool aggregate_check_distinct(uchar *arg) override;
3928 bool aggregate_check_group(uchar *arg) override;
3929 Bool3 local_column(const Query_block *sl) const override;
3930
3931 void print(const THD *thd, String *str,
3932 enum_query_type query_type) const override {
3933 print(thd, str, query_type, db_name, table_name);
3934 }
3935
3936 protected:
3937 /**
3938 Function to print column name for a table
3939
3940 To print a column for a permanent table (picks up database and table from
3941 Item_ident object):
3942
3943 item->print(str, qt)
3944
3945 To print a column for a temporary table:
3946
3947 item->print(str, qt, specific_db, specific_table)
3948
3949 Items of temporary table fields have empty/NULL values of table_name and
3950 db_name. To print column names in a 3D form (`database`.`table`.`column`),
3951 this function prints db_name_arg and table_name_arg parameters instead of
3952 this->db_name and this->table_name respectively.
3953
3954 @param thd Thread handle.
3955 @param [out] str Output string buffer.
3956 @param query_type Bitmap to control printing details.
3957 @param db_name_arg String to output as a column database name.
3958 @param table_name_arg String to output as a column table name.
3959 */
3960 void print(const THD *thd, String *str, enum_query_type query_type,
3961 const char *db_name_arg, const char *table_name_arg) const;
3962
3963 public:
3964 ///< Argument object to change_context_processor
3968 };
3969 bool change_context_processor(uchar *arg) override {
3970 context = reinterpret_cast<Change_context *>(arg)->m_context;
3971 return false;
3972 }
3973
3974 /// @returns true if this Item's name is alias of SELECT expression
3975 bool is_alias_of_expr() const { return m_alias_of_expr; }
3976 /// Marks that this Item's name is alias of SELECT expression
3978
3979 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override {
3980 /*
3981 Item_ident processors like aggregate_check*() use
3982 enum_walk::PREFIX|enum_walk::POSTFIX and depend on the processor being
3983 called twice then.
3984 */
3985 return ((walk & enum_walk::PREFIX) && (this->*processor)(arg)) ||
3986 ((walk & enum_walk::POSTFIX) && (this->*processor)(arg));
3987 }
3988
3989 /**
3990 Argument structure for walk processor Item::update_depended_from
3991 */
3993 Query_block *old_depended_from; // the transformed query block
3994 Query_block *new_depended_from; // the new derived table for grouping
3995 };
3996
3997 bool update_depended_from(uchar *) override;
3998
3999 /**
4000 @returns true if a part of this Item's full name (name or table name) is
4001 an alias.
4002 */
4003 virtual bool alias_name_used() const { return m_alias_of_expr; }
4005 const char *db_name, const char *table_name,
4007 bool any_privileges);
4008 bool is_strong_side_column_not_in_fd(uchar *arg) override;
4009 bool is_column_not_in_fd(uchar *arg) override;
4010};
4011
4012class Item_ident_for_show final : public Item {
4013 public:
4015 const char *db_name;
4016 const char *table_name;
4017
4018 Item_ident_for_show(Field *par_field, const char *db_arg,
4019 const char *table_name_arg)
4020 : field(par_field), db_name(db_arg), table_name(table_name_arg) {}
4021
4022 enum Type type() const override { return FIELD_ITEM; }
4023 bool fix_fields(THD *thd, Item **ref) override;
4024 double val_real() override { return field->val_real(); }
4025 longlong val_int() override { return field->val_int(); }
4026 String *val_str(String *str) override { return field->val_str(str); }
4028 return field->val_decimal(dec);
4029 }
4030 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
4031 return field->get_date(ltime, fuzzydate);
4032 }
4033 bool get_time(MYSQL_TIME *ltime) override { return field->get_time(ltime); }
4034 void make_field(Send_field *tmp_field) override;
4036 return field->charset_for_protocol();
4037 }
4038};
4039
4040class COND_EQUAL;
4041class Item_equal;
4042
4043class Item_field : public Item_ident {
4045
4046 protected:
4047 void set_field(Field *field);
4048 void fix_after_pullout(Query_block *parent_query_block,
4049 Query_block *removed_query_block) override {
4050 super::fix_after_pullout(parent_query_block, removed_query_block);
4051
4052 // Update nullability information, as the table may have taken over
4053 // null_row status from the derived table it was part of.
4055 field->table->is_nullable());
4056 }
4058 bool no_conversions) override;
4059
4060 public:
4061 /**
4062 Table containing this resolved field. This is required e.g for calculation
4063 of table map. Notice that for the following types of "tables",
4064 no TABLE_LIST object is assigned and hence table_ref is NULL:
4065 - Temporary tables assigned by join optimizer for sorting and aggregation.
4066 - Stored procedure dummy tables.
4067 For fields referencing such tables, table number is always 0, and other
4068 uses of table_ref is not needed.
4069 */
4071 /// Source field
4073
4074 private:
4075 /// Result field
4077
4078 // save_in_field() and save_org_in_field() are often called repeatedly
4079 // with the same destination field (although the destination for the
4080 // two are distinct, thus two distinct caches). We detect this case by
4081 // storing the last destination, and whether it was of a compatible type
4082 // that we can memcpy into (see fields_are_memcpyable()). This saves time
4083 // doing the same type checking over and over again.
4084 //
4085 // The _memcpyable fields are uint32_t(-1) if the fields are not memcpyable,
4086 // and pack_length() (ie., the amount of bytes to copy) if they are.
4087 // See field_conv_with_cache(), where this logic is encapsulated.
4092
4093 /**
4094 If this field is derived from another field, e.g. it is reading a column
4095 from a temporary table which is populated from a base table, this member
4096 points to the field used to populate the temporary table column.
4097 */
4099
4100 public:
4101 /**
4102 Used during optimization to perform multiple equality analysis,
4103 this analysis should be performed during preparation instead, so that
4104 Item_field can be const after preparation.
4105 */
4107 /**
4108 Index for this field in table->field array. Holds NO_FIELD_INDEX
4109 if index value is not known.
4110 */
4112
4113 void set_item_equal(Item_equal *item_equal_arg) {
4114 if (item_equal == nullptr && item_equal_arg != nullptr) {
4115 item_equal = item_equal_arg;
4116 }
4117 }
4118
4120 if (item_equal != nullptr) {
4122 }
4123 }
4124
4125 // A list of fields that are considered "equal" to this field. E.g., a query
4126 // on the form "a JOIN b ON a.i = b.i JOIN c ON b.i = c.i" would consider
4127 // a.i, b.i and c.i equal due to equality propagation. This is the same as
4128 // "item_equal" above, except that "item_equal" will only contain fields from
4129 // the same join nest. This is used by hash join and BKA when they need to
4130 // undo multi-equality propagation done by the optimizer. (The optimizer may
4131 // generate join conditions that references unreachable fields for said
4132 // iterators.) The split is done because NDB expects the list to only
4133 // contain fields from the same join nest.
4135 /// If true, the optimizer's constant propagation will not replace this item
4136 /// with an equal constant.
4138 /*
4139 if any_privileges set to true then here real effective privileges will
4140 be stored
4141 */
4143 /* field need any privileges (for VIEW creation) */
4145 /*
4146 if this field is used in a context where covering prefix keys
4147 are supported.
4148 */
4150
4151 Item_field(Name_resolution_context *context_arg, const char *db_arg,
4152 const char *table_name_arg, const char *field_name_arg);
4153 Item_field(const POS &pos, const char *db_arg, const char *table_name_arg,
4154 const char *field_name_arg);
4155 Item_field(THD *thd, Item_field *item);
4156 Item_field(THD *thd, Name_resolution_context *context_arg, TABLE_LIST *tr,
4157 Field *field);
4159
4160 bool itemize(Parse_context *pc, Item **res) override;
4161
4162 enum Type type() const override { return FIELD_ITEM; }
4163 bool eq(const Item *item, bool binary_cmp) const override;
4164 double val_real() override;
4165 longlong val_int() override;
4166 longlong val_time_temporal() override;
4167 longlong val_date_temporal() override;
4170 my_decimal *val_decimal(my_decimal *) override;
4171 String *val_str(String *) override;
4172 bool val_json(Json_wrapper *result) override;
4173 bool send(Protocol *protocol, String *str_arg) override;
4174 void reset_field(Field *f);
4175 bool fix_fields(THD *, Item **) override;
4176 void make_field(Send_field *tmp_field) override;
4177 void save_org_in_field(Field *field) override;
4178 table_map used_tables() const override;
4179 Item_result result_type() const override { return field->result_type(); }
4182 }
4183 TYPELIB *get_typelib() const override;
4185 return field->cast_to_int_type();
4186 }
4189 }
4190 longlong val_int_endpoint(bool left_endp, bool *incl_endp) override;
4191 void set_result_field(Field *field_arg) override { result_field = field_arg; }
4193 Field *tmp_table_field(TABLE *) override { return result_field; }
4196 item->base_item_field() != nullptr ? item->base_item_field() : item;
4197 }
4199 return m_base_item_field ? m_base_item_field : this;
4200 }
4201 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
4202 bool get_time(MYSQL_TIME *ltime) override;
4203 bool get_timeval(my_timeval *tm, int *warnings) override;
4204 bool is_null() override {
4205 // NOTE: May return true even if maybe_null is not set!
4206 // This can happen if the underlying TABLE did not have a NULL row
4207 // at set_field() time (ie., table->is_null_row() was false),
4208 // but does now.
4209 return field->is_null();
4210 }
4211 Item *get_tmp_table_item(THD *thd) override;
4212 bool collect_item_field_processor(uchar *arg) override;
4213 bool collect_item_field_or_ref_processor(uchar *arg) override;
4215 bool add_field_to_set_processor(uchar *arg) override;
4216 bool add_field_to_cond_set_processor(uchar *) override;
4217 bool remove_column_from_bitmap(uchar *arg) override;
4218 bool find_item_in_field_list_processor(uchar *arg) override;
4219 bool find_field_processor(uchar *arg) override {
4220 return pointer_cast<Field *>(arg) == field;
4221 }
4222 bool check_function_as_value_generator(uchar *args) override;
4223 bool mark_field_in_map(uchar *arg) override {
4224 auto mark_field = pointer_cast<Mark_field *>(arg);
4225 bool rc = Item::mark_field_in_map(mark_field, field);
4227 rc |= Item::mark_field_in_map(mark_field, result_field);
4228 return rc;
4229 }
4230 bool used_tables_for_level(uchar *arg) override;
4231 bool check_column_privileges(uchar *arg) override;
4232 bool check_partition_func_processor(uchar *) override { return false; }
4233 void bind_fields() override;
4234 bool is_valid_for_pushdown(uchar *arg) override;
4235 bool check_column_in_window_functions(uchar *arg) override;
4236 bool check_column_in_group_by(uchar *arg) override;
4237 Item *replace_with_derived_expr(uchar *arg) override;
4239 void cleanup() override;
4240 void reset_field();
4241 Item_equal *find_item_equal(COND_EQUAL *cond_equal) const;
4242 bool subst_argument_checker(uchar **arg) override;
4243 Item *equal_fields_propagator(uchar *arg) override;
4244 Item *replace_item_field(uchar *) override;
4247 return false;
4248 }
4249 Item *replace_equal_field(uchar *) override;
4251 Item_field *field_for_view_update() override { return this; }
4252 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
4253 int fix_outer_field(THD *thd, Field **field, Item **reference);
4254 Item *update_value_transformer(uchar *select_arg) override;
4255 void print(const THD *thd, String *str,
4256 enum_query_type query_type) const override;
4257 bool is_outer_field() const override {
4258 assert(fixed);
4260 }
4262 assert(data_type() == MYSQL_TYPE_GEOMETRY);
4263 return field->get_geometry_type();
4264 }
4265 const CHARSET_INFO *charset_for_protocol(void) override {
4266 return field->charset_for_protocol();
4267 }
4268
4269#ifndef NDEBUG
4270 void dbug_print() const {
4271 fprintf(DBUG_FILE, "<field ");
4272 if (field) {
4273 fprintf(DBUG_FILE, "'%s.%s': ", field->table->alias, field->field_name);
4274 field->dbug_print();
4275 } else
4276 fprintf(DBUG_FILE, "NULL");
4277
4278 fprintf(DBUG_FILE, ", result_field: ");
4279 if (result_field) {
4280 fprintf(DBUG_FILE, "'%s.%s': ", result_field->table->alias,
4283 } else
4284 fprintf(DBUG_FILE, "NULL");
4285 fprintf(DBUG_FILE, ">\n");
4286 }
4287#endif
4288
4289 float get_filtering_effect(THD *thd, table_map filter_for_table,
4290 table_map read_tables,
4291 const MY_BITMAP *fields_to_ignore,
4292 double rows_in_table) override;
4293
4294 /**
4295 Returns the probability for the predicate "col OP <val>" to be
4296 true for a row in the case where no index statistics or range
4297 estimates are available for 'col'.
4298
4299 The probability depends on the number of rows in the table: it is by
4300 default 'default_filter', but never lower than 1/max_distinct_values
4301 (e.g. number of rows in the table, or the number of distinct values
4302 possible for the datatype if the field provides that kind of
4303 information).
4304
4305 @param max_distinct_values The maximum number of distinct values,
4306 typically the number of rows in the table
4307 @param default_filter The default filter for the predicate
4308
4309 @return the estimated filtering effect for this predicate
4310 */
4311
4312 float get_cond_filter_default_probability(double max_distinct_values,
4313 float default_filter) const;
4314
4315 /**
4316 @note that field->table->alias_name_used is reliable only if
4317 thd->lex->need_correct_ident() is true.
4318 */
4319 bool alias_name_used() const override {
4320 return m_alias_of_expr ||
4321 // maybe the qualifying table was given an alias ("t1 AS foo"):
4323 }
4324
4325 bool repoint_const_outer_ref(uchar *arg) override;
4326 bool returns_array() const override { return field && field->is_array(); }
4327
4328 void set_can_use_prefix_key() override { can_use_prefix_key = true; }
4329
4330 bool replace_field_processor(uchar *arg) override;
4331 bool strip_db_table_name_processor(uchar *) override;
4332
4333 /**
4334 Checks if the current object represents an asterisk select list item
4335
4336 @returns false if a regular column reference, true if an asterisk
4337 select list item.
4338 */
4339 virtual bool is_asterisk() const { return false; }
4340};
4341
4342/**
4343 Represents [schema.][table.]* in a select list
4344
4345 Item_asterisk is used to insert placeholder objects for the special
4346 select list item * (asterisk) into AST.
4347 Those placeholder objects are to be substituted later with e.g. a list of real
4348 table columns by a resolver (@see setup_wild).
4349
4350 @todo The parent class Item_field is redundant. Refactor setup_wild() to
4351 replace Item_field with a simpler one.
4352*/
4355
4356 public:
4357 /**
4358 Constructor
4359
4360 @param pos Location of the * (asterisk) lexeme.
4361 @param opt_schema_name Schema name or nullptr.
4362 @param opt_table_name Table name or nullptr.
4363 */
4364 Item_asterisk(const POS &pos, const char *opt_schema_name,
4365 const char *opt_table_name)
4366 : super(pos, opt_schema_name, opt_table_name, "*") {}
4367
4368 bool itemize(Parse_context *pc, Item **res) override;
4369 bool fix_fields(THD *, Item **) override {
4370 assert(false); // should never happen: see setup_wild()
4371 return true;
4372 }
4373 bool is_asterisk() const override { return true; }
4374};
4375
4376// See if the provided item points to a reachable field (one that belongs to a
4377// table within 'reachable_tables'). If not, go through the list of 'equal'
4378// items in the item and see if we have a field that is reachable. If any such
4379// field is found, create a new Item_field that points to this reachable field
4380// and return it. If the provided item is already reachable, or if we cannot
4381// find a reachable field, return the provided item unchanged. This is used when
4382// creating a hash join iterator, where the join condition may point to a
4383// non-reachable field due to multi-equality propagation during optimization.
4384// (Ideally, the optimizer should not set up such condition in the first place.
4385// This is difficult, if not impossible, to accomplish, given that the plan
4386// created by the optimizer does not map 100% to the iterator executor.) Note
4387// that if the field is not reachable, and we cannot find a reachable field, we
4388// provided field is returned unchanged. The effect is that the hash join will
4389// degrade into a nested loop.
4390Item_field *FindEqualField(Item_field *item_field, table_map reachable_tables);
4391
4394
4395 void init() {
4396 set_nullable(true);
4397 null_value = true;
4399 max_length = 0;
4400 fixed = true;
4402 }
4403
4404 protected:
4406 bool no_conversions) override;
4407
4408 public:
4410 init();
4411 item_name = NAME_STRING("NULL");
4412 }
4413 explicit Item_null(const POS &pos) : super(pos) {
4414 init();
4415 item_name = NAME_STRING("NULL");
4416 }
4417
4418 Item_null(const Name_string &name_par) {
4419 init();
4420 item_name = name_par;
4421 }
4422
4423 enum Type type() const override { return NULL_ITEM; }
4424 bool eq(const Item *item, bool binary_cmp) const override;
4425 double val_real() override;
4426 longlong val_int() override;
4427 longlong val_time_temporal() override { return val_int(); }
4428 longlong val_date_temporal() override { return val_int(); }
4429 String *val_str(String *str) override;
4430 my_decimal *val_decimal(my_decimal *) override;
4431 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
4432 bool get_time(MYSQL_TIME *) override { return true; }
4433 bool val_json(Json_wrapper *wr) override;
4434 bool send(Protocol *protocol, String *str) override;
4435 Item_result result_type() const override { return STRING_RESULT; }
4436 Item *clone_item() const override { return new Item_null(item_name); }
4437 bool is_null() override { return true; }
4438
4439 void print(const THD *, String *str,
4440 enum_query_type query_type) const override {
4441 str->append(query_type == QT_NORMALIZED_FORMAT ? "?" : "NULL");
4442 }
4443
4444 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
4445 bool check_partition_func_processor(uchar *) override { return false; }
4446};
4447
4448/// Dynamic parameters used as placeholders ('?') inside prepared statements
4449
4450class Item_param final : public Item, private Settable_routine_parameter {
4451 typedef Item super;
4452
4453 protected:
4455 bool no_conversions) override;
4456
4457 public:
4464 TIME_VALUE, ///< holds TIME, DATE, DATETIME
4468
4470 m_param_state = state;
4471 }
4472
4474
4475 void mark_json_as_scalar() override { m_json_as_scalar = true; }
4476
4477 /*
4478 A buffer for string and long data values. Historically all allocated
4479 values returned from val_str() were treated as eligible to
4480 modification. I. e. in some cases Item_func_concat can append it's
4481 second argument to return value of the first one. Because of that we
4482 can't return the original buffer holding string data from val_str(),
4483 and have to have one buffer for data and another just pointing to
4484 the data. This is the latter one and it's returned from val_str().
4485 Can not be declared inside the union as it's not a POD type.
4486 */
4489 union {
4491 double real;
4494
4495 private:
4496 /**
4497 True if type of parameter is inherited from parent object (like a typecast).
4498 Reprepare of statement will not change this type.
4499 E.g, we have CAST(? AS DOUBLE), the parameter gets data type
4500 MYSQL_TYPE_DOUBLE and m_type_inherited is set true.
4501 */
4502 bool m_type_inherited{false};
4503 /**
4504 True if type of parameter has been pinned, attempt to use an incompatible
4505 actual type will cause error (no repreparation occurs), and value is
4506 subject to range check. This is used when the parameter is in a context
4507 where its type is imposed. For example, in LIMIT ?, we assign
4508 data_type() == integer, unsigned; and the provided value must be
4509 convertible to unsigned integer: passing a DOUBLE, instead of causing a
4510 repreparation as for an ordinary parameter, will cause an error; passing
4511 integer '-1' will also cause an error.
4512 */
4513 bool m_type_pinned{false};
4514 /**
4515 Parameter objects have a rather complex handling of data type, in order
4516 to consistently handle required type conversion semantics. There are
4517 three data type properties involved:
4518
4519 1. The data_type() property contains the desired type of the parameter
4520 value, as defined by an explicit CAST, the operation the parameter
4521 is part of, and/or the context given by other values and expressions.
4522 After implicit repreparation it may also be assigned from provided
4523 parameter values.
4524
4525 2. The data_type_source() property is the data type of the parameter value,
4526 as given by the supplied user variable or from the protocol buffer.
4527
4528 3. The data_type_actual() property is the data type of the parameter value,
4529 after possible conversion from the source data type.
4530 Conversions may involve
4531 - Character set conversion of string value.
4532 - Conversion from string or number into temporal value, if the
4533 resolved data type is a temporal.
4534 - Conversion from string to number, if the resolved data type is numeric.
4535
4536 In addition, each data type property may have extra attributes to enforce
4537 correct character set, collation and signedness of integers.
4538 */
4539 /**
4540 The "source" data type of the provided parameter.
4541 Used when the parameter comes through protocol buffers.
4542 Notice that signedness of integers is stored in m_unsigned_actual.
4543 */