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