MySQL 8.0.30
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, 2022, 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 virtual 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 necessary 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 expression 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 of this item's
1621 argument, the form of $CMP$ comparison is specified through the
1622 function's arguments. The call 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 protected:
2562 /**
2563 Helper function for mark_field_in_map(uchar *arg).
2564
2565 @param mark_field Mark_field object
2566 @param field Field to be marked for read/write
2567 */
2568 static inline bool mark_field_in_map(Mark_field *mark_field, Field *field) {
2569 TABLE *table = mark_field->table;
2570 if (table != nullptr && table != field->table) return false;
2571
2572 table = field->table;
2573 table->mark_column_used(field, mark_field->mark);
2574
2575 return false;
2576 }
2577
2578 public:
2579 /**
2580 Reset execution state for such window function types
2581 as determined by arg
2582
2583 @param arg pointing to a bool which, if true, says to reset state
2584 for framing window function, else for non-framing
2585 */
2586 virtual bool reset_wf_state(uchar *arg [[maybe_unused]]) { return false; }
2587
2588 /**
2589 Return used table information for the specified query block (level).
2590 For a field that is resolved from this query block, return the table number.
2591 For a field that is resolved from a query block outer to the specified one,
2592 return OUTER_REF_TABLE_BIT
2593
2594 @param[in,out] arg pointer to an instance of class Used_tables, which is
2595 constructed with the query block as argument.
2596 The used tables information is accumulated in the field
2597 used_tables in this class.
2598
2599 @note This function is used to update used tables information after
2600 merging a query block (a subquery) with its parent.
2601 */
2602 virtual bool used_tables_for_level(uchar *arg [[maybe_unused]]) {
2603 return false;
2604 }
2605 /**
2606 Check privileges.
2607
2608 @param thd thread handle
2609 */
2610 virtual bool check_column_privileges(uchar *thd [[maybe_unused]]) {
2611 return false;
2612 }
2613 virtual bool inform_item_in_cond_of_tab(uchar *) { return false; }
2614 /**
2615 Bind objects from the current execution context to field objects in
2616 item trees. Typically used to bind Field objects from TABLEs to
2617 Item_field objects.
2618 */
2619 virtual void bind_fields() {}
2620
2621 /**
2622 Context object for (functions that override)
2623 Item::clean_up_after_removal().
2624 */
2626 public:
2628 assert(root != nullptr);
2629 }
2630
2632
2633 private:
2634 /**
2635 Pointer to Cleanup_after_removal_context containing from which
2636 select the walk started, i.e., the Query_block that contained the clause
2637 that was removed.
2638 */
2640 };
2641 /**
2642 Clean up after removing the item from the item tree.
2643
2644 param arg pointer to a Cleanup_after_removal_context object
2645 */
2646 virtual bool clean_up_after_removal(uchar *arg [[maybe_unused]]) {
2647 assert(arg != nullptr);
2648 return false;
2649 }
2650
2651 /**
2652 Propagate components that use referenced columns from derived tables.
2653 Some columns from derived tables may be determined to be unused, but
2654 may actually reference other columns that are used. This function will
2655 return true for such columns when called with Item::walk(), which then
2656 means that this column can also be marked as used.
2657 @see also Query_block::delete_unused_merged_columns().
2658 */
2660
2661 /**
2662 Called by Item::walk() to set all the referenced items' derived_used flag.
2663 */
2666 return false;
2667 }
2668
2669 /// @see Distinct_check::check_query()
2670 virtual bool aggregate_check_distinct(uchar *) { return false; }
2671 /// @see Group_check::check_query()
2672 virtual bool aggregate_check_group(uchar *) { return false; }
2673 /// @see Group_check::analyze_conjunct()
2674 virtual bool is_strong_side_column_not_in_fd(uchar *) { return false; }
2675 /// @see Group_check::is_in_fd_of_underlying()
2676 virtual bool is_column_not_in_fd(uchar *) { return false; }
2677 virtual Bool3 local_column(const Query_block *) const {
2678 return Bool3::false3();
2679 }
2680
2681 /**
2682 Minion class under Collect_scalar_subquery_info. Information about one
2683 scalar subquery being considered for transformation
2684 */
2685 struct Css_info {
2686 /// set of locations
2688 /// the scalar subquery
2691 /// Where did we find item above? Used when m_location == L_JOIN_COND,
2692 /// nullptr for other locations.
2694 /// If true, we can forego cardinality checking of the derived table
2696 /// If true, add a COALESCE around replaced subquery: used for implicitly
2697 /// grouped COUNT() in subquery select list when subquery is correlated
2698 bool m_add_coalesce{false};
2699 };
2700
2701 /**
2702 Context struct used by walk method collect_scalar_subqueries to
2703 accumulate information about scalar subqueries found.
2704
2705 In: m_location of expression walked, m_join_condition_context
2706 Out: m_list
2707 */
2709 enum Location { L_SELECT = 1, L_WHERE = 2, L_HAVING = 4, L_JOIN_COND = 8 };
2710 /// accumulated all scalar subqueries found
2711 std::vector<Css_info> m_list;
2712 /// we are currently looking at this kind of clause, cf. enum Location
2717 friend class Item_sum;
2719 };
2720
2721 virtual bool collect_scalar_subqueries(uchar *) { return false; }
2722 virtual bool collect_grouped_aggregates(uchar *) { return false; }
2723 virtual bool collect_subqueries(uchar *) { return false; }
2724 virtual bool update_depended_from(uchar *) { return false; }
2725 /**
2726 Check if an aggregate is referenced from within the GROUP BY
2727 clause of the query block in which it is aggregated. Such
2728 references will be rejected.
2729 @see Item_ref::fix_fields()
2730 @retval true if this is an aggregate which is referenced from
2731 the GROUP BY clause of the aggregating query block
2732 @retval false otherwise
2733 */
2734 virtual bool has_aggregate_ref_in_group_by(uchar *) { return false; }
2735
2736 bool visit_all_analyzer(uchar **) { return true; }
2737 virtual bool cache_const_expr_analyzer(uchar **cache_item);
2739
2740 virtual bool equality_substitution_analyzer(uchar **) { return false; }
2741
2742 virtual Item *equality_substitution_transformer(uchar *) { return this; }
2743
2744 /**
2745 Check if a partition function is allowed.
2746
2747 @return whether a partition function is not accepted
2748
2749 @details
2750 check_partition_func_processor is used to check if a partition function
2751 uses an allowed function. An allowed function will always ensure that
2752 X=Y guarantees that also part_function(X)=part_function(Y) where X is
2753 a set of partition fields and so is Y. The problems comes mainly from
2754 character sets where two equal strings can be quite unequal. E.g. the
2755 german character for double s is equal to 2 s.
2756
2757 The default is that an item is not allowed
2758 in a partition function. Allowed functions
2759 can never depend on server version, they cannot depend on anything
2760 related to the environment. They can also only depend on a set of
2761 fields in the table itself. They cannot depend on other tables and
2762 cannot contain any queries and cannot contain udf's or similar.
2763 If a new Item class is defined and it inherits from a class that is
2764 allowed in a partition function then it is very important to consider
2765 whether this should be inherited to the new class. If not the function
2766 below should be defined in the new Item class.
2767
2768 The general behaviour is that most integer functions are allowed.
2769 If the partition function contains any multi-byte collations then
2770 the function check_part_func_fields will report an error on the
2771 partition function independent of what functions are used. So the
2772 only character sets allowed are single character collation and
2773 even for those only a limited set of functions are allowed. The
2774 problem with multi-byte collations is that almost every string
2775 function has the ability to change things such that two strings
2776 that are equal will not be equal after manipulated by a string
2777 function. E.g. two strings one contains a double s, there is a
2778 special german character that is equal to two s. Now assume a
2779 string function removes one character at this place, then in
2780 one the double s will be removed and in the other there will
2781 still be one s remaining and the strings are no longer equal
2782 and thus the partition function will not sort equal strings into
2783 the same partitions.
2784
2785 So the check if a partition function is valid is two steps. First
2786 check that the field types are valid, next check that the partition
2787 function is valid. The current set of partition functions valid
2788 assumes that there are no multi-byte collations amongst the partition
2789 fields.
2790 */
2791 virtual bool check_partition_func_processor(uchar *) { return true; }
2792 virtual bool subst_argument_checker(uchar **arg) {
2793 if (*arg) *arg = nullptr;
2794 return true;
2795 }
2796 virtual bool explain_subquery_checker(uchar **) { return true; }
2797 virtual Item *explain_subquery_propagator(uchar *) { return this; }
2798
2799 virtual Item *equal_fields_propagator(uchar *) { return this; }
2800 // Mark the item to not be part of substitution.
2801 virtual bool disable_constant_propagation(uchar *) { return false; }
2802 virtual Item *replace_equal_field(uchar *) { return this; }
2803 /*
2804 Check if an expression value has allowed arguments, like DATE/DATETIME
2805 for date functions. Also used by partitioning code to reject
2806 timezone-dependent expressions in a (sub)partitioning function.
2807 */
2808 virtual bool check_valid_arguments_processor(uchar *) { return false; }
2809
2810 /**
2811 Check if this item is allowed for a virtual column or inside a
2812 default expression. Should be overridden in child classes.
2813
2814 @param[in,out] args Due to the limitation of Item::walk()
2815 it is declared as a pointer to uchar, underneath there's a actually a
2816 structure of type Check_function_as_value_generator_parameters.
2817 It is used mainly in Item_field.
2818
2819 @returns true if function is not accepted
2820 */
2821 virtual bool check_function_as_value_generator(uchar *args);
2822
2823 /**
2824 Check if a generated expression depends on DEFAULT function with
2825 specific column name as argument.
2826
2827 @param[in] args Name of column used as DEFAULT function argument.
2828
2829 @returns false if the function is not DEFAULT(args), otherwise true.
2830 */
2832 [[maybe_unused]]) {
2833 return false;
2834 }
2835 /**
2836 Check if all the columns present in this expression are from the
2837 derived table. Used in determining if a condition can be pushed
2838 down to derived table.
2839 */
2840 virtual bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) {
2841 // A generic item cannot be pushed down unless constant.
2842 return !const_item();
2843 }
2844
2845 /**
2846 Check if all the columns present in this expression are present
2847 in PARTITION clause of window functions of the derived table.
2848 Used in checking if a condition can be pushed down to derived table.
2849 */
2850 virtual bool check_column_in_window_functions(uchar *arg [[maybe_unused]]) {
2851 return false;
2852 }
2853 /**
2854 Check if all the columns present in this expression are present
2855 in GROUP BY clause of the derived table. Used in checking if
2856 a condition can be pushed down to derived table.
2857 */
2858 virtual bool check_column_in_group_by(uchar *arg [[maybe_unused]]) {
2859 return false;
2860 }
2861 /**
2862 Assuming this expression is part of a condition that would be pushed to the
2863 WHERE clause of a materialized derived table, replace, in this expression,
2864 each derived table's column with a clone of the expression lying under it
2865 in the derived table's definition. We replace with a clone, because the
2866 condition can be pushed further down in case of nested derived tables.
2867 */
2868 virtual Item *replace_with_derived_expr(uchar *arg [[maybe_unused]]) {
2869 return this;
2870 }
2871 /**
2872 Assuming this expression is part of a condition that would be pushed to the
2873 HAVING clause of a materialized derived table, replace, in this expression,
2874 each derived table's column with a reference to the expression lying under
2875 it in the derived table's definition. Unlike replace_with_derived_expr, a
2876 clone is not used because HAVING condition will not be pushed further
2877 down in case of nested derived tables.
2878 */
2879 virtual Item *replace_with_derived_expr_ref(uchar *arg [[maybe_unused]]) {
2880 return this;
2881 }
2882 /**
2883 Assuming this expression is part of a condition that would be pushed to a
2884 materialized derived table, replace, in this expression, each view reference
2885 with a clone of the expression in merged derived table's definition.
2886 We replace with a clone, because the referenced item in a view reference
2887 is shared by all the view references to that expression.
2888 */
2889 virtual Item *replace_view_refs_with_clone(uchar *arg [[maybe_unused]]) {
2890 return this;
2891 }
2892 /*
2893 For SP local variable returns pointer to Item representing its
2894 current value and pointer to current Item otherwise.
2895 */
2896 virtual Item *this_item() { return this; }
2897 virtual const Item *this_item() const { return this; }
2898
2899 /*
2900 For SP local variable returns address of pointer to Item representing its
2901 current value and pointer passed via parameter otherwise.
2902 */
2903 virtual Item **this_item_addr(THD *, Item **addr_arg) { return addr_arg; }
2904
2905 // Row emulation
2906 virtual uint cols() const { return 1; }
2907 virtual Item *element_index(uint) { return this; }
2908 virtual Item **addr(uint) { return nullptr; }
2909 virtual bool check_cols(uint c);
2910 // It is not row => null inside is impossible
2911 virtual bool null_inside() { return false; }
2912 // used in row subselects to get value of elements
2913 virtual void bring_value() {}
2914
2915 Field *tmp_table_field_from_field_type(TABLE *table, bool fixed_length) const;
2916 virtual Item_field *field_for_view_update() { return nullptr; }
2917 /**
2918 Informs an item that it is wrapped in a truth test, in case it wants to
2919 transforms itself to implement this test by itself.
2920 @param thd Thread handle
2921 @param test Truth test
2922 */
2923 virtual Item *truth_transformer(THD *thd [[maybe_unused]],
2924 Bool_test test [[maybe_unused]]) {
2925 return nullptr;
2926 }
2927 virtual Item *update_value_transformer(uchar *) { return this; }
2928
2930 Query_block *m_trans_block; ///< Transformed query block
2931 Query_block *m_curr_block; ///< Transformed query block or a contained
2932 ///< subquery. Pushed when diving into
2933 ///< subqueries.
2934 Item_replacement(Query_block *transformed_block, Query_block *current_block)
2935 : m_trans_block(transformed_block), m_curr_block(current_block) {}
2936 };
2938 Field *m_target; ///< The field to be replaced
2939 Item_field *m_item; ///< The replacement field
2941 : Item_replacement(select, select), m_target(target), m_item(item) {}
2942 };
2943
2945 Item *m_target; ///< The item identifying the view_ref to be replaced
2946 Field *m_field; ///< The replacement field
2947 ///< subquery. Pushed when diving into
2948 ///< subqueries.
2950 : Item_replacement(select, select), m_target(target), m_field(field) {}
2951 };
2952
2957 : m_target(target), m_replacement(replacement) {}
2958 };
2959
2960 /**
2961 When walking the item tree seeing an Item_singlerow_subselect matching
2962 a target, replace it with a substitute field used when transforming
2963 scalar subqueries into derived tables. Cf.
2964 Query_block::transform_scalar_subqueries_to_join_with_derived.
2965 */
2966 virtual Item *replace_scalar_subquery(uchar *) { return this; }
2967
2968 /**
2969 Transform processor used by Query_block::transform_grouped_to_derived
2970 to replace fields which used to be at the transformed query block
2971 with corresponding fields in the new derived table containing the grouping
2972 operation of the original transformed query block.
2973 */
2974 virtual Item *replace_item_field(uchar *) { return this; }
2975 virtual Item *replace_item_view_ref(uchar *) { return this; }
2976 virtual Item *replace_aggregate(uchar *) { return this; }
2977 virtual Item *replace_outer_ref(uchar *) { return this; }
2978
2983 : m_target(target), m_owner(owner) {}
2984 };
2985
2986 /**
2987 A walker processor overridden by Item_aggregate_ref, q.v.
2988 */
2989 virtual bool update_aggr_refs(uchar *) { return false; }
2990
2991 virtual Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs);
2992 /**
2993 Delete this item.
2994 Note that item must have been cleanup up by calling Item::cleanup().
2995 */
2996 void delete_self() { delete this; }
2997
2998 /** @return whether the item is local to a stored procedure */
2999 virtual bool is_splocal() const { return false; }
3000
3001 /*
3002 Return Settable_routine_parameter interface of the Item. Return 0
3003 if this Item is not Settable_routine_parameter.
3004 */
3006 return nullptr;
3007 }
3008 inline bool is_temporal_with_date() const {
3010 }
3013 }
3014 inline bool is_temporal_with_time() const {
3016 }
3017 inline bool is_temporal() const {
3019 }
3020 /**
3021 Check whether this and the given item has compatible comparison context.
3022 Used by the equality propagation. See Item_field::equal_fields_propagator.
3023
3024 @return
3025 true if the context is the same or if fields could be
3026 compared as DATETIME values by the Arg_comparator.
3027 false otherwise.
3028 */
3029 inline bool has_compatible_context(Item *item) const {
3030 /* Same context. */
3032 return true;
3033 /* DATETIME comparison context. */
3035 return item->is_temporal_with_date() ||
3036 item->cmp_context == STRING_RESULT;
3037 if (item->is_temporal_with_date())
3039 return false;
3040 }
3042 return Field::GEOM_GEOMETRY;
3043 }
3044 String *check_well_formed_result(String *str, bool send_error, bool truncate);
3045 bool eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO *cs);
3046
3047 /*
3048 Test whether an expression is expensive to compute. Used during
3049 optimization to avoid computing expensive expressions during this
3050 phase. Also used to force temp tables when sorting on expensive
3051 functions.
3052 TODO:
3053 Normally we should have a method:
3054 cost Item::execution_cost(),
3055 where 'cost' is either 'double' or some structure of various cost
3056 parameters.
3057 */
3058 virtual bool is_expensive() {
3059 if (is_expensive_cache < 0)
3062 return is_expensive_cache;
3063 }
3064
3065 /**
3066 @return maximum number of characters that this Item can store
3067 If Item is of string or blob type, return max string length in bytes
3068 divided by bytes per character, otherwise return max_length.
3069 @todo - check if collation for other types should have mbmaxlen = 1
3070 */
3072 /*
3073 Length of e.g. 5.5e5 in an expression such as GREATEST(5.5e5, '5') is 5
3074 (length of that string) although length of the actual value is 6.
3075 Return MAX_DOUBLE_STR_LENGTH to prevent truncation of data without having
3076 to evaluate the value of the item.
3077 */
3078 uint32 max_len =
3080 if (result_type() == STRING_RESULT)
3081 return max_len / collation.collation->mbmaxlen;
3082 return max_len;
3083 }
3084
3086 if (cs == &my_charset_bin && result_type() == STRING_RESULT) {
3087 return max_length;
3088 }
3089 return max_char_length();
3090 }
3091
3092 inline void fix_char_length(uint32 max_char_length_arg) {
3093 max_length = char_to_byte_length_safe(max_char_length_arg,
3095 }
3096
3097 /*
3098 Return true if the item points to a column of an outer-joined table.
3099 */
3100 virtual bool is_outer_field() const {
3101 assert(fixed);
3102 return false;
3103 }
3104
3105 /**
3106 Check if an item either is a blob field, or will be represented as a BLOB
3107 field if a field is created based on this item.
3108
3109 @retval true If a field based on this item will be a BLOB field,
3110 @retval false Otherwise.
3111 */
3112 bool is_blob_field() const;
3113
3114 protected:
3115 /// Set accumulated properties for an Item
3116 void set_accum_properties(const Item *item) {
3118 }
3119
3120 /// Add more accumulated properties to an Item
3121 void add_accum_properties(const Item *item) {
3123 }
3124
3125 /// Set the "has subquery" property
3127
3128 /// Set the "has stored program" property
3130
3131 public:
3132 /// @return true if this item or any of its descendants contains a subquery.
3134
3135 /// @return true if this item or any of its descendants refers a stored func.
3136 bool has_stored_program() const {
3138 }
3139
3140 /// @return true if this item or any of its descendants is an aggregated func.
3142
3143 /// Set the "has aggregation" property
3145
3146 /// Reset the "has aggregation" property
3147 void reset_aggregation() { m_accum_properties &= ~PROP_AGGREGATION; }
3148
3149 /// @return true if this item or any of its descendants is a window func.
3151
3152 /// Set the "has window function" property
3154
3155 /**
3156 @return true if this item or any of its descendants within the same query
3157 has a reference to a ROLLUP expression
3158 */
3160
3161 /// Set the property: this item (tree) contains a reference to a ROLLUP expr
3163
3164 /**
3165 @return true if this item or any of underlying items is a GROUPING function
3166 */
3167 bool has_grouping_func() const {
3169 }
3170
3171 /// Set the property: this item is a call to GROUPING
3173
3174 /// Whether this Item was created by the IN->EXISTS subquery transformation
3175 virtual bool created_by_in2exists() const { return false; }
3176
3177 // @return true if an expression in select list of derived table is used
3178 bool is_derived_used() const { return derived_used; }
3179
3181 if (has_subquery())
3183 }
3184
3185 /**
3186 Analyzer function for GC substitution. @see substitute_gc()
3187 */
3188 virtual bool gc_subst_analyzer(uchar **) { return false; }
3189 /**
3190 Transformer function for GC substitution. @see substitute_gc()
3191 */
3192 virtual Item *gc_subst_transformer(uchar *) { return this; }
3193
3194 /**
3195 A processor that replaces any Fields with a Create_field_wrapper. This
3196 will allow us to resolve functions during CREATE TABLE, where we only have
3197 Create_field available and not Field. Used for functional index
3198 implementation.
3199 */
3200 virtual bool replace_field_processor(uchar *) { return false; }
3201 /**
3202 Check if this item is of a type that is eligible for GC
3203 substitution. All items that belong to subclasses of Item_func are
3204 eligible for substitution. @see substitute_gc()
3205 Item_fields can also be eligible if they are given as an argument to
3206 a function that takes an array (the field can be substituted with a
3207 generated column that backs a multi-valued index on that field).
3208
3209 @param array true if the item is an argument to a function that takes an
3210 array, or false otherwise
3211 @return true if the expression is eligible for substitution, false otherwise
3212 */
3213 bool can_be_substituted_for_gc(bool array = false) const;
3214
3215 void aggregate_decimal_properties(Item **item, uint nitems);
3216 void aggregate_float_properties(Item **item, uint nitems);
3217 void aggregate_char_length(Item **args, uint nitems);
3218 void aggregate_temporal_properties(Item **item, uint nitems);
3219 bool aggregate_string_properties(const char *name, Item **item, uint nitems);
3220 void aggregate_num_type(Item_result result_type, Item **item, uint nitems);
3221
3222 /**
3223 This function applies only to Item_field objects referred to by an Item_ref
3224 object that has been marked as a const_item.
3225
3226 @param arg Keep track of whether an Item_ref refers to an Item_field.
3227 */
3228 virtual bool repoint_const_outer_ref(uchar *arg [[maybe_unused]]) {
3229 return false;
3230 }
3231 virtual bool strip_db_table_name_processor(uchar *) { return false; }
3232
3233 private:
3234 virtual bool subq_opt_away_processor(uchar *) { return false; }
3235
3236 // Set an expression from select list of derived table as used.
3238
3239 public: // Start of data fields
3240 /**
3241 Intrusive list pointer for free list. If not null, points to the next
3242 Item on some Query_arena's free list. For instance, stored procedures
3243 have their own Query_arena's.
3244
3245 @see Query_arena::free_list
3246 */
3248
3249 protected:
3250 /// str_values's main purpose is to cache the value in save_in_field
3252
3253 public:
3254 /**
3255 Character set and collation properties assigned for this Item.
3256 Used if Item represents a character string expression.
3257 */
3259 Item_name_string item_name; ///< Name from query
3260 Item_name_string orig_name; ///< Original item name (if it was renamed)
3261 /**
3262 Maximum length of result of evaluating this item, in number of bytes.
3263 - For character or blob data types, max char length multiplied by max
3264 character size (collation.mbmaxlen).
3265 - For decimal type, it is the precision in digits plus sign (unless
3266 unsigned) plus decimal point (unless it has zero decimals).
3267 - For other numeric types, the default or specific display length.
3268 - For date/time types, the display length (10 for DATE, 10 + optional FSP
3269 for TIME, 19 + optional fsp for datetime/timestamp).
3270 - For bit, the number of bits.
3271 - For enum, the string length of the widest enum element.
3272 - For set, the sum of the string length of each set element plus separators.
3273 - For geometry, the maximum size of a BLOB (it's underlying storage type).
3274 - For json, the maximum size of a BLOB (it's underlying storage type).
3275 */
3276 uint32 max_length; ///< Maximum length, in bytes
3277 enum item_marker ///< Values for member 'marker'
3279 /// When contextualization or itemization adds an implicit comparison '0<>'
3280 /// (see make_condition()), to record that this Item_func_ne was created for
3281 /// this purpose; this value is tested during resolution.
3283 /// When doing constant propagation (e.g. change_cond_ref_to_const(), to
3284 /// remember that we have already processed the item.
3286 /// When creating an internal temporary table: says how to store BIT fields.
3288 /// When analyzing functional dependencies for only_full_group_by (says
3289 /// whether a nullable column can be treated at not nullable).
3291 /// When we change DISTINCT to GROUP BY: used for book-keeping of
3292 /// fields.
3294 /// When pushing conditions down to derived table: it says a condition
3295 /// contains only derived table's columns.
3297 /// When pushing index conditions: it says whether a condition uses only
3298 /// indexed columns.
3300 /**
3301 This member has several successive meanings, depending on the phase we're
3302 in (@see item_marker).
3303 The important property is that a phase must have a value (or few values)
3304 which is reserved for this phase. If it wants to set "marked", it assigns
3305 the value; it it wants to test if it is marked, it tests marker !=
3306 value. If the value has been assigned and the phase wants to cancel it can
3307 set marker to MARKER_NONE, which is a magic number which no phase
3308 reserves.
3309 A phase can expect 'marker' to be MARKER_NONE at the start of execution of
3310 a normal statement, at the start of preparation of a PS, and at the start
3311 of execution of a PS.
3312 A phase should not expect marker's value to survive after the phase's
3313 end - as a following phase may change it.
3314 */
3316 Item_result cmp_context; ///< Comparison context
3317 private:
3318 const bool is_parser_item; ///< true if allocated directly by parser
3319 int8 is_expensive_cache; ///< Cache of result of is_expensive()
3320 uint8 m_data_type; ///< Data type assigned to Item
3321 public:
3322 bool fixed; ///< True if item has been resolved
3323 /**
3324 Number of decimals in result when evaluating this item
3325 - For integer type, always zero.
3326 - For decimal type, number of decimals.
3327 - For float type, it may be DECIMAL_NOT_SPECIFIED
3328 - For time, datetime and timestamp, number of decimals in fractional second
3329 - For string types, may be decimals of cast source or DECIMAL_NOT_SPECIFIED
3330 */
3332
3333 bool is_nullable() const { return m_nullable; }
3334 void set_nullable(bool nullable) { m_nullable = nullable; }
3335
3336 private:
3337 /**
3338 True if this item may hold the NULL value(if null_value may be set to true).
3339
3340 For items that represent rows, it is true if one of the columns
3341 may be null.
3342
3343 For items that represent scalar or row subqueries, it is true if
3344 one of the returned columns could be null, or if the subquery
3345 could return zero rows.
3346
3347 It is worth noting that this information is correct only until
3348 equality propagation has been run by the optimization phase.
3349 Indeed, consider:
3350 select * from t1, t2,t3 where t1.pk=t2.a and t1.pk+1...
3351 the '+' is not nullable as t1.pk is not nullable;
3352 but if the optimizer chooses plan is t2-t3-t1, then, due to equality
3353 propagation it will replace t1.pk in '+' with t2.a (as t2 is before t1
3354 in plan), making the '+' capable of returning NULL when t2.a is NULL.
3355 */
3357
3358 public:
3359 bool null_value; ///< True if item is null
3361 bool m_is_window_function; ///< True if item represents window func
3362 /**
3363 If the item is in a SELECT list (Query_block::fields) and hidden is true,
3364 the item wasn't actually in the list as given by the user (it was added
3365 by the optimizer, to e.g. make sure it was part of a given
3366 materialization), and should not be returned in the actual result.
3367
3368 If the item is not in a SELECT list, the value is irrelevant.
3369 */
3370 bool hidden{false};
3371 /**
3372 True if item is a top most element in the expression being
3373 evaluated for a check constraint.
3374 */
3376
3377 private:
3378 /**
3379 True if this is an expression from the select list of a derived table
3380 which is actually used by outer query.
3381 */
3383
3384 protected:
3385 /**
3386 Set of properties that are calculated by accumulation from underlying items.
3387 Computed by constructors and fix_fields() and updated by
3388 update_used_tables(). The properties are accumulated up to the root of the
3389 current item tree, except they are not accumulated across subqueries and
3390 functions.
3391 */
3392 static constexpr uint8 PROP_SUBQUERY = 0x01;
3393 static constexpr uint8 PROP_STORED_PROGRAM = 0x02;
3394 static constexpr uint8 PROP_AGGREGATION = 0x04;
3395 static constexpr uint8 PROP_WINDOW_FUNCTION = 0x08;
3396 /**
3397 Set if the item or one or more of the underlying items contains a
3398 ROLLUP expression. The rolled up expression itself is not so marked.
3399 */
3400 static constexpr uint8 PROP_ROLLUP_EXPR = 0x10;
3401 /**
3402 Set if the item or one or more of the underlying items is a GROUPING
3403 function.
3404 */
3405 static constexpr uint8 PROP_GROUPING_FUNC = 0x20;
3407
3408 public:
3409 /**
3410 Check if this expression can be used for partial update of a given
3411 JSON column.
3412
3413 For example, the expression `JSON_REPLACE(col, '$.foo', 'bar')`
3414 can be used to partially update the column `col`.
3415
3416 @param field the JSON column that is being updated
3417 @return true if this expression can be used for partial update,
3418 false otherwise
3419 */
3420 virtual bool supports_partial_update(const Field_json *field
3421 [[maybe_unused]]) const {
3422 return false;
3423 }
3424
3425 /**
3426 Whether the item returns array of its data type
3427 */
3428 virtual bool returns_array() const { return false; }
3429
3430 /**
3431 A helper function to ensure proper usage of CAST(.. AS .. ARRAY)
3432 */
3433 virtual void allow_array_cast() {}
3434};
3435
3436/**
3437 Descriptor of what and how to cache for
3438 Item::cache_const_expr_transformer/analyzer.
3439
3440*/
3441
3443 /// Path from the expression's top to the current item in item tree
3444 /// used to track parent of current item for caching JSON data
3446 /// Item to cache. Used as a binary flag, but kept as Item* for assertion
3447 Item *cache_item{nullptr};
3448 /// How to cache JSON data. @see Item::enum_const_item_cache
3450};
3451
3452/**
3453 A helper class to give in a functor to Item::walk(). Use as e.g.:
3454
3455 bool result = WalkItem(root_item, enum_walk::POSTFIX, [](Item *item) { ... });
3456
3457 TODO: Make Item::walk() just take in a functor in the first place, instead of
3458 a pointer-to-member and an opaque argument.
3459 */
3460template <class T>
3461inline bool WalkItem(Item *item, enum_walk walk, T &&functor) {
3462 return item->walk(&Item::walk_helper_thunk<T>, walk,
3463 reinterpret_cast<uchar *>(&functor));
3464}
3465
3466/**
3467 Same as WalkItem, but for Item::compile(). Use as e.g.:
3468
3469 Item *item = CompileItem(root_item,
3470 [](Item *item) { return true; }, // Analyzer.
3471 [](Item *item) { return item; }); // Transformer.
3472 */
3473template <class T, class U>
3474inline Item *CompileItem(Item *item, T &&analyzer, U &&transformer) {
3475 uchar *analyzer_ptr = reinterpret_cast<uchar *>(&analyzer);
3476 return item->compile(&Item::analyze_helper_thunk<T>, &analyzer_ptr,
3477 &Item::walk_helper_thunk<U>,
3478 reinterpret_cast<uchar *>(&transformer));
3479}
3480
3481class sp_head;
3482
3485
3486 public:
3489
3490 /// @todo add implementation of basic_const_item
3491 /// and remove from subclasses as appropriate.
3492
3494 table_map used_tables() const override { return used_table_map; }
3495 bool check_function_as_value_generator(uchar *) override { return false; }
3496 /* to prevent drop fixed flag (no need parent cleanup call) */
3497 void cleanup() override {
3498 // @todo We should ensure we never change "basic constant" nodes.
3499 // We should then be able to add this assert:
3500 // assert(marker == MARKER_NONE);
3501 // and remove the call to Item::cleanup()
3502 Item::cleanup();
3503 }
3504 bool basic_const_item() const override { return true; }
3506};
3507
3508/*****************************************************************************
3509 The class is a base class for representation of stored routine variables in
3510 the Item-hierarchy. There are the following kinds of SP-vars:
3511 - local variables (Item_splocal);
3512 - CASE expression (Item_case_expr);
3513*****************************************************************************/
3514
3515class Item_sp_variable : public Item {
3516 public:
3518
3519 public:
3520#ifndef NDEBUG
3521 /*
3522 Routine to which this Item_splocal belongs. Used for checking if correct
3523 runtime context is used for variable handling.
3524 */
3525 sp_head *m_sp{nullptr};
3526#endif
3527
3528 public:
3529 Item_sp_variable(const Name_string sp_var_name);
3530
3531 table_map used_tables() const override { return INNER_TABLE_BIT; }
3532 bool fix_fields(THD *thd, Item **) override;
3533
3534 double val_real() override;
3535 longlong val_int() override;
3536 String *val_str(String *sp) override;
3537 my_decimal *val_decimal(my_decimal *decimal_value) override;
3538 bool val_json(Json_wrapper *result) override;
3539 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
3540 bool get_time(MYSQL_TIME *ltime) override;
3541 bool is_null() override;
3542
3543 public:
3544 inline void make_field(Send_field *field) override;
3545 bool send(Protocol *protocol, String *str) override {
3546 // Need to override send() in case this_item() is an Item_field with a
3547 // ZEROFILL attribute.
3548 return this_item()->send(protocol, str);
3549 }
3550 bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) override {
3551 // It is ok to push down a condition like "column > SP_variable"
3552 return false;
3553 }
3554
3555 protected:
3557 Field *field, bool no_conversions) override;
3558};
3559
3560/*****************************************************************************
3561 Item_sp_variable inline implementation.
3562*****************************************************************************/
3563
3565 Item *it = this_item();
3567 it->make_field(field);
3568}
3569
3571 Field *field, bool no_conversions) {
3572 return this_item()->save_in_field(field, no_conversions);
3573}
3574
3575/*****************************************************************************
3576 A reference to local SP variable (incl. reference to SP parameter), used in
3577 runtime.
3578*****************************************************************************/
3579
3580class Item_splocal final : public Item_sp_variable,
3583
3586
3587 public:
3588 /*
3589 If this variable is a parameter in LIMIT clause.
3590 Used only during NAME_CONST substitution, to not append
3591 NAME_CONST to the resulting query and thus not break
3592 the slave.
3593 */
3595 /*
3596 Position of this reference to SP variable in the statement (the
3597 statement itself is in sp_instr_stmt::m_query).
3598 This is valid only for references to SP variables in statements,
3599 excluding DECLARE CURSOR statement. It is used to replace references to SP
3600 variables with NAME_CONST calls when putting statements into the binary
3601 log.
3602 Value of 0 means that this object doesn't corresponding to reference to
3603 SP variable in query text.
3604 */
3606 /*
3607 Byte length of SP variable name in the statement (see pos_in_query).
3608 The value of this field may differ from the name_length value because
3609 name_length contains byte length of UTF8-encoded item name, but
3610 the query string (see sp_instr_stmt::m_query) is currently stored with
3611 a charset from the SET NAMES statement.
3612 */
3614
3615 Item_splocal(const Name_string sp_var_name, uint sp_var_idx,
3616 enum_field_types sp_var_type, uint pos_in_q = 0,
3617 uint len_in_q = 0);
3618
3619 bool is_splocal() const override { return true; }
3620
3621 Item *this_item() override;
3622 const Item *this_item() const override;
3623 Item **this_item_addr(THD *thd, Item **) override;
3624
3625 void print(const THD *thd, String *str,
3626 enum_query_type query_type) const override;
3627
3628 public:
3629 inline uint get_var_idx() const { return m_var_idx; }
3630
3631 inline enum Type type() const override { return m_type; }
3632 inline Item_result result_type() const override { return m_result_type; }
3633 bool val_json(Json_wrapper *result) override;
3634
3635 private:
3636 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3637
3638 public:
3640 return this;
3641 }
3642};
3643
3644/*****************************************************************************
3645 A reference to case expression in SP, used in runtime.
3646*****************************************************************************/
3647
3648class Item_case_expr final : public Item_sp_variable {
3649 public:
3650 Item_case_expr(uint case_expr_id);
3651
3652 public:
3653 Item *this_item() override;
3654 const Item *this_item() const override;
3655 Item **this_item_addr(THD *thd, Item **) override;
3656
3657 Type type() const override { return this_item()->type(); }
3658 Item_result result_type() const override {
3659 return this_item()->result_type();
3660 }
3661 /*
3662 NOTE: print() is intended to be used from views and for debug.
3663 Item_case_expr can not occur in views, so here it is only for debug
3664 purposes.
3665 */
3666 void print(const THD *thd, String *str,
3667 enum_query_type query_type) const override;
3668
3669 private:
3671};
3672
3673/*
3674 NAME_CONST(given_name, const_value).
3675 This 'function' has all properties of the supplied const_value (which is
3676 assumed to be a literal constant), and the name given_name.
3677
3678 This is used to replace references to SP variables when we write PROCEDURE
3679 statements into the binary log.
3680
3681 TODO
3682 Together with Item_splocal and Item::this_item() we can actually extract
3683 common a base of this class and Item_splocal. Maybe it is possible to
3684 extract a common base with class Item_ref, too.
3685*/
3686
3687class Item_name_const final : public Item {
3688 typedef Item super;
3689
3693
3694 public:
3695 Item_name_const(const POS &pos, Item *name_arg, Item *val);
3696
3697 bool itemize(Parse_context *pc, Item **res) override;
3698 bool fix_fields(THD *, Item **) override;
3699
3700 enum Type type() const override;
3701 double val_real() override;
3702 longlong val_int() override;
3703 String *val_str(String *sp) override;
3704 my_decimal *val_decimal(my_decimal *) override;
3705 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
3706 bool get_time(MYSQL_TIME *ltime) override;
3707 bool is_null() override;
3708 void print(const THD *thd, String *str,
3709 enum_query_type query_type) const override;
3710
3711 Item_result result_type() const override { return value_item->result_type(); }
3712
3714 // Item_name_const always wraps a literal, so there is no need to cache it.
3715 return false;
3716 }
3717
3718 protected:
3720 bool no_conversions) override {
3721 return value_item->save_in_field(field, no_conversions);
3722 }
3723};
3724
3726 Item **items, uint nitems, uint flags);
3727bool agg_item_set_converter(DTCollation &coll, const char *fname, Item **args,
3728 uint nargs, uint flags, int item_sep,
3729 bool only_consts);
3730bool agg_item_charsets(DTCollation &c, const char *name, Item **items,
3731 uint nitems, uint flags, int item_sep, bool only_consts);
3733 const char *name, Item **items,
3734 uint nitems, int item_sep = 1) {
3737 return agg_item_charsets(c, name, items, nitems, flags, item_sep, false);
3738}
3740 Item **items, uint nitems,
3741 int item_sep = 1) {
3744 return agg_item_charsets(c, name, items, nitems, flags, item_sep, true);
3745}
3746
3749
3750 public:
3752 explicit Item_num(const POS &pos) : super(pos) { collation.set_numeric(); }
3753
3754 virtual Item_num *neg() = 0;
3755 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
3756 bool check_partition_func_processor(uchar *) override { return false; }
3757};
3758
3759#define NO_FIELD_INDEX ((uint16)(-1))
3760
3761class Item_ident : public Item {
3762 typedef Item super;
3763
3764 protected:
3765 /**
3766 The fields m_orig_db_name, m_orig_table_name and m_orig_field_name are
3767 maintained so that we can provide information about the origin of a
3768 column that may have been renamed within the query, e.g. as required by
3769 connectors.
3770
3771 Names the original schema of the table that is the source of the field.
3772 If field is from
3773 - a non-aliased base table, the same as db_name.
3774 - an aliased base table, the name of the schema of the base table.
3775 - an expression (including aggregation), a NULL pointer.
3776 - a derived table, the name of the schema of the underlying base table.
3777 - a view, the name of the schema of the underlying base table.
3778 - a temporary table (in optimization stage), the name of the schema of
3779 the source base table.
3780 */
3781 const char *m_orig_db_name;
3782 /**
3783 Names the original table that is the source of the field. If field is from
3784 - a non-aliased base table, the same as table_name.
3785 - an aliased base table, the name of the base table.
3786 - an expression (including aggregation), a NULL pointer.
3787 - a derived table, the name of the underlying base table.
3788 - a view, the name of the underlying base table.
3789 - a temporary table (in optimization stage), the name of the source base tbl
3790 */
3792 /**
3793 Names the field in the source base table. If field is from
3794 - an expression, a NULL pointer.
3795 - a view or base table and is not aliased, the same as field_name.
3796 - a view or base table and is aliased, the column name of the view or
3797 base table.
3798 - a derived table, the column name of the underlying base table.
3799 - a temporary table (in optimization stage), the name of the source column.
3800 */
3802 bool m_alias_of_expr; ///< if this Item's name is alias of SELECT expression
3803
3804 public:
3805 /**
3806 For regularly resolved column references, 'context' points to a name
3807 resolution context object belonging to the query block which simply
3808 contains the reference. To further clarify, in
3809 SELECT (SELECT t.a) FROM t;
3810 t.a is an Item_ident whose 'context' belongs to the subquery
3811 (context->query_block == that of the subquery).
3812 For column references that are part of a generated column expression,
3813 'context' points to a temporary name resolution context object during
3814 resolving, but is set to nullptr after resolving is done. Note that
3815 Item_ident::local_column() depends on that.
3816 */
3818 /**
3819 Schema name of the base table or view the column is part of.
3820 If an expression, a NULL pointer.
3821 If from a derived table, a NULL pointer.
3822 */
3823 const char *db_name;
3824 /**
3825 If column is from a non-aliased base table or view, name of base table or
3826 view.
3827 If column is from an aliased base table or view, the alias name.
3828 If column is from a derived table, the name of the derived table.
3829 If column is from an expression, a NULL pointer.
3830 */
3831 const char *table_name;
3832 /**
3833 If column is aliased, the column alias name.
3834 If column is from a non-aliased base table or view, the name of the
3835 column in that base table or view.
3836 If column is from an expression, a string generated from that expression.
3837
3838 Notice that a column can be aliased in two ways:
3839 1. With an explicit column alias, or @<as clause@>, or
3840 2. With only a column name specified, which differs from the table's
3841 column name due to case insensitivity.
3842 In both cases field_name will differ from m_orig_field_name.
3843 field_name is normally identical to Item::item_name.
3844 */
3845 const char *field_name;
3846
3847 /*
3848 Cached pointer to table which contains this field, used for the same reason
3849 by prep. stmt. too in case then we have not-fully qualified field.
3850 0 - means no cached value.
3851 @todo Notice that this is usually the same as Item_field::table_ref.
3852 cached_table should be replaced by table_ref ASAP.
3853 */
3856
3857 Item_ident(Name_resolution_context *context_arg, const char *db_name_arg,
3858 const char *table_name_arg, const char *field_name_arg)
3859 : m_orig_db_name(db_name_arg),
3860 m_orig_table_name(table_name_arg),
3861 m_orig_field_name(field_name_arg),
3862 m_alias_of_expr(false),
3863 context(context_arg),
3864 db_name(db_name_arg),
3865 table_name(table_name_arg),
3866 field_name(field_name_arg),
3869 item_name.set(field_name_arg);
3870 }
3871
3872 Item_ident(const POS &pos, const char *db_name_arg,
3873 const char *table_name_arg, const char *field_name_arg)
3874 : super(pos),
3875 m_orig_db_name(db_name_arg),
3876 m_orig_table_name(table_name_arg),
3877 m_orig_field_name(field_name_arg),
3878 m_alias_of_expr(false),
3879 db_name(db_name_arg),
3880 table_name(table_name_arg),
3881 field_name(field_name_arg),
3884 item_name.set(field_name_arg);
3885 }
3886
3887 /// Constructor used by Item_field & Item_*_ref (see Item comment)
3888
3890 : Item(thd, item),
3895 context(item->context),
3896 db_name(item->db_name),
3897 table_name(item->table_name),
3898 field_name(item->field_name),
3901
3902 bool itemize(Parse_context *pc, Item **res) override;
3903
3904 const char *full_name() const override;
3905 void set_orignal_db_name(const char *name_arg) { m_orig_db_name = name_arg; }
3906 void set_original_table_name(const char *name_arg) {
3907 m_orig_table_name = name_arg;
3908 }
3909 void set_original_field_name(const char *name_arg) {
3910 m_orig_field_name = name_arg;
3911 }
3912 const char *original_db_name() const { return m_orig_db_name; }
3913 const char *original_table_name() const { return m_orig_table_name; }
3914 const char *original_field_name() const { return m_orig_field_name; }
3915 void fix_after_pullout(Query_block *parent_query_block,
3916 Query_block *removed_query_block) override;
3917 bool aggregate_check_distinct(uchar *arg) override;
3918 bool aggregate_check_group(uchar *arg) override;
3919 Bool3 local_column(const Query_block *sl) const override;
3920
3921 void print(const THD *thd, String *str,
3922 enum_query_type query_type) const override {
3923 print(thd, str, query_type, db_name, table_name);
3924 }
3925
3926 protected:
3927 /**
3928 Function to print column name for a table
3929
3930 To print a column for a permanent table (picks up database and table from
3931 Item_ident object):
3932
3933 item->print(str, qt)
3934
3935 To print a column for a temporary table:
3936
3937 item->print(str, qt, specific_db, specific_table)
3938
3939 Items of temporary table fields have empty/NULL values of table_name and
3940 db_name. To print column names in a 3D form (`database`.`table`.`column`),
3941 this function prints db_name_arg and table_name_arg parameters instead of
3942 this->db_name and this->table_name respectively.
3943
3944 @param thd Thread handle.
3945 @param [out] str Output string buffer.
3946 @param query_type Bitmap to control printing details.
3947 @param db_name_arg String to output as a column database name.
3948 @param table_name_arg String to output as a column table name.
3949 */
3950 void print(const THD *thd, String *str, enum_query_type query_type,
3951 const char *db_name_arg, const char *table_name_arg) const;
3952
3953 public:
3954 ///< Argument object to change_context_processor
3958 };
3959 bool change_context_processor(uchar *arg) override {
3960 context = reinterpret_cast<Change_context *>(arg)->m_context;
3961 return false;
3962 }
3963
3964 /// @returns true if this Item's name is alias of SELECT expression
3965 bool is_alias_of_expr() const { return m_alias_of_expr; }
3966 /// Marks that this Item's name is alias of SELECT expression
3968
3969 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override {
3970 /*
3971 Item_ident processors like aggregate_check*() use
3972 enum_walk::PREFIX|enum_walk::POSTFIX and depend on the processor being
3973 called twice then.
3974 */
3975 return ((walk & enum_walk::PREFIX) && (this->*processor)(arg)) ||
3976 ((walk & enum_walk::POSTFIX) && (this->*processor)(arg));
3977 }
3978
3979 /**
3980 Argument structure for walk processor Item::update_depended_from
3981 */
3983 Query_block *old_depended_from; // the transformed query block
3984 Query_block *new_depended_from; // the new derived table for grouping
3985 };
3986
3987 bool update_depended_from(uchar *) override;
3988
3989 /**
3990 @returns true if a part of this Item's full name (name or table name) is
3991 an alias.
3992 */
3993 virtual bool alias_name_used() const { return m_alias_of_expr; }
3995 const char *db_name, const char *table_name,
3997 bool any_privileges);
3998 bool is_strong_side_column_not_in_fd(uchar *arg) override;
3999 bool is_column_not_in_fd(uchar *arg) override;
4000};
4001
4002class Item_ident_for_show final : public Item {
4003 public:
4005 const char *db_name;
4006 const char *table_name;
4007
4008 Item_ident_for_show(Field *par_field, const char *db_arg,
4009 const char *table_name_arg)
4010 : field(par_field), db_name(db_arg), table_name(table_name_arg) {}
4011
4012 enum Type type() const override { return FIELD_ITEM; }
4013 bool fix_fields(THD *thd, Item **ref) override;
4014 double val_real() override { return field->val_real(); }
4015 longlong val_int() override { return field->val_int(); }
4016 String *val_str(String *str) override { return field->val_str(str); }
4018 return field->val_decimal(dec);
4019 }
4020 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
4021 return field->get_date(ltime, fuzzydate);
4022 }
4023 bool get_time(MYSQL_TIME *ltime) override { return field->get_time(ltime); }
4024 void make_field(Send_field *tmp_field) override;
4026 return field->charset_for_protocol();
4027 }
4028};
4029
4030class COND_EQUAL;
4031class Item_equal;
4032
4033class Item_field : public Item_ident {
4035
4036 protected:
4037 void set_field(Field *field);
4038 void fix_after_pullout(Query_block *parent_query_block,
4039 Query_block *removed_query_block) override {
4040 super::fix_after_pullout(parent_query_block, removed_query_block);
4041
4042 // Update nullability information, as the table may have taken over
4043 // null_row status from the derived table it was part of.
4045 field->table->is_nullable());
4046 }
4048 bool no_conversions) override;
4049
4050 public:
4051 /**
4052 Table containing this resolved field. This is required e.g for calculation
4053 of table map. Notice that for the following types of "tables",
4054 no TABLE_LIST object is assigned and hence table_ref is NULL:
4055 - Temporary tables assigned by join optimizer for sorting and aggregation.
4056 - Stored procedure dummy tables.
4057 For fields referencing such tables, table number is always 0, and other
4058 uses of table_ref is not needed.
4059 */
4061 /// Source field
4063
4064 private:
4065 /// Result field
4067
4068 // save_in_field() and save_org_in_field() are often called repeatedly
4069 // with the same destination field (although the destination for the
4070 // two are distinct, thus two distinct caches). We detect this case by
4071 // storing the last destination, and whether it was of a compatible type
4072 // that we can memcpy into (see fields_are_memcpyable()). This saves time
4073 // doing the same type checking over and over again.
4074 //
4075 // The _memcpyable fields are uint32_t(-1) if the fields are not memcpyable,
4076 // and pack_length() (ie., the amount of bytes to copy) if they are.
4077 // See field_conv_with_cache(), where this logic is encapsulated.
4082
4083 /**
4084 If this field is derived from another field, e.g. it is reading a column
4085 from a temporary table which is populated from a base table, this member
4086 points to the field used to populate the temporary table column.
4087 */
4089
4090 public:
4091 /**
4092 Used during optimization to perform multiple equality analysis,
4093 this analysis should be performed during preparation instead, so that
4094 Item_field can be const after preparation.
4095 */
4097 /**
4098 Index for this field in table->field array. Holds NO_FIELD_INDEX
4099 if index value is not known.
4100 */
4102
4103 void set_item_equal(Item_equal *item_equal_arg) {
4104 if (item_equal == nullptr && item_equal_arg != nullptr) {
4105 item_equal = item_equal_arg;
4106 }
4107 }
4108
4110 if (item_equal != nullptr) {
4112 }
4113 }
4114
4115 // A list of fields that are considered "equal" to this field. E.g., a query
4116 // on the form "a JOIN b ON a.i = b.i JOIN c ON b.i = c.i" would consider
4117 // a.i, b.i and c.i equal due to equality propagation. This is the same as
4118 // "item_equal" above, except that "item_equal" will only contain fields from
4119 // the same join nest. This is used by hash join and BKA when they need to
4120 // undo multi-equality propagation done by the optimizer. (The optimizer may
4121 // generate join conditions that references unreachable fields for said
4122 // iterators.) The split is done because NDB expects the list to only
4123 // contain fields from the same join nest.
4125 /// If true, the optimizer's constant propagation will not replace this item
4126 /// with an equal constant.
4128 /*
4129 if any_privileges set to true then here real effective privileges will
4130 be stored
4131 */
4133 /* field need any privileges (for VIEW creation) */
4135 /*
4136 if this field is used in a context where covering prefix keys
4137 are supported.
4138 */
4140
4141 Item_field(Name_resolution_context *context_arg, const char *db_arg,
4142 const char *table_name_arg, const char *field_name_arg);
4143 Item_field(const POS &pos, const char *db_arg, const char *table_name_arg,
4144 const char *field_name_arg);
4145 Item_field(THD *thd, Item_field *item);
4146 Item_field(THD *thd, Name_resolution_context *context_arg, TABLE_LIST *tr,
4147 Field *field);
4149
4150 bool itemize(Parse_context *pc, Item **res) override;
4151
4152 enum Type type() const override { return FIELD_ITEM; }
4153 bool eq(const Item *item, bool binary_cmp) const override;
4154 double val_real() override;
4155 longlong val_int() override;
4156 longlong val_time_temporal() override;
4157 longlong val_date_temporal() override;
4160 my_decimal *val_decimal(my_decimal *) override;
4161 String *val_str(String *) override;
4162 bool val_json(Json_wrapper *result) override;
4163 bool send(Protocol *protocol, String *str_arg) override;
4164 void reset_field(Field *f);
4165 bool fix_fields(THD *, Item **) override;
4166 void make_field(Send_field *tmp_field) override;
4167 void save_org_in_field(Field *field) override;
4168 table_map used_tables() const override;
4169 Item_result result_type() const override { return field->result_type(); }
4172 }
4173 TYPELIB *get_typelib() const override;
4175 return field->cast_to_int_type();
4176 }
4179 }
4180 longlong val_int_endpoint(bool left_endp, bool *incl_endp) override;
4181 void set_result_field(Field *field_arg) override { result_field = field_arg; }
4183 Field *tmp_table_field(TABLE *) override { return result_field; }
4186 item->base_item_field() != nullptr ? item->base_item_field() : item;
4187 }
4189 return m_base_item_field ? m_base_item_field : this;
4190 }
4191 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
4192 bool get_time(MYSQL_TIME *ltime) override;
4193 bool get_timeval(my_timeval *tm, int *warnings) override;
4194 bool is_null() override {
4195 // NOTE: May return true even if maybe_null is not set!
4196 // This can happen if the underlying TABLE did not have a NULL row
4197 // at set_field() time (ie., table->is_null_row() was false),
4198 // but does now.
4199 return field->is_null();
4200 }
4201 Item *get_tmp_table_item(THD *thd) override;
4202 bool collect_item_field_processor(uchar *arg) override;
4203 bool collect_item_field_or_ref_processor(uchar *arg) override;
4205 bool add_field_to_set_processor(uchar *arg) override;
4206 bool add_field_to_cond_set_processor(uchar *) override;
4207 bool remove_column_from_bitmap(uchar *arg) override;
4208 bool find_item_in_field_list_processor(uchar *arg) override;
4209 bool find_field_processor(uchar *arg) override {
4210 return pointer_cast<Field *>(arg) == field;
4211 }
4212 bool check_function_as_value_generator(uchar *args) override;
4213 bool mark_field_in_map(uchar *arg) override {
4214 auto mark_field = pointer_cast<Mark_field *>(arg);
4215 bool rc = Item::mark_field_in_map(mark_field, field);
4217 rc |= Item::mark_field_in_map(mark_field, result_field);
4218 return rc;
4219 }
4220 bool used_tables_for_level(uchar *arg) override;
4221 bool check_column_privileges(uchar *arg) override;
4222 bool check_partition_func_processor(uchar *) override { return false; }
4223 void bind_fields() override;
4224 bool is_valid_for_pushdown(uchar *arg) override;
4225 bool check_column_in_window_functions(uchar *arg) override;
4226 bool check_column_in_group_by(uchar *arg) override;
4227 Item *replace_with_derived_expr(uchar *arg) override;
4229 void cleanup() override;
4230 void reset_field();
4231 Item_equal *find_item_equal(COND_EQUAL *cond_equal) const;
4232 bool subst_argument_checker(uchar **arg) override;
4233 Item *equal_fields_propagator(uchar *arg) override;
4234 Item *replace_item_field(uchar *) override;
4237 return false;
4238 }
4239 Item *replace_equal_field(uchar *) override;
4241 Item_field *field_for_view_update() override { return this; }
4242 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
4243 int fix_outer_field(THD *thd, Field **field, Item **reference);
4244 Item *update_value_transformer(uchar *select_arg) override;
4245 void print(const THD *thd, String *str,
4246 enum_query_type query_type) const override;
4247 bool is_outer_field() const override {
4248 assert(fixed);
4250 }
4252 assert(data_type() == MYSQL_TYPE_GEOMETRY);
4253 return field->get_geometry_type();
4254 }
4255 const CHARSET_INFO *charset_for_protocol(void) override {
4256 return field->charset_for_protocol();
4257 }
4258
4259#ifndef NDEBUG
4260 void dbug_print() const {
4261 fprintf(DBUG_FILE, "<field ");
4262 if (field) {
4263 fprintf(DBUG_FILE, "'%s.%s': ", field->table->alias, field->field_name);
4264 field->dbug_print();
4265 } else
4266 fprintf(DBUG_FILE, "NULL");
4267
4268 fprintf(DBUG_FILE, ", result_field: ");
4269 if (result_field) {
4270 fprintf(DBUG_FILE, "'%s.%s': ", result_field->table->alias,
4273 } else
4274 fprintf(DBUG_FILE, "NULL");
4275 fprintf(DBUG_FILE, ">\n");
4276 }
4277#endif
4278
4279 float get_filtering_effect(THD *thd, table_map filter_for_table,
4280 table_map read_tables,
4281 const MY_BITMAP *fields_to_ignore,
4282 double rows_in_table) override;
4283
4284 /**
4285 Returns the probability for the predicate "col OP <val>" to be
4286 true for a row in the case where no index statistics or range
4287 estimates are available for 'col'.
4288
4289 The probability depends on the number of rows in the table: it is by
4290 default 'default_filter', but never lower than 1/max_distinct_values
4291 (e.g. number of rows in the table, or the number of distinct values
4292 possible for the datatype if the field provides that kind of
4293 information).
4294
4295 @param max_distinct_values The maximum number of distinct values,
4296 typically the number of rows in the table
4297 @param default_filter The default filter for the predicate
4298
4299 @return the estimated filtering effect for this predicate
4300 */
4301
4302 float get_cond_filter_default_probability(double max_distinct_values,
4303 float default_filter) const;
4304
4305 /**
4306 @note that field->table->alias_name_used is reliable only if
4307 thd->lex->need_correct_ident() is true.
4308 */
4309 bool alias_name_used() const override {
4310 return m_alias_of_expr ||
4311 // maybe the qualifying table was given an alias ("t1 AS foo"):
4313 }
4314
4315 bool repoint_const_outer_ref(uchar *arg) override;
4316 bool returns_array() const override { return field && field->is_array(); }
4317
4318 void set_can_use_prefix_key() override { can_use_prefix_key = true; }
4319
4320 bool replace_field_processor(uchar *arg) override;
4321 bool strip_db_table_name_processor(uchar *) override;
4322
4323 /**
4324 Checks if the current object represents an asterisk select list item
4325
4326 @returns false if a regular column reference, true if an asterisk
4327 select list item.
4328 */
4329 virtual bool is_asterisk() const { return false; }
4330};
4331
4332/**
4333 Represents [schema.][table.]* in a select list
4334
4335 Item_asterisk is used to insert placeholder objects for the special
4336 select list item * (asterisk) into AST.
4337 Those placeholder objects are to be substituted later with e.g. a list of real
4338 table columns by a resolver (@see setup_wild).
4339
4340 @todo The parent class Item_field is redundant. Refactor setup_wild() to
4341 replace Item_field with a simpler one.
4342*/
4345
4346 public:
4347 /**
4348 Constructor
4349
4350 @param pos Location of the * (asterisk) lexeme.
4351 @param opt_schema_name Schema name or nullptr.
4352 @param opt_table_name Table name or nullptr.
4353 */
4354 Item_asterisk(const POS &pos, const char *opt_schema_name,
4355 const char *opt_table_name)
4356 : super(pos, opt_schema_name, opt_table_name, "*") {}
4357
4358 bool itemize(Parse_context *pc, Item **res) override;
4359 bool fix_fields(THD *, Item **) override {
4360 assert(false); // should never happen: see setup_wild()
4361 return true;
4362 }
4363 bool is_asterisk() const override { return true; }
4364};
4365
4366// See if the provided item points to a reachable field (one that belongs to a
4367// table within 'reachable_tables'). If not, go through the list of 'equal'
4368// items in the item and see if we have a field that is reachable. If any such
4369// field is found, set "found" to true and create a new Item_field that points
4370// to this reachable field and return it if we are asked to "replace". If the
4371// provided item is already reachable, or if we cannot find a reachable field,
4372// return the provided item unchanged and set "found" to false. This is used
4373// when creating a hash join iterator, where the join condition may point to a
4374// non-reachable field due to multi-equality propagation during optimization.
4375// (Ideally, the optimizer should not set up such condition in the first place.
4376// This is difficult, if not impossible, to accomplish, given that the plan
4377// created by the optimizer does not map 100% to the iterator executor.) Note
4378// that if the field is not reachable, and we cannot find a reachable field, we
4379// provided field is returned unchanged. The effect is that the hash join will
4380// degrade into a nested loop.
4381Item_field *FindEqualField(Item_field *item_field, table_map reachable_tables,
4382 bool replace, bool *found);
4383
4386
4387 void init() {
4388 set_nullable(true);
4389 null_value = true;
4391 max_length = 0;
4392 fixed = true;
4394 }
4395
4396 protected:
4398 bool no_conversions) override;
4399
4400 public:
4402 init();
4403 item_name = NAME_STRING("NULL");
4404 }
4405 explicit Item_null(const POS &pos) : super(pos) {
4406 init();
4407 item_name = NAME_STRING("NULL");
4408 }
4409
4410 Item_null(const Name_string &name_par) {
4411 init();
4412 item_name = name_par;
4413 }
4414
4415 enum Type type() const override { return NULL_ITEM; }
4416 bool eq(const Item *item, bool binary_cmp) const override;
4417 double val_real() override;
4418 longlong val_int() override;
4419 longlong val_time_temporal() override { return val_int(); }
4420 longlong val_date_temporal() override { return val_int(); }
4421 String *val_str(String *str) override;
4422 my_decimal *val_decimal(my_decimal *) override;
4423 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
4424 bool get_time(MYSQL_TIME *) override { return true; }
4425 bool val_json(Json_wrapper *wr) override;
4426 bool send(Protocol *protocol, String *str) override;
4427 Item_result result_type() const override { return STRING_RESULT; }
4428 Item *clone_item() const override { return new Item_null(item_name); }
4429 bool is_null() override { return true; }
4430
4431 void print(const THD *, String *str,
4432 enum_query_type query_type) const override {
4433 str->append(query_type == QT_NORMALIZED_FORMAT ? "?" : "NULL");
4434 }
4435
4436 Item *safe_charset_converter(THD *thd, const CHARSET_INFO *tocs) override;
4437 bool check_partition_func_processor(uchar *) override { return false; }
4438};
4439
4440/// Dynamic parameters used as placeholders ('?') inside prepared statements
4441
4442class Item_param final : public Item, private Settable_routine_parameter {
4443 typedef Item super;
4444
4445 protected:
4447 bool no_conversions) override;
4448
4449 public:
4456 TIME_VALUE, ///< holds TIME, DATE, DATETIME
4460
4462 m_param_state = state;
4463 }
4464
4466
4467 void mark_json_as_scalar() override { m_json_as_scalar = true; }
4468
4469 /*
4470 A buffer for string and long data values. Historically all allocated
4471 values returned from val_str() were treated as eligible to
4472 modification. I. e. in some cases Item_func_concat can append it's
4473 second argument to return value of the first one. Because of that we
4474 can't return the original buffer holding string data from val_str(),
4475 and have to have one buffer for data and another just pointing to
4476 the data. This is the latter one and it's returned from val_str().
4477 Can not be declared inside the union as it's not a POD type.
4478 */
4481 union {
4483 double real;
4486
4487 private:
4488 /**
4489 True if type of parameter is inherited from parent object (like a typecast).
4490 Reprepare of statement will not change this type.
4491 E.g, we have CAST(? AS DOUBLE), the parameter gets data type
4492 MYSQL_TYPE_DOUBLE and m_type_inherited is set true.
4493 */
4494 bool m_type_inherited{false};
4495 /**
4496 True if type of parameter has been pinned, attempt to use an incompatible
4497 actual type will cause error (no repreparation occurs), and value is
4498 subject to range check. This is used when the parameter is in a context
4499 where its type is imposed. For example, in LIMIT ?, we assign
4500 data_type() == integer, unsigned; and the provided value must be
4501 convertible to unsigned integer: passing a DOUBLE, instead of causing a
4502 repreparation as for an ordinary parameter, will cause an error; passing
4503 integer '-1' will also cause an error.
4504 */
4505 bool m_type_pinned{false};
4506 /**
4507 Parameter objects have a rather complex handling of data type, in order
4508 to consistently handle required type conversion semantics. There are
4509 three data type properties involved:
4510
4511 1. The data_type() property contains the desired type of the parameter
4512 value, as defined by an explicit CAST, the operation the parameter
4513 is part of, and/or the context given by other values and expressions.
4514 After implicit repreparation it may also be assigned from provided
4515 parameter values.
4516
4517 2. The data_type_source() property is the data type of the parameter value,
4518 as given by the supplied user variable or from the protocol buffer.
4519
4520 3. The data_type_actual() property is the data type of the parameter value,
4521