MySQL 9.1.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 {
357 };
364 };
365 enum Type type() const override { return FUNC_ITEM; }
366 virtual enum Functype functype() const { return UNKNOWN_FUNC; }
368
369 explicit Item_func(const POS &pos)
371
373 args[0] = a;
375 }
376 Item_func(const POS &pos, Item *a)
378 args[0] = a;
379 }
380
382 args[0] = a;
383 args[1] = b;
387 }
388 Item_func(const POS &pos, Item *a, Item *b)
390 args[0] = a;
391 args[1] = b;
392 }
393
394 Item_func(Item *a, Item *b, Item *c) {
395 if (alloc_args(*THR_MALLOC, 3)) return;
396 args[0] = a;
397 args[1] = b;
398 args[2] = c;
403 }
404
405 Item_func(const POS &pos, Item *a, Item *b, Item *c)
406 : Item_result_field(pos) {
407 if (alloc_args(*THR_MALLOC, 3)) return;
408 args[0] = a;
409 args[1] = b;
410 args[2] = c;
411 }
412
413 Item_func(Item *a, Item *b, Item *c, Item *d) {
414 if (alloc_args(*THR_MALLOC, 4)) return;
415 args[0] = a;
416 args[1] = b;
417 args[2] = c;
418 args[3] = d;
424 }
425
426 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
427 : Item_result_field(pos) {
428 if (alloc_args(*THR_MALLOC, 4)) return;
429 args[0] = a;
430 args[1] = b;
431 args[2] = c;
432 args[3] = d;
433 }
434 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
435 if (alloc_args(*THR_MALLOC, 5)) return;
436 args[0] = a;
437 args[1] = b;
438 args[2] = c;
439 args[3] = d;
440 args[4] = e;
447 }
448 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
449 : Item_result_field(pos) {
450 if (alloc_args(*THR_MALLOC, 5)) return;
451 args[0] = a;
452 args[1] = b;
453 args[2] = c;
454 args[3] = d;
455 args[4] = e;
456 }
457 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e, Item *f) {
458 if (alloc_args(*THR_MALLOC, 6)) return;
459 args[0] = a;
460 args[1] = b;
461 args[2] = c;
462 args[3] = d;
463 args[4] = e;
464 args[5] = f;
472 }
473 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e,
474 Item *f)
475 : Item_result_field(pos) {
476 if (alloc_args(*THR_MALLOC, 6)) return;
477 args[0] = a;
478 args[1] = b;
479 args[2] = c;
480 args[3] = d;
481 args[4] = e;
482 args[5] = f;
483 }
485 set_arguments(list, false);
486 }
487
488 Item_func(const POS &pos, PT_item_list *opt_list);
489
490 // Constructor used for Item_cond_and/or (see Item comment)
491 Item_func(THD *thd, const Item_func *item);
492
493 /// Get the i'th argument of the function that this object represents.
494 virtual Item *get_arg(uint i) { return args[i]; }
495
496 /// Get the i'th argument of the function that this object represents.
497 virtual const Item *get_arg(uint i) const { return args[i]; }
498 virtual Item *set_arg(THD *, uint, Item *) {
499 assert(0);
500 return nullptr;
501 }
502
503 bool do_itemize(Parse_context *pc, Item **res) override;
504
505 bool fix_fields(THD *, Item **ref) override;
506 bool fix_func_arg(THD *, Item **arg);
507 void fix_after_pullout(Query_block *parent_query_block,
508 Query_block *removed_query_block) override;
509 /**
510 Resolve type of function after all arguments have had their data types
511 resolved. Called from resolve_type() when no dynamic parameters
512 are used and from propagate_type() otherwise.
513 */
514 virtual bool resolve_type_inner(THD *) {
515 assert(false);
516 return false;
517 }
518 bool propagate_type(THD *thd, const Type_properties &type) override;
519 /**
520 Returns the pseudo tables depended upon in order to evaluate this
521 function expression. The default implementation returns the empty
522 set.
523 */
524 virtual table_map get_initial_pseudo_tables() const { return 0; }
525 table_map used_tables() const override { return used_tables_cache; }
527 void update_used_tables() override;
529 bool eq(const Item *item) const override;
530 /**
531 Provide a more specific equality check for a function.
532 Combine with Item::eq() to implement a complete equality check.
533 */
534 virtual bool eq_specific(const Item *) const { return true; }
535 virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
536 virtual bool have_rev_func() const { return false; }
537 virtual Item *key_item() const { return args[0]; }
538 /**
539 Copy arguments from list to args array
540
541 @param list function argument list
542 @param context_free true: for use in context-independent
543 constructors (Item_func(POS,...)) i.e. for use
544 in the parser
545 @return true on OOM, false otherwise
546 */
547 bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
548 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
549 mem_root_deque<Item *> *fields) override;
550 void print(const THD *thd, String *str,
551 enum_query_type query_type) const override;
552 void print_op(const THD *thd, String *str, enum_query_type query_type) const;
553 void print_args(const THD *thd, String *str, uint from,
554 enum_query_type query_type) const;
555 virtual void fix_num_length_and_dec();
556 virtual bool is_deprecated() const { return false; }
557 bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
558 return (null_value = args[0]->get_date(ltime, fuzzy_date));
559 }
560 inline bool get_arg0_time(MYSQL_TIME *ltime) {
561 return (null_value = args[0]->get_time(ltime));
562 }
563 bool is_null() override { return update_null_value() || null_value; }
566 friend class udf_handler;
567 Field *tmp_table_field(TABLE *t_arg) override;
568 Item *get_tmp_table_item(THD *thd) override;
569
570 my_decimal *val_decimal(my_decimal *) override;
571
572 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
573 int item_sep) {
574 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep);
575 }
576 /*
577 Aggregate arguments for string result, e.g: CONCAT(a,b)
578 - convert to @@character_set_connection if all arguments are numbers
579 - allow DERIVATION_NONE
580 */
582 uint nitems, int item_sep = 1) {
583 return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
584 item_sep);
585 }
586 /*
587 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
588 - don't convert to @@character_set_connection if all arguments are numbers
589 - don't allow DERIVATION_NONE
590 */
592 uint nitems, int item_sep = 1) {
593 return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
594 item_sep);
595 }
596
597 Item *replace_func_call(uchar *) override;
598
599 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
600 Item *transform(Item_transformer transformer, uchar *arg) override;
601 Item *compile(Item_analyzer analyzer, uchar **arg_p,
602 Item_transformer transformer, uchar *arg_t) override;
603 void traverse_cond(Cond_traverser traverser, void *arg,
604 traverse_order order) override;
605
606 bool replace_equal_field_checker(uchar **arg) override {
607 Replace_equal *replace = pointer_cast<Replace_equal *>(*arg);
608 replace->stack.push_front(this);
609 return true;
610 }
611
613 pointer_cast<Replace_equal *>(arg)->stack.pop();
614 return this;
615 }
616
617 /**
618 Check whether a function allows replacement of a field with another item:
619 In particular, a replacement that changes the metadata of some Item
620 from non-nullable to nullable is not allowed.
621 Notice that e.g. changing the nullability of an operand of a comparison
622 operator in a WHERE clause that ignores UNKNOWN values is allowed,
623 according to this criterion.
624
625 @param original the field that could be replaced
626 @param subst the item that could be the replacement
627
628 @returns true if replacement is allowed, false otherwise
629 */
630 virtual bool allow_replacement(Item_field *const original,
631 Item *const subst) {
632 return original->is_nullable() || !subst->is_nullable();
633 }
634
635 /**
636 Throw an error if the input double number is not finite, i.e. is either
637 +/-INF or NAN.
638 */
639 inline double check_float_overflow(double value) {
640 return std::isfinite(value) ? value : raise_float_overflow();
641 }
642 /**
643 Throw an error if the input BIGINT value represented by the
644 (longlong value, bool unsigned flag) pair cannot be returned by the
645 function, i.e. is not compatible with this Item's unsigned_flag.
646 */
647 inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
648 if ((unsigned_flag && !val_unsigned && value < 0) ||
649 (!unsigned_flag && val_unsigned &&
650 (ulonglong)value > (ulonglong)LLONG_MAX))
651 return raise_integer_overflow();
652 return value;
653 }
654 /**
655 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
656 */
659 }
660
662 assert(fixed);
663 for (uint i = 0; i < arg_count; i++) {
664 if (args[i]->type() == Item::FIELD_ITEM &&
666 return true;
667 }
668 return false;
669 }
670
672 assert(fixed);
673 for (uint i = 0; i < arg_count; i++) {
674 if (args[i]->type() == Item::FIELD_ITEM &&
675 (args[i]->data_type() == MYSQL_TYPE_DATE ||
677 return true;
678 }
679 return false;
680 }
681
683 assert(fixed);
684 for (uint i = 0; i < arg_count; i++) {
685 if (args[i]->type() == Item::FIELD_ITEM &&
686 (args[i]->data_type() == MYSQL_TYPE_TIME ||
688 return true;
689 }
690 return false;
691 }
692
694 assert(fixed);
695 for (uint i = 0; i < arg_count; i++) {
696 if (args[i]->type() == Item::FIELD_ITEM &&
698 return true;
699 }
700 return false;
701 }
702
703 /*
704 We assume the result of any function that has a TIMESTAMP argument to be
705 timezone-dependent, since a TIMESTAMP value in both numeric and string
706 contexts is interpreted according to the current timezone.
707 The only exception is UNIX_TIMESTAMP() which returns the internal
708 representation of a TIMESTAMP argument verbatim, and thus does not depend on
709 the timezone.
710 */
712 return has_timestamp_args();
713 }
714
715 Item *gc_subst_transformer(uchar *arg) override;
716
717 bool resolve_type(THD *thd) override {
718 // By default, pick PS-param's type from other arguments, or VARCHAR
719 return param_type_uses_non_param(thd);
720 }
721
722 /**
723 Whether an arg of a JSON function can be cached to avoid repetitive
724 string->JSON conversion. This function returns true only for those args,
725 which are the source of JSON data. JSON path args are cached independently
726 and for them this function returns false. Same as for all other type of
727 args.
728
729 @param arg the arg to cache
730
731 @retval true arg can be cached
732 @retval false otherwise
733 */
734 virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
735 return CACHE_NONE;
736 }
737
738 /// Whether this Item is an equi-join condition. If this Item is a compound
739 /// item (i.e. multiple condition AND'ed together), it will only return true
740 /// if the Item contains only equi-join conditions AND'ed together. This is
741 /// used to determine whether the condition can be used as a join condition
742 /// for hash join (join conditions in hash join must be equi-join conditions),
743 /// or if it should be placed as a filter after the join.
744 virtual bool contains_only_equi_join_condition() const { return false; }
745
746 protected:
747 /**
748 Whether or not an item should contribute to the filtering effect
749 (@see get_filtering_effect()). First it verifies that table
750 requirements are satisfied as follows:
751
752 1) The item must refer to a field in 'filter_for_table' in some
753 way. This reference may be indirect through any number of
754 intermediate items. For example, this item may be an
755 Item_cond_and which refers to an Item_func_eq which refers to
756 the field.
757 2) The item must not refer to other tables than those already
758 read and the table in 'filter_for_table'
759
760 Then it contines to other properties as follows:
761
762 Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
763 Item_func is to contribute to the filtering effect, then
764
765 1) one of the operands must be a field from 'filter_for_table' that is not
766 in 'fields_to_ignore', and
767 2) depending on the Item_func type filtering effect is calculated
768 for, one or all [1] of the other operand(s) must be an available
769 value, i.e.:
770 - a constant, or
771 - a constant subquery, or
772 - a field value read from a table in 'read_tables', or
773 - a second field in 'filter_for_table', or
774 - a function that only refers to constants or tables in
775 'read_tables', or
776 - special case: an implicit value like NULL in the case of
777 "field IS NULL". Such Item_funcs have arg_count==1.
778
779 [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
780 for the rest (e.g. BETWEEN)
781
782 @param thd The current thread.
783 @param read_tables Tables earlier in the join sequence.
784 Predicates for table 'filter_for_table' that
785 rely on values from these tables can be part of
786 the filter effect.
787 @param filter_for_table The table we are calculating filter effect for
788 @param fields_to_ignore Columns that should be ignored.
789
790
791 @return Item_field that participates in the predicate if none of the
792 requirements are broken, NULL otherwise
793
794 @note: This function only applies to items doing comparison, i.e.
795 boolean predicates. Unfortunately, some of those items do not
796 inherit from Item_bool_func so the member function has to be
797 placed in Item_func.
798 */
800 THD *thd, table_map read_tables, table_map filter_for_table,
801 const MY_BITMAP *fields_to_ignore) const;
802 /**
803 Named parameters are allowed in a parameter list
804
805 The syntax to name parameters in a function call is as follow:
806 <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
807 where "AS" is optional.
808 Only UDF function support that syntax.
809
810 @return true if the function item can have named parameters
811 */
812 virtual bool may_have_named_parameters() const { return false; }
813 bool is_non_const_over_literals(uchar *) override { return false; }
814
815 bool check_function_as_value_generator(uchar *checker_args) override {
816 if (is_deprecated()) {
818 pointer_cast<Check_function_as_value_generator_parameters *>(
819 checker_args);
820 func_arg->banned_function_name = func_name();
821 return true;
822 }
823 return false;
824 }
825 bool is_valid_for_pushdown(uchar *arg) override;
826 bool check_column_in_window_functions(uchar *arg) override;
827 bool check_column_in_group_by(uchar *arg) override;
828
830};
831
832class Item_real_func : public Item_func {
833 public:
835 explicit Item_real_func(const POS &pos) : Item_func(pos) {
837 }
838
840 Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
842 }
843
845
846 Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
848 }
849
852 }
853
856 }
857
858 String *val_str(String *str) override;
859 my_decimal *val_decimal(my_decimal *decimal_value) override;
860 longlong val_int() override {
861 assert(fixed);
863 }
864 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
865 return get_date_from_real(ltime, fuzzydate);
866 }
867 bool get_time(MYSQL_TIME *ltime) override {
868 return get_time_from_real(ltime);
869 }
870 enum Item_result result_type() const override { return REAL_RESULT; }
871};
872
874 protected:
876
877 public:
880 }
882 : Item_func(pos, a), hybrid_type(REAL_RESULT) {
884 }
885
889 }
890 Item_func_numhybrid(const POS &pos, Item *a, Item *b)
891 : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
893 }
894
898 }
902 }
903
904 enum Item_result result_type() const override { return hybrid_type; }
906 return MYSQL_TYPE_DOUBLE;
907 }
908 bool resolve_type(THD *thd) override;
909 bool resolve_type_inner(THD *thd) override;
910 void fix_num_length_and_dec() override;
911 virtual void set_numeric_type() = 0; // To be called from resolve_type()
912
913 double val_real() override;
914 longlong val_int() override;
915 my_decimal *val_decimal(my_decimal *) override;
916 String *val_str(String *str) override;
917 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
918 bool get_time(MYSQL_TIME *ltime) override;
919 /**
920 @brief Performs the operation that this functions implements when the
921 result type is INT.
922
923 @return The result of the operation.
924 */
925 virtual longlong int_op() = 0;
926
927 /**
928 @brief Performs the operation that this functions implements when the
929 result type is REAL.
930
931 @return The result of the operation.
932 */
933 virtual double real_op() = 0;
934
935 /**
936 @brief Performs the operation that this functions implements when the
937 result type is DECIMAL.
938
939 @param decimal_value A pointer where the DECIMAL value will be allocated.
940 @return
941 - 0 If the result is NULL
942 - The same pointer it was given, with the area initialized to the
943 result of the operation.
944 */
945 virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
946
947 /**
948 @brief Performs the operation that this functions implements when the
949 result type is a string type.
950
951 @return The result of the operation.
952 */
953 virtual String *str_op(String *) = 0;
954 /**
955 @brief Performs the operation that this functions implements when the
956 result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
957
958 @return The result of the operation.
959 */
960 virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
961 virtual bool time_op(MYSQL_TIME *ltime) = 0;
962 bool is_null() override { return update_null_value() || null_value; }
963};
964
965/* function where type of result detected by first argument */
967 public:
969 Item_func_num1(const POS &pos, Item *a) : Item_func_numhybrid(pos, a) {}
970
972 Item_func_num1(const POS &pos, Item *a, Item *b)
973 : Item_func_numhybrid(pos, a, b) {}
974
975 void fix_num_length_and_dec() override;
976 void set_numeric_type() override;
977 String *str_op(String *) override {
978 assert(0);
979 return nullptr;
980 }
982 assert(0);
983 return false;
984 }
985 bool time_op(MYSQL_TIME *) override {
986 assert(0);
987 return false;
988 }
989};
990
991/* Base class for operations like '+', '-', '*' */
993 public:
995 Item_num_op(const POS &pos, Item *a, Item *b)
996 : Item_func_numhybrid(pos, a, b) {}
997
998 virtual void result_precision() = 0;
999
1000 void print(const THD *thd, String *str,
1001 enum_query_type query_type) const override {
1002 print_op(thd, str, query_type);
1003 }
1004
1005 void set_numeric_type() override;
1006 String *str_op(String *) override {
1007 assert(0);
1008 return nullptr;
1009 }
1011 assert(0);
1012 return false;
1013 }
1014 bool time_op(MYSQL_TIME *) override {
1015 assert(0);
1016 return false;
1017 }
1018};
1019
1020class Item_int_func : public Item_func {
1021 public:
1023 explicit Item_int_func(const POS &pos) : Item_func(pos) {
1025 }
1026
1028 Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
1030 }
1031
1034 }
1035 Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
1037 }
1038
1039 Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
1041 }
1042 Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
1043 : Item_func(pos, a, b, c) {
1045 }
1046
1047 Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
1049 }
1050 Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
1051 : Item_func(pos, a, b, c, d) {
1053 }
1054
1057 }
1058 Item_int_func(const POS &pos, PT_item_list *opt_list)
1059 : Item_func(pos, opt_list) {
1061 }
1062
1063 Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
1065 }
1066 double val_real() override;
1067 String *val_str(String *str) override;
1068 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1069 return get_date_from_int(ltime, fuzzydate);
1070 }
1071 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1072 enum Item_result result_type() const override { return INT_RESULT; }
1073 /*
1074 Concerning PS-param types,
1075 resolve_type(THD *) is not overridden here, as experience shows that for
1076 most child classes of this class, VARCHAR is the best default
1077 */
1078};
1079
1082
1083 public:
1085
1087 return INNER_TABLE_BIT;
1088 }
1089 bool do_itemize(Parse_context *pc, Item **res) override;
1090 const char *func_name() const override { return "connection_id"; }
1091 bool resolve_type(THD *thd) override;
1092 bool fix_fields(THD *thd, Item **ref) override;
1093 longlong val_int() override;
1094 bool check_function_as_value_generator(uchar *checker_args) override {
1096 pointer_cast<Check_function_as_value_generator_parameters *>(
1097 checker_args);
1098 func_arg->banned_function_name = func_name();
1099 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1100 (func_arg->source == VGS_CHECK_CONSTRAINT));
1101 }
1102};
1103
1105 public:
1106 Item_typecast_signed(const POS &pos, Item *a) : Item_int_func(pos, a) {
1107 unsigned_flag = false;
1108 }
1109 const char *func_name() const override { return "cast_as_signed"; }
1110 longlong val_int() override;
1111 bool resolve_type(THD *thd) override;
1112 void print(const THD *thd, String *str,
1113 enum_query_type query_type) const override;
1114 enum Functype functype() const override { return TYPECAST_FUNC; }
1115};
1116
1118 public:
1119 Item_typecast_unsigned(const POS &pos, Item *a) : Item_int_func(pos, a) {
1120 unsigned_flag = true;
1121 }
1122 const char *func_name() const override { return "cast_as_unsigned"; }
1123 longlong val_int() override;
1124 bool resolve_type(THD *thd) override;
1125 void print(const THD *thd, String *str,
1126 enum_query_type query_type) const override;
1127 enum Functype functype() const override { return TYPECAST_FUNC; }
1128};
1129
1130class Item_typecast_decimal final : public Item_func {
1131 protected:
1132 void add_json_info(Json_object *obj) override;
1133
1134 public:
1135 Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1136 : Item_func(pos, a) {
1138 }
1139 String *val_str(String *str) override;
1140 double val_real() override;
1141 longlong val_int() override;
1142 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1143 return get_date_from_decimal(ltime, fuzzydate);
1144 }
1145 bool get_time(MYSQL_TIME *ltime) override {
1146 return get_time_from_decimal(ltime);
1147 }
1148 my_decimal *val_decimal(my_decimal *) override;
1149 enum Item_result result_type() const override { return DECIMAL_RESULT; }
1150 bool resolve_type(THD *thd) override {
1151 if (reject_vector_args()) return true;
1152 if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1153 return true;
1154 return false;
1155 }
1156 const char *func_name() const override { return "cast_as_decimal"; }
1157 enum Functype functype() const override { return TYPECAST_FUNC; }
1158 void print(const THD *thd, String *str,
1159 enum_query_type query_type) const override;
1160};
1161
1162/**
1163 Class used to implement CAST to floating-point data types.
1164*/
1165class Item_typecast_real final : public Item_func {
1166 protected:
1167 void add_json_info(Json_object *obj) override {
1168 obj->add_alias("is_double", create_dom_ptr<Json_boolean>(
1170 }
1171
1172 public:
1173 Item_typecast_real(const POS &pos, Item *a, bool as_double)
1174 : Item_func(pos, a) {
1175 if (as_double)
1177 else
1179 }
1181 String *val_str(String *str) override;
1182 double val_real() override;
1183 longlong val_int() override { return val_int_from_real(); }
1184 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1185 bool get_time(MYSQL_TIME *ltime) override;
1186 my_decimal *val_decimal(my_decimal *decimal_value) override;
1187 enum Item_result result_type() const override { return REAL_RESULT; }
1188 bool resolve_type(THD *thd) override {
1189 if (reject_vector_args()) return true;
1190 return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1191 }
1192 const char *func_name() const override { return "cast_as_real"; }
1193 enum Functype functype() const override { return TYPECAST_FUNC; }
1194 void print(const THD *thd, String *str,
1195 enum_query_type query_type) const override;
1196};
1197
1199 public:
1202 : Item_num_op(pos, a, b) {}
1203
1204 void result_precision() override;
1205 bool check_partition_func_processor(uchar *) override { return false; }
1206 bool check_function_as_value_generator(uchar *) override { return false; }
1207};
1208
1210 public:
1212 Item_func_plus(const POS &pos, Item *a, Item *b)
1213 : Item_func_additive_op(pos, a, b) {}
1214
1215 const char *func_name() const override { return "+"; }
1216
1217 // SUPPRESS_UBSAN: signed integer overflow
1218 longlong int_op() override SUPPRESS_UBSAN;
1219
1220 double real_op() override;
1221 my_decimal *decimal_op(my_decimal *) override;
1222 enum Functype functype() const override { return PLUS_FUNC; }
1223};
1224
1226 public:
1228 Item_func_minus(const POS &pos, Item *a, Item *b)
1229 : Item_func_additive_op(pos, a, b) {}
1230
1231 const char *func_name() const override { return "-"; }
1232
1233 // SUPPRESS_UBSAN: signed integer overflow
1234 longlong int_op() override SUPPRESS_UBSAN;
1235
1236 double real_op() override;
1237 my_decimal *decimal_op(my_decimal *) override;
1238 bool resolve_type(THD *thd) override;
1239 enum Functype functype() const override { return MINUS_FUNC; }
1240};
1241
1242class Item_func_mul final : public Item_num_op {
1243 public:
1245 Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1246
1247 const char *func_name() const override { return "*"; }
1248 longlong int_op() override;
1249 double real_op() override;
1250 my_decimal *decimal_op(my_decimal *) override;
1251 void result_precision() override;
1252 bool check_partition_func_processor(uchar *) override { return false; }
1253 bool check_function_as_value_generator(uchar *) override { return false; }
1254 enum Functype functype() const override { return MUL_FUNC; }
1255};
1256
1258 public:
1259 Item_func_div_base(const POS &pos, Item *a, Item *b)
1260 : Item_num_op(pos, a, b) {}
1262 longlong int_op() override;
1263 double real_op() override;
1264 my_decimal *decimal_op(my_decimal *) override;
1265 enum Functype functype() const override { return DIV_FUNC; }
1266
1267 protected:
1269};
1270
1271class Item_func_div final : public Item_func_div_base {
1272 public:
1273 Item_func_div(const POS &pos, Item *a, Item *b)
1274 : Item_func_div_base(pos, a, b) {}
1275 const char *func_name() const override { return "/"; }
1276 bool resolve_type(THD *thd) override;
1277 void result_precision() override;
1278};
1279
1281 public:
1283 Item_func_div_int(const POS &pos, Item *a, Item *b)
1284 : Item_func_div_base(pos, a, b) {}
1285 const char *func_name() const override { return "DIV"; }
1287 return MYSQL_TYPE_LONGLONG;
1288 }
1289 bool resolve_type(THD *thd) override;
1290 void result_precision() override;
1291 void set_numeric_type() override;
1292 bool check_partition_func_processor(uchar *) override { return false; }
1293 bool check_function_as_value_generator(uchar *) override { return false; }
1294};
1295
1296class Item_func_mod final : public Item_num_op {
1297 public:
1299 Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1300
1301 longlong int_op() override;
1302 double real_op() override;
1303 my_decimal *decimal_op(my_decimal *) override;
1304 const char *func_name() const override { return "%"; }
1305 void result_precision() override;
1306 bool resolve_type(THD *thd) override;
1307 bool check_partition_func_processor(uchar *) override { return false; }
1308 bool check_function_as_value_generator(uchar *) override { return false; }
1309 enum Functype functype() const override { return MOD_FUNC; }
1310};
1311
1312class Item_func_neg final : public Item_func_num1 {
1313 public:
1315 Item_func_neg(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1316
1317 double real_op() override;
1318 longlong int_op() override;
1319 my_decimal *decimal_op(my_decimal *) override;
1320 const char *func_name() const override { return "-"; }
1321 enum Functype functype() const override { return NEG_FUNC; }
1322 bool resolve_type(THD *thd) override;
1323 void fix_num_length_and_dec() override;
1324 bool check_partition_func_processor(uchar *) override { return false; }
1325 bool check_function_as_value_generator(uchar *) override { return false; }
1326};
1327
1328class Item_func_abs final : public Item_func_num1 {
1329 public:
1330 Item_func_abs(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1331 double real_op() override;
1332 longlong int_op() override;
1333 my_decimal *decimal_op(my_decimal *) override;
1334 const char *func_name() const override { return "abs"; }
1335 bool resolve_type(THD *) override;
1336 bool check_partition_func_processor(uchar *) override { return false; }
1337 bool check_function_as_value_generator(uchar *) override { return false; }
1338 enum Functype functype() const override { return ABS_FUNC; }
1339};
1340
1341// A class to handle logarithmic and trigonometric functions
1342
1344 public:
1346 Item_dec_func(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1347
1348 Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1349 bool resolve_type(THD *thd) override;
1350};
1351
1352class Item_func_exp final : public Item_dec_func {
1353 public:
1354 Item_func_exp(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1355 double val_real() override;
1356 const char *func_name() const override { return "exp"; }
1357 enum Functype functype() const override { return EXP_FUNC; }
1358};
1359
1360class Item_func_ln final : public Item_dec_func {
1361 public:
1362 Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1363 double val_real() override;
1364 const char *func_name() const override { return "ln"; }
1365 enum Functype functype() const override { return LN_FUNC; }
1366};
1367
1368class Item_func_log final : public Item_dec_func {
1369 public:
1370 Item_func_log(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1371 Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1372 double val_real() override;
1373 const char *func_name() const override { return "log"; }
1374 enum Functype functype() const override { return LOG_FUNC; }
1375};
1376
1377class Item_func_log2 final : public Item_dec_func {
1378 public:
1379 Item_func_log2(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1380 double val_real() override;
1381 const char *func_name() const override { return "log2"; }
1382};
1383
1384class Item_func_log10 final : public Item_dec_func {
1385 public:
1386 Item_func_log10(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1387 double val_real() override;
1388 const char *func_name() const override { return "log10"; }
1389 enum Functype functype() const override { return LOG10_FUNC; }
1390};
1391
1392class Item_func_sqrt final : public Item_dec_func {
1393 public:
1394 Item_func_sqrt(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1395 double val_real() override;
1396 const char *func_name() const override { return "sqrt"; }
1397 enum Functype functype() const override { return SQRT_FUNC; }
1398};
1399
1400class Item_func_pow final : public Item_dec_func {
1401 public:
1402 Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1403 double val_real() override;
1404 const char *func_name() const override { return "pow"; }
1405 enum Functype functype() const override { return POW_FUNC; }
1406};
1407
1408class Item_func_acos final : public Item_dec_func {
1409 public:
1410 Item_func_acos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1411 double val_real() override;
1412 const char *func_name() const override { return "acos"; }
1413 enum Functype functype() const override { return ACOS_FUNC; }
1414};
1415
1416class Item_func_asin final : public Item_dec_func {
1417 public:
1418 Item_func_asin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1419 double val_real() override;
1420 const char *func_name() const override { return "asin"; }
1421 enum Functype functype() const override { return ASIN_FUNC; }
1422};
1423
1424class Item_func_atan final : public Item_dec_func {
1425 public:
1426 Item_func_atan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1427 Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1428 double val_real() override;
1429 const char *func_name() const override { return "atan"; }
1430 enum Functype functype() const override { return ATAN_FUNC; }
1431};
1432
1433class Item_func_cos final : public Item_dec_func {
1434 public:
1435 Item_func_cos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1436 double val_real() override;
1437 const char *func_name() const override { return "cos"; }
1438 enum Functype functype() const override { return COS_FUNC; }
1439};
1440
1441class Item_func_sin final : public Item_dec_func {
1442 public:
1443 Item_func_sin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1444 double val_real() override;
1445 const char *func_name() const override { return "sin"; }
1446 enum Functype functype() const override { return SIN_FUNC; }
1447};
1448
1449class Item_func_tan final : public Item_dec_func {
1450 public:
1451 Item_func_tan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1452 double val_real() override;
1453 const char *func_name() const override { return "tan"; }
1454 enum Functype functype() const override { return TAN_FUNC; }
1455};
1456
1457class Item_func_cot final : public Item_dec_func {
1458 public:
1459 Item_func_cot(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1460 double val_real() override;
1461 const char *func_name() const override { return "cot"; }
1462 enum Functype functype() const override { return COT_FUNC; }
1463};
1464
1466 public:
1468 Item_func_int_val(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1469 bool resolve_type_inner(THD *thd) override;
1470};
1471
1473 public:
1475 Item_func_ceiling(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1476 const char *func_name() const override { return "ceiling"; }
1477 longlong int_op() override;
1478 double real_op() override;
1479 my_decimal *decimal_op(my_decimal *) override;
1480 bool check_partition_func_processor(uchar *) override { return false; }
1481 bool check_function_as_value_generator(uchar *) override { return false; }
1482 enum Functype functype() const override { return CEILING_FUNC; }
1483};
1484
1486 public:
1488 Item_func_floor(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1489 const char *func_name() const override { return "floor"; }
1490 longlong int_op() override;
1491 double real_op() override;
1492 my_decimal *decimal_op(my_decimal *) override;
1493 bool check_partition_func_processor(uchar *) override { return false; }
1494 bool check_function_as_value_generator(uchar *) override { return false; }
1495 enum Functype functype() const override { return FLOOR_FUNC; }
1496};
1497
1498/* This handles round and truncate */
1499
1500class Item_func_round final : public Item_func_num1 {
1502
1503 public:
1504 Item_func_round(Item *a, Item *b, bool trunc_arg)
1505 : Item_func_num1(a, b), truncate(trunc_arg) {}
1506 Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1507 : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1508
1509 const char *func_name() const override {
1510 return truncate ? "truncate" : "round";
1511 }
1512 double real_op() override;
1513 longlong int_op() override;
1514 my_decimal *decimal_op(my_decimal *) override;
1515 bool resolve_type(THD *) override;
1516 enum Functype functype() const override {
1518 }
1519};
1520
1521class Item_func_rand final : public Item_real_func {
1523
1525 bool first_eval{true}; // true if val_real() is called 1st time
1526 public:
1528 Item_func_rand(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1529 explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1530
1531 bool do_itemize(Parse_context *pc, Item **res) override;
1532 double val_real() override;
1533 const char *func_name() const override { return "rand"; }
1534 /**
1535 This function is non-deterministic and hence depends on the
1536 'RAND' pseudo-table.
1537
1538 @returns RAND_TABLE_BIT
1539 */
1541 return RAND_TABLE_BIT;
1542 }
1543 bool fix_fields(THD *thd, Item **ref) override;
1544 bool resolve_type(THD *thd) override;
1545 void cleanup() override {
1546 first_eval = true;
1548 }
1549 bool check_function_as_value_generator(uchar *checker_args) override {
1551 pointer_cast<Check_function_as_value_generator_parameters *>(
1552 checker_args);
1553 func_arg->banned_function_name = func_name();
1554 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1555 (func_arg->source == VGS_CHECK_CONSTRAINT));
1556 }
1557
1558 private:
1559 void seed_random(Item *val);
1560};
1561
1562class Item_func_sign final : public Item_int_func {
1563 public:
1564 Item_func_sign(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1565 const char *func_name() const override { return "sign"; }
1566 enum Functype functype() const override { return SIGN_FUNC; }
1567 longlong val_int() override;
1568 bool resolve_type(THD *thd) override;
1569};
1570
1571// Common base class for the DEGREES and RADIANS functions.
1573 double mul, add;
1574
1575 protected:
1576 Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1577 : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1578
1579 public:
1580 double val_real() override;
1581 bool resolve_type(THD *thd) override;
1582};
1583
1585 public:
1587 : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1588 const char *func_name() const override { return "degrees"; }
1589 enum Functype functype() const override { return DEGREES_FUNC; }
1590};
1591
1593 public:
1595 : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1596 const char *func_name() const override { return "radians"; }
1597 enum Functype functype() const override { return RADIANS_FUNC; }
1598};
1600 public:
1601 Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1602 : Item_func_numhybrid(pos, opt_list),
1603 m_is_least_func(is_least_func),
1605
1606 longlong val_int() override;
1607 double val_real() override;
1608 my_decimal *val_decimal(my_decimal *) override;
1609 longlong int_op() override;
1610 double real_op() override;
1611 my_decimal *decimal_op(my_decimal *) override;
1612 String *str_op(String *) override;
1613 bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1614 bool time_op(MYSQL_TIME *ltime) override;
1616 return MYSQL_TYPE_VARCHAR;
1617 }
1618 bool resolve_type(THD *thd) override;
1619 bool resolve_type_inner(THD *thd) override;
1620 void set_numeric_type() override {}
1621 enum Item_result result_type() const override { return hybrid_type; }
1622 TYPELIB *get_typelib() const override;
1623
1624 /**
1625 Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1626 return a number in format YYMMDDhhmmss.
1627 */
1628 enum Item_result cast_to_int_type() const override {
1630 }
1631
1632 /// Returns true if arguments to this function should be compared as dates.
1633 bool compare_as_dates() const;
1634
1635 /// Returns true if at least one of the arguments was of temporal type.
1636 bool has_temporal_arg() const { return temporal_item; }
1637
1638 private:
1639 /// True if LEAST function, false if GREATEST.
1642 /*
1643 Used for determining whether one of the arguments is of temporal type and
1644 for converting arguments to a common output format if arguments are
1645 compared as dates and result type is character string. For example,
1646 LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1647 '95-05-05'.
1648 */
1650
1651 /**
1652 Fractional seconds precision to use when converting a time or timestamp
1653 expression into a string.
1654 */
1656 /**
1657 Compare arguments as datetime values.
1658
1659 @param value Pointer to which the datetime value of the winning argument
1660 is written.
1661
1662 @return true if error, false otherwise.
1663 */
1664 bool cmp_datetimes(longlong *value);
1665
1666 /**
1667 Compare arguments as time values.
1668
1669 @param value Pointer to which the time value of the winning argument is
1670 written.
1671
1672 @return true if error, false otherwise.
1673 */
1674 bool cmp_times(longlong *value);
1675};
1676
1677class Item_func_min final : public Item_func_min_max {
1678 public:
1679 Item_func_min(const POS &pos, PT_item_list *opt_list)
1680 : Item_func_min_max(pos, opt_list, true) {}
1681 const char *func_name() const override { return "least"; }
1682 enum Functype functype() const override { return LEAST_FUNC; }
1683};
1684
1685class Item_func_max final : public Item_func_min_max {
1686 public:
1687 Item_func_max(const POS &pos, PT_item_list *opt_list)
1688 : Item_func_min_max(pos, opt_list, false) {}
1689 const char *func_name() const override { return "greatest"; }
1690 enum Functype functype() const override { return GREATEST_FUNC; }
1691};
1692
1693/**
1694 A wrapper Item that normally returns its parameter, but becomes NULL when
1695 processing rows for rollup. Rollup is implemented by AggregateIterator, and
1696 works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1697 where only one group level is active, and so on. E.g., for a query with GROUP
1698 BY a,b, the rows will look like this:
1699
1700 a b rollup level
1701 1 1 2
1702 1 2 2
1703 1 NULL 1
1704 2 1 2
1705 2 NULL 1
1706 NULL NULL 0
1707
1708 Each rollup group item has a minimum level for when it becomes NULL. In the
1709 example above, a would have minimum level 0 and b would have minimum level 1.
1710 For simplicity, the JOIN carries a list of all rollup group items, and they
1711 are being given the current rollup level when it changes. A rollup level of
1712 INT_MAX essentially always disables rollup, which is useful when there are
1713 leftover group items in places that are not relevant for rollup
1714 (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1715 tables that are created before grouping, which should then effectively be
1716 disabled).
1717 */
1718class Item_rollup_group_item final : public Item_func {
1719 public:
1724 // We're going to replace inner_item in the SELECT list, so copy its hidden
1725 // status. (We could have done this in the caller, but it fits naturally in
1726 // with all the other copying done here.)
1728 set_nullable(true);
1730 }
1731 double val_real() override;
1732 longlong val_int() override;
1733 String *val_str(String *str) override;
1735 bool val_json(Json_wrapper *result) override;
1736 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1737 bool get_time(MYSQL_TIME *ltime) override;
1738 const char *func_name() const override { return "rollup_group_item"; }
1739 table_map used_tables() const override {
1740 /*
1741 If underlying item is non-constant, return its used_tables value.
1742 Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1743 */
1744 return args[0]->const_for_execution()
1745 ? (args[0]->used_tables() | RAND_TABLE_BIT)
1746 : args[0]->used_tables();
1747 }
1748 void update_used_tables() override {
1751 }
1752 Item_result result_type() const override { return args[0]->result_type(); }
1753 bool resolve_type(THD *) override {
1754 // needn't handle dynamic parameter as its const_item() is false.
1756
1757 // The item could be a NULL constant.
1758 null_value = args[0]->is_null();
1759 return false;
1760 }
1761 Item *inner_item() { return args[0]; }
1762 const Item *inner_item() const { return args[0]; }
1763 bool rollup_null() const {
1765 }
1766 enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1767 void print(const THD *thd, String *str,
1768 enum_query_type query_type) const override;
1769 bool eq_specific(const Item *item) const override;
1770 TYPELIB *get_typelib() const override;
1771
1772 // Used by AggregateIterator.
1774
1775 // Used when cloning the item only.
1776 int min_rollup_level() const { return m_min_rollup_level; }
1777
1778 private:
1781};
1782
1785
1786 public:
1787 Item_func_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1788 longlong val_int() override;
1789 const char *func_name() const override { return "length"; }
1790 bool resolve_type(THD *thd) override {
1791 if (param_type_is_default(thd, 0, 1)) return true;
1792 max_length = 10;
1793 return false;
1794 }
1795};
1796
1799
1800 public:
1801 Item_func_vector_dim(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1802 longlong val_int() override;
1803 const char *func_name() const override { return "vector_dim"; }
1804 bool resolve_type(THD *thd) override {
1805 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_VECTOR)) {
1806 return true;
1807 }
1808 bool valid_type = (args[0]->data_type() == MYSQL_TYPE_VECTOR) ||
1809 (args[0]->result_type() == STRING_RESULT &&
1811 if (!valid_type) {
1812 my_error(ER_WRONG_ARGUMENTS, MYF(0), func_name());
1813 return true;
1814 }
1815 max_length = 10;
1816 return false;
1817 }
1818};
1819
1821 public:
1822 Item_func_bit_length(const POS &pos, Item *a) : Item_func_length(pos, a) {}
1823 longlong val_int() override {
1824 assert(fixed);
1825 return Item_func_length::val_int() * 8;
1826 }
1827 const char *func_name() const override { return "bit_length"; }
1828};
1829
1832
1833 public:
1835 Item_func_char_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1836 longlong val_int() override;
1837 const char *func_name() const override { return "char_length"; }
1838 bool resolve_type(THD *thd) override {
1839 max_length = 10;
1840 return Item_int_func::resolve_type(thd);
1841 }
1842};
1843
1845 public:
1846 Item_func_coercibility(const POS &pos, Item *a) : Item_int_func(pos, a) {
1847 null_on_null = false;
1848 }
1849 longlong val_int() override;
1850 const char *func_name() const override { return "coercibility"; }
1851 bool resolve_type(THD *thd) override {
1852 max_length = 10;
1853 set_nullable(false);
1854 return Item_int_func::resolve_type(thd);
1855 }
1856};
1857
1860
1861 public:
1863 Item_func_locate(const POS &pos, Item *a, Item *b)
1864 : Item_int_func(pos, a, b) {}
1865 Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1866 : Item_int_func(pos, a, b, c) {}
1867
1868 const char *func_name() const override { return "locate"; }
1869 longlong val_int() override;
1870 bool resolve_type(THD *thd) override;
1871 void print(const THD *thd, String *str,
1872 enum_query_type query_type) const override;
1873};
1874
1875class Item_func_instr final : public Item_func_locate {
1876 public:
1877 Item_func_instr(const POS &pos, Item *a, Item *b)
1878 : Item_func_locate(pos, a, b) {}
1879
1880 const char *func_name() const override { return "instr"; }
1881};
1882
1884 public:
1886 : Item_int_func(pos, a) {}
1887 longlong val_int() override;
1888 const char *func_name() const override {
1889 return "validate_password_strength";
1890 }
1891 bool resolve_type(THD *thd) override {
1892 max_length = 10;
1893 set_nullable(true);
1894 return Item_int_func::resolve_type(thd);
1895 }
1896};
1897
1898class Item_func_field final : public Item_int_func {
1901
1902 public:
1903 Item_func_field(const POS &pos, PT_item_list *opt_list)
1904 : Item_int_func(pos, opt_list) {}
1905 longlong val_int() override;
1906 const char *func_name() const override { return "field"; }
1907 bool resolve_type(THD *thd) override;
1908};
1909
1910class Item_func_ascii final : public Item_int_func {
1912
1913 public:
1914 Item_func_ascii(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1915 longlong val_int() override;
1916 const char *func_name() const override { return "ascii"; }
1917 bool resolve_type(THD *thd) override {
1918 max_length = 3;
1919 return Item_int_func::resolve_type(thd);
1920 }
1921};
1922
1923class Item_func_ord final : public Item_int_func {
1925
1926 public:
1927 Item_func_ord(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1928 longlong val_int() override;
1929 const char *func_name() const override { return "ord"; }
1930};
1931
1934 /*
1935 if m_enum_value is non-zero, it indicates the index of the value of
1936 argument 0 in the set in argument 1, given that argument 0 is
1937 a constant value and argument 1 is a field of type SET.
1938 */
1941
1942 public:
1944 : Item_int_func(pos, a, b) {}
1945 longlong val_int() override;
1946 const char *func_name() const override { return "find_in_set"; }
1947 bool resolve_type(THD *) override;
1948 const CHARSET_INFO *compare_collation() const override {
1949 return cmp_collation.collation;
1950 }
1951};
1952
1953/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1954
1955class Item_func_bit : public Item_func {
1956 protected:
1957 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1959 /// Buffer storing the determined value
1961 /**
1962 @returns true if the second argument should be of binary type for the
1963 result to be of binary type.
1964 */
1966
1967 public:
1968 Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1969 Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1970
1971 bool resolve_type(THD *) override;
1972 enum Item_result result_type() const override { return hybrid_type; }
1973
1974 longlong val_int() override;
1975 String *val_str(String *str) override;
1976 double val_real() override;
1977 my_decimal *val_decimal(my_decimal *decimal_value) override;
1978
1979 void print(const THD *thd, String *str,
1980 enum_query_type query_type) const override {
1981 print_op(thd, str, query_type);
1982 }
1983 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1984 if (hybrid_type == INT_RESULT)
1985 return get_date_from_int(ltime, fuzzydate);
1986 else
1987 return get_date_from_string(ltime, fuzzydate);
1988 }
1989 bool get_time(MYSQL_TIME *ltime) override {
1990 if (hybrid_type == INT_RESULT)
1991 return get_time_from_int(ltime);
1992 else
1993 return get_time_from_string(ltime);
1994 }
1995
1996 private:
1997 /**
1998 @brief Performs the operation on integers to produce a result of type
1999 INT_RESULT.
2000 @return The result of the operation.
2001 */
2002 virtual longlong int_op() = 0;
2003
2004 /**
2005 @brief Performs the operation on binary strings to produce a result of
2006 type STRING_RESULT.
2007 @return The result of the operation.
2008 */
2009 virtual String *str_op(String *) = 0;
2010};
2011
2012/**
2013 Base class for all the bit functions that work with two binary
2014 arguments: '&', '|', '^'.
2015*/
2016
2018 protected:
2020 return true;
2021 }
2022 template <class Char_func, class Int_func>
2023 String *eval_str_op(String *, Char_func char_func, Int_func int_func);
2024 template <class Int_func>
2025 longlong eval_int_op(Int_func int_func);
2026
2027 public:
2029 : Item_func_bit(pos, a, b) {}
2030};
2031
2033 public:
2034 Item_func_bit_or(const POS &pos, Item *a, Item *b)
2035 : Item_func_bit_two_param(pos, a, b) {}
2036 const char *func_name() const override { return "|"; }
2037
2038 private:
2039 longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
2040 String *str_op(String *str) override {
2041 return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
2042 }
2043};
2044
2046 public:
2047 Item_func_bit_and(const POS &pos, Item *a, Item *b)
2048 : Item_func_bit_two_param(pos, a, b) {}
2049 const char *func_name() const override { return "&"; }
2050
2051 private:
2052 longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
2053 String *str_op(String *str) override {
2054 return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
2055 }
2056};
2057
2059 public:
2060 Item_func_bit_xor(const POS &pos, Item *a, Item *b)
2061 : Item_func_bit_two_param(pos, a, b) {}
2062 const char *func_name() const override { return "^"; }
2063
2064 private:
2065 longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
2066 String *str_op(String *str) override {
2067 return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
2068 }
2069};
2070
2072 public:
2073 Item_func_bit_count(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2074 longlong val_int() override;
2075 const char *func_name() const override { return "bit_count"; }
2076 bool resolve_type(THD *thd) override {
2077 // Default: binary string; reprepare if integer
2078 if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
2079 args[0]->propagate_type(
2081 return true;
2083 return false;
2084 }
2085};
2086
2088 protected:
2090 return false;
2091 }
2092 template <bool to_left>
2094 template <bool to_left>
2096
2097 public:
2098 Item_func_shift(const POS &pos, Item *a, Item *b)
2099 : Item_func_bit(pos, a, b) {}
2100};
2101
2103 public:
2104 Item_func_shift_left(const POS &pos, Item *a, Item *b)
2105 : Item_func_shift(pos, a, b) {}
2106 const char *func_name() const override { return "<<"; }
2107
2108 private:
2109 longlong int_op() override;
2110 String *str_op(String *str) override;
2111};
2112
2114 public:
2116 : Item_func_shift(pos, a, b) {}
2117 const char *func_name() const override { return ">>"; }
2118
2119 private:
2120 longlong int_op() override;
2121 String *str_op(String *str) override;
2122};
2123
2124class Item_func_bit_neg final : public Item_func_bit {
2125 protected:
2127 return false;
2128 }
2129
2130 public:
2131 Item_func_bit_neg(const POS &pos, Item *a) : Item_func_bit(pos, a) {}
2132 const char *func_name() const override { return "~"; }
2133 void print(const THD *thd, String *str,
2134 enum_query_type query_type) const override {
2135 Item_func::print(thd, str, query_type);
2136 }
2137
2138 private:
2139 longlong int_op() override;
2140 String *str_op(String *str) override;
2141};
2142
2145
2146 public:
2148 explicit Item_func_last_insert_id(const POS &pos) : Item_int_func(pos) {}
2149 Item_func_last_insert_id(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2150
2151 bool do_itemize(Parse_context *pc, Item **res) override;
2152 longlong val_int() override;
2153 const char *func_name() const override { return "last_insert_id"; }
2154
2156 return INNER_TABLE_BIT;
2157 }
2158
2159 bool resolve_type(THD *thd) override {
2160 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2161 unsigned_flag = true;
2162 return false;
2163 }
2164 bool check_function_as_value_generator(uchar *checker_args) override {
2166 pointer_cast<Check_function_as_value_generator_parameters *>(
2167 checker_args);
2168 func_arg->banned_function_name = func_name();
2169 return true;
2170 }
2171};
2172
2175
2176 public:
2177 Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
2178 : Item_int_func(pos, count_expr, expr) {}
2179
2180 /// Ensure that "benchmark()" is never optimized away
2182 return RAND_TABLE_BIT;
2183 }
2184
2185 bool do_itemize(Parse_context *pc, Item **res) override;
2186 longlong val_int() override;
2187 const char *func_name() const override { return "benchmark"; }
2188 bool resolve_type(THD *thd) override {
2189 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2190 if (param_type_is_default(thd, 1, 2)) return true;
2191 max_length = 1;
2192 set_nullable(true);
2193 return false;
2194 }
2195 void print(const THD *thd, String *str,
2196 enum_query_type query_type) const override;
2197 bool check_function_as_value_generator(uchar *checker_args) override {
2199 pointer_cast<Check_function_as_value_generator_parameters *>(
2200 checker_args);
2201 func_arg->banned_function_name = func_name();
2202 return true;
2203 }
2204};
2205
2208
2209class Item_func_sleep final : public Item_int_func {
2211
2212 public:
2213 Item_func_sleep(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2214
2215 bool do_itemize(Parse_context *pc, Item **res) override;
2216 const char *func_name() const override { return "sleep"; }
2217 /**
2218 This function is non-deterministic and hence depends on the
2219 'RAND' pseudo-table.
2220
2221 @returns RAND_TABLE_BIT
2222 */
2224 return RAND_TABLE_BIT;
2225 }
2226 bool check_function_as_value_generator(uchar *checker_args) override {
2228 pointer_cast<Check_function_as_value_generator_parameters *>(
2229 checker_args);
2230 func_arg->banned_function_name = func_name();
2231 return true;
2232 }
2233 bool resolve_type(THD *thd) override {
2234 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2235 return Item_int_func::resolve_type(thd);
2236 }
2237 longlong val_int() override;
2238};
2239
2240class Item_udf_func : public Item_func {
2242
2243 protected:
2245
2246 public:
2247 Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2248 : Item_func(pos, opt_list), udf(udf_arg) {
2249 null_on_null = false;
2250 }
2251 ~Item_udf_func() override = default;
2252
2253 bool do_itemize(Parse_context *pc, Item **res) override;
2254 const char *func_name() const override { return udf.name(); }
2255 enum Functype functype() const override { return UDF_FUNC; }
2258 }
2259 bool fix_fields(THD *thd, Item **ref) override;
2260 void cleanup() override;
2261 Item_result result_type() const override { return udf.result_type(); }
2262 void print(const THD *thd, String *str,
2263 enum_query_type query_type) const override;
2264
2265 bool check_function_as_value_generator(uchar *checker_args) override {
2267 pointer_cast<Check_function_as_value_generator_parameters *>(
2268 checker_args);
2269 func_arg->banned_function_name = func_name();
2270 return true;
2271 }
2272
2273 void compute_cost(CostOfItem *root_cost) const override {
2274 root_cost->MarkExpensive();
2275 }
2276
2277 protected:
2278 bool may_have_named_parameters() const override { return true; }
2279
2280 private:
2281 /**
2282 This member is set during resolving and is used by update_used_tables() and
2283 fix_after_pullout() to preserve the non-deterministic property.
2284 */
2286};
2287
2289 public:
2290 Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2291 : Item_udf_func(pos, udf_arg, opt_list) {}
2292 longlong val_int() override {
2293 assert(fixed);
2295 }
2296 my_decimal *val_decimal(my_decimal *dec_buf) override {
2297 const double res = val_real();
2298 if (null_value) return nullptr;
2299 double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2300 return dec_buf;
2301 }
2302 double val_real() override;
2303 String *val_str(String *str) override;
2304 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2305 return get_date_from_real(ltime, fuzzydate);
2306 }
2307 bool get_time(MYSQL_TIME *ltime) override {
2308 return get_time_from_real(ltime);
2309 }
2310 bool resolve_type(THD *) override {
2312 fix_num_length_and_dec(); // @todo - needed?
2313 return false;
2314 }
2315};
2316
2317class Item_func_udf_int final : public Item_udf_func {
2318 public:
2319 Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2320 : Item_udf_func(pos, udf_arg, opt_list) {}
2321 longlong val_int() override;
2322 double val_real() override {
2323 return static_cast<double>(Item_func_udf_int::val_int());
2324 }
2325 String *val_str(String *str) override;
2326 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2327 return get_date_from_int(ltime, fuzzydate);
2328 }
2329 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2330 enum Item_result result_type() const override { return INT_RESULT; }
2331 bool resolve_type(THD *) override {
2333 return false;
2334 }
2335};
2336
2338 public:
2339 Item_func_udf_decimal(const POS &pos, udf_func *udf_arg,
2340 PT_item_list *opt_list)
2341 : Item_udf_func(pos, udf_arg, opt_list) {}
2342 longlong val_int() override;
2343 double val_real() override;
2344 my_decimal *val_decimal(my_decimal *) override;
2345 String *val_str(String *str) override;
2346 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2347 return get_date_from_decimal(ltime, fuzzydate);
2348 }
2349 bool get_time(MYSQL_TIME *ltime) override {
2350 return get_time_from_decimal(ltime);
2351 }
2352 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2353 bool resolve_type(THD *thd) override;
2354};
2355
2357 public:
2358 Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2359 : Item_udf_func(pos, udf_arg, opt_list) {}
2360
2361 String *val_str(String *) override;
2362 double val_real() override {
2363 int err_not_used;
2364 const char *end_not_used;
2365 String *res;
2366 res = val_str(&str_value);
2367 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2368 &end_not_used, &err_not_used)
2369 : 0.0;
2370 }
2371 longlong val_int() override {
2372 int err_not_used;
2373 String *res;
2374 res = val_str(&str_value);
2375 return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2376 nullptr, &err_not_used)
2377 : (longlong)0;
2378 }
2379 my_decimal *val_decimal(my_decimal *dec_buf) override {
2380 String *res = val_str(&str_value);
2381 if (!res) return nullptr;
2382 str2my_decimal(E_DEC_FATAL_ERROR, res->ptr(), res->length(), res->charset(),
2383 dec_buf);
2384 return dec_buf;
2385 }
2386 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2387 return get_date_from_string(ltime, fuzzydate);
2388 }
2389 bool get_time(MYSQL_TIME *ltime) override {
2390 return get_time_from_string(ltime);
2391 }
2392 enum Item_result result_type() const override { return STRING_RESULT; }
2393 bool resolve_type(THD *thd) override;
2394};
2395
2396void mysql_ull_cleanup(THD *thd);
2398
2399class Item_func_get_lock final : public Item_int_func {
2401
2403
2404 public:
2405 Item_func_get_lock(const POS &pos, Item *a, Item *b)
2406 : Item_int_func(pos, a, b) {}
2407
2408 bool do_itemize(Parse_context *pc, Item **res) override;
2409 longlong val_int() override;
2410 const char *func_name() const override { return "get_lock"; }
2412 return INNER_TABLE_BIT;
2413 }
2414 bool resolve_type(THD *thd) override {
2415 if (param_type_is_default(thd, 0, 1)) return true;
2416 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
2417 max_length = 1;
2418 set_nullable(true);
2419 return false;
2420 }
2421 bool is_non_const_over_literals(uchar *) override { return true; }
2422 bool check_function_as_value_generator(uchar *checker_args) override {
2424 pointer_cast<Check_function_as_value_generator_parameters *>(
2425 checker_args);
2426 func_arg->banned_function_name = func_name();
2427 return true;
2428 }
2429};
2430
2433
2435
2436 public:
2437 Item_func_release_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2438 bool do_itemize(Parse_context *pc, Item **res) override;
2439
2440 longlong val_int() override;
2441 const char *func_name() const override { return "release_lock"; }
2443 return INNER_TABLE_BIT;
2444 }
2445 bool resolve_type(THD *thd) override {
2446 if (param_type_is_default(thd, 0, 1)) return true;
2447 max_length = 1;
2448 set_nullable(true);
2449 return false;
2450 }
2451 bool is_non_const_over_literals(uchar *) override { return true; }
2452 bool check_function_as_value_generator(uchar *checker_args) override {
2454 pointer_cast<Check_function_as_value_generator_parameters *>(
2455 checker_args);
2456 func_arg->banned_function_name = func_name();
2457 return true;
2458 }
2459};
2460
2463
2464 public:
2465 explicit Item_func_release_all_locks(const POS &pos) : Item_int_func(pos) {}
2466 bool do_itemize(Parse_context *pc, Item **res) override;
2467
2468 longlong val_int() override;
2469 const char *func_name() const override { return "release_all_locks"; }
2471 return INNER_TABLE_BIT;
2472 }
2473 bool resolve_type(THD *) override {
2474 unsigned_flag = true;
2475 return false;
2476 }
2477 bool is_non_const_over_literals(uchar *) override { return true; }
2478 bool check_function_as_value_generator(uchar *checker_args) override {
2480 pointer_cast<Check_function_as_value_generator_parameters *>(
2481 checker_args);
2482 func_arg->banned_function_name = func_name();
2483 return true;
2484 }
2485};
2486
2487/* replication functions */
2488
2492
2493 public:
2494 Item_source_pos_wait(const POS &pos, Item *a, Item *b)
2495 : Item_int_func(pos, a, b) {}
2496 Item_source_pos_wait(const POS &pos, Item *a, Item *b, Item *c)
2497 : Item_int_func(pos, a, b, c) {}
2498 Item_source_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2499 : Item_int_func(pos, a, b, c, d) {}
2500
2501 bool do_itemize(Parse_context *pc, Item **res) override;
2502 longlong val_int() override;
2503 const char *func_name() const override { return "source_pos_wait"; }
2505 return INNER_TABLE_BIT;
2506 }
2507 bool resolve_type(THD *thd) override {
2508 if (param_type_is_default(thd, 0, 1)) return true;
2509 if (param_type_is_default(thd, 1, 3, MYSQL_TYPE_LONGLONG)) return true;
2510 if (param_type_is_default(thd, 3, 4)) return true;
2511 max_length = 21;
2512 set_nullable(true);
2513 return false;
2514 }
2515 bool check_function_as_value_generator(uchar *checker_args) override {
2517 pointer_cast<Check_function_as_value_generator_parameters *>(
2518 checker_args);
2519 func_arg->banned_function_name = func_name();
2520 return true;
2521 }
2522};
2523
2525 public:
2526 Item_master_pos_wait(const POS &pos, Item *a, Item *b)
2527 : Item_source_pos_wait(pos, a, b) {}
2528 Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c)
2529 : Item_source_pos_wait(pos, a, b, c) {}
2530 Item_master_pos_wait(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2531 : Item_source_pos_wait(pos, a, b, c, d) {}
2532 longlong val_int() override;
2533};
2534
2535/**
2536 Internal functions used by INFORMATION_SCHEMA implementation to check
2537 if user have access to given database/table/column.
2538*/
2539
2541 public:
2543 : Item_int_func(pos, a) {}
2544 longlong val_int() override;
2545 const char *func_name() const override { return "can_access_database"; }
2546 bool resolve_type(THD *) override {
2547 set_nullable(true);
2548 return false;
2549 }
2550};
2551
2553 public:
2555 : Item_int_func(pos, a, b) {}
2556 longlong val_int() override;
2557 const char *func_name() const override { return "can_access_table"; }
2558 bool resolve_type(THD *) override {
2559 set_nullable(true);
2560 return false;
2561 }
2562};
2563
2565 public:
2567 : Item_int_func(pos, a, b) {}
2568 longlong val_int() override;
2569 const char *func_name() const override { return "can_access_user"; }
2570 bool resolve_type(THD *) override {
2571 set_nullable(true);
2572 return false;
2573 }
2574};
2575
2577 public:
2579 : Item_int_func(pos, a, b) {}
2580 longlong val_int() override;
2581 const char *func_name() const override { return "can_access_trigger"; }
2582 bool resolve_type(THD *) override {
2583 max_length = 4;
2584 set_nullable(true);
2585 return false;
2586 }
2587};
2588
2590 public:
2592 : Item_int_func(pos, list) {}
2593 longlong val_int() override;
2594 const char *func_name() const override { return "can_access_routine"; }
2595 bool resolve_type(THD *) override {
2596 max_length = 4;
2597 set_nullable(true);
2598 return false;
2599 }
2600};
2601
2603 public:
2605 longlong val_int() override;
2606 const char *func_name() const override { return "can_access_event"; }
2607 bool resolve_type(THD *) override {
2608 set_nullable(true);
2609 return false;
2610 }
2611};
2612
2614 public:
2616 : Item_int_func(pos, a) {}
2617 longlong val_int() override;
2618 const char *func_name() const override { return "can_access_resource_group"; }
2619 bool resolve_type(THD *) override {
2620 max_length = 1; // Function can return 0 or 1.
2621 set_nullable(true);
2622 return false;
2623 }
2624};
2625
2627 public:
2628 Item_func_can_access_view(const POS &pos, Item *a, Item *b, Item *c, Item *d)
2629 : Item_int_func(pos, a, b, c, d) {}
2630 longlong val_int() override;
2631 const char *func_name() const override { return "can_access_view"; }
2632 bool resolve_type(THD *) override {
2633 set_nullable(true);
2634 return false;
2635 }
2636};
2637
2639 public:
2641 : Item_int_func(pos, a, b, c) {}
2642 longlong val_int() override;
2643 const char *func_name() const override { return "can_access_column"; }
2644 bool resolve_type(THD *) override {
2645 set_nullable(true);
2646 return false;
2647 }
2648};
2649
2651 public:
2653 : Item_int_func(pos, a) {}
2655 : Item_int_func(pos, a, b) {}
2657 : Item_int_func(pos, a, b, c) {}
2658 longlong val_int() override;
2659 const char *func_name() const override { return "is_visible_dd_object"; }
2660 bool resolve_type(THD *) override {
2661 max_length = 1;
2662 set_nullable(true);
2663 return false;
2664 }
2665};
2666
2668 public:
2670 : Item_int_func(pos, list) {}
2671 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2672 longlong val_int() override;
2673 const char *func_name() const override { return "internal_table_rows"; }
2674 bool resolve_type(THD *) override {
2675 set_nullable(true);
2676 unsigned_flag = true;
2677 null_on_null = false;
2678 return false;
2679 }
2680};
2681
2683 public:
2685 : Item_int_func(pos, list) {}
2686 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2687 longlong val_int() override;
2688 const char *func_name() const override { return "internal_avg_row_length"; }
2689 bool resolve_type(THD *) override {
2690 set_nullable(true);
2691 unsigned_flag = true;
2692 null_on_null = false;
2693 return false;
2694 }
2695};
2696
2698 public:
2700 : Item_int_func(pos, list) {}
2701 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2702 longlong val_int() override;
2703 const char *func_name() const override { return "internal_data_length"; }
2704 bool resolve_type(THD *) override {
2705 set_nullable(true);
2706 unsigned_flag = true;
2707 null_on_null = false;
2708 return false;
2709 }
2710};
2711
2713 public:
2715 : Item_int_func(pos, list) {}
2716 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2717 longlong val_int() override;
2718 const char *func_name() const override { return "internal_max_data_length"; }
2719 bool resolve_type(THD *) override {
2720 set_nullable(true);
2721 unsigned_flag = true;
2722 null_on_null = false;
2723 return false;
2724 }
2725};
2726
2728 public:
2730 : Item_int_func(pos, list) {}
2731 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2732 longlong val_int() override;
2733 const char *func_name() const override { return "internal_index_length"; }
2734 bool resolve_type(THD *) override {
2735 set_nullable(true);
2736 unsigned_flag = true;
2737 null_on_null = false;
2738 return false;
2739 }
2740};
2741
2743 public:
2745 : Item_int_func(pos, list) {}
2746 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2747 longlong val_int() override;
2748 const char *func_name() const override { return "internal_data_free"; }
2749 bool resolve_type(THD *) override {
2750 set_nullable(true);
2751 unsigned_flag = true;
2752 null_on_null = false;
2753 return false;
2754 }
2755};
2756
2758 public:
2760 : Item_int_func(pos, list) {}
2761 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2762 longlong val_int() override;
2763 const char *func_name() const override { return "internal_auto_increment"; }
2764 bool resolve_type(THD *) override {
2765 set_nullable(true);
2766 unsigned_flag = true;
2767 null_on_null = false;
2768 return false;
2769 }
2770};
2771
2773 public:
2775 : Item_int_func(pos, list) {}
2776 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2777 longlong val_int() override;
2778 const char *func_name() const override { return "internal_checksum"; }
2779 bool resolve_type(THD *) override {
2780 set_nullable(true);
2781 null_on_null = false;
2782 return false;
2783 }
2784};
2785
2787 public:
2789 : Item_int_func(pos, a) {}
2790 longlong val_int() override;
2791 const char *func_name() const override { return "internal_keys_disabled"; }
2792 bool resolve_type(THD *) override {
2793 set_nullable(false);
2794 null_on_null = false;
2795 return false;
2796 }
2797};
2798
2800 public:
2803 : Item_int_func(pos, list) {}
2804 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2805 longlong val_int() override;
2806 const char *func_name() const override {
2807 return "internal_index_column_cardinality";
2808 }
2809 bool resolve_type(THD *) override {
2810 set_nullable(true);
2811 null_on_null = false;
2812 return false;
2813 }
2814};
2815
2817 public:
2819 Item *d)
2820 : Item_int_func(pos, a, b, c, d) {}
2821 longlong val_int() override;
2822 const char *func_name() const override { return "internal_dd_char_length"; }
2823 bool resolve_type(THD *) override {
2824 set_nullable(true);
2825 null_on_null = false;
2826 return false;
2827 }
2828};
2829
2831 : public Item_int_func {
2832 public:
2835 : Item_int_func(pos, list) {}
2836 longlong val_int() override;
2837 const char *func_name() const override {
2838 return "internal_get_view_warning_or_error";
2839 }
2840 bool resolve_type(THD *) override {
2841 max_length = 1;
2842 set_nullable(false);
2843 null_on_null = false;
2844 return false;
2845 }
2846};
2847
2849 public:
2851 : Item_int_func(pos, list) {}
2852 longlong val_int() override;
2853 bool resolve_type(THD *) override {
2854 set_nullable(true);
2855 null_on_null = false;
2856 return false;
2857 }
2858 const char *func_name() const override {
2859 return "get_dd_index_sub_part_length";
2860 }
2861};
2862
2864 public:
2866 Item *d)
2867 : Item_int_func(pos, a, b, c, d) {}
2868 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2869 longlong val_int() override;
2870 const char *func_name() const override { return "internal_tablespace_id"; }
2871 bool resolve_type(THD *) override {
2872 set_nullable(true);
2873 null_on_null = false;
2874 return false;
2875 }
2876};
2877
2879 : public Item_int_func {
2880 public:
2882 Item *b, Item *c, Item *d)
2883 : Item_int_func(pos, a, b, c, d) {}
2884
2885 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2886 longlong val_int() override;
2887
2888 const char *func_name() const override {
2889 return "internal_tablespace_logfile_group_number";
2890 }
2891
2892 bool resolve_type(THD *) override {
2893 set_nullable(true);
2894 null_on_null = false;
2895 return false;
2896 }
2897};
2898
2900 public:
2902 Item *c, Item *d)
2903 : Item_int_func(pos, a, b, c, d) {}
2904
2905 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2906 longlong val_int() override;
2907
2908 const char *func_name() const override {
2909 return "internal_tablespace_free_extents";
2910 }
2911
2912 bool resolve_type(THD *) override {
2913 set_nullable(true);
2914 null_on_null = false;
2915 return false;
2916 }
2917};
2918
2920 public:
2922 Item *c, Item *d)
2923 : Item_int_func(pos, a, b, c, d) {}
2924
2925 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2926 longlong val_int() override;
2927
2928 const char *func_name() const override {
2929 return "internal_tablespace_total_extents";
2930 }
2931
2932 bool resolve_type(THD *) override {
2933 set_nullable(true);
2934 null_on_null = false;
2935 return false;
2936 }
2937};
2938
2940 public:
2942 Item *c, Item *d)
2943 : Item_int_func(pos, a, b, c, d) {}
2944
2945 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2946 longlong val_int() override;
2947
2948 const char *func_name() const override {
2949 return "internal_tablespace_extent_size";
2950 }
2951
2952 bool resolve_type(THD *) override {
2953 set_nullable(true);
2954 null_on_null = false;
2955 return false;
2956 }
2957};
2958
2960 public:
2962 Item *c, Item *d)
2963 : Item_int_func(pos, a, b, c, d) {}
2964
2965 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2966 longlong val_int() override;
2967
2968 const char *func_name() const override {
2969 return "internal_tablespace_initial_size";
2970 }
2971
2972 bool resolve_type(THD *) override {
2973 set_nullable(true);
2974 null_on_null = false;
2975 return false;
2976 }
2977};
2978
2980 public:
2982 Item *c, Item *d)
2983 : Item_int_func(pos, a, b, c, d) {}
2984
2985 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2986 longlong val_int() override;
2987
2988 const char *func_name() const override {
2989 return "internal_tablespace_maximum_size";
2990 }
2991
2992 bool resolve_type(THD *) override {
2993 set_nullable(true);
2994 null_on_null = false;
2995 return false;
2996 }
2997};
2998
3000 public:
3002 Item *b, Item *c, Item *d)
3003 : Item_int_func(pos, a, b, c, d) {}
3004
3005 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3006 longlong val_int() override;
3007
3008 const char *func_name() const override {
3009 return "internal_tablespace_autoextend_size";
3010 }
3011
3012 bool resolve_type(THD *) override {
3013 set_nullable(true);
3014 null_on_null = false;
3015 return false;
3016 }
3017};
3018
3020 public:
3022 Item *c, Item *d)
3023 : Item_int_func(pos, a, b, c, d) {}
3024
3025 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3026 longlong val_int() override;
3027
3028 const char *func_name() const override {
3029 return "internal_tablespace_version";
3030 }
3031
3032 bool resolve_type(THD *) override {
3033 set_nullable(true);
3034 null_on_null = false;
3035 return false;
3036 }
3037};
3038
3040 public:
3042 Item *c, Item *d)
3043 : Item_int_func(pos, a, b, c, d) {}
3044
3045 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
3046 longlong val_int() override;
3047
3048 const char *func_name() const override {
3049 return "internal_tablespace_data_free";
3050 }
3051
3052 bool resolve_type(THD *) override {
3053 set_nullable(true);
3054 null_on_null = false;
3055 return false;
3056 }
3057};
3058
3059/**
3060 Common class for:
3061 Item_func_get_system_var
3062 Item_func_get_user_var
3063 Item_func_set_user_var
3064*/
3065class Item_var_func : public Item_func {
3066 public:
3068 explicit Item_var_func(const POS &pos) : Item_func(pos) {}
3069
3070 Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
3071
3073 Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
3074
3075 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3076 return get_date_from_non_temporal(ltime, fuzzydate);
3077 }
3078 bool get_time(MYSQL_TIME *ltime) override {
3079 return get_time_from_non_temporal(ltime);
3080 }
3081 bool check_function_as_value_generator(uchar *checker_args) override {
3083 pointer_cast<Check_function_as_value_generator_parameters *>(
3084 checker_args);
3085 func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
3086 ? ER_CHECK_CONSTRAINT_VARIABLES
3087 : ER_DEFAULT_VAL_GENERATED_VARIABLES;
3088 return true;
3089 }
3090};
3091
3092/* Handling of user definable variables */
3093
3094// this is needed for user_vars hash
3097 m_ptr = nullptr;
3098 m_length = 0;
3099 }
3100 void set_value(char *value, size_t length) {
3101 m_ptr = value;
3102 m_length = length;
3103 }
3104
3105 /**
3106 Position inside a user_var_entry where small values are stored:
3107 double values, longlong values and string values with length
3108 up to extra_size (should be 8 bytes on all platforms).
3109 String values with length longer than 8 are stored in a separate
3110 memory buffer, which is allocated when needed using the method realloc().
3111 */
3113 return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
3114 }
3115
3116 /**
3117 Position inside a user_var_entry where a null-terminates array
3118 of characters representing the variable name is stored.
3119 */
3121
3122 /**
3123 Initialize m_ptr to the internal buffer (if the value is small enough),
3124 or allocate a separate buffer.
3125 @param length - length of the value to be stored.
3126 */
3127 bool mem_realloc(size_t length);
3128
3129 /**
3130 Check if m_ptr points to an external buffer previously allocated by
3131 realloc().
3132 @retval true - an external buffer is allocated.
3133 @retval false - m_ptr is null, or points to the internal buffer.
3134 */
3135 bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
3136
3137 /**
3138 Free the external value buffer, if it's allocated.
3139 */
3140 void free_value() {
3141 if (alloced()) my_free(m_ptr);
3142 }
3143
3144 /**
3145 Copy the array of characters from the given name into the internal
3146 name buffer and initialize entry_name to point to it.
3147 */
3149 name.strcpy(name_ptr());
3150 entry_name = Name_string(name_ptr(), name.length());
3151 }
3152
3153 /**
3154 Initialize all members
3155
3156 @param thd Current session.
3157 @param name Name of the user_var_entry instance.
3158 @param cs charset information of the user_var_entry instance.
3159 */
3160 void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
3161
3162 /**
3163 Store a value of the given type into a user_var_entry instance.
3164 @param from Value
3165 @param length Size of the value
3166 @param type type
3167 @retval false on success
3168 @retval true on memory allocation error
3169 */
3170 bool store(const void *from, size_t length, Item_result type);
3171
3172 /**
3173 Assert the user variable is locked.
3174 This is debug code only.
3175 The thread LOCK_thd_data mutex protects:
3176 - the thd->user_vars hash itself
3177 - the values in the user variable itself.
3178 The protection is required for monitoring,
3179 as a different thread can inspect this session
3180 user variables, on a live session.
3181 */
3182 void assert_locked() const;
3183
3184 static const size_t extra_size = sizeof(double);
3185 char *m_ptr; ///< Value
3186 size_t m_length; ///< Value length
3187 Item_result m_type; ///< Value type
3189 /**
3190 Set to the id of the most recent query that has used the variable.
3191 Used in binlogging: When set, there is no need to add a reference to this
3192 variable to the binlog. Imagine it is this:
3193
3194 INSERT INTO t SELECT @a:=10, @a:=@a+1.
3195
3196 Then we have a Item_func_get_user_var (because of the `@a+1`) so we
3197 think we have to write the value of `@a` to the binlog. But before that,
3198 we have a Item_func_set_user_var to create `@a` (`@a:=10`), in this we mark
3199 the variable as "already logged" so that it won't be logged
3200 by Item_func_get_user_var (because that's not necessary).
3201 */
3203
3204 public:
3205 user_var_entry() = default; /* Remove gcc warning */
3206
3207 THD *owner_session() const { return m_owner; }
3208
3209 Simple_cstring entry_name; // Variable name
3210 DTCollation collation; // Collation with attributes
3211 bool unsigned_flag; // true if unsigned, false if signed
3212
3213 /**
3214 Set value to user variable.
3215
3216 @param ptr pointer to buffer with new value
3217 @param length length of new value
3218 @param type type of new value
3219 @param cs charset info for new value
3220 @param dv derivation for new value
3221 @param unsigned_arg indicates if a value of type INT_RESULT is unsigned
3222
3223 @note Sets error and fatal error if allocation fails.
3224
3225 @retval
3226 false success
3227 @retval
3228 true failure
3229 */
3230 bool store(const void *ptr, size_t length, Item_result type,
3231 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3232 /**
3233 Set type of to the given value.
3234 @param type Data type.
3235 */
3237 assert_locked();
3238 m_type = type;
3239 }
3240 /**
3241 Set value to NULL
3242 @param type Data type.
3243 */
3244
3246 assert_locked();
3247 free_value();
3248 reset_value();
3249 m_type = type;
3250 }
3251
3252 void set_used_query_id(query_id_t query_id) { m_used_query_id = query_id; }
3254
3255 /**
3256 Allocates and initializes a user variable instance.
3257
3258 @param thd Current session.
3259 @param name Name of the variable.
3260 @param cs Charset of the variable.
3261
3262 @return Address of the allocated and initialized user_var_entry instance.
3263 @retval NULL On allocation error.
3264 */
3265 static user_var_entry *create(THD *thd, const Name_string &name,
3266 const CHARSET_INFO *cs);
3267
3268 /**
3269 Free all memory used by a user_var_entry instance
3270 previously created by create().
3271 */
3272 void destroy() {
3273 assert_locked();
3274 free_value(); // Free the external value buffer
3275 my_free(this); // Free the instance itself
3276 }
3277
3278 void lock();
3279 void unlock();
3280
3281 /* Routines to access the value and its type */
3282 const char *ptr() const { return m_ptr; }
3283 size_t length() const { return m_length; }
3284 /// The data type of this variable.
3285 Item_result type() const { return m_type; }
3286 /* Item-alike routines to access the value */
3287 double val_real(bool *null_value) const;
3288 longlong val_int(bool *null_value) const;
3289 String *val_str(bool *null_value, String *str, uint decimals) const;
3290 my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
3291};
3292
3293/**
3294 This class is used to implement operations like
3295 SET \@variable or \@variable:= expression.
3296*/
3297
3304 union {
3306 double vreal;
3310
3311 public:
3312 Name_string name; // keep it public
3313
3316 : Item_var_func(pos, b), name(a) {}
3317
3319 : Item_var_func(thd, item),
3321 entry(item->entry),
3322 value(item->value),
3324 null_item(item->null_item),
3325 save_result(item->save_result),
3326 name(item->name) {}
3327 enum Functype functype() const override { return SUSERVAR_FUNC; }
3328 double val_real() override;
3329 longlong val_int() override;
3330 String *val_str(String *str) override;
3331 my_decimal *val_decimal(my_decimal *) override;
3332 bool update_hash(const void *ptr, uint length, enum Item_result type,
3333 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3334 bool send(Protocol *protocol, String *str_arg) override;
3335 void make_field(Send_field *tmp_field) override;
3336 bool check(bool use_result_field);
3337 void save_item_result(Item *item);
3338 bool update();
3339 enum Item_result result_type() const override { return cached_result_type; }
3340 bool fix_fields(THD *thd, Item **ref) override;
3341 bool resolve_type(THD *) override;
3342 void print(const THD *thd, String *str,
3343 enum_query_type query_type) const override;
3344 void print_assignment(const THD *thd, String *str,
3345 enum_query_type query_type) const;
3346 const char *func_name() const override { return "set_user_var"; }
3347
3348 type_conversion_status save_in_field(Field *field, bool no_conversions,
3349 bool can_use_result_field);
3350
3351 void save_org_in_field(Field *field) override {
3352 save_in_field(field, true, false);
3353 }
3354
3355 bool set_entry(THD *thd, bool create_if_not_exists);
3356 void cleanup() override;
3357
3358 protected:
3360 bool no_conversions) override {
3361 return save_in_field(field, no_conversions, true);
3362 }
3363};
3364
3369
3370 public:
3371 Name_string name; // keep it public
3372
3377
3378 enum Functype functype() const override { return GUSERVAR_FUNC; }
3379 double val_real() override;
3380 longlong val_int() override;
3381 my_decimal *val_decimal(my_decimal *) override;
3382 String *val_str(String *str) override;
3383 const CHARSET_INFO *charset_for_protocol() override;
3384 bool resolve_type(THD *) override;
3385 bool propagate_type(THD *thd, const Type_properties &type) override;
3386 void cleanup() override;
3387 void update_used_tables() override {} // Keep existing used tables
3388 void print(const THD *thd, String *str,
3389 enum_query_type query_type) const override;
3390 enum Item_result result_type() const override;
3391 /*
3392 We must always return variables as strings to guard against selects of type
3393 select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3394 */
3395 const char *func_name() const override { return "get_user_var"; }
3396 bool is_non_const_over_literals(uchar *) override { return true; }
3397 bool eq_specific(const Item *item) const override;
3398
3399 private:
3400 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3401
3402 public:
3404 return this;
3405 }
3406};
3407
3408/*
3409 This item represents user variable used as out parameter (e.g in LOAD DATA),
3410 and it is supposed to be used only for this purprose. So it is simplified
3411 a lot. Actually you should never obtain its value.
3412
3413 The only two reasons for this thing being an Item is possibility to store it
3414 in const mem_root_deque<Item> and desire to place this code somewhere near
3415 other functions working with user variables.
3416*/
3420
3421 public:
3423 : Item(pos), name(a) {
3424 item_name.copy(a);
3425 }
3426 /* We should return something different from FIELD_ITEM here */
3427 enum Type type() const override { return STRING_ITEM; }
3428 double val_real() override;
3429 longlong val_int() override;
3430 String *val_str(String *str) override;
3431 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3433 assert(0);
3434 return true;
3435 }
3436 bool get_time(MYSQL_TIME *) override {
3437 assert(0);
3438 return true;
3439 }
3440
3441 /* fix_fields() binds variable name with its entry structure */
3442 bool fix_fields(THD *thd, Item **ref) override;
3443 void print(const THD *thd, String *str,
3444 enum_query_type query_type) const override;
3445 void set_null_value(const CHARSET_INFO *cs);
3446 void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3447};
3448
3449/* A system variable */
3450
3451#define GET_SYS_VAR_CACHE_LONG 1
3452#define GET_SYS_VAR_CACHE_DOUBLE 2
3453#define GET_SYS_VAR_CACHE_STRING 4
3454
3456
3457/** Class to log audit event EVENT_TRACKING_GLOBAL_VARIABLE_GET. */
3459 public:
3463
3464 private:
3465 // Thread handle.
3467
3468 // Item_func_get_system_var instance.
3470
3471 /*
3472 Value conversion type.
3473 Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3474 member while creating the object. While converting value if there are any
3475 intermediate conversions in the same query then this member is used to avoid
3476 auditing more than once.
3477 */
3479
3480 /*
3481 To indicate event auditing is required or not. Event is not audited if
3482 * scope of the variable is *not* GLOBAL.
3483 * or the event is already audited for global variable for the same query.
3484 */
3486};
3487
3497
3498 template <typename T>
3500
3502
3503 public:
3505 enum_var_type scope);
3506 enum Functype functype() const override { return GSYSVAR_FUNC; }
3508 return INNER_TABLE_BIT;
3509 }
3510 bool resolve_type(THD *) override;
3511 void print(const THD *thd, String *str,
3512 enum_query_type query_type) const override;
3513 bool is_non_const_over_literals(uchar *) override { return true; }
3514 enum Item_result result_type() const override {
3515 assert(fixed);
3516 return type_to_result(data_type());
3517 }
3518 double val_real() override;
3519 longlong val_int() override;
3520 String *val_str(String *) override;
3521 my_decimal *val_decimal(my_decimal *dec_buf) override {
3522 return val_decimal_from_real(dec_buf);
3523 }
3524 /* TODO: fix to support views */
3525 const char *func_name() const override { return "get_system_var"; }
3526 bool eq_specific(const Item *item) const override;
3527 bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) override {
3528 // Expressions which have system variables cannot be pushed as of
3529 // now because Item_func_get_system_var::print does not print the
3530 // original expression which leads to an incorrect clone.
3531 return true;
3532 }
3533
3534 void cleanup() override;
3535};
3536
3537class JOIN;
3538
3539class Item_func_match final : public Item_real_func {
3541
3542 protected:
3543 void add_json_info(Json_object *obj) override;
3544
3545 public:
3547 uint key, flags;
3548 /// True if we are doing a full-text index scan with this MATCH function as a
3549 /// predicate, and the score can be retrieved with get_relevance(). If it is
3550 /// false, the score of the document must be retrieved with find_relevance().
3555 /**
3556 Master item means that if identical items are present in the
3557 statement, they use the same FT handler. FT handler is initialized
3558 only for master item and slave items just use it. FT hints initialized
3559 for master only, slave items HINTS are not accessed.
3560 */
3562 Item *concat_ws; // Item_func_concat_ws
3563 String value; // value of concat_ws
3564 String search_value; // key_item()'s value converted to cmp_collation
3565
3566 /**
3567 Constructor for Item_func_match class.
3568
3569 @param pos Position of token in the parser.
3570 @param a List of arguments.
3571 @param against_arg Expression to match against.
3572 @param b FT Flags.
3573 */
3574 Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3575 : Item_real_func(pos, a),
3576 against(against_arg),
3577 key(0),
3578 flags(b),
3581 master(nullptr),
3583 hints(nullptr),
3584 simple_expression(false),
3585 used_in_where_only(false) {
3586 null_on_null = false;
3587 }
3588
3589 bool do_itemize(Parse_context *pc, Item **res) override;
3590
3591 void cleanup() override {
3592 DBUG_TRACE;
3594 if (master == nullptr && ft_handler != nullptr) {
3596 }
3597 score_from_index_scan = false;
3598 ft_handler = nullptr;
3599 concat_ws = nullptr;
3600 return;
3601 }
3602 Item *key_item() const override { return against; }
3603 enum Functype functype() const override { return FT_FUNC; }
3604 const char *func_name() const override { return "match"; }
3605 bool fix_fields(THD *thd, Item **ref) override;
3606 void update_used_tables() override;
3607 bool eq_specific(const Item *item) const override;
3608 /* The following should be safe, even if we compare doubles */
3609 longlong val_int() override {
3610 assert(fixed);
3611 return val_real() != 0.0;
3612 }
3613 double val_real() override;
3614 void print(const THD *thd, String *str,
3615 enum_query_type query_type) const override;
3616
3617 bool fix_index(const THD *thd);
3618 bool init_search(THD *thd);
3619 bool check_function_as_value_generator(uchar *checker_args) override {
3621 pointer_cast<Check_function_as_value_generator_parameters *>(
3622 checker_args);
3623 func_arg->banned_function_name = func_name();
3624 return true;
3625 }
3626
3627 /**
3628 Get number of matching rows from FT handler.
3629
3630 @note Requires that FT handler supports the extended API
3631
3632 @return Number of matching rows in result
3633 */
3635 assert(ft_handler);
3637
3638 return ((FT_INFO_EXT *)ft_handler)
3639 ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3640 }
3641
3642 /**
3643 Check whether FT result is ordered on rank
3644
3645 @return true if result is ordered
3646 @return false otherwise
3647 */
3649 assert(!master);
3650 if (hints->get_flags() & FT_SORTED) return true;
3651
3653 return false;
3654
3655 assert(ft_handler);
3656 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3658 }
3659
3660 /**
3661 Check whether FT result contains the document ID
3662
3663 @return true if document ID is available
3664 @return false otherwise
3665 */
3667 assert(ft_handler);
3668
3670 return false;
3671
3672 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3674 }
3675
3676 float get_filtering_effect(THD *thd, table_map filter_for_table,
3677 table_map read_tables,
3678 const MY_BITMAP *fields_to_ignore,
3679 double rows_in_table) override;
3680
3681 /**
3682 Returns master MATCH function.
3683
3684 @return pointer to master MATCH function.
3685 */
3687 if (master) return master->get_master();
3688 return this;
3689 }
3690
3691 /**
3692 Set master MATCH function and adjust used_in_where_only value.
3693
3694 @param item item for which master should be set.
3695 */
3698 item->master = this;
3699 }
3700
3701 /**
3702 Returns pointer to Ft_hints object belonging to master MATCH function.
3703
3704 @return pointer to Ft_hints object
3705 */
3707 assert(!master);
3708 return hints;
3709 }
3710
3711 /**
3712 Set comparison operation type and and value for master MATCH function.
3713
3714 @param type comparison operation type
3715 @param value_arg comparison operation value
3716 */
3717 void set_hints_op(enum ft_operation type, double value_arg) {
3718 assert(!master);
3719 hints->set_hint_op(type, value_arg);
3720 }
3721
3722 /**
3723 Set FT hints.
3724 */
3725 void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3726
3727 /**
3728 Check if ranking is not needed.
3729
3730 @return true if ranking is not needed
3731 @return false otherwise
3732 */
3734 assert(!master);
3735 return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3736 used_in_where_only && // MATCH result is not used
3737 // in expression
3738 hints->get_op_type() == FT_OP_NO); // MATCH is single function
3739 }
3740
3741 /**
3742 Set flag that the function is a simple expression.
3743
3744 @param val true if the function is a simple expression, false otherwise
3745 */
3746 void set_simple_expression(bool val) {
3747 assert(!master);
3748 simple_expression = val;
3749 }
3750
3751 /**
3752 Check if this MATCH function is a simple expression in WHERE condition.
3753
3754 @return true if simple expression
3755 @return false otherwise
3756 */
3758 assert(!master);
3759 return simple_expression;
3760 }
3761
3762 private:
3763 /**
3764 Fulltext index hints, initialized for master MATCH function only.
3765 */
3767 /**
3768 Flag is true when MATCH function is used as a simple expression in
3769 WHERE condition, i.e. there is no AND/OR combinations, just simple
3770 MATCH function or [MATCH, rank] comparison operation.
3771 */
3773 /**
3774 true if MATCH function is used in WHERE condition only.
3775 Used to determine what hints can be used for FT handler.
3776 Note that only master MATCH function has valid value.
3777 it's ok since only master function is involved in the hint processing.
3778 */
3780 /**
3781 Check whether storage engine for given table,
3782 allows FTS Boolean search on non-indexed columns.
3783
3784 @todo A flag should be added to the extended fulltext API so that
3785 it may be checked whether search on non-indexed columns are
3786 supported. Currently, it is not possible to check for such a
3787 flag since @c this->ft_handler is not yet set when this function is
3788 called. The current hack is to assume that search on non-indexed
3789 columns are supported for engines that does not support the extended
3790 fulltext API (e.g., MyISAM), while it is not supported for other
3791 engines (e.g., InnoDB)
3792
3793 @param tr Table for which storage engine to check
3794
3795 @retval true if BOOLEAN search on non-indexed columns is supported
3796 @retval false otherwise
3797 */
3799 // Only Boolean search may support non_indexed columns
3800 if (!(flags & FT_BOOL)) return false;
3801
3802 assert(tr && tr->file);
3803
3804 // Assume that if extended fulltext API is not supported,
3805 // non-indexed columns are allowed. This will be true for MyISAM.
3806 if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3807
3808 return false;
3809 }
3810};
3811
3812/**
3813 A visitor that calls the specified function on every non-aggregated full-text
3814 search function (Item_func_match) it encounters when it is used in a
3815 PREFIX+POSTFIX walk with WalkItem(). It skips every item that is wrapped in an
3816 aggregate function, and also every item wrapped in a reference, as the items
3817 behind the reference are already handled elsewhere (in another query block or
3818 in another element of the SELECT list).
3819 */
3821 public:
3823 std::function<bool(Item_func_match *)> func);
3824 bool operator()(Item *item);
3825
3826 private:
3827 std::function<bool(Item_func_match *)> m_func;
3828};
3829
3832
3834
3835 public:
3836 Item_func_is_free_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
3837
3838 bool do_itemize(Parse_context *pc, Item **res) override;
3839 longlong val_int() override;
3840 const char *func_name() const override { return "is_free_lock"; }
3842 return INNER_TABLE_BIT;
3843 }
3844 bool resolve_type(THD *thd) override {
3845 if (param_type_is_default(thd, 0, 1)) return true;
3846 max_length = 1;
3847 set_nullable(true);
3848 return false;
3849 }
3850 bool