MySQL 9.0.0
Source Code Documentation
item_func.h
Go to the documentation of this file.
1#ifndef ITEM_FUNC_INCLUDED
2#define ITEM_FUNC_INCLUDED
3
4/* Copyright (c) 2000, 2024, 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 designed to work 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 either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <sys/types.h>
28
29#include <climits>
30#include <cmath> // isfinite
31#include <cstddef>
32#include <functional>
33
34#include "decimal.h"
35#include "field_types.h"
36#include "ft_global.h"
37#include "lex_string.h"
38#include "my_alloc.h"
39#include "my_base.h"
40#include "my_compiler.h"
41#include "my_dbug.h"
42#include "my_inttypes.h"
44#include "my_table_map.h"
45#include "my_thread_local.h"
46#include "my_time.h"
50#include "mysql_time.h"
51#include "mysqld_error.h"
52#include "sql-common/my_decimal.h" // str2my_decimal
53#include "sql/enum_query_type.h"
54#include "sql/field.h"
55#include "sql/handler.h"
56#include "sql/item.h" // Item_result_field
57#include "sql/parse_location.h" // POS
58#include "sql/set_var.h" // enum_var_type
59#include "sql/sql_const.h"
60#include "sql/sql_udf.h" // udf_handler
61#include "sql/table.h"
62#include "sql/thr_malloc.h"
63#include "sql_string.h"
64#include "template_utils.h"
65
66class Json_wrapper;
67class PT_item_list;
68class Protocol;
69class Query_block;
70class THD;
71class sp_rcontext;
72struct MY_BITMAP;
73struct Parse_context;
74struct TYPELIB;
75
76template <class T>
77class List;
78
79/* Function items used by mysql */
80
81void unsupported_json_comparison(size_t arg_count, Item **args,
82 const char *msg);
83
84void report_conversion_error(const CHARSET_INFO *to_cs, const char *from,
85 size_t from_length, const CHARSET_INFO *from_cs);
86
87bool simplify_string_args(THD *thd, const DTCollation &c, Item **items,
88 uint nitems);
89
91 String *buffer);
92
93inline String *eval_string_arg(const CHARSET_INFO *to_cs, Item *arg,
94 String *buffer) {
95 if (my_charset_same(to_cs, arg->collation.collation))
96 return arg->val_str(buffer);
97 return eval_string_arg_noinline(to_cs, arg, buffer);
98}
99
101 protected:
102 /**
103 Array of pointers to arguments. If there are max 2 arguments, this array
104 is often just m_embedded_arguments; otherwise it's explicitly allocated in
105 the constructor.
106 */
108
109 private:
111
112 /// Allocates space for the given number of arguments, if needed. Uses
113 /// #m_embedded_arguments if it's big enough.
114 bool alloc_args(MEM_ROOT *mem_root, unsigned num_args) {
115 if (num_args <= array_elements(m_embedded_arguments)) {
117 } else {
118 args = mem_root->ArrayAlloc<Item *>(num_args);
119 if (args == nullptr) {
120 // OOM
121 arg_count = 0;
122 return true;
123 }
124 }
125 arg_count = num_args;
126 return false;
127 }
128
129 public:
130 uint arg_count; ///< How many arguments in 'args'
131 virtual uint argument_count() const { return arg_count; }
132 inline Item **arguments() const {
133 return (argument_count() > 0) ? args : nullptr;
134 }
135 /*
136 This function is used to provide a unique textual name for the specific
137 subclass of Item_func. E.g, it returns "+" for the arithmetic addition
138 operator, "abs" for the absolute value function, "avg" for the average
139 aggregate function, etc. The function value is currently used to distinguish
140 Item_func subclasses from each other in Item_func::eq(),
141 since Item_func::functype() is not implemented for every subclass.
142 In addition, the function value is used when printing a textual
143 representation of a function reference, which is used within the dictionary
144 implementation and when printing SQL text for explain purposes.
145 Note that for calls to stored functions and UDF functions, func_name()
146 returns the name of the function. This may overlap with the name of an
147 internal function, thus the functype() must be used together with
148 func_name() to get a unique function reference.
149 For runtime type identification, it is adviced to use Item_func::functype()
150 and Item_sum::sum_func() instead.
151 The value is returned in ASCII character set, except for user-defined
152 functions, whose names are returned in the system character set.
153 */
154 virtual const char *func_name() const = 0;
155
156 bool reject_vector_args();
157 uint num_vector_args();
159
160 protected:
161 /*
162 These decide of types of arguments which are prepared-statement
163 parameters.
164 */
167 bool param_type_is_default(THD *thd, uint start, uint end, uint step,
168 enum_field_types def);
169 bool param_type_is_default(THD *thd, uint start, uint end,
171 return param_type_is_default(thd, start, end, 1, def);
172 }
173 bool param_type_is_rejected(uint start, uint end);
174
175 /**
176 Affects how to determine that NULL argument implies a NULL function return.
177 Default behaviour in this class is:
178 - if true, any NULL argument means the function returns NULL.
179 - if false, no such assumption is made and not_null_tables_cache is thus
180 set to 0.
181 null_on_null is true for all Item_func derived classes, except Item_func_sp,
182 all CASE derived functions and a few other functions.
183 RETURNS NULL ON NULL INPUT can be implemented for stored functions by
184 modifying this member in class Item_func_sp.
185 */
186 bool null_on_null{true};
187 /*
188 Allowed numbers of columns in result (usually 1, which means scalar value)
189 0 means get this number from first argument
190 */
192 /// Value used in calculation of result of used_tables()
194 /// Value used in calculation of result of not_null_tables()
196
197 public:
198 bool is_null_on_null() const { return null_on_null; }
199
200 /*
201 When updating Functype with new spatial functions,
202 is_spatial_operator() should also be updated.
203
204 DD_INTERNAL_FUNC:
205 Some of the internal functions introduced for the INFORMATION_SCHEMA views
206 opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
207 of functions.
208 */
209 enum Functype {
355 };
362 };
363 enum Type type() const override { return FUNC_ITEM; }
364 virtual enum Functype functype() const { return UNKNOWN_FUNC; }
366
367 explicit Item_func(const POS &pos)
369
371 args[0] = a;
373 }
374 Item_func(const POS &pos, Item *a)
376 args[0] = a;
377 }
378
380 args[0] = a;
381 args[1] = b;
385 }
386 Item_func(const POS &pos, Item *a, Item *b)
388 args[0] = a;
389 args[1] = b;
390 }
391
392 Item_func(Item *a, Item *b, Item *c) {
393 if (alloc_args(*THR_MALLOC, 3)) return;
394 args[0] = a;
395 args[1] = b;
396 args[2] = c;
401 }
402
403 Item_func(const POS &pos, Item *a, Item *b, Item *c)
404 : Item_result_field(pos) {
405 if (alloc_args(*THR_MALLOC, 3)) return;
406 args[0] = a;
407 args[1] = b;
408 args[2] = c;
409 }
410
411 Item_func(Item *a, Item *b, Item *c, Item *d) {
412 if (alloc_args(*THR_MALLOC, 4)) return;
413 args[0] = a;
414 args[1] = b;
415 args[2] = c;
416 args[3] = d;
422 }
423
424 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
425 : Item_result_field(pos) {
426 if (alloc_args(*THR_MALLOC, 4)) return;
427 args[0] = a;
428 args[1] = b;
429 args[2] = c;
430 args[3] = d;
431 }
432 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
433 if (alloc_args(*THR_MALLOC, 5)) return;
434 args[0] = a;
435 args[1] = b;
436 args[2] = c;
437 args[3] = d;
438 args[4] = e;
445 }
446 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
447 : Item_result_field(pos) {
448 if (alloc_args(*THR_MALLOC, 5)) return;
449 args[0] = a;
450 args[1] = b;
451 args[2] = c;
452 args[3] = d;
453 args[4] = e;
454 }
455 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e, Item *f) {
456 if (alloc_args(*THR_MALLOC, 6)) return;
457 args[0] = a;
458 args[1] = b;
459 args[2] = c;
460 args[3] = d;
461 args[4] = e;
462 args[5] = f;
470 }
471 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e,
472 Item *f)
473 : Item_result_field(pos) {
474 if (alloc_args(*THR_MALLOC, 6)) return;
475 args[0] = a;
476 args[1] = b;
477 args[2] = c;
478 args[3] = d;
479 args[4] = e;
480 args[5] = f;
481 }
483 set_arguments(list, false);
484 }
485
486 Item_func(const POS &pos, PT_item_list *opt_list);
487
488 // Constructor used for Item_cond_and/or (see Item comment)
489 Item_func(THD *thd, const Item_func *item);
490
491 /// Get the i'th argument of the function that this object represents.
492 virtual Item *get_arg(uint i) { return args[i]; }
493
494 /// Get the i'th argument of the function that this object represents.
495 virtual const Item *get_arg(uint i) const { return args[i]; }
496 virtual Item *set_arg(THD *, uint, Item *) {
497 assert(0);
498 return nullptr;
499 }
500
501 bool do_itemize(Parse_context *pc, Item **res) override;
502
503 bool fix_fields(THD *, Item **ref) override;
504 bool fix_func_arg(THD *, Item **arg);
505 void fix_after_pullout(Query_block *parent_query_block,
506 Query_block *removed_query_block) override;
507 /**
508 Resolve type of function after all arguments have had their data types
509 resolved. Called from resolve_type() when no dynamic parameters
510 are used and from propagate_type() otherwise.
511 */
512 virtual bool resolve_type_inner(THD *) {
513 assert(false);
514 return false;
515 }
516 bool propagate_type(THD *thd, const Type_properties &type) override;
517 /**
518 Returns the pseudo tables depended upon in order to evaluate this
519 function expression. The default implementation returns the empty
520 set.
521 */
522 virtual table_map get_initial_pseudo_tables() const { return 0; }
523 table_map used_tables() const override { return used_tables_cache; }
525 void update_used_tables() override;
527 bool eq(const Item *item) const override;
528 /**
529 Provide a more specific equality check for a function.
530 Combine with Item::eq() to implement a complete equality check.
531 */
532 virtual bool eq_specific(const Item *) const { return true; }
533 virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
534 virtual bool have_rev_func() const { return false; }
535 virtual Item *key_item() const { return args[0]; }
536 /**
537 Copy arguments from list to args array
538
539 @param list function argument list
540 @param context_free true: for use in context-independent
541 constructors (Item_func(POS,...)) i.e. for use
542 in the parser
543 @return true on OOM, false otherwise
544 */
545 bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
546 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
547 mem_root_deque<Item *> *fields) override;
548 void print(const THD *thd, String *str,
549 enum_query_type query_type) const override;
550 void print_op(const THD *thd, String *str, enum_query_type query_type) const;
551 void print_args(const THD *thd, String *str, uint from,
552 enum_query_type query_type) const;
553 virtual void fix_num_length_and_dec();
554 virtual bool is_deprecated() const { return false; }
555 bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
556 return (null_value = args[0]->get_date(ltime, fuzzy_date));
557 }
558 inline bool get_arg0_time(MYSQL_TIME *ltime) {
559 return (null_value = args[0]->get_time(ltime));
560 }
561 bool is_null() override { return update_null_value() || null_value; }
564 friend class udf_handler;
565 Field *tmp_table_field(TABLE *t_arg) override;
566 Item *get_tmp_table_item(THD *thd) override;
567
568 my_decimal *val_decimal(my_decimal *) override;
569
570 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
571 int item_sep) {
572 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
573 false);
574 }
575 /*
576 Aggregate arguments for string result, e.g: CONCAT(a,b)
577 - convert to @@character_set_connection if all arguments are numbers
578 - allow DERIVATION_NONE
579 */
581 uint nitems, int item_sep = 1) {
582 return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
583 item_sep);
584 }
585 /*
586 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
587 - don't convert to @@character_set_connection if all arguments are numbers
588 - don't allow DERIVATION_NONE
589 */
591 uint nitems, int item_sep = 1) {
592 return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
593 item_sep);
594 }
595
596 Item *replace_func_call(uchar *) override;
597
598 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
599 Item *transform(Item_transformer transformer, uchar *arg) override;
600 Item *compile(Item_analyzer analyzer, uchar **arg_p,
601 Item_transformer transformer, uchar *arg_t) override;
602 void traverse_cond(Cond_traverser traverser, void *arg,
603 traverse_order order) override;
604
605 bool replace_equal_field_checker(uchar **arg) override {
606 Replace_equal *replace = pointer_cast<Replace_equal *>(*arg);
607 replace->stack.push_front(this);
608 return true;
609 }
610
612 pointer_cast<Replace_equal *>(arg)->stack.pop();
613 return this;
614 }
615
616 /**
617 Check whether a function allows replacement of a field with another item:
618 In particular, a replacement that changes the metadata of some Item
619 from non-nullable to nullable is not allowed.
620 Notice that e.g. changing the nullability of an operand of a comparison
621 operator in a WHERE clause that ignores UNKNOWN values is allowed,
622 according to this criterion.
623
624 @param original the field that could be replaced
625 @param subst the item that could be the replacement
626
627 @returns true if replacement is allowed, false otherwise
628 */
629 virtual bool allow_replacement(Item_field *const original,
630 Item *const subst) {
631 return original->is_nullable() || !subst->is_nullable();
632 }
633
634 /**
635 Throw an error if the input double number is not finite, i.e. is either
636 +/-INF or NAN.
637 */
638 inline double check_float_overflow(double value) {
639 return std::isfinite(value) ? value : raise_float_overflow();
640 }
641 /**
642 Throw an error if the input BIGINT value represented by the
643 (longlong value, bool unsigned flag) pair cannot be returned by the
644 function, i.e. is not compatible with this Item's unsigned_flag.
645 */
646 inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
647 if ((unsigned_flag && !val_unsigned && value < 0) ||
648 (!unsigned_flag && val_unsigned &&
649 (ulonglong)value > (ulonglong)LLONG_MAX))
650 return raise_integer_overflow();
651 return value;
652 }
653 /**
654 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
655 */
658 }
659
661 assert(fixed);
662 for (uint i = 0; i < arg_count; i++) {
663 if (args[i]->type() == Item::FIELD_ITEM &&
665 return true;
666 }
667 return false;
668 }
669
671 assert(fixed);
672 for (uint i = 0; i < arg_count; i++) {
673 if (args[i]->type() == Item::FIELD_ITEM &&
674 (args[i]->data_type() == MYSQL_TYPE_DATE ||
676 return true;
677 }
678 return false;
679 }
680
682 assert(fixed);
683 for (uint i = 0; i < arg_count; i++) {
684 if (args[i]->type() == Item::FIELD_ITEM &&
685 (args[i]->data_type() == MYSQL_TYPE_TIME ||
687 return true;
688 }
689 return false;
690 }
691
693 assert(fixed);
694 for (uint i = 0; i < arg_count; i++) {
695 if (args[i]->type() == Item::FIELD_ITEM &&
697 return true;
698 }
699 return false;
700 }
701
702 /*
703 We assume the result of any function that has a TIMESTAMP argument to be
704 timezone-dependent, since a TIMESTAMP value in both numeric and string
705 contexts is interpreted according to the current timezone.
706 The only exception is UNIX_TIMESTAMP() which returns the internal
707 representation of a TIMESTAMP argument verbatim, and thus does not depend on
708 the timezone.
709 */
711 return has_timestamp_args();
712 }
713
714 Item *gc_subst_transformer(uchar *arg) override;
715
716 bool resolve_type(THD *thd) override {
717 // By default, pick PS-param's type from other arguments, or VARCHAR
718 return param_type_uses_non_param(thd);
719 }
720
721 /**
722 Whether an arg of a JSON function can be cached to avoid repetitive
723 string->JSON conversion. This function returns true only for those args,
724 which are the source of JSON data. JSON path args are cached independently
725 and for them this function returns false. Same as for all other type of
726 args.
727
728 @param arg the arg to cache
729
730 @retval true arg can be cached
731 @retval false otherwise
732 */
733 virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
734 return CACHE_NONE;
735 }
736
737 /// Whether this Item is an equi-join condition. If this Item is a compound
738 /// item (i.e. multiple condition AND'ed together), it will only return true
739 /// if the Item contains only equi-join conditions AND'ed together. This is
740 /// used to determine whether the condition can be used as a join condition
741 /// for hash join (join conditions in hash join must be equi-join conditions),
742 /// or if it should be placed as a filter after the join.
743 virtual bool contains_only_equi_join_condition() const { return false; }
744
745 protected:
746 /**
747 Whether or not an item should contribute to the filtering effect
748 (@see get_filtering_effect()). First it verifies that table
749 requirements are satisfied as follows:
750
751 1) The item must refer to a field in 'filter_for_table' in some
752 way. This reference may be indirect through any number of
753 intermediate items. For example, this item may be an
754 Item_cond_and which refers to an Item_func_eq which refers to
755 the field.
756 2) The item must not refer to other tables than those already
757 read and the table in 'filter_for_table'
758
759 Then it contines to other properties as follows:
760
761 Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
762 Item_func is to contribute to the filtering effect, then
763
764 1) one of the operands must be a field from 'filter_for_table' that is not
765 in 'fields_to_ignore', and
766 2) depending on the Item_func type filtering effect is calculated
767 for, one or all [1] of the other operand(s) must be an available
768 value, i.e.:
769 - a constant, or
770 - a constant subquery, or
771 - a field value read from a table in 'read_tables', or
772 - a second field in 'filter_for_table', or
773 - a function that only refers to constants or tables in
774 'read_tables', or
775 - special case: an implicit value like NULL in the case of
776 "field IS NULL". Such Item_funcs have arg_count==1.
777
778 [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
779 for the rest (e.g. BETWEEN)
780
781 @param thd The current thread.
782 @param read_tables Tables earlier in the join sequence.
783 Predicates for table 'filter_for_table' that
784 rely on values from these tables can be part of
785 the filter effect.
786 @param filter_for_table The table we are calculating filter effect for
787 @param fields_to_ignore Columns that should be ignored.
788
789
790 @return Item_field that participates in the predicate if none of the
791 requirements are broken, NULL otherwise
792
793 @note: This function only applies to items doing comparison, i.e.
794 boolean predicates. Unfortunately, some of those items do not
795 inherit from Item_bool_func so the member function has to be
796 placed in Item_func.
797 */
799 THD *thd, table_map read_tables, table_map filter_for_table,
800 const MY_BITMAP *fields_to_ignore) const;
801 /**
802 Named parameters are allowed in a parameter list
803
804 The syntax to name parameters in a function call is as follow:
805 <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
806 where "AS" is optional.
807 Only UDF function support that syntax.
808
809 @return true if the function item can have named parameters
810 */
811 virtual bool may_have_named_parameters() const { return false; }
812 bool is_non_const_over_literals(uchar *) override { return false; }
813
814 bool check_function_as_value_generator(uchar *checker_args) override {
815 if (is_deprecated()) {
817 pointer_cast<Check_function_as_value_generator_parameters *>(
818 checker_args);
819 func_arg->banned_function_name = func_name();
820 return true;
821 }
822 return false;
823 }
824 bool is_valid_for_pushdown(uchar *arg) override;
825 bool check_column_in_window_functions(uchar *arg) override;
826 bool check_column_in_group_by(uchar *arg) override;
827
829};
830
831class Item_real_func : public Item_func {
832 public:
834 explicit Item_real_func(const POS &pos) : Item_func(pos) {
836 }
837
839 Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
841 }
842
844
845 Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
847 }
848
851 }
852
855 }
856
857 String *val_str(String *str) override;
858 my_decimal *val_decimal(my_decimal *decimal_value) override;
859 longlong val_int() override {
860 assert(fixed);
862 }
863 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
864 return get_date_from_real(ltime, fuzzydate);
865 }
866 bool get_time(MYSQL_TIME *ltime) override {
867 return get_time_from_real(ltime);
868 }
869 enum Item_result result_type() const override { return REAL_RESULT; }
870};
871
873 protected:
875
876 public:
879 }
881 : Item_func(pos, a), hybrid_type(REAL_RESULT) {
883 }
884
888 }
889 Item_func_numhybrid(const POS &pos, Item *a, Item *b)
890 : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
892 }
893
897 }
901 }
902
903 enum Item_result result_type() const override { return hybrid_type; }
905 return MYSQL_TYPE_DOUBLE;
906 }
907 bool resolve_type(THD *thd) override;
908 bool resolve_type_inner(THD *thd) override;
909 void fix_num_length_and_dec() override;
910 virtual void set_numeric_type() = 0; // To be called from resolve_type()
911
912 double val_real() override;
913 longlong val_int() override;
914 my_decimal *val_decimal(my_decimal *) override;
915 String *val_str(String *str) override;
916 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
917 bool get_time(MYSQL_TIME *ltime) override;
918 /**
919 @brief Performs the operation that this functions implements when the
920 result type is INT.
921
922 @return The result of the operation.
923 */
924 virtual longlong int_op() = 0;
925
926 /**
927 @brief Performs the operation that this functions implements when the
928 result type is REAL.
929
930 @return The result of the operation.
931 */
932 virtual double real_op() = 0;
933
934 /**
935 @brief Performs the operation that this functions implements when the
936 result type is DECIMAL.
937
938 @param decimal_value A pointer where the DECIMAL value will be allocated.
939 @return
940 - 0 If the result is NULL
941 - The same pointer it was given, with the area initialized to the
942 result of the operation.
943 */
944 virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
945
946 /**
947 @brief Performs the operation that this functions implements when the
948 result type is a string type.
949
950 @return The result of the operation.
951 */
952 virtual String *str_op(String *) = 0;
953 /**
954 @brief Performs the operation that this functions implements when the
955 result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
956
957 @return The result of the operation.
958 */
959 virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
960 virtual bool time_op(MYSQL_TIME *ltime) = 0;
961 bool is_null() override { return update_null_value() || null_value; }
962};
963
964/* function where type of result detected by first argument */
966 public:
968 Item_func_num1(const POS &pos, Item *a) : Item_func_numhybrid(pos, a) {}
969
971 Item_func_num1(const POS &pos, Item *a, Item *b)
972 : Item_func_numhybrid(pos, a, b) {}
973
974 void fix_num_length_and_dec() override;
975 void set_numeric_type() override;
976 String *str_op(String *) override {
977 assert(0);
978 return nullptr;
979 }
981 assert(0);
982 return false;
983 }
984 bool time_op(MYSQL_TIME *) override {
985 assert(0);
986 return false;
987 }
988};
989
990/* Base class for operations like '+', '-', '*' */
992 public:
994 Item_num_op(const POS &pos, Item *a, Item *b)
995 : Item_func_numhybrid(pos, a, b) {}
996
997 virtual void result_precision() = 0;
998
999 void print(const THD *thd, String *str,
1000 enum_query_type query_type) const override {
1001 print_op(thd, str, query_type);
1002 }
1003
1004 void set_numeric_type() override;
1005 String *str_op(String *) override {
1006 assert(0);
1007 return nullptr;
1008 }
1010 assert(0);
1011 return false;
1012 }
1013 bool time_op(MYSQL_TIME *) override {
1014 assert(0);
1015 return false;
1016 }
1017};
1018
1019class Item_int_func : public Item_func {
1020 public:
1022 explicit Item_int_func(const POS &pos) : Item_func(pos) {
1024 }
1025
1027 Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
1029 }
1030
1033 }
1034 Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
1036 }
1037
1038 Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
1040 }
1041 Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
1042 : Item_func(pos, a, b, c) {
1044 }
1045
1046 Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
1048 }
1049 Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
1050 : Item_func(pos, a, b, c, d) {
1052 }
1053
1056 }
1057 Item_int_func(const POS &pos, PT_item_list *opt_list)
1058 : Item_func(pos, opt_list) {
1060 }
1061
1062 Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
1064 }
1065 double val_real() override;
1066 String *val_str(String *str) override;
1067 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1068 return get_date_from_int(ltime, fuzzydate);
1069 }
1070 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1071 enum Item_result result_type() const override { return INT_RESULT; }
1072 /*
1073 Concerning PS-param types,
1074 resolve_type(THD *) is not overridden here, as experience shows that for
1075 most child classes of this class, VARCHAR is the best default
1076 */
1077};
1078
1081
1082 public:
1084
1086 return INNER_TABLE_BIT;
1087 }
1088 bool do_itemize(Parse_context *pc, Item **res) override;
1089 const char *func_name() const override { return "connection_id"; }
1090 bool resolve_type(THD *thd) override;
1091 bool fix_fields(THD *thd, Item **ref) override;
1092 longlong val_int() override;
1093 bool check_function_as_value_generator(uchar *checker_args) override {
1095 pointer_cast<Check_function_as_value_generator_parameters *>(
1096 checker_args);
1097 func_arg->banned_function_name = func_name();
1098 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1099 (func_arg->source == VGS_CHECK_CONSTRAINT));
1100 }
1101};
1102
1104 public:
1105 Item_typecast_signed(const POS &pos, Item *a) : Item_int_func(pos, a) {
1106 unsigned_flag = false;
1107 }
1108 const char *func_name() const override { return "cast_as_signed"; }
1109 longlong val_int() override;
1110 bool resolve_type(THD *thd) override;
1111 void print(const THD *thd, String *str,
1112 enum_query_type query_type) const override;
1113 enum Functype functype() const override { return TYPECAST_FUNC; }
1114};
1115
1117 public:
1118 Item_typecast_unsigned(const POS &pos, Item *a) : Item_int_func(pos, a) {
1119 unsigned_flag = true;
1120 }
1121 const char *func_name() const override { return "cast_as_unsigned"; }
1122 longlong val_int() override;
1123 bool resolve_type(THD *thd) override;
1124 void print(const THD *thd, String *str,
1125 enum_query_type query_type) const override;
1126 enum Functype functype() const override { return TYPECAST_FUNC; }
1127};
1128
1129class Item_typecast_decimal final : public Item_func {
1130 protected:
1131 void add_json_info(Json_object *obj) override;
1132
1133 public:
1134 Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1135 : Item_func(pos, a) {
1137 }
1138 String *val_str(String *str) override;
1139 double val_real() override;
1140 longlong val_int() override;
1141 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1142 return get_date_from_decimal(ltime, fuzzydate);
1143 }
1144 bool get_time(MYSQL_TIME *ltime) override {
1145 return get_time_from_decimal(ltime);
1146 }
1147 my_decimal *val_decimal(my_decimal *) override;
1148 enum Item_result result_type() const override { return DECIMAL_RESULT; }
1149 bool resolve_type(THD *thd) override {
1150 if (reject_vector_args()) return true;
1151 if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1152 return true;
1153 return false;
1154 }
1155 const char *func_name() const override { return "cast_as_decimal"; }
1156 enum Functype functype() const override { return TYPECAST_FUNC; }
1157 void print(const THD *thd, String *str,
1158 enum_query_type query_type) const override;
1159};
1160
1161/**
1162 Class used to implement CAST to floating-point data types.
1163*/
1164class Item_typecast_real final : public Item_func {
1165 protected:
1166 void add_json_info(Json_object *obj) override {
1167 obj->add_alias("is_double", create_dom_ptr<Json_boolean>(
1169 }
1170
1171 public:
1172 Item_typecast_real(const POS &pos, Item *a, bool as_double)
1173 : Item_func(pos, a) {
1174 if (as_double)
1176 else
1178 }
1180 String *val_str(String *str) override;
1181 double val_real() override;
1182 longlong val_int() override { return val_int_from_real(); }
1183 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1184 bool get_time(MYSQL_TIME *ltime) override;
1185 my_decimal *val_decimal(my_decimal *decimal_value) override;
1186 enum Item_result result_type() const override { return REAL_RESULT; }
1187 bool resolve_type(THD *thd) override {
1188 if (reject_vector_args()) return true;
1189 return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1190 }
1191 const char *func_name() const override { return "cast_as_real"; }
1192 enum Functype functype() const override { return TYPECAST_FUNC; }
1193 void print(const THD *thd, String *str,
1194 enum_query_type query_type) const override;
1195};
1196
1198 public:
1201 : Item_num_op(pos, a, b) {}
1202
1203 void result_precision() override;
1204 bool check_partition_func_processor(uchar *) override { return false; }
1205 bool check_function_as_value_generator(uchar *) override { return false; }
1206};
1207
1209 public:
1211 Item_func_plus(const POS &pos, Item *a, Item *b)
1212 : Item_func_additive_op(pos, a, b) {}
1213
1214 const char *func_name() const override { return "+"; }
1215
1216 // SUPPRESS_UBSAN: signed integer overflow
1217 longlong int_op() override SUPPRESS_UBSAN;
1218
1219 double real_op() override;
1220 my_decimal *decimal_op(my_decimal *) override;
1221 enum Functype functype() const override { return PLUS_FUNC; }
1222};
1223
1225 public:
1227 Item_func_minus(const POS &pos, Item *a, Item *b)
1228 : Item_func_additive_op(pos, a, b) {}
1229
1230 const char *func_name() const override { return "-"; }
1231
1232 // SUPPRESS_UBSAN: signed integer overflow
1233 longlong int_op() override SUPPRESS_UBSAN;
1234
1235 double real_op() override;
1236 my_decimal *decimal_op(my_decimal *) override;
1237 bool resolve_type(THD *thd) override;
1238 enum Functype functype() const override { return MINUS_FUNC; }
1239};
1240
1241class Item_func_mul final : public Item_num_op {
1242 public:
1244 Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1245
1246 const char *func_name() const override { return "*"; }
1247 longlong int_op() override;
1248 double real_op() override;
1249 my_decimal *decimal_op(my_decimal *) override;
1250 void result_precision() override;
1251 bool check_partition_func_processor(uchar *) override { return false; }
1252 bool check_function_as_value_generator(uchar *) override { return false; }
1253 enum Functype functype() const override { return MUL_FUNC; }
1254};
1255
1257 public:
1258 Item_func_div_base(const POS &pos, Item *a, Item *b)
1259 : Item_num_op(pos, a, b) {}
1261 longlong int_op() override;
1262 double real_op() override;
1263 my_decimal *decimal_op(my_decimal *) override;
1264 enum Functype functype() const override { return DIV_FUNC; }
1265
1266 protected:
1268};
1269
1270class Item_func_div final : public Item_func_div_base {
1271 public:
1272 Item_func_div(const POS &pos, Item *a, Item *b)
1273 : Item_func_div_base(pos, a, b) {}
1274 const char *func_name() const override { return "/"; }
1275 bool resolve_type(THD *thd) override;
1276 void result_precision() override;
1277};
1278
1280 public:
1282 Item_func_div_int(const POS &pos, Item *a, Item *b)
1283 : Item_func_div_base(pos, a, b) {}
1284 const char *func_name() const override { return "DIV"; }
1286 return MYSQL_TYPE_LONGLONG;
1287 }
1288 bool resolve_type(THD *thd) override;
1289 void result_precision() override;
1290 void set_numeric_type() override;
1291 bool check_partition_func_processor(uchar *) override { return false; }
1292 bool check_function_as_value_generator(uchar *) override { return false; }
1293};
1294
1295class Item_func_mod final : public Item_num_op {
1296 public:
1298 Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1299
1300 longlong int_op() override;
1301 double real_op() override;
1302 my_decimal *decimal_op(my_decimal *) override;
1303 const char *func_name() const override { return "%"; }
1304 void result_precision() override;
1305 bool resolve_type(THD *thd) override;
1306 bool check_partition_func_processor(uchar *) override { return false; }
1307 bool check_function_as_value_generator(uchar *) override { return false; }
1308 enum Functype functype() const override { return MOD_FUNC; }
1309};
1310
1311class Item_func_neg final : public Item_func_num1 {
1312 public:
1314 Item_func_neg(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1315
1316 double real_op() override;
1317 longlong int_op() override;
1318 my_decimal *decimal_op(my_decimal *) override;
1319 const char *func_name() const override { return "-"; }
1320 enum Functype functype() const override { return NEG_FUNC; }
1321 bool resolve_type(THD *thd) override;
1322 void fix_num_length_and_dec() override;
1323 bool check_partition_func_processor(uchar *) override { return false; }
1324 bool check_function_as_value_generator(uchar *) override { return false; }
1325};
1326
1327class Item_func_abs final : public Item_func_num1 {
1328 public:
1329 Item_func_abs(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1330 double real_op() override;
1331 longlong int_op() override;
1332 my_decimal *decimal_op(my_decimal *) override;
1333 const char *func_name() const override { return "abs"; }
1334 bool resolve_type(THD *) override;
1335 bool check_partition_func_processor(uchar *) override { return false; }
1336 bool check_function_as_value_generator(uchar *) override { return false; }
1337 enum Functype functype() const override { return ABS_FUNC; }
1338};
1339
1340// A class to handle logarithmic and trigonometric functions
1341
1343 public:
1345 Item_dec_func(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1346
1347 Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1348 bool resolve_type(THD *thd) override;
1349};
1350
1351class Item_func_exp final : public Item_dec_func {
1352 public:
1353 Item_func_exp(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1354 double val_real() override;
1355 const char *func_name() const override { return "exp"; }
1356 enum Functype functype() const override { return EXP_FUNC; }
1357};
1358
1359class Item_func_ln final : public Item_dec_func {
1360 public:
1361 Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1362 double val_real() override;
1363 const char *func_name() const override { return "ln"; }
1364 enum Functype functype() const override { return LN_FUNC; }
1365};
1366
1367class Item_func_log final : public Item_dec_func {
1368 public:
1369 Item_func_log(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1370 Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1371 double val_real() override;
1372 const char *func_name() const override { return "log"; }
1373 enum Functype functype() const override { return LOG_FUNC; }
1374};
1375
1376class Item_func_log2 final : public Item_dec_func {
1377 public:
1378 Item_func_log2(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1379 double val_real() override;
1380 const char *func_name() const override { return "log2"; }
1381};
1382
1383class Item_func_log10 final : public Item_dec_func {
1384 public:
1385 Item_func_log10(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1386 double val_real() override;
1387 const char *func_name() const override { return "log10"; }
1388 enum Functype functype() const override { return LOG10_FUNC; }
1389};
1390
1391class Item_func_sqrt final : public Item_dec_func {
1392 public:
1393 Item_func_sqrt(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1394 double val_real() override;
1395 const char *func_name() const override { return "sqrt"; }
1396 enum Functype functype() const override { return SQRT_FUNC; }
1397};
1398
1399class Item_func_pow final : public Item_dec_func {
1400 public:
1401 Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1402 double val_real() override;
1403 const char *func_name() const override { return "pow"; }
1404 enum Functype functype() const override { return POW_FUNC; }
1405};
1406
1407class Item_func_acos final : public Item_dec_func {
1408 public:
1409 Item_func_acos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1410 double val_real() override;
1411 const char *func_name() const override { return "acos"; }
1412 enum Functype functype() const override { return ACOS_FUNC; }
1413};
1414
1415class Item_func_asin final : public Item_dec_func {
1416 public:
1417 Item_func_asin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1418 double val_real() override;
1419 const char *func_name() const override { return "asin"; }
1420 enum Functype functype() const override { return ASIN_FUNC; }
1421};
1422
1423class Item_func_atan final : public Item_dec_func {
1424 public:
1425 Item_func_atan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1426 Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1427 double val_real() override;
1428 const char *func_name() const override { return "atan"; }
1429 enum Functype functype() const override { return ATAN_FUNC; }
1430};
1431
1432class Item_func_cos final : public Item_dec_func {
1433 public:
1434 Item_func_cos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1435 double val_real() override;
1436 const char *func_name() const override { return "cos"; }
1437 enum Functype functype() const override { return COS_FUNC; }
1438};
1439
1440class Item_func_sin final : public Item_dec_func {
1441 public:
1442 Item_func_sin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1443 double val_real() override;
1444 const char *func_name() const override { return "sin"; }
1445 enum Functype functype() const override { return SIN_FUNC; }
1446};
1447
1448class Item_func_tan final : public Item_dec_func {
1449 public:
1450 Item_func_tan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1451 double val_real() override;
1452 const char *func_name() const override { return "tan"; }
1453 enum Functype functype() const override { return TAN_FUNC; }
1454};
1455
1456class Item_func_cot final : public Item_dec_func {
1457 public:
1458 Item_func_cot(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1459 double val_real() override;
1460 const char *func_name() const override { return "cot"; }
1461 enum Functype functype() const override { return COT_FUNC; }
1462};
1463
1465 public:
1467 Item_func_int_val(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1468 bool resolve_type_inner(THD *thd) override;
1469};
1470
1472 public:
1474 Item_func_ceiling(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1475 const char *func_name() const override { return "ceiling"; }
1476 longlong int_op() override;
1477 double real_op() override;
1478 my_decimal *decimal_op(my_decimal *) override;
1479 bool check_partition_func_processor(uchar *) override { return false; }
1480 bool check_function_as_value_generator(uchar *) override { return false; }
1481 enum Functype functype() const override { return CEILING_FUNC; }
1482};
1483
1485 public:
1487 Item_func_floor(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1488 const char *func_name() const override { return "floor"; }
1489 longlong int_op() override;
1490 double real_op() override;
1491 my_decimal *decimal_op(my_decimal *) override;
1492 bool check_partition_func_processor(uchar *) override { return false; }
1493 bool check_function_as_value_generator(uchar *) override { return false; }
1494 enum Functype functype() const override { return FLOOR_FUNC; }
1495};
1496
1497/* This handles round and truncate */
1498
1499class Item_func_round final : public Item_func_num1 {
1501
1502 public:
1503 Item_func_round(Item *a, Item *b, bool trunc_arg)
1504 : Item_func_num1(a, b), truncate(trunc_arg) {}
1505 Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1506 : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1507
1508 const char *func_name() const override {
1509 return truncate ? "truncate" : "round";
1510 }
1511 double real_op() override;
1512 longlong int_op() override;
1513 my_decimal *decimal_op(my_decimal *) override;
1514 bool resolve_type(THD *) override;
1515 enum Functype functype() const override {
1517 }
1518};
1519
1520class Item_func_rand final : public Item_real_func {
1522
1524 bool first_eval{true}; // true if val_real() is called 1st time
1525 public:
1527 Item_func_rand(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1528 explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1529
1530 bool do_itemize(Parse_context *pc, Item **res) override;
1531 double val_real() override;
1532 const char *func_name() const override { return "rand"; }
1533 /**
1534 This function is non-deterministic and hence depends on the
1535 'RAND' pseudo-table.
1536
1537 @returns RAND_TABLE_BIT
1538 */
1540 return RAND_TABLE_BIT;
1541 }
1542 bool fix_fields(THD *thd, Item **ref) override;
1543 bool resolve_type(THD *thd) override;
1544 void cleanup() override {
1545 first_eval = true;
1547 }
1548 bool check_function_as_value_generator(uchar *checker_args) override {
1550 pointer_cast<Check_function_as_value_generator_parameters *>(
1551 checker_args);
1552 func_arg->banned_function_name = func_name();
1553 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1554 (func_arg->source == VGS_CHECK_CONSTRAINT));
1555 }
1556
1557 private:
1558 void seed_random(Item *val);
1559};
1560
1561class Item_func_sign final : public Item_int_func {
1562 public:
1563 Item_func_sign(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1564 const char *func_name() const override { return "sign"; }
1565 enum Functype functype() const override { return SIGN_FUNC; }
1566 longlong val_int() override;
1567 bool resolve_type(THD *thd) override;
1568};
1569
1570// Common base class for the DEGREES and RADIANS functions.
1572 double mul, add;
1573
1574 protected:
1575 Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1576 : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1577
1578 public:
1579 double val_real() override;
1580 bool resolve_type(THD *thd) override;
1581};
1582
1584 public:
1586 : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1587 const char *func_name() const override { return "degrees"; }
1588 enum Functype functype() const override { return DEGREES_FUNC; }
1589};
1590
1592 public:
1594 : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1595 const char *func_name() const override { return "radians"; }
1596 enum Functype functype() const override { return RADIANS_FUNC; }
1597};
1599 public:
1600 Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1601 : Item_func_numhybrid(pos, opt_list),
1602 m_is_least_func(is_least_func),
1604
1605 longlong val_int() override;
1606 double val_real() override;
1607 my_decimal *val_decimal(my_decimal *) override;
1608 longlong int_op() override;
1609 double real_op() override;
1610 my_decimal *decimal_op(my_decimal *) override;
1611 String *str_op(String *) override;
1612 bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1613 bool time_op(MYSQL_TIME *ltime) override;
1615 return MYSQL_TYPE_VARCHAR;
1616 }
1617 bool resolve_type(THD *thd) override;
1618 bool resolve_type_inner(THD *thd) override;
1619 void set_numeric_type() override {}
1620 enum Item_result result_type() const override { return hybrid_type; }
1621 TYPELIB *get_typelib() const override;
1622
1623 /**
1624 Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1625 return a number in format YYMMDDhhmmss.
1626 */
1627 enum Item_result cast_to_int_type() const override {
1629 }
1630
1631 /// Returns true if arguments to this function should be compared as dates.
1632 bool compare_as_dates() const;
1633
1634 /// Returns true if at least one of the arguments was of temporal type.
1635 bool has_temporal_arg() const { return temporal_item; }
1636
1637 private:
1638 /// True if LEAST function, false if GREATEST.
1641 /*
1642 Used for determining whether one of the arguments is of temporal type and
1643 for converting arguments to a common output format if arguments are
1644 compared as dates and result type is character string. For example,
1645 LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1646 '95-05-05'.
1647 */
1649
1650 /**
1651 Fractional seconds precision to use when converting a time or timestamp
1652 expression into a string.
1653 */
1655 /**
1656 Compare arguments as datetime values.
1657
1658 @param value Pointer to which the datetime value of the winning argument
1659 is written.
1660
1661 @return true if error, false otherwise.
1662 */
1663 bool cmp_datetimes(longlong *value);
1664
1665 /**
1666 Compare arguments as time values.
1667
1668 @param value Pointer to which the time value of the winning argument is
1669 written.
1670
1671 @return true if error, false otherwise.
1672 */
1673 bool cmp_times(longlong *value);
1674};
1675
1676class Item_func_min final : public Item_func_min_max {
1677 public:
1678 Item_func_min(const POS &pos, PT_item_list *opt_list)
1679 : Item_func_min_max(pos, opt_list, true) {}
1680 const char *func_name() const override { return "least"; }
1681 enum Functype functype() const override { return LEAST_FUNC; }
1682};
1683
1684class Item_func_max final : public Item_func_min_max {
1685 public:
1686 Item_func_max(const POS &pos, PT_item_list *opt_list)
1687 : Item_func_min_max(pos, opt_list, false) {}
1688 const char *func_name() const override { return "greatest"; }
1689 enum Functype functype() const override { return GREATEST_FUNC; }
1690};
1691
1692/**
1693 A wrapper Item that normally returns its parameter, but becomes NULL when
1694 processing rows for rollup. Rollup is implemented by AggregateIterator, and
1695 works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1696 where only one group level is active, and so on. E.g., for a query with GROUP
1697 BY a,b, the rows will look like this:
1698
1699 a b rollup level
1700 1 1 2
1701 1 2 2
1702 1 NULL 1
1703 2 1 2
1704 2 NULL 1
1705 NULL NULL 0
1706
1707 Each rollup group item has a minimum level for when it becomes NULL. In the
1708 example above, a would have minimum level 0 and b would have minimum level 1.
1709 For simplicity, the JOIN carries a list of all rollup group items, and they
1710 are being given the current rollup level when it changes. A rollup level of
1711 INT_MAX essentially always disables rollup, which is useful when there are
1712 leftover group items in places that are not relevant for rollup
1713 (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1714 tables that are created before grouping, which should then effectively be
1715 disabled).
1716 */
1717class Item_rollup_group_item final : public Item_func {
1718 public:
1723 // We're going to replace inner_item in the SELECT list, so copy its hidden
1724 // status. (We could have done this in the caller, but it fits naturally in
1725 // with all the other copying done here.)
1727 set_nullable(true);
1729 }
1730 double val_real() override;
1731 longlong val_int() override;
1732 String *val_str(String *str) override;
1734 bool val_json(Json_wrapper *result) override;
1735 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1736 bool get_time(MYSQL_TIME *ltime) override;
1737 const char *func_name() const override { return "rollup_group_item"; }
1738 table_map used_tables() const override {
1739 /*
1740 If underlying item is non-constant, return its used_tables value.
1741 Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1742 */
1743 return args[0]->const_for_execution()
1744 ? (args[0]->used_tables() | RAND_TABLE_BIT)
1745 : args[0]->used_tables();
1746 }
1747 void update_used_tables() override {
1750 }
1751 Item_result result_type() const override { return args[0]->result_type(); }
1752 bool resolve_type(THD *) override {
1753 // needn't handle dynamic parameter as its const_item() is false.
1755
1756 // The item could be a NULL constant.
1757 null_value = args[0]->is_null();
1758 return false;
1759 }
1760 Item *inner_item() { return args[0]; }
1761 const Item *inner_item() const { return args[0]; }
1762 bool rollup_null() const {
1764 }
1765 enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1766 void print(const THD *thd, String *str,
1767 enum_query_type query_type) const override;
1768 bool eq_specific(const Item *item) const override;
1769 TYPELIB *get_typelib() const override;
1770
1771 // Used by AggregateIterator.
1773
1774 // Used when cloning the item only.
1775 int min_rollup_level() const { return m_min_rollup_level; }
1776
1777 private:
1780};
1781
1784
1785 public:
1786 Item_func_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1787 longlong val_int() override;
1788 const char *func_name() const override { return "length"; }
1789 bool resolve_type(THD *thd) override {
1790 if (param_type_is_default(thd, 0, 1)) return true;
1791 max_length = 10;
1792 return false;
1793 }
1794};
1795
1798
1799 public:
1800 Item_func_vector_dim(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1801 longlong val_int() override;
1802 const char *func_name() const override { return "vector_dim"; }
1803 bool resolve_type(THD *thd) override {
1804 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_VECTOR)) {
1805 return true;
1806 }
1807 bool valid_type = (args[0]->data_type() == MYSQL_TYPE_VECTOR) ||
1808 (args[0]->result_type() == STRING_RESULT &&
1810 if (!valid_type) {
1811 my_error(ER_WRONG_ARGUMENTS, MYF(0), func_name());
1812 return true;
1813 }
1814 max_length = 10;
1815 return false;
1816 }
1817};
1818
1820 public:
1821 Item_func_bit_length(const POS &pos, Item *a) : Item_func_length(pos, a) {}
1822 longlong val_int() override {
1823 assert(fixed);
1824 return Item_func_length::val_int() * 8;
1825 }
1826 const char *func_name() const override { return "bit_length"; }
1827};
1828
1831
1832 public:
1834 Item_func_char_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1835 longlong val_int() override;
1836 const char *func_name() const override { return "char_length"; }
1837 bool resolve_type(THD *thd) override {
1838 max_length = 10;
1839 return Item_int_func::resolve_type(thd);
1840 }
1841};
1842
1844 public:
1845 Item_func_coercibility(const POS &pos, Item *a) : Item_int_func(pos, a) {
1846 null_on_null = false;
1847 }
1848 longlong val_int() override;
1849 const char *func_name() const override { return "coercibility"; }
1850 bool resolve_type(THD *thd) override {
1851 max_length = 10;
1852 set_nullable(false);
1853 return Item_int_func::resolve_type(thd);
1854 }
1855};
1856
1859
1860 public:
1862 Item_func_locate(const POS &pos, Item *a, Item *b)
1863 : Item_int_func(pos, a, b) {}
1864 Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1865 : Item_int_func(pos, a, b, c) {}
1866
1867 const char *func_name() const override { return "locate"; }
1868 longlong val_int() override;
1869 bool resolve_type(THD *thd) override;
1870 void print(const THD *thd, String *str,
1871 enum_query_type query_type) const override;
1872};
1873
1874class Item_func_instr final : public Item_func_locate {
1875 public:
1876 Item_func_instr(const POS &pos, Item *a, Item *b)
1877 : Item_func_locate(pos, a, b) {}
1878
1879 const char *func_name() const override { return "instr"; }
1880};
1881
1883 public:
1885 : Item_int_func(pos, a) {}
1886 longlong val_int() override;
1887 const char *func_name() const override {
1888 return "validate_password_strength";
1889 }
1890 bool resolve_type(THD *thd) override {
1891 max_length = 10;
1892 set_nullable(true);
1893 return Item_int_func::resolve_type(thd);
1894 }
1895};
1896
1897class Item_func_field final : public Item_int_func {
1900
1901 public:
1902 Item_func_field(const POS &pos, PT_item_list *opt_list)
1903 : Item_int_func(pos, opt_list) {}
1904 longlong val_int() override;
1905 const char *func_name() const override { return "field"; }
1906 bool resolve_type(THD *thd) override;
1907};
1908
1909class Item_func_ascii final : public Item_int_func {
1911
1912 public:
1913 Item_func_ascii(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1914 longlong val_int() override;
1915 const char *func_name() const override { return "ascii"; }
1916 bool resolve_type(THD *thd) override {
1917 max_length = 3;
1918 return Item_int_func::resolve_type(thd);
1919 }
1920};
1921
1922class Item_func_ord final : public Item_int_func {
1924
1925 public:
1926 Item_func_ord(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1927 longlong val_int() override;
1928 const char *func_name() const override { return "ord"; }
1929};
1930
1933 /*
1934 if m_enum_value is non-zero, it indicates the index of the value of
1935 argument 0 in the set in argument 1, given that argument 0 is
1936 a constant value and argument 1 is a field of type SET.
1937 */
1940
1941 public:
1943 : Item_int_func(pos, a, b) {}
1944 longlong val_int() override;
1945 const char *func_name() const override { return "find_in_set"; }
1946 bool resolve_type(THD *) override;
1947 const CHARSET_INFO *compare_collation() const override {
1948 return cmp_collation.collation;
1949 }
1950};
1951
1952/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1953
1954class Item_func_bit : public Item_func {
1955 protected:
1956 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1958 /// Buffer storing the determined value
1960 /**
1961 @returns true if the second argument should be of binary type for the
1962 result to be of binary type.
1963 */
1965
1966 public:
1967 Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1968 Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1969
1970 bool resolve_type(THD *) override;
1971 enum Item_result result_type() const override { return hybrid_type; }
1972
1973 longlong val_int() override;
1974 String *val_str(String *str) override;
1975 double val_real() override;
1976 my_decimal *val_decimal(my_decimal *decimal_value) override;
1977
1978 void print(const THD *thd, String *str,
1979 enum_query_type query_type) const override {
1980 print_op(thd, str, query_type);
1981 }
1982 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1983 if (hybrid_type == INT_RESULT)
1984 return get_date_from_int(ltime, fuzzydate);
1985 else
1986 return get_date_from_string(ltime, fuzzydate);
1987 }
1988 bool get_time(MYSQL_TIME *ltime) override {
1989 if (hybrid_type == INT_RESULT)
1990 return get_time_from_int(ltime);
1991 else
1992 return get_time_from_string(ltime);
1993 }
1994
1995 private:
1996 /**
1997 @brief Performs the operation on integers to produce a result of type
1998 INT_RESULT.
1999 @return The result of the operation.
2000 */
2001 virtual longlong int_op() = 0;
2002
2003 /**
2004 @brief Performs the operation on binary strings to produce a result of
2005 type STRING_RESULT.
2006 @return The result of the operation.
2007 */
2008 virtual String *str_op(String *) = 0;
2009};
2010
2011/**
2012 Base class for all the bit functions that work with two binary
2013 arguments: '&', '|', '^'.
2014*/
2015
2017 protected:
2019 return true;
2020 }
2021 template <class Char_func, class Int_func>
2022 String *eval_str_op(String *, Char_func char_func, Int_func int_func);
2023 template <class Int_func>
2024 longlong eval_int_op(Int_func int_func);
2025
2026 public:
2028 : Item_func_bit(pos, a, b) {}
2029};
2030
2032 public:
2033 Item_func_bit_or(const POS &pos, Item *a, Item *b)
2034 : Item_func_bit_two_param(pos, a, b) {}
2035 const char *func_name() const override { return "|"; }
2036
2037 private:
2038 longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
2039 String *str_op(String *str) override {
2040 return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
2041 }
2042};
2043
2045 public:
2046 Item_func_bit_and(const POS &pos, Item *a, Item *b)
2047 : Item_func_bit_two_param(pos, a, b) {}
2048 const char *func_name() const override { return "&"; }
2049
2050 private:
2051 longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
2052 String *str_op(String *str) override {
2053 return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
2054 }
2055};
2056
2058 public:
2059 Item_func_bit_xor(const POS &pos, Item *a, Item *b)
2060 : Item_func_bit_two_param(pos, a, b) {}
2061 const char *func_name() const override { return "^"; }
2062
2063 private:
2064 longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
2065 String *str_op(String *str) override {
2066 return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
2067 }
2068};
2069
2071 public:
2072 Item_func_bit_count(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2073 longlong val_int() override;
2074 const char *func_name() const override { return "bit_count"; }
2075 bool resolve_type(THD *thd) override {
2076 // Default: binary string; reprepare if integer
2077 if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
2078 args[0]->propagate_type(
2080 return true;
2082 return false;
2083 }
2084};
2085
2087 protected:
2089 return false;
2090 }
2091 template <bool to_left>
2093 template <bool to_left>
2095
2096 public:
2097 Item_func_shift(const POS &pos, Item *a, Item *b)
2098 : Item_func_bit(pos, a, b) {}
2099};
2100
2102 public:
2103 Item_func_shift_left(const POS &pos, Item *a, Item *b)
2104 : Item_func_shift(pos, a, b) {}
2105 const char *func_name() const override { return "<<"; }
2106
2107 private:
2108 longlong int_op() override;
2109 String *str_op(String *str) override;
2110};
2111
2113 public:
2115 : Item_func_shift(pos, a, b) {}
2116 const char *func_name() const override { return ">>"; }
2117
2118 private:
2119 longlong int_op() override;
2120 String *str_op(String *str) override;
2121};
2122
2123class Item_func_bit_neg final : public Item_func_bit {
2124 protected:
2126 return false;
2127 }
2128
2129 public:
2130 Item_func_bit_neg(const POS &pos, Item *a) : Item_func_bit(pos, a) {}
2131 const char *func_name() const override { return "~"; }
2132 void print(const THD *thd, String *str,
2133 enum_query_type query_type) const override {
2134 Item_func::print(thd, str, query_type);
2135 }
2136
2137 private:
2138 longlong int_op() override;
2139 String *str_op(String *str) override;
2140};
2141
2144
2145 public:
2147 explicit Item_func_last_insert_id(const POS &pos) : Item_int_func(pos) {}
2148 Item_func_last_insert_id(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2149
2150 bool do_itemize(Parse_context *pc, Item **res) override;
2151 longlong val_int() override;
2152 const char *func_name() const override { return "last_insert_id"; }
2153
2155 return INNER_TABLE_BIT;
2156 }
2157
2158 bool resolve_type(THD *thd) override {
2159 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2160 unsigned_flag = true;
2161 return false;
2162 }
2163 bool check_function_as_value_generator(uchar *checker_args) override {
2165 pointer_cast<Check_function_as_value_generator_parameters *>(
2166 checker_args);
2167 func_arg->banned_function_name = func_name();
2168 return true;
2169 }
2170};
2171
2174
2175 public:
2176 Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
2177 : Item_int_func(pos, count_expr, expr) {}
2178
2179 /// Ensure that "benchmark()" is never optimized away
2181 return RAND_TABLE_BIT;
2182 }
2183
2184 bool do_itemize(Parse_context *pc, Item **res) override;
2185 longlong val_int() override;
2186 const char *func_name() const override { return "benchmark"; }
2187 bool resolve_type(THD *thd) override {
2188 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2189 if (param_type_is_default(thd, 1, 2)) return true;
2190 max_length = 1;
2191 set_nullable(true);
2192 return false;
2193 }
2194 void print(const THD *thd, String *str,
2195 enum_query_type query_type) const override;
2196 bool check_function_as_value_generator(uchar *checker_args) override {
2198 pointer_cast<Check_function_as_value_generator_parameters *>(
2199 checker_args);
2200 func_arg->banned_function_name = func_name();
2201 return true;
2202 }
2203};
2204
2207
2208class Item_func_sleep final : public Item_int_func {
2210
2211 public:
2212 Item_func_sleep(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2213
2214 bool do_itemize(Parse_context *pc, Item **res) override;
2215 const char *func_name() const override { return "sleep"; }
2216 /**
2217 This function is non-deterministic and hence depends on the
2218 'RAND' pseudo-table.
2219
2220 @returns RAND_TABLE_BIT
2221 */
2223 return RAND_TABLE_BIT;
2224 }
2225 bool check_function_as_value_generator(uchar *checker_args) override {
2227 pointer_cast<Check_function_as_value_generator_parameters *>(
2228 checker_args);
2229 func_arg->banned_function_name = func_name();
2230 return true;
2231 }
2232 bool resolve_type(THD *thd) override {
2233 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2234 return Item_int_func::resolve_type(thd);
2235 }
2236 longlong val_int() override;
2237};
2238
2239class Item_udf_func : public Item_func {
2241
2242 protected:
2244
2245 public:
2246 Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2247 : Item_func(pos, opt_list), udf(udf_arg) {
2248 null_on_null = false;
2249 }
2250 ~Item_udf_func() override = default;
2251
2252 bool do_itemize(Parse_context *pc, Item **res) override;
2253 const char *func_name() const override { return udf.name(); }
2254 enum Functype functype() const override { return UDF_FUNC; }
2257 }
2258 bool fix_fields(THD *thd, Item **ref) override;
2259 void cleanup() override;
2260 Item_result result_type() const override { return udf.result_type(); }
2261 void print(const THD *thd, String *str,
2262 enum_query_type query_type) const override;
2263
2264 bool check_function_as_value_generator(uchar *checker_args) override {
2266 pointer_cast<Check_function_as_value_generator_parameters *>(
2267 checker_args);
2268 func_arg->banned_function_name = func_name();
2269 return true;
2270 }
2271
2272 void compute_cost(CostOfItem *root_cost) const override {
2273 root_cost->MarkExpensive();
2274 }
2275
2276 protected:
2277 bool may_have_named_parameters() const override { return true; }
2278
2279 private:
2280 /**
2281 This member is set during resolving and is used by update_used_tables() and
2282 fix_after_pullout() to preserve the non-deterministic property.
2283 */
2285};
2286
2288 public:
2289 Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2290 : Item_udf_func(pos, udf_arg, opt_list) {}
2291 longlong val_int() override {
2292 assert(fixed);
2294 }
2295 my_decimal *val_decimal(my_decimal *dec_buf) override {
2296 const double res = val_real();
2297 if (null_value) return nullptr;
2298 double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2299 return dec_buf;
2300 }
2301 double val_real() override;
2302 String *val_str(String *str) override;
2303 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2304 return get_date_from_real(ltime, fuzzydate);
2305 }
2306 bool get_time(MYSQL_TIME *ltime) override {
2307 return get_time_from_real(ltime);
2308 }
2309 bool resolve_type(THD *) override {
2311 fix_num_length_and_dec(); // @todo - needed?
2312 return false;
2313 }
2314};
2315
2316class Item_func_udf_int final : public Item_udf_func {
2317 public:
2318 Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2319 : Item_udf_func(pos, udf_arg, opt_list) {}
2320 longlong val_int() override;
2321 double val_real() override {
2322 return static_cast<double>(Item_func_udf_int::val_int());
2323 }
2324 String *val_str(String *str) override;
2325 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2326 return get_date_from_int(ltime, fuzzydate);
2327 }
2328 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2329 enum Item_result result_type() const override { return INT_RESULT; }
2330 bool resolve_type(THD *) override {
2332 return false;
2333 }
2334};
2335
2337 public:
2338 Item_func_udf_decimal(const POS &pos, udf_func *udf_arg,
2339 PT_item_list *opt_list)
2340 : Item_udf_func(pos, udf_arg, opt_list) {}
2341 longlong val_int() override;
2342 double val_real() override;
2343 my_decimal *val_decimal(my_decimal *) override;
2344 String *val_str(String *str) override;
2345 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2346 return get_date_from_decimal(ltime, fuzzydate);
2347 }
2348 bool get_time(MYSQL_TIME *ltime) override {
2349 return get_time_from_decimal(ltime);
2350 }
2351 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2352 bool resolve_type(THD *thd) override;
2353};
2354
2356 public:
2357 Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2358 : Item_udf_func(pos, udf_arg, opt_list) {}
2359
2360 String *val_str(String *) override;
2361 double val_real() override {
2362 int err_not_used;
2363 const char *end_not_used;
2364 String *res;
2365 res = val_str(&str_value);
2366 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2367 &end_not_used, &err_not_used)
2368 : 0.0;
2369 }
2370 longlong val_int() override {
2371 int err_not_used;
2372 String *res;
2373 res = val_str(&str_value);
2374 return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2375 nullptr, &err_not_used)
2376 : (longlong)0;
2377 }
2378 my_decimal *val_decimal(my_decimal *dec_buf) override {
2379 String *res = val_str(&str_value);
2380 if (!res) return nullptr;
2381 str2my_decimal(E_DEC_FATAL_ERROR, res->ptr(), res->length(), res->charset(),
2382 dec_buf);
2383 return dec_buf;
2384 }
2385 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2386 return get_date_from_string(ltime, fuzzydate);
2387 }
2388 bool get_time(MYSQL_TIME *ltime) override {
2389 return get_time_from_string(ltime);
2390 }
2391 enum Item_result result_type() const override { return STRING_RESULT; }
2392 bool resolve_type(THD *thd) override;
2393};
2394
2395void mysql_ull_cleanup(THD *thd);
2397
2398class Item_func_get_lock final : public Item_int_func {
2400
2402
2403 public:
2404 Item_func_get_lock(const POS &pos, Item *a, Item *b)
2405 : Item_int_func(pos, a, b) {}
2406
2407 bool do_itemize(Parse_context *pc, Item **res) override;
2408 longlong val_int() override;
2409 const char *func_name() const override { return "get_lock"; }
2410 bool resolve_type(THD *thd) override {
2411 if (param_type_is_default(thd, 0, 1)) return true;
2412 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
2413 max_length = 1;
2414 set_nullable(true);
2415 return false;
2416 }
2417 bool is_non_const_over_literals(uchar *) override { return true; }
2418 bool check_function_as_value_generator(uchar *checker_args) override {
2420 pointer_cast<Check_function_as_value_generator_parameters *>(
2421 checker_args);
2422 func_arg->banned_function_name = func_name();
2423 return true;
2424 }
2425};
2426
2429
2431
2432 public:
2433 Item_func_release_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2434 bool do_itemize(Parse_context *pc, Item **res) override;
2435
2436 longlong val_int() override;
2437 const char *func_name() const override { return "release_lock"; }
2438 bool resolve_type(THD *thd) override {
2439 if (param_type_is_default(thd, 0, 1)) return true;
2440 max_length = 1;
2441 set_nullable(true);
2442 return false;
2443 }
2444 bool is_non_const_over_literals(uchar *) override { return true; }
2445 bool check_function_as_value_generator(uchar *checker_args) override {
2447 pointer_cast<Check_function_as_value_generator_parameters *>(
2448 checker_args);
2449 func_arg->banned_function_name = func_name();
2450 return true;
2451 }
2452};
2453
2456
2457 public:
2458 explicit Item_func_release_all_locks(const POS &pos) : Item_int_func(pos) {}
2459 bool do_itemize(Parse_context *pc, Item **res) override;
2460
2461 longlong val_int() override;
2462 const char *func_name() const override { return "release_all_locks"; }
2463 bool resolve_type(THD *) override {
2464 unsigned_flag = true;
2465 return false;
2466 }
2467 bool is_non_const_over_literals(uchar *) override { return true; }
2468 bool check_function_as_value_generator(uchar *checker_args) override {
2470 pointer_cast<Check_function_as_value_generator_parameters *>(
2471 checker_args);
2472 func_arg->banned_function_name = func_name();
2473 return true;
2474 }
2475};
2476
2477/* replication functions */
2478
2482
2483 public:
2484 Item_source_pos_wait(const POS &pos, Item *a, Item *b)
2485 : Item_int_func(pos, a, b) {}
2486 Item_source_pos_wait(const POS &pos, Item *a, Item *b, Item *c)
2487 : Item_int_func(pos, a, b, c) {}
2488 Item_source_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2489 : Item_int_func(pos, a, b, c, d) {}
2490
2491 bool do_itemize(Parse_context *pc, Item **res) override;
2492 longlong val_int() override;
2493 const char *func_name() const override { return "source_pos_wait"; }
2494 bool resolve_type(THD *thd) override {
2495 if (param_type_is_default(thd, 0, 1)) return true;
2496 if (param_type_is_default(thd, 1, 3, MYSQL_TYPE_LONGLONG)) return true;
2497 if (param_type_is_default(thd, 3, 4)) return true;
2498 max_length = 21;
2499 set_nullable(true);
2500 return false;
2501 }
2502 bool check_function_as_value_generator(uchar *checker_args) override {
2504 pointer_cast<Check_function_as_value_generator_parameters *>(
2505 checker_args);
2506 func_arg->banned_function_name = func_name();
2507 return true;
2508 }
2509};
2510
2512 public:
2513 Item_master_pos_wait(const POS &pos, Item *a, Item *b)
2514 : Item_source_pos_wait(pos, a, b) {}
2515 Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c)
2516 : Item_source_pos_wait(pos, a, b, c) {}
2517 Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2518 : Item_source_pos_wait(pos, a, b, c, d) {}
2519 longlong val_int() override;
2520};
2521
2522/**
2523 Internal functions used by INFORMATION_SCHEMA implementation to check
2524 if user have access to given database/table/column.
2525*/
2526
2528 public:
2530 : Item_int_func(pos, a) {}
2531 longlong val_int() override;
2532 const char *func_name() const override { return "can_access_database"; }
2533 bool resolve_type(THD *) override {
2534 set_nullable(true);
2535 return false;
2536 }
2537};
2538
2540 public:
2542 : Item_int_func(pos, a, b) {}
2543 longlong val_int() override;
2544 const char *func_name() const override { return "can_access_table"; }
2545 bool resolve_type(THD *) override {
2546 set_nullable(true);
2547 return false;
2548 }
2549};
2550
2552 public:
2554 : Item_int_func(pos, a, b) {}
2555 longlong val_int() override;
2556 const char *func_name() const override { return "can_access_user"; }
2557 bool resolve_type(THD *) override {
2558 set_nullable(true);
2559 return false;
2560 }
2561};
2562
2564 public:
2566 : Item_int_func(pos, a, b) {}
2567 longlong val_int() override;
2568 const char *func_name() const override { return "can_access_trigger"; }
2569 bool resolve_type(THD *) override {
2570 max_length = 4;
2571 set_nullable(true);
2572 return false;
2573 }
2574};
2575
2577 public:
2579 : Item_int_func(pos, list) {}
2580 longlong val_int() override;
2581 const char *func_name() const override { return "can_access_routine"; }
2582 bool resolve_type(THD *) override {
2583 max_length = 4;
2584 set_nullable(true);
2585 return false;
2586 }
2587};
2588
2590 public:
2592 longlong val_int() override;
2593 const char *func_name() const override { return "can_access_event"; }
2594 bool resolve_type(THD *) override {
2595 set_nullable(true);
2596 return false;
2597 }
2598};
2599
2601 public:
2603 : Item_int_func(pos, a) {}
2604 longlong val_int() override;
2605 const char *func_name() const override { return "can_access_resource_group"; }
2606 bool resolve_type(THD *) override {
2607 max_length = 1; // Function can return 0 or 1.
2608 set_nullable(true);
2609 return false;
2610 }
2611};
2612
2614 public:
2615 Item_func_can_access_view(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2616 : Item_int_func(pos, a, b, c, d) {}
2617 longlong val_int() override;
2618 const char *func_name() const override { return "can_access_view"; }
2619 bool resolve_type(THD *) override {
2620 set_nullable(true);
2621 return false;
2622 }
2623};
2624
2626 public:
2628 : Item_int_func(pos, a, b, c) {}
2629 longlong val_int() override;
2630 const char *func_name() const override { return "can_access_column"; }
2631 bool resolve_type(THD *) override {
2632 set_nullable(true);
2633 return false;
2634 }
2635};
2636
2638 public:
2640 : Item_int_func(pos, a) {}
2642 : Item_int_func(pos, a, b) {}
2644 : Item_int_func(pos, a, b, c) {}
2645 longlong val_int() override;
2646 const char *func_name() const override { return "is_visible_dd_object"; }
2647 bool resolve_type(THD *) override {
2648 max_length = 1;
2649 set_nullable(true);
2650 return false;
2651 }
2652};
2653
2655 public:
2657 : Item_int_func(pos, list) {}
2658 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2659 longlong val_int() override;
2660 const char *func_name() const override { return "internal_table_rows"; }
2661 bool resolve_type(THD *) override {
2662 set_nullable(true);
2663 unsigned_flag = true;
2664 null_on_null = false;
2665 return false;
2666 }
2667};
2668
2670 public:
2672 : Item_int_func(pos, list) {}
2673 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2674 longlong val_int() override;
2675 const char *func_name() const override { return "internal_avg_row_length"; }
2676 bool resolve_type(THD *) override {
2677 set_nullable(true);
2678 unsigned_flag = true;
2679 null_on_null = false;
2680 return false;
2681 }
2682};
2683
2685 public:
2687 : Item_int_func(pos, list) {}
2688 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2689 longlong val_int() override;
2690 const char *func_name() const override { return "internal_data_length"; }
2691 bool resolve_type(THD *) override {
2692 set_nullable(true);
2693 unsigned_flag = true;
2694 null_on_null = false;
2695 return false;
2696 }
2697};
2698
2700 public:
2702 : Item_int_func(pos, list) {}
2703 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2704 longlong val_int() override;
2705 const char *func_name() const override { return "internal_max_data_length"; }
2706 bool resolve_type(THD *) override {
2707 set_nullable(true);
2708 unsigned_flag = true;
2709 null_on_null = false;
2710 return false;
2711 }
2712};
2713
2715 public:
2717 : Item_int_func(pos, list) {}
2718 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2719 longlong val_int() override;
2720 const char *func_name() const override { return "internal_index_length"; }
2721 bool resolve_type(THD *) override {
2722 set_nullable(true);
2723 unsigned_flag = true;
2724 null_on_null = false;
2725 return false;
2726 }
2727};
2728
2730 public:
2732 : Item_int_func(pos, list) {}
2733 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2734 longlong val_int() override;
2735 const char *func_name() const override { return "internal_data_free"; }
2736 bool resolve_type(THD *) override {
2737 set_nullable(true);
2738 unsigned_flag = true;
2739 null_on_null = false;
2740 return false;
2741 }
2742};
2743
2745 public:
2747 : Item_int_func(pos, list) {}
2748 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2749 longlong val_int() override;
2750 const char *func_name() const override { return "internal_auto_increment"; }
2751 bool resolve_type(THD *) override {
2752 set_nullable(true);
2753 unsigned_flag = true;
2754 null_on_null = false;
2755 return false;
2756 }
2757};
2758
2760 public:
2762 : Item_int_func(pos, list) {}
2763 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2764 longlong val_int() override;
2765 const char *func_name() const override { return "internal_checksum"; }
2766 bool resolve_type(THD *) override {
2767 set_nullable(true);
2768 null_on_null = false;
2769 return false;
2770 }
2771};
2772
2774 public:
2776 : Item_int_func(pos, a) {}
2777 longlong val_int() override;
2778 const char *func_name() const override { return "internal_keys_disabled"; }
2779 bool resolve_type(THD *) override {
2780 set_nullable(false);
2781 null_on_null = false;
2782 return false;
2783 }
2784};
2785
2787 public:
2790 : Item_int_func(pos, list) {}
2791 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2792 longlong val_int() override;
2793 const char *func_name() const override {
2794 return "internal_index_column_cardinality";
2795 }
2796 bool resolve_type(THD *) override {
2797 set_nullable(true);
2798 null_on_null = false;
2799 return false;
2800 }
2801};
2802
2804 public:
2806 Item *d)
2807 : Item_int_func(pos, a, b, c, d) {}
2808 longlong val_int() override;
2809 const char *func_name() const override { return "internal_dd_char_length"; }
2810 bool resolve_type(THD *) override {
2811 set_nullable(true);
2812 null_on_null = false;
2813 return false;
2814 }
2815};
2816
2818 : public Item_int_func {
2819 public:
2822 : Item_int_func(pos, list) {}
2823 longlong val_int() override;
2824 const char *func_name() const override {
2825 return "internal_get_view_warning_or_error";
2826 }
2827 bool resolve_type(THD *) override {
2828 max_length = 1;
2829 set_nullable(false);
2830 null_on_null = false;
2831 return false;
2832 }
2833};
2834
2836 public:
2838 : Item_int_func(pos, list) {}
2839 longlong val_int() override;
2840 bool resolve_type(THD *) override {
2841 set_nullable(true);
2842 null_on_null = false;
2843 return false;
2844 }
2845 const char *func_name() const override {
2846 return "get_dd_index_sub_part_length";
2847 }
2848};
2849
2851 public:
2853 Item *d)
2854 : Item_int_func(pos, a, b, c, d) {}
2855 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2856 longlong val_int() override;
2857 const char *func_name() const override { return "internal_tablespace_id"; }
2858 bool resolve_type(THD *) override {
2859 set_nullable(true);
2860 null_on_null = false;
2861 return false;
2862 }
2863};
2864
2866 : public Item_int_func {
2867 public:
2869 Item *b, Item *c, Item *d)
2870 : Item_int_func(pos, a, b, c, d) {}
2871
2872 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2873 longlong val_int() override;
2874
2875 const char *func_name() const override {
2876 return "internal_tablespace_logfile_group_number";
2877 }
2878
2879 bool resolve_type(THD *) override {
2880 set_nullable(true);
2881 null_on_null = false;
2882 return false;
2883 }
2884};
2885
2887 public:
2889 Item *c, Item *d)
2890 : Item_int_func(pos, a, b, c, d) {}
2891
2892 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2893 longlong val_int() override;
2894
2895 const char *func_name() const override {
2896 return "internal_tablespace_free_extents";
2897 }
2898
2899 bool resolve_type(THD *) override {
2900 set_nullable(true);
2901 null_on_null = false;
2902 return false;
2903 }
2904};
2905
2907 public:
2909 Item *c, Item *d)
2910 : Item_int_func(pos, a, b, c, d) {}
2911
2912 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2913 longlong val_int() override;
2914
2915 const char *func_name() const override {
2916 return "internal_tablespace_total_extents";
2917 }
2918
2919 bool resolve_type(THD *) override {
2920 set_nullable(true);
2921 null_on_null = false;
2922 return false;
2923 }
2924};
2925
2927 public:
2929 Item *c, Item *d)
2930 : Item_int_func(pos, a, b, c, d) {}
2931
2932 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2933 longlong val_int() override;
2934
2935 const char *func_name() const override {
2936 return "internal_tablespace_extent_size";
2937 }
2938
2939 bool resolve_type(THD *) override {
2940 set_nullable(true);
2941 null_on_null = false;
2942 return false;
2943 }
2944};
2945
2947 public:
2949 Item *c, Item *d)
2950 : Item_int_func(pos, a, b, c, d) {}
2951
2952 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2953 longlong val_int() override;
2954
2955 const char *func_name() const override {
2956 return "internal_tablespace_initial_size";
2957 }
2958
2959 bool resolve_type(THD *) override {
2960 set_nullable(true);
2961 null_on_null = false;
2962 return false;
2963 }
2964};
2965
2967 public:
2969 Item *c, Item *d)
2970 : Item_int_func(pos, a, b, c, d) {}
2971
2972 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2973 longlong val_int() override;
2974
2975 const char *func_name() const override {
2976 return "internal_tablespace_maximum_size";
2977 }
2978
2979 bool resolve_type(THD *) override {
2980 set_nullable(true);
2981 null_on_null = false;
2982 return false;
2983 }
2984};
2985
2987 public:
2989 Item *b, Item *c, Item *d)
2990 : Item_int_func(pos, a, b, c, d) {}
2991
2992 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2993 longlong val_int() override;
2994
2995 const char *func_name() const override {
2996 return "internal_tablespace_autoextend_size";
2997 }
2998
2999 bool resolve_type(THD *) override {
3000 set_nullable(true);
3001 null_on_null = false;
3002 return false;
3003 }
3004};
3005
3007 public:
3009 Item *c, Item *d)
3010 : Item_int_func(pos, a, b, c, d) {}
3011
3012 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3013 longlong val_int() override;
3014
3015 const char *func_name() const override {
3016 return "internal_tablespace_version";
3017 }
3018
3019 bool resolve_type(THD *) override {
3020 set_nullable(true);
3021 null_on_null = false;
3022 return false;
3023 }
3024};
3025
3027 public:
3029 Item *c, Item *d)
3030 : Item_int_func(pos, a, b, c, d) {}
3031
3032 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3033 longlong val_int() override;
3034
3035 const char *func_name() const override {
3036 return "internal_tablespace_data_free";
3037 }
3038
3039 bool resolve_type(THD *) override {
3040 set_nullable(true);
3041 null_on_null = false;
3042 return false;
3043 }
3044};
3045
3046/**
3047 Common class for:
3048 Item_func_get_system_var
3049 Item_func_get_user_var
3050 Item_func_set_user_var
3051*/
3052class Item_var_func : public Item_func {
3053 public:
3055 explicit Item_var_func(const POS &pos) : Item_func(pos) {}
3056
3057 Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
3058
3060 Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
3061
3062 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3063 return get_date_from_non_temporal(ltime, fuzzydate);
3064 }
3065 bool get_time(MYSQL_TIME *ltime) override {
3066 return get_time_from_non_temporal(ltime);
3067 }
3068 bool check_function_as_value_generator(uchar *checker_args) override {
3070 pointer_cast<Check_function_as_value_generator_parameters *>(
3071 checker_args);
3072 func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
3073 ? ER_CHECK_CONSTRAINT_VARIABLES
3074 : ER_DEFAULT_VAL_GENERATED_VARIABLES;
3075 return true;
3076 }
3077};
3078
3079/* Handling of user definable variables */
3080
3081// this is needed for user_vars hash
3084 m_ptr = nullptr;
3085 m_length = 0;
3086 }
3087 void set_value(char *value, size_t length) {
3088 m_ptr = value;
3089 m_length = length;
3090 }
3091
3092 /**
3093 Position inside a user_var_entry where small values are stored:
3094 double values, longlong values and string values with length
3095 up to extra_size (should be 8 bytes on all platforms).
3096 String values with length longer than 8 are stored in a separate
3097 memory buffer, which is allocated when needed using the method realloc().
3098 */
3100 return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
3101 }
3102
3103 /**
3104 Position inside a user_var_entry where a null-terminates array
3105 of characters representing the variable name is stored.
3106 */
3108
3109 /**
3110 Initialize m_ptr to the internal buffer (if the value is small enough),
3111 or allocate a separate buffer.
3112 @param length - length of the value to be stored.
3113 */
3114 bool mem_realloc(size_t length);
3115
3116 /**
3117 Check if m_ptr points to an external buffer previously allocated by
3118 realloc().
3119 @retval true - an external buffer is allocated.
3120 @retval false - m_ptr is null, or points to the internal buffer.
3121 */
3122 bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
3123
3124 /**
3125 Free the external value buffer, if it's allocated.
3126 */
3127 void free_value() {
3128 if (alloced()) my_free(m_ptr);
3129 }
3130
3131 /**
3132 Copy the array of characters from the given name into the internal
3133 name buffer and initialize entry_name to point to it.
3134 */
3136 name.strcpy(name_ptr());
3137 entry_name = Name_string(name_ptr(), name.length());
3138 }
3139
3140 /**
3141 Initialize all members
3142
3143 @param thd Current session.
3144 @param name Name of the user_var_entry instance.
3145 @param cs charset information of the user_var_entry instance.
3146 */
3147 void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
3148
3149 /**
3150 Store a value of the given type into a user_var_entry instance.
3151 @param from Value
3152 @param length Size of the value
3153 @param type type
3154 @retval false on success
3155 @retval true on memory allocation error
3156 */
3157 bool store(const void *from, size_t length, Item_result type);
3158
3159 /**
3160 Assert the user variable is locked.
3161 This is debug code only.
3162 The thread LOCK_thd_data mutex protects:
3163 - the thd->user_vars hash itself
3164 - the values in the user variable itself.
3165 The protection is required for monitoring,
3166 as a different thread can inspect this session
3167 user variables, on a live session.
3168 */
3169 void assert_locked() const;
3170
3171 static const size_t extra_size = sizeof(double);
3172 char *m_ptr; ///< Value
3173 size_t m_length; ///< Value length
3174 Item_result m_type; ///< Value type
3176 /**
3177 Set to the id of the most recent query that has used the variable.
3178 Used in binlogging: When set, there is no need to add a reference to this
3179 variable to the binlog. Imagine it is this:
3180
3181 INSERT INTO t SELECT @a:=10, @a:=@a+1.
3182
3183 Then we have a Item_func_get_user_var (because of the `@a+1`) so we
3184 think we have to write the value of `@a` to the binlog. But before that,
3185 we have a Item_func_set_user_var to create `@a` (`@a:=10`), in this we mark
3186 the variable as "already logged" so that it won't be logged
3187 by Item_func_get_user_var (because that's not necessary).
3188 */
3190
3191 public:
3192 user_var_entry() = default; /* Remove gcc warning */
3193
3194 THD *owner_session() const { return m_owner; }
3195
3196 Simple_cstring entry_name; // Variable name
3197 DTCollation collation; // Collation with attributes
3198 bool unsigned_flag; // true if unsigned, false if signed
3199
3200 /**
3201 Set value to user variable.
3202
3203 @param ptr pointer to buffer with new value
3204 @param length length of new value
3205 @param type type of new value
3206 @param cs charset info for new value
3207 @param dv derivation for new value
3208 @param unsigned_arg indicates if a value of type INT_RESULT is unsigned
3209
3210 @note Sets error and fatal error if allocation fails.
3211
3212 @retval
3213 false success
3214 @retval
3215 true failure
3216 */
3217 bool store(const void *ptr, size_t length, Item_result type,
3218 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3219 /**
3220 Set type of to the given value.
3221 @param type Data type.
3222 */
3224 assert_locked();
3225 m_type = type;
3226 }
3227 /**
3228 Set value to NULL
3229 @param type Data type.
3230 */
3231
3233 assert_locked();
3234 free_value();
3235 reset_value();
3236 m_type = type;
3237 }
3238
3239 void set_used_query_id(query_id_t query_id) { m_used_query_id = query_id; }
3241
3242 /**
3243 Allocates and initializes a user variable instance.
3244
3245 @param thd Current session.
3246 @param name Name of the variable.
3247 @param cs Charset of the variable.
3248
3249 @return Address of the allocated and initialized user_var_entry instance.
3250 @retval NULL On allocation error.
3251 */
3252 static user_var_entry *create(THD *thd, const Name_string &name,
3253 const CHARSET_INFO *cs);
3254
3255 /**
3256 Free all memory used by a user_var_entry instance
3257 previously created by create().
3258 */
3259 void destroy() {
3260 assert_locked();
3261 free_value(); // Free the external value buffer
3262 my_free(this); // Free the instance itself
3263 }
3264
3265 void lock();
3266 void unlock();
3267
3268 /* Routines to access the value and its type */
3269 const char *ptr() const { return m_ptr; }
3270 size_t length() const { return m_length; }
3271 /// The data type of this variable.
3272 Item_result type() const { return m_type; }
3273 /* Item-alike routines to access the value */
3274 double val_real(bool *null_value) const;
3275 longlong val_int(bool *null_value) const;
3276 String *val_str(bool *null_value, String *str, uint decimals) const;
3277 my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
3278};
3279
3280/**
3281 This class is used to implement operations like
3282 SET \@variable or \@variable:= expression.
3283*/
3284
3291 union {
3293 double vreal;
3297
3298 public:
3299 Name_string name; // keep it public
3300
3303 : Item_var_func(pos, b), name(a) {}
3304
3306 : Item_var_func(thd, item),
3308 entry(item->entry),
3309 value(item->value),
3311 null_item(item->null_item),
3312 save_result(item->save_result),
3313 name(item->name) {}
3314 enum Functype functype() const override { return SUSERVAR_FUNC; }
3315 double val_real() override;
3316 longlong val_int() override;
3317 String *val_str(String *str) override;
3318 my_decimal *val_decimal(my_decimal *) override;
3319 bool update_hash(const void *ptr, uint length, enum Item_result type,
3320 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3321 bool send(Protocol *protocol, String *str_arg) override;
3322 void make_field(Send_field *tmp_field) override;
3323 bool check(bool use_result_field);
3324 void save_item_result(Item *item);
3325 bool update();
3326 enum Item_result result_type() const override { return cached_result_type; }
3327 bool fix_fields(THD *thd, Item **ref) override;
3328 bool resolve_type(THD *) override;
3329 void print(const THD *thd, String *str,
3330 enum_query_type query_type) const override;
3331 void print_assignment(const THD *thd, String *str,
3332 enum_query_type query_type) const;
3333 const char *func_name() const override { return "set_user_var"; }
3334
3335 type_conversion_status save_in_field(Field *field, bool no_conversions,
3336 bool can_use_result_field);
3337
3338 void save_org_in_field(Field *field) override {
3339 save_in_field(field, true, false);
3340 }
3341
3342 bool set_entry(THD *thd, bool create_if_not_exists);
3343 void cleanup() override;
3344
3345 protected:
3347 bool no_conversions) override {
3348 return save_in_field(field, no_conversions, true);
3349 }
3350};
3351
3356
3357 public:
3358 Name_string name; // keep it public
3359
3364
3365 enum Functype functype() const override { return GUSERVAR_FUNC; }
3366 double val_real() override;
3367 longlong val_int() override;
3368 my_decimal *val_decimal(my_decimal *) override;
3369 String *val_str(String *str) override;
3370 const CHARSET_INFO *charset_for_protocol() override;
3371 bool resolve_type(THD *) override;
3372 bool propagate_type(THD *thd, const Type_properties &type) override;
3373 void cleanup() override;
3374 void update_used_tables() override {} // Keep existing used tables
3375 void print(const THD *thd, String *str,
3376 enum_query_type query_type) const override;
3377 enum Item_result result_type() const override;
3378 /*
3379 We must always return variables as strings to guard against selects of type
3380 select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3381 */
3382 const char *func_name() const override { return "get_user_var"; }
3383 bool is_non_const_over_literals(uchar *) override { return true; }
3384 bool eq_specific(const Item *item) const override;
3385
3386 private:
3387 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3388
3389 public:
3391 return this;
3392 }
3393};
3394
3395/*
3396 This item represents user variable used as out parameter (e.g in LOAD DATA),
3397 and it is supposed to be used only for this purprose. So it is simplified
3398 a lot. Actually you should never obtain its value.
3399
3400 The only two reasons for this thing being an Item is possibility to store it
3401 in const mem_root_deque<Item> and desire to place this code somewhere near
3402 other functions working with user variables.
3403*/
3407
3408 public:
3410 : Item(pos), name(a) {
3411 item_name.copy(a);
3412 }
3413 /* We should return something different from FIELD_ITEM here */
3414 enum Type type() const override { return STRING_ITEM; }
3415 double val_real() override;
3416 longlong val_int() override;
3417 String *val_str(String *str) override;
3418 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3420 assert(0);
3421 return true;
3422 }
3423 bool get_time(MYSQL_TIME *) override {
3424 assert(0);
3425 return true;
3426 }
3427
3428 /* fix_fields() binds variable name with its entry structure */
3429 bool fix_fields(THD *thd, Item **ref) override;
3430 void print(const THD *thd, String *str,
3431 enum_query_type query_type) const override;
3432 void set_null_value(const CHARSET_INFO *cs);
3433 void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3434};
3435
3436/* A system variable */
3437
3438#define GET_SYS_VAR_CACHE_LONG 1
3439#define GET_SYS_VAR_CACHE_DOUBLE 2
3440#define GET_SYS_VAR_CACHE_STRING 4
3441
3443
3444/** Class to log audit event EVENT_TRACKING_GLOBAL_VARIABLE_GET. */
3446 public:
3450
3451 private:
3452 // Thread handle.
3454
3455 // Item_func_get_system_var instance.
3457
3458 /*
3459 Value conversion type.
3460 Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3461 member while creating the object. While converting value if there are any
3462 intermediate conversions in the same query then this member is used to avoid
3463 auditing more than once.
3464 */
3466
3467 /*
3468 To indicate event auditing is required or not. Event is not audited if
3469 * scope of the variable is *not* GLOBAL.
3470 * or the event is already audited for global variable for the same query.
3471 */
3473};
3474
3484
3485 template <typename T>
3487
3489
3490 public:
3492 enum_var_type scope);
3493 enum Functype functype() const override { return GSYSVAR_FUNC; }
3495 return INNER_TABLE_BIT;
3496 }
3497 bool resolve_type(THD *) override;
3498 void print(const THD *thd, String *str,
3499 enum_query_type query_type) const override;
3500 bool is_non_const_over_literals(uchar *) override { return true; }
3501 enum Item_result result_type() const override {
3502 assert(fixed);
3503 return type_to_result(data_type());
3504 }
3505 double val_real() override;
3506 longlong val_int() override;
3507 String *val_str(String *) override;
3508 my_decimal *val_decimal(my_decimal *dec_buf) override {
3509 return val_decimal_from_real(dec_buf);
3510 }
3511 /* TODO: fix to support views */
3512 const char *func_name() const override { return "get_system_var"; }
3513 bool eq_specific(const Item *item) const override;
3514 bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) override {
3515 // Expressions which have system variables cannot be pushed as of
3516 // now because Item_func_get_system_var::print does not print the
3517 // original expression which leads to an incorrect clone.
3518 return true;
3519 }
3520
3521 void cleanup() override;
3522};
3523
3524class JOIN;
3525
3526class Item_func_match final : public Item_real_func {
3528
3529 protected:
3530 void add_json_info(Json_object *obj) override;
3531
3532 public:
3534 uint key, flags;
3535 /// True if we are doing a full-text index scan with this MATCH function as a
3536 /// predicate, and the score can be retrieved with get_relevance(). If it is
3537 /// false, the score of the document must be retrieved with find_relevance().
3542 /**
3543 Master item means that if identical items are present in the
3544 statement, they use the same FT handler. FT handler is initialized
3545 only for master item and slave items just use it. FT hints initialized
3546 for master only, slave items HINTS are not accessed.
3547 */
3549 Item *concat_ws; // Item_func_concat_ws
3550 String value; // value of concat_ws
3551 String search_value; // key_item()'s value converted to cmp_collation
3552
3553 /**
3554 Constructor for Item_func_match class.
3555
3556 @param pos Position of token in the parser.
3557 @param a List of arguments.
3558 @param against_arg Expression to match against.
3559 @param b FT Flags.
3560 */
3561 Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3562 : Item_real_func(pos, a),
3563 against(against_arg),
3564 key(0),
3565 flags(b),
3568 master(nullptr),
3570 hints(nullptr),
3571 simple_expression(false),
3572 used_in_where_only(false) {
3573 null_on_null = false;
3574 }
3575
3576 bool do_itemize(Parse_context *pc, Item **res) override;
3577
3578 void cleanup() override {
3579 DBUG_TRACE;
3581 if (master == nullptr && ft_handler != nullptr) {
3583 }
3584 score_from_index_scan = false;
3585 ft_handler = nullptr;
3586 concat_ws = nullptr;
3587 return;
3588 }
3589 Item *key_item() const override { return against; }
3590 enum Functype functype() const override { return FT_FUNC; }
3591 const char *func_name() const override { return "match"; }
3592 bool fix_fields(THD *thd, Item **ref) override;
3593 void update_used_tables() override;
3594 bool eq_specific(const Item *item) const override;
3595 /* The following should be safe, even if we compare doubles */
3596 longlong val_int() override {
3597 assert(fixed);
3598 return val_real() != 0.0;
3599 }
3600 double val_real() override;
3601 void print(const THD *thd, String *str,
3602 enum_query_type query_type) const override;
3603
3604 bool fix_index(const THD *thd);
3605 bool init_search(THD *thd);
3606 bool check_function_as_value_generator(uchar *checker_args) override {
3608 pointer_cast<Check_function_as_value_generator_parameters *>(
3609 checker_args);
3610 func_arg->banned_function_name = func_name();
3611 return true;
3612 }
3613
3614 /**
3615 Get number of matching rows from FT handler.
3616
3617 @note Requires that FT handler supports the extended API
3618
3619 @return Number of matching rows in result
3620 */
3622 assert(ft_handler);
3624
3625 return ((FT_INFO_EXT *)ft_handler)
3626 ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3627 }
3628
3629 /**
3630 Check whether FT result is ordered on rank
3631
3632 @return true if result is ordered
3633 @return false otherwise
3634 */
3636 assert(!master);
3637 if (hints->get_flags() & FT_SORTED) return true;
3638
3640 return false;
3641
3642 assert(ft_handler);
3643 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3645 }
3646
3647 /**
3648 Check whether FT result contains the document ID
3649
3650 @return true if document ID is available
3651 @return false otherwise
3652 */
3654 assert(ft_handler);
3655
3657 return false;
3658
3659 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3661 }
3662
3663 float get_filtering_effect(THD *thd, table_map filter_for_table,
3664 table_map read_tables,
3665 const MY_BITMAP *fields_to_ignore,
3666 double rows_in_table) override;
3667
3668 /**
3669 Returns master MATCH function.
3670
3671 @return pointer to master MATCH function.
3672 */
3674 if (master) return master->get_master();
3675 return this;
3676 }
3677
3678 /**
3679 Set master MATCH function and adjust used_in_where_only value.
3680
3681 @param item item for which master should be set.
3682 */
3685 item->master = this;
3686 }
3687
3688 /**
3689 Returns pointer to Ft_hints object belonging to master MATCH function.
3690
3691 @return pointer to Ft_hints object
3692 */
3694 assert(!master);
3695 return hints;
3696 }
3697
3698 /**
3699 Set comparison operation type and and value for master MATCH function.
3700
3701 @param type comparison operation type
3702 @param value_arg comparison operation value
3703 */
3704 void set_hints_op(enum ft_operation type, double value_arg) {
3705 assert(!master);
3706 hints->set_hint_op(type, value_arg);
3707 }
3708
3709 /**
3710 Set FT hints.
3711 */
3712 void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3713
3714 /**
3715 Check if ranking is not needed.
3716
3717 @return true if ranking is not needed
3718 @return false otherwise
3719 */
3721 assert(!master);
3722 return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3723 used_in_where_only && // MATCH result is not used
3724 // in expression
3725 hints->get_op_type() == FT_OP_NO); // MATCH is single function
3726 }
3727
3728 /**
3729 Set flag that the function is a simple expression.
3730
3731 @param val true if the function is a simple expression, false otherwise
3732 */
3733 void set_simple_expression(bool val) {
3734 assert(!master);
3735 simple_expression = val;
3736 }
3737
3738 /**
3739 Check if this MATCH function is a simple expression in WHERE condition.
3740
3741 @return true if simple expression
3742 @return false otherwise
3743 */
3745 assert(!master);
3746 return simple_expression;
3747 }
3748
3749 private:
3750 /**
3751 Fulltext index hints, initialized for master MATCH function only.
3752 */
3754 /**
3755 Flag is true when MATCH function is used as a simple expression in
3756 WHERE condition, i.e. there is no AND/OR combinations, just simple
3757 MATCH function or [MATCH, rank] comparison operation.
3758 */
3760 /**
3761 true if MATCH function is used in WHERE condition only.
3762 Used to determine what hints can be used for FT handler.
3763 Note that only master MATCH function has valid value.
3764 it's ok since only master function is involved in the hint processing.
3765 */
3767 /**
3768 Check whether storage engine for given table,
3769 allows FTS Boolean search on non-indexed columns.
3770
3771 @todo A flag should be added to the extended fulltext API so that
3772 it may be checked whether search on non-indexed columns are
3773 supported. Currently, it is not possible to check for such a
3774 flag since @c this->ft_handler is not yet set when this function is
3775 called. The current hack is to assume that search on non-indexed
3776 columns are supported for engines that does not support the extended
3777 fulltext API (e.g., MyISAM), while it is not supported for other
3778 engines (e.g., InnoDB)
3779
3780 @param tr Table for which storage engine to check
3781
3782 @retval true if BOOLEAN search on non-indexed columns is supported
3783 @retval false otherwise
3784 */
3786 // Only Boolean search may support non_indexed columns
3787 if (!(flags & FT_BOOL)) return false;
3788
3789 assert(tr && tr->file);
3790
3791 // Assume that if extended fulltext API is not supported,
3792 // non-indexed columns are allowed. This will be true for MyISAM.
3793 if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3794
3795 return false;
3796 }
3797};
3798
3799/**
3800 A visitor that calls the specified function on every non-aggregated full-text
3801 search function (Item_func_match) it encounters when it is used in a
3802 PREFIX+POSTFIX walk with WalkItem(). It skips every item that is wrapped in an
3803 aggregate function, and also every item wrapped in a reference, as the items
3804 behind the reference are already handled elsewhere (in another query block or
3805 in another element of the SELECT list).
3806 */
3808 public:
3810 std::function<bool(Item_func_match *)> func);
3811 bool operator()(Item *item);
3812
3813 private:
3814 std::function<bool(Item_func_match *)> m_func;
3815};
3816
3819
3821
3822 public:
3823 Item_func_is_free_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
3824
3825 bool do_itemize(Parse_context *pc, Item **res) override;
3826 longlong val_int() override;
3827 const char *func_name() const override { return "is_free_lock"; }
3828 bool resolve_type(THD *thd) override {
3829 if (param_type_is_default(thd, 0, 1)) return true;
3830 max_length = 1;
3831 set_nullable(true);
3832 return false;
3833 }
3834 bool is_non_const_over_literals(uchar *) override { return true; }
3835 bool check_function_as_value_generator(uchar *checker_args) override {
3837 pointer_cast<Check_function_as_value_generator_parameters *>(
3838 checker_args);
3839 func_arg->banned_function_name = func_name();
3840 return true;
3841 }
3842};
3843
3846
3848
3849 public:
3850 Item_func_is_used_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
3851
3852 bool do_itemize(