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