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