MySQL 8.1.0
Source Code Documentation
item_func.h
Go to the documentation of this file.
1#ifndef ITEM_FUNC_INCLUDED
2#define ITEM_FUNC_INCLUDED
3
4/* Copyright (c) 2000, 2023, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <sys/types.h>
27
28#include <climits>
29#include <cmath> // isfinite
30#include <cstddef>
31#include <functional>
32
33#include "decimal.h"
34#include "field_types.h"
35#include "ft_global.h"
36#include "lex_string.h"
37#include "my_alloc.h"
38#include "my_base.h"
39#include "my_compiler.h"
40#include "my_dbug.h"
41#include "my_inttypes.h"
43#include "my_table_map.h"
44#include "my_thread_local.h"
45#include "my_time.h"
49#include "mysql_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 String *buffer);
93
94inline String *eval_string_arg(const CHARSET_INFO *to_cs, Item *arg,
95 String *buffer) {
96 if (my_charset_same(to_cs, arg->collation.collation))
97 return arg->val_str(buffer);
98 return eval_string_arg_noinline(to_cs, arg, buffer);
99}
100
102 protected:
103 /**
104 Array of pointers to arguments. If there are max 2 arguments, this array
105 is often just m_embedded_arguments; otherwise it's explicitly allocated in
106 the constructor.
107 */
109
110 private:
112
113 /// Allocates space for the given number of arguments, if needed. Uses
114 /// #m_embedded_arguments if it's big enough.
115 bool alloc_args(MEM_ROOT *mem_root, unsigned num_args) {
116 if (num_args <= array_elements(m_embedded_arguments)) {
118 } else {
119 args = mem_root->ArrayAlloc<Item *>(num_args);
120 if (args == nullptr) {
121 // OOM
122 arg_count = 0;
123 return true;
124 }
125 }
126 arg_count = num_args;
127 return false;
128 }
129
130 public:
131 uint arg_count; ///< How many arguments in 'args'
132 virtual uint argument_count() const { return arg_count; }
133 inline Item **arguments() const {
134 return (argument_count() > 0) ? args : nullptr;
135 }
136
137 protected:
138 /*
139 These decide of types of arguments which are prepared-statement
140 parameters.
141 */
144 bool param_type_is_default(THD *thd, uint start, uint end, uint step,
145 enum_field_types def);
146 bool param_type_is_default(THD *thd, uint start, uint end,
148 return param_type_is_default(thd, start, end, 1, def);
149 }
150 bool param_type_is_rejected(uint start, uint end);
151
152 /**
153 Affects how to determine that NULL argument implies a NULL function return.
154 Default behaviour in this class is:
155 - if true, any NULL argument means the function returns NULL.
156 - if false, no such assumption is made and not_null_tables_cache is thus
157 set to 0.
158 null_on_null is true for all Item_func derived classes, except Item_func_sp,
159 all CASE derived functions and a few other functions.
160 RETURNS NULL ON NULL INPUT can be implemented for stored functions by
161 modifying this member in class Item_func_sp.
162 */
163 bool null_on_null{true};
164 /*
165 Allowed numbers of columns in result (usually 1, which means scalar value)
166 0 means get this number from first argument
167 */
169 /// Value used in calculation of result of used_tables()
171 /// Value used in calculation of result of not_null_tables()
173
174 public:
175 /*
176 When updating Functype with new spatial functions,
177 is_spatial_operator() should also be updated.
178
179 DD_INTERNAL_FUNC:
180 Some of the internal functions introduced for the INFORMATION_SCHEMA views
181 opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
182 of functions.
183 */
184 enum Functype {
314 };
321 };
322 enum Type type() const override { return FUNC_ITEM; }
323 virtual enum Functype functype() const { return UNKNOWN_FUNC; }
325
326 explicit Item_func(const POS &pos)
328
330 args[0] = a;
332 }
333 Item_func(const POS &pos, Item *a)
335 args[0] = a;
336 }
337
339 args[0] = a;
340 args[1] = b;
344 }
345 Item_func(const POS &pos, Item *a, Item *b)
347 args[0] = a;
348 args[1] = b;
349 }
350
351 Item_func(Item *a, Item *b, Item *c) {
352 if (alloc_args(*THR_MALLOC, 3)) return;
353 args[0] = a;
354 args[1] = b;
355 args[2] = c;
360 }
361
362 Item_func(const POS &pos, Item *a, Item *b, Item *c)
363 : Item_result_field(pos) {
364 if (alloc_args(*THR_MALLOC, 3)) return;
365 args[0] = a;
366 args[1] = b;
367 args[2] = c;
368 }
369
370 Item_func(Item *a, Item *b, Item *c, Item *d) {
371 if (alloc_args(*THR_MALLOC, 4)) return;
372 args[0] = a;
373 args[1] = b;
374 args[2] = c;
375 args[3] = d;
381 }
382
383 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
384 : Item_result_field(pos) {
385 if (alloc_args(*THR_MALLOC, 4)) return;
386 args[0] = a;
387 args[1] = b;
388 args[2] = c;
389 args[3] = d;
390 }
391 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
392 if (alloc_args(*THR_MALLOC, 5)) return;
393 args[0] = a;
394 args[1] = b;
395 args[2] = c;
396 args[3] = d;
397 args[4] = e;
404 }
405 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
406 : Item_result_field(pos) {
407 if (alloc_args(*THR_MALLOC, 5)) return;
408 args[0] = a;
409 args[1] = b;
410 args[2] = c;
411 args[3] = d;
412 args[4] = e;
413 }
414 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e, Item *f) {
415 if (alloc_args(*THR_MALLOC, 6)) return;
416 args[0] = a;
417 args[1] = b;
418 args[2] = c;
419 args[3] = d;
420 args[4] = e;
421 args[5] = f;
429 }
430 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e,
431 Item *f)
432 : Item_result_field(pos) {
433 if (alloc_args(*THR_MALLOC, 6)) return;
434 args[0] = a;
435 args[1] = b;
436 args[2] = c;
437 args[3] = d;
438 args[4] = e;
439 args[5] = f;
440 }
442 set_arguments(list, false);
443 }
444
445 Item_func(const POS &pos, PT_item_list *opt_list);
446
447 // Constructor used for Item_cond_and/or (see Item comment)
448 Item_func(THD *thd, const Item_func *item);
449
450 /// Get the i'th argument of the function that this object represents.
451 virtual Item *get_arg(uint i) { return args[i]; }
452
453 /// Get the i'th argument of the function that this object represents.
454 virtual const Item *get_arg(uint i) const { return args[i]; }
455 virtual Item *set_arg(THD *, uint, Item *) {
456 assert(0);
457 return nullptr;
458 }
459
460 bool do_itemize(Parse_context *pc, Item **res) override;
461
462 bool fix_fields(THD *, Item **ref) override;
463 bool fix_func_arg(THD *, Item **arg);
464 void fix_after_pullout(Query_block *parent_query_block,
465 Query_block *removed_query_block) override;
466 /**
467 Resolve type of function after all arguments have had their data types
468 resolved. Called from resolve_type() when no dynamic parameters
469 are used and from propagate_type() otherwise.
470 */
471 virtual bool resolve_type_inner(THD *) {
472 assert(false);
473 return false;
474 }
475 bool propagate_type(THD *thd, const Type_properties &type) override;
476 /**
477 Returns the pseudo tables depended upon in order to evaluate this
478 function expression. The default implementation returns the empty
479 set.
480 */
481 virtual table_map get_initial_pseudo_tables() const { return 0; }
482 table_map used_tables() const override { return used_tables_cache; }
484 void update_used_tables() override;
486 bool eq(const Item *item, bool binary_cmp) const override;
487 virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
488 virtual bool have_rev_func() const { return false; }
489 virtual Item *key_item() const { return args[0]; }
490 /**
491 Copy arguments from list to args array
492
493 @param list function argument list
494 @param context_free true: for use in context-independent
495 constructors (Item_func(POS,...)) i.e. for use
496 in the parser
497 @return true on OOM, false otherwise
498 */
499 bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
500 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
501 mem_root_deque<Item *> *fields) override;
502 void print(const THD *thd, String *str,
503 enum_query_type query_type) const override;
504 void print_op(const THD *thd, String *str, enum_query_type query_type) const;
505 void print_args(const THD *thd, String *str, uint from,
506 enum_query_type query_type) const;
507 virtual void fix_num_length_and_dec();
508 virtual bool is_deprecated() const { return false; }
509 bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
510 return (null_value = args[0]->get_date(ltime, fuzzy_date));
511 }
512 inline bool get_arg0_time(MYSQL_TIME *ltime) {
513 return (null_value = args[0]->get_time(ltime));
514 }
515 bool is_null() override { return update_null_value() || null_value; }
518 friend class udf_handler;
519 Field *tmp_table_field(TABLE *t_arg) override;
520 Item *get_tmp_table_item(THD *thd) override;
521
522 my_decimal *val_decimal(my_decimal *) override;
523
524 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
525 int item_sep) {
526 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
527 false);
528 }
529 /*
530 Aggregate arguments for string result, e.g: CONCAT(a,b)
531 - convert to @@character_set_connection if all arguments are numbers
532 - allow DERIVATION_NONE
533 */
535 uint nitems, int item_sep = 1) {
536 return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
537 item_sep);
538 }
539 /*
540 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
541 - don't convert to @@character_set_connection if all arguments are numbers
542 - don't allow DERIVATION_NONE
543 */
545 uint nitems, int item_sep = 1) {
546 return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
547 item_sep);
548 }
549
550 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
551 Item *transform(Item_transformer transformer, uchar *arg) override;
552 Item *compile(Item_analyzer analyzer, uchar **arg_p,
553 Item_transformer transformer, uchar *arg_t) override;
554 void traverse_cond(Cond_traverser traverser, void *arg,
555 traverse_order order) override;
556
557 /**
558 Throw an error if the input double number is not finite, i.e. is either
559 +/-INF or NAN.
560 */
561 inline double check_float_overflow(double value) {
562 return std::isfinite(value) ? value : raise_float_overflow();
563 }
564 /**
565 Throw an error if the input BIGINT value represented by the
566 (longlong value, bool unsigned flag) pair cannot be returned by the
567 function, i.e. is not compatible with this Item's unsigned_flag.
568 */
569 inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
570 if ((unsigned_flag && !val_unsigned && value < 0) ||
571 (!unsigned_flag && val_unsigned &&
572 (ulonglong)value > (ulonglong)LLONG_MAX))
573 return raise_integer_overflow();
574 return value;
575 }
576 /**
577 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
578 */
581 }
582
584 assert(fixed);
585 for (uint i = 0; i < arg_count; i++) {
586 if (args[i]->type() == Item::FIELD_ITEM &&
588 return true;
589 }
590 return false;
591 }
592
594 assert(fixed);
595 for (uint i = 0; i < arg_count; i++) {
596 if (args[i]->type() == Item::FIELD_ITEM &&
597 (args[i]->data_type() == MYSQL_TYPE_DATE ||
599 return true;
600 }
601 return false;
602 }
603
605 assert(fixed);
606 for (uint i = 0; i < arg_count; i++) {
607 if (args[i]->type() == Item::FIELD_ITEM &&
608 (args[i]->data_type() == MYSQL_TYPE_TIME ||
610 return true;
611 }
612 return false;
613 }
614
616 assert(fixed);
617 for (uint i = 0; i < arg_count; i++) {
618 if (args[i]->type() == Item::FIELD_ITEM &&
620 return true;
621 }
622 return false;
623 }
624
625 /*
626 We assume the result of any function that has a TIMESTAMP argument to be
627 timezone-dependent, since a TIMESTAMP value in both numeric and string
628 contexts is interpreted according to the current timezone.
629 The only exception is UNIX_TIMESTAMP() which returns the internal
630 representation of a TIMESTAMP argument verbatim, and thus does not depend on
631 the timezone.
632 */
634 return has_timestamp_args();
635 }
636
637 Item *gc_subst_transformer(uchar *arg) override;
638
639 bool resolve_type(THD *thd) override {
640 // By default, pick PS-param's type from other arguments, or VARCHAR
641 return param_type_uses_non_param(thd);
642 }
643
644 /**
645 Whether an arg of a JSON function can be cached to avoid repetitive
646 string->JSON conversion. This function returns true only for those args,
647 which are the source of JSON data. JSON path args are cached independently
648 and for them this function returns false. Same as for all other type of
649 args.
650
651 @param arg the arg to cache
652
653 @retval true arg can be cached
654 @retval false otherwise
655 */
656 virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
657 return CACHE_NONE;
658 }
659
660 /// Whether this Item is an equi-join condition. If this Item is a compound
661 /// item (i.e. multiple condition AND'ed together), it will only return true
662 /// if the Item contains only equi-join conditions AND'ed together. This is
663 /// used to determine whether the condition can be used as a join condition
664 /// for hash join (join conditions in hash join must be equi-join conditions),
665 /// or if it should be placed as a filter after the join.
666 virtual bool contains_only_equi_join_condition() const { return false; }
667
668 protected:
669 /**
670 Whether or not an item should contribute to the filtering effect
671 (@see get_filtering_effect()). First it verifies that table
672 requirements are satisfied as follows:
673
674 1) The item must refer to a field in 'filter_for_table' in some
675 way. This reference may be indirect through any number of
676 intermediate items. For example, this item may be an
677 Item_cond_and which refers to an Item_func_eq which refers to
678 the field.
679 2) The item must not refer to other tables than those already
680 read and the table in 'filter_for_table'
681
682 Then it contines to other properties as follows:
683
684 Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
685 Item_func is to contribute to the filtering effect, then
686
687 1) one of the operands must be a field from 'filter_for_table' that is not
688 in 'fields_to_ignore', and
689 2) depending on the Item_func type filtering effect is calculated
690 for, one or all [1] of the other operand(s) must be an available
691 value, i.e.:
692 - a constant, or
693 - a constant subquery, or
694 - a field value read from a table in 'read_tables', or
695 - a second field in 'filter_for_table', or
696 - a function that only refers to constants or tables in
697 'read_tables', or
698 - special case: an implicit value like NULL in the case of
699 "field IS NULL". Such Item_funcs have arg_count==1.
700
701 [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
702 for the rest (e.g. BETWEEN)
703
704 @param read_tables Tables earlier in the join sequence.
705 Predicates for table 'filter_for_table' that
706 rely on values from these tables can be part of
707 the filter effect.
708 @param filter_for_table The table we are calculating filter effect for
709 @param fields_to_ignore Columns that should be ignored.
710
711
712 @return Item_field that participates in the predicate if none of the
713 requirements are broken, NULL otherwise
714
715 @note: This function only applies to items doing comparison, i.e.
716 boolean predicates. Unfortunately, some of those items do not
717 inherit from Item_bool_func so the member function has to be
718 placed in Item_func.
719 */
721 table_map read_tables, table_map filter_for_table,
722 const MY_BITMAP *fields_to_ignore) const;
723 /**
724 Named parameters are allowed in a parameter list
725
726 The syntax to name parameters in a function call is as follow:
727 <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
728 where "AS" is optional.
729 Only UDF function support that syntax.
730
731 @return true if the function item can have named parameters
732 */
733 virtual bool may_have_named_parameters() const { return false; }
734 bool is_non_const_over_literals(uchar *) override { return false; }
735
736 bool check_function_as_value_generator(uchar *checker_args) override {
737 if (is_deprecated()) {
739 pointer_cast<Check_function_as_value_generator_parameters *>(
740 checker_args);
741 func_arg->banned_function_name = func_name();
742 return true;
743 }
744 return false;
745 }
746 bool is_valid_for_pushdown(uchar *arg) override;
747 bool check_column_in_window_functions(uchar *arg) override;
748 bool check_column_in_group_by(uchar *arg) override;
749
751};
752
753class Item_real_func : public Item_func {
754 public:
756 explicit Item_real_func(const POS &pos) : Item_func(pos) {
758 }
759
761 Item_real_func(const POS &pos, Item *a) : Item_func(pos, a) {
763 }
764
766
767 Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
769 }
770
773 }
774
777 }
778
779 String *val_str(String *str) override;
780 my_decimal *val_decimal(my_decimal *decimal_value) override;
781 longlong val_int() override {
782 assert(fixed);
784 }
785 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
786 return get_date_from_real(ltime, fuzzydate);
787 }
788 bool get_time(MYSQL_TIME *ltime) override {
789 return get_time_from_real(ltime);
790 }
791 enum Item_result result_type() const override { return REAL_RESULT; }
792};
793
795 protected:
797
798 public:
801 }
803 : Item_func(pos, a), hybrid_type(REAL_RESULT) {
805 }
806
810 }
811 Item_func_numhybrid(const POS &pos, Item *a, Item *b)
812 : Item_func(pos, a, b), hybrid_type(REAL_RESULT) {
814 }
815
819 }
823 }
824
825 enum Item_result result_type() const override { return hybrid_type; }
827 return MYSQL_TYPE_DOUBLE;
828 }
829 bool resolve_type(THD *thd) override;
830 bool resolve_type_inner(THD *thd) override;
831 void fix_num_length_and_dec() override;
832 virtual void set_numeric_type() = 0; // To be called from resolve_type()
833
834 double val_real() override;
835 longlong val_int() override;
836 my_decimal *val_decimal(my_decimal *) override;
837 String *val_str(String *str) override;
838 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
839 bool get_time(MYSQL_TIME *ltime) override;
840 /**
841 @brief Performs the operation that this functions implements when the
842 result type is INT.
843
844 @return The result of the operation.
845 */
846 virtual longlong int_op() = 0;
847
848 /**
849 @brief Performs the operation that this functions implements when the
850 result type is REAL.
851
852 @return The result of the operation.
853 */
854 virtual double real_op() = 0;
855
856 /**
857 @brief Performs the operation that this functions implements when the
858 result type is DECIMAL.
859
860 @param decimal_value A pointer where the DECIMAL value will be allocated.
861 @return
862 - 0 If the result is NULL
863 - The same pointer it was given, with the area initialized to the
864 result of the operation.
865 */
866 virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
867
868 /**
869 @brief Performs the operation that this functions implements when the
870 result type is a string type.
871
872 @return The result of the operation.
873 */
874 virtual String *str_op(String *) = 0;
875 /**
876 @brief Performs the operation that this functions implements when the
877 result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
878
879 @return The result of the operation.
880 */
881 virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
882 virtual bool time_op(MYSQL_TIME *ltime) = 0;
883 bool is_null() override { return update_null_value() || null_value; }
884};
885
886/* function where type of result detected by first argument */
888 public:
890 Item_func_num1(const POS &pos, Item *a) : Item_func_numhybrid(pos, a) {}
891
893 Item_func_num1(const POS &pos, Item *a, Item *b)
894 : Item_func_numhybrid(pos, a, b) {}
895
896 void fix_num_length_and_dec() override;
897 void set_numeric_type() override;
898 String *str_op(String *) override {
899 assert(0);
900 return nullptr;
901 }
903 assert(0);
904 return false;
905 }
906 bool time_op(MYSQL_TIME *) override {
907 assert(0);
908 return false;
909 }
910};
911
912/* Base class for operations like '+', '-', '*' */
914 public:
916 Item_num_op(const POS &pos, Item *a, Item *b)
917 : Item_func_numhybrid(pos, a, b) {}
918
919 virtual void result_precision() = 0;
920
921 void print(const THD *thd, String *str,
922 enum_query_type query_type) const override {
923 print_op(thd, str, query_type);
924 }
925
926 void set_numeric_type() override;
927 String *str_op(String *) override {
928 assert(0);
929 return nullptr;
930 }
932 assert(0);
933 return false;
934 }
935 bool time_op(MYSQL_TIME *) override {
936 assert(0);
937 return false;
938 }
939};
940
941class Item_int_func : public Item_func {
942 public:
944 explicit Item_int_func(const POS &pos) : Item_func(pos) {
946 }
947
949 Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
951 }
952
955 }
956 Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
958 }
959
960 Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
962 }
963 Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
964 : Item_func(pos, a, b, c) {
966 }
967
968 Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
970 }
971 Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
972 : Item_func(pos, a, b, c, d) {
974 }
975
978 }
979 Item_int_func(const POS &pos, PT_item_list *opt_list)
980 : Item_func(pos, opt_list) {
982 }
983
984 Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
986 }
987 double val_real() override;
988 String *val_str(String *str) override;
989 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
990 return get_date_from_int(ltime, fuzzydate);
991 }
992 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
993 enum Item_result result_type() const override { return INT_RESULT; }
994 /*
995 Concerning PS-param types,
996 resolve_type(THD *) is not overridden here, as experience shows that for
997 most child classes of this class, VARCHAR is the best default
998 */
999};
1000
1003
1004 public:
1006
1008 return INNER_TABLE_BIT;
1009 }
1010 bool do_itemize(Parse_context *pc, Item **res) override;
1011 const char *func_name() const override { return "connection_id"; }
1012 bool resolve_type(THD *thd) override;
1013 bool fix_fields(THD *thd, Item **ref) override;
1014 longlong val_int() override;
1015 bool check_function_as_value_generator(uchar *checker_args) override {
1017 pointer_cast<Check_function_as_value_generator_parameters *>(
1018 checker_args);
1019 func_arg->banned_function_name = func_name();
1020 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1021 (func_arg->source == VGS_CHECK_CONSTRAINT));
1022 }
1023};
1024
1026 public:
1027 Item_typecast_signed(const POS &pos, Item *a) : Item_int_func(pos, a) {
1028 unsigned_flag = false;
1029 }
1030 const char *func_name() const override { return "cast_as_signed"; }
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
1039 public:
1040 Item_typecast_unsigned(const POS &pos, Item *a) : Item_int_func(pos, a) {
1041 unsigned_flag = true;
1042 }
1043 const char *func_name() const override { return "cast_as_unsigned"; }
1044 longlong val_int() override;
1045 bool resolve_type(THD *thd) override;
1046 void print(const THD *thd, String *str,
1047 enum_query_type query_type) const override;
1048 enum Functype functype() const override { return TYPECAST_FUNC; }
1049};
1050
1051class Item_typecast_decimal final : public Item_func {
1052 protected:
1053 void add_json_info(Json_object *obj) override;
1054
1055 public:
1056 Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1057 : Item_func(pos, a) {
1059 }
1060 String *val_str(String *str) override;
1061 double val_real() override;
1062 longlong val_int() override;
1063 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1064 return get_date_from_decimal(ltime, fuzzydate);
1065 }
1066 bool get_time(MYSQL_TIME *ltime) override {
1067 return get_time_from_decimal(ltime);
1068 }
1069 my_decimal *val_decimal(my_decimal *) override;
1070 enum Item_result result_type() const override { return DECIMAL_RESULT; }
1071 bool resolve_type(THD *thd) override {
1072 if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1073 return true;
1074 return false;
1075 }
1076 const char *func_name() const override { return "cast_as_decimal"; }
1077 enum Functype functype() const override { return TYPECAST_FUNC; }
1078 void print(const THD *thd, String *str,
1079 enum_query_type query_type) const override;
1080};
1081
1082/**
1083 Class used to implement CAST to floating-point data types.
1084*/
1085class Item_typecast_real final : public Item_func {
1086 protected:
1087 void add_json_info(Json_object *obj) override {
1088 obj->add_alias("is_double", create_dom_ptr<Json_boolean>(
1090 }
1091
1092 public:
1093 Item_typecast_real(const POS &pos, Item *a, bool as_double)
1094 : Item_func(pos, a) {
1095 if (as_double)
1097 else
1099 }
1101 String *val_str(String *str) override;
1102 double val_real() override;
1103 longlong val_int() override { return val_int_from_real(); }
1104 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1105 bool get_time(MYSQL_TIME *ltime) override;
1106 my_decimal *val_decimal(my_decimal *decimal_value) override;
1107 enum Item_result result_type() const override { return REAL_RESULT; }
1108 bool resolve_type(THD *thd) override {
1109 return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1110 }
1111 const char *func_name() const override { return "cast_as_real"; }
1112 enum Functype functype() const override { return TYPECAST_FUNC; }
1113 void print(const THD *thd, String *str,
1114 enum_query_type query_type) const override;
1115};
1116
1118 public:
1121 : Item_num_op(pos, a, b) {}
1122
1123 void result_precision() override;
1124 bool check_partition_func_processor(uchar *) override { return false; }
1125 bool check_function_as_value_generator(uchar *) override { return false; }
1126};
1127
1129 public:
1131 Item_func_plus(const POS &pos, Item *a, Item *b)
1132 : Item_func_additive_op(pos, a, b) {}
1133
1134 const char *func_name() const override { return "+"; }
1135
1136 // SUPPRESS_UBSAN: signed integer overflow
1137 longlong int_op() override SUPPRESS_UBSAN;
1138
1139 double real_op() override;
1140 my_decimal *decimal_op(my_decimal *) override;
1141 enum Functype functype() const override { return PLUS_FUNC; }
1142};
1143
1145 public:
1147 Item_func_minus(const POS &pos, Item *a, Item *b)
1148 : Item_func_additive_op(pos, a, b) {}
1149
1150 const char *func_name() const override { return "-"; }
1151
1152 // SUPPRESS_UBSAN: signed integer overflow
1153 longlong int_op() override SUPPRESS_UBSAN;
1154
1155 double real_op() override;
1156 my_decimal *decimal_op(my_decimal *) override;
1157 bool resolve_type(THD *thd) override;
1158 enum Functype functype() const override { return MINUS_FUNC; }
1159};
1160
1161class Item_func_mul final : public Item_num_op {
1162 public:
1164 Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1165
1166 const char *func_name() const override { return "*"; }
1167 longlong int_op() override;
1168 double real_op() override;
1169 my_decimal *decimal_op(my_decimal *) override;
1170 void result_precision() override;
1171 bool check_partition_func_processor(uchar *) override { return false; }
1172 bool check_function_as_value_generator(uchar *) override { return false; }
1173 enum Functype functype() const override { return MUL_FUNC; }
1174};
1175
1177 public:
1178 Item_func_div_base(const POS &pos, Item *a, Item *b)
1179 : Item_num_op(pos, a, b) {}
1181 longlong int_op() override;
1182 double real_op() override;
1183 my_decimal *decimal_op(my_decimal *) override;
1184 enum Functype functype() const override { return DIV_FUNC; }
1185
1186 protected:
1188};
1189
1190class Item_func_div final : public Item_func_div_base {
1191 public:
1192 Item_func_div(const POS &pos, Item *a, Item *b)
1193 : Item_func_div_base(pos, a, b) {}
1194 const char *func_name() const override { return "/"; }
1195 bool resolve_type(THD *thd) override;
1196 void result_precision() override;
1197};
1198
1200 public:
1202 Item_func_div_int(const POS &pos, Item *a, Item *b)
1203 : Item_func_div_base(pos, a, b) {}
1204 const char *func_name() const override { return "DIV"; }
1206 return MYSQL_TYPE_LONGLONG;
1207 }
1208 bool resolve_type(THD *thd) override;
1209 void result_precision() override;
1210 void set_numeric_type() override;
1211 bool check_partition_func_processor(uchar *) override { return false; }
1212 bool check_function_as_value_generator(uchar *) override { return false; }
1213};
1214
1215class Item_func_mod final : public Item_num_op {
1216 public:
1218 Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1219
1220 longlong int_op() override;
1221 double real_op() override;
1222 my_decimal *decimal_op(my_decimal *) override;
1223 const char *func_name() const override { return "%"; }
1224 void result_precision() override;
1225 bool resolve_type(THD *thd) override;
1226 bool check_partition_func_processor(uchar *) override { return false; }
1227 bool check_function_as_value_generator(uchar *) override { return false; }
1228 enum Functype functype() const override { return MOD_FUNC; }
1229};
1230
1231class Item_func_neg final : public Item_func_num1 {
1232 public:
1234 Item_func_neg(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1235
1236 double real_op() override;
1237 longlong int_op() override;
1238 my_decimal *decimal_op(my_decimal *) override;
1239 const char *func_name() const override { return "-"; }
1240 enum Functype functype() const override { return NEG_FUNC; }
1241 bool resolve_type(THD *thd) override;
1242 void fix_num_length_and_dec() override;
1243 bool check_partition_func_processor(uchar *) override { return false; }
1244 bool check_function_as_value_generator(uchar *) override { return false; }
1245};
1246
1247class Item_func_abs final : public Item_func_num1 {
1248 public:
1249 Item_func_abs(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1250 double real_op() override;
1251 longlong int_op() override;
1252 my_decimal *decimal_op(my_decimal *) override;
1253 const char *func_name() const override { return "abs"; }
1254 bool resolve_type(THD *) override;
1255 bool check_partition_func_processor(uchar *) override { return false; }
1256 bool check_function_as_value_generator(uchar *) override { return false; }
1257 enum Functype functype() const override { return ABS_FUNC; }
1258};
1259
1260// A class to handle logarithmic and trigonometric functions
1261
1263 public:
1265 Item_dec_func(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1266
1267 Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1268 bool resolve_type(THD *thd) override;
1269};
1270
1271class Item_func_exp final : public Item_dec_func {
1272 public:
1273 Item_func_exp(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1274 double val_real() override;
1275 const char *func_name() const override { return "exp"; }
1276 enum Functype functype() const override { return EXP_FUNC; }
1277};
1278
1279class Item_func_ln final : public Item_dec_func {
1280 public:
1281 Item_func_ln(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1282 double val_real() override;
1283 const char *func_name() const override { return "ln"; }
1284 enum Functype functype() const override { return LN_FUNC; }
1285};
1286
1287class Item_func_log final : public Item_dec_func {
1288 public:
1289 Item_func_log(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1290 Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1291 double val_real() override;
1292 const char *func_name() const override { return "log"; }
1293 enum Functype functype() const override { return LOG_FUNC; }
1294};
1295
1296class Item_func_log2 final : public Item_dec_func {
1297 public:
1298 Item_func_log2(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1299 double val_real() override;
1300 const char *func_name() const override { return "log2"; }
1301};
1302
1303class Item_func_log10 final : public Item_dec_func {
1304 public:
1305 Item_func_log10(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1306 double val_real() override;
1307 const char *func_name() const override { return "log10"; }
1308 enum Functype functype() const override { return LOG10_FUNC; }
1309};
1310
1311class Item_func_sqrt final : public Item_dec_func {
1312 public:
1313 Item_func_sqrt(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1314 double val_real() override;
1315 const char *func_name() const override { return "sqrt"; }
1316 enum Functype functype() const override { return SQRT_FUNC; }
1317};
1318
1319class Item_func_pow final : public Item_dec_func {
1320 public:
1321 Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1322 double val_real() override;
1323 const char *func_name() const override { return "pow"; }
1324 enum Functype functype() const override { return POW_FUNC; }
1325};
1326
1327class Item_func_acos final : public Item_dec_func {
1328 public:
1329 Item_func_acos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1330 double val_real() override;
1331 const char *func_name() const override { return "acos"; }
1332 enum Functype functype() const override { return ACOS_FUNC; }
1333};
1334
1335class Item_func_asin final : public Item_dec_func {
1336 public:
1337 Item_func_asin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1338 double val_real() override;
1339 const char *func_name() const override { return "asin"; }
1340 enum Functype functype() const override { return ASIN_FUNC; }
1341};
1342
1343class Item_func_atan final : public Item_dec_func {
1344 public:
1345 Item_func_atan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1346 Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1347 double val_real() override;
1348 const char *func_name() const override { return "atan"; }
1349 enum Functype functype() const override { return ATAN_FUNC; }
1350};
1351
1352class Item_func_cos final : public Item_dec_func {
1353 public:
1354 Item_func_cos(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1355 double val_real() override;
1356 const char *func_name() const override { return "cos"; }
1357 enum Functype functype() const override { return COS_FUNC; }
1358};
1359
1360class Item_func_sin final : public Item_dec_func {
1361 public:
1362 Item_func_sin(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1363 double val_real() override;
1364 const char *func_name() const override { return "sin"; }
1365 enum Functype functype() const override { return SIN_FUNC; }
1366};
1367
1368class Item_func_tan final : public Item_dec_func {
1369 public:
1370 Item_func_tan(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1371 double val_real() override;
1372 const char *func_name() const override { return "tan"; }
1373 enum Functype functype() const override { return TAN_FUNC; }
1374};
1375
1376class Item_func_cot final : public Item_dec_func {
1377 public:
1378 Item_func_cot(const POS &pos, Item *a) : Item_dec_func(pos, a) {}
1379 double val_real() override;
1380 const char *func_name() const override { return "cot"; }
1381 enum Functype functype() const override { return COT_FUNC; }
1382};
1383
1385 public:
1387 Item_func_int_val(const POS &pos, Item *a) : Item_func_num1(pos, a) {}
1388 bool resolve_type_inner(THD *thd) override;
1389};
1390
1392 public:
1394 Item_func_ceiling(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1395 const char *func_name() const override { return "ceiling"; }
1396 longlong int_op() override;
1397 double real_op() override;
1398 my_decimal *decimal_op(my_decimal *) override;
1399 bool check_partition_func_processor(uchar *) override { return false; }
1400 bool check_function_as_value_generator(uchar *) override { return false; }
1401 enum Functype functype() const override { return CEILING_FUNC; }
1402};
1403
1405 public:
1407 Item_func_floor(const POS &pos, Item *a) : Item_func_int_val(pos, a) {}
1408 const char *func_name() const override { return "floor"; }
1409 longlong int_op() override;
1410 double real_op() override;
1411 my_decimal *decimal_op(my_decimal *) override;
1412 bool check_partition_func_processor(uchar *) override { return false; }
1413 bool check_function_as_value_generator(uchar *) override { return false; }
1414 enum Functype functype() const override { return FLOOR_FUNC; }
1415};
1416
1417/* This handles round and truncate */
1418
1419class Item_func_round final : public Item_func_num1 {
1421
1422 public:
1423 Item_func_round(Item *a, Item *b, bool trunc_arg)
1424 : Item_func_num1(a, b), truncate(trunc_arg) {}
1425 Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1426 : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1427
1428 const char *func_name() const override {
1429 return truncate ? "truncate" : "round";
1430 }
1431 double real_op() override;
1432 longlong int_op() override;
1433 my_decimal *decimal_op(my_decimal *) override;
1434 bool resolve_type(THD *) override;
1435 enum Functype functype() const override {
1437 }
1438};
1439
1440class Item_func_rand final : public Item_real_func {
1442
1444 bool first_eval{true}; // true if val_real() is called 1st time
1445 public:
1446 Item_func_rand(const POS &pos, Item *a) : Item_real_func(pos, a) {}
1447 explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1448
1449 bool do_itemize(Parse_context *pc, Item **res) override;
1450 double val_real() override;
1451 const char *func_name() const override { return "rand"; }
1452 /**
1453 This function is non-deterministic and hence depends on the
1454 'RAND' pseudo-table.
1455
1456 @retval RAND_TABLE_BIT
1457 */
1459 return RAND_TABLE_BIT;
1460 }
1461 bool fix_fields(THD *thd, Item **ref) override;
1462 bool resolve_type(THD *thd) override;
1463 void cleanup() override {
1464 first_eval = true;
1466 }
1467 bool check_function_as_value_generator(uchar *checker_args) override {
1469 pointer_cast<Check_function_as_value_generator_parameters *>(
1470 checker_args);
1471 func_arg->banned_function_name = func_name();
1472 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1473 (func_arg->source == VGS_CHECK_CONSTRAINT));
1474 }
1475
1476 private:
1477 void seed_random(Item *val);
1478};
1479
1480class Item_func_sign final : public Item_int_func {
1481 public:
1482 Item_func_sign(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1483 const char *func_name() const override { return "sign"; }
1484 enum Functype functype() const override { return SIGN_FUNC; }
1485 longlong val_int() override;
1486 bool resolve_type(THD *thd) override;
1487};
1488
1489// Common base class for the DEGREES and RADIANS functions.
1491 double mul, add;
1492
1493 protected:
1494 Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1495 : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1496
1497 public:
1498 double val_real() override;
1499 bool resolve_type(THD *thd) override;
1500};
1501
1503 public:
1505 : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1506 const char *func_name() const override { return "degrees"; }
1507 enum Functype functype() const override { return DEGREES_FUNC; }
1508};
1509
1511 public:
1513 : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1514 const char *func_name() const override { return "radians"; }
1515 enum Functype functype() const override { return RADIANS_FUNC; }
1516};
1518 public:
1519 Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1520 : Item_func_numhybrid(pos, opt_list),
1521 m_is_least_func(is_least_func),
1523
1524 longlong val_int() override;
1525 double val_real() override;
1526 my_decimal *val_decimal(my_decimal *) override;
1527 longlong int_op() override;
1528 double real_op() override;
1529 my_decimal *decimal_op(my_decimal *) override;
1530 String *str_op(String *) override;
1531 bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1532 bool time_op(MYSQL_TIME *ltime) override;
1534 return MYSQL_TYPE_VARCHAR;
1535 }
1536 bool resolve_type(THD *thd) override;
1537 bool resolve_type_inner(THD *thd) override;
1538 void set_numeric_type() override {}
1539 enum Item_result result_type() const override { return hybrid_type; }
1540
1541 /**
1542 Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1543 return a number in format YYMMDDhhmmss.
1544 */
1545 enum Item_result cast_to_int_type() const override {
1547 }
1548
1549 /// Returns true if arguments to this function should be compared as dates.
1550 bool compare_as_dates() const;
1551
1552 /// Returns true if at least one of the arguments was of temporal type.
1553 bool has_temporal_arg() const { return temporal_item; }
1554
1555 private:
1556 /// True if LEAST function, false if GREATEST.
1559 /*
1560 Used for determining whether one of the arguments is of temporal type and
1561 for converting arguments to a common output format if arguments are
1562 compared as dates and result type is character string. For example,
1563 LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1564 '95-05-05'.
1565 */
1567
1568 /**
1569 Fractional seconds precision to use when converting a time or timestamp
1570 expression into a string.
1571 */
1573 /**
1574 Compare arguments as datetime values.
1575
1576 @param value Pointer to which the datetime value of the winning argument
1577 is written.
1578
1579 @return true if error, false otherwise.
1580 */
1581 bool cmp_datetimes(longlong *value);
1582
1583 /**
1584 Compare arguments as time values.
1585
1586 @param value Pointer to which the time value of the winning argument is
1587 written.
1588
1589 @return true if error, false otherwise.
1590 */
1591 bool cmp_times(longlong *value);
1592};
1593
1594class Item_func_min final : public Item_func_min_max {
1595 public:
1596 Item_func_min(const POS &pos, PT_item_list *opt_list)
1597 : Item_func_min_max(pos, opt_list, true) {}
1598 const char *func_name() const override { return "least"; }
1599 enum Functype functype() const override { return LEAST_FUNC; }
1600};
1601
1602class Item_func_max final : public Item_func_min_max {
1603 public:
1604 Item_func_max(const POS &pos, PT_item_list *opt_list)
1605 : Item_func_min_max(pos, opt_list, false) {}
1606 const char *func_name() const override { return "greatest"; }
1607 enum Functype functype() const override { return GREATEST_FUNC; }
1608};
1609
1610/**
1611 A wrapper Item that normally returns its parameter, but becomes NULL when
1612 processing rows for rollup. Rollup is implemented by AggregateIterator, and
1613 works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1614 where only one group level is active, and so on. E.g., for a query with GROUP
1615 BY a,b, the rows will look like this:
1616
1617 a b rollup level
1618 1 1 2
1619 1 2 2
1620 1 NULL 1
1621 2 1 2
1622 2 NULL 1
1623 NULL NULL 0
1624
1625 Each rollup group item has a minimum level for when it becomes NULL. In the
1626 example above, a would have minimum level 0 and b would have minimum level 1.
1627 For simplicity, the JOIN carries a list of all rollup group items, and they
1628 are being given the current rollup level when it changes. A rollup level of
1629 INT_MAX essentially always disables rollup, which is useful when there are
1630 leftover group items in places that are not relevant for rollup
1631 (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1632 tables that are created before grouping, which should then effectively be
1633 disabled).
1634 */
1635class Item_rollup_group_item final : public Item_func {
1636 public:
1641 // We're going to replace inner_item in the SELECT list, so copy its hidden
1642 // status. (We could have done this in the caller, but it fits naturally in
1643 // with all the other copying done here.)
1645 set_nullable(true);
1647 }
1648 double val_real() override;
1649 longlong val_int() override;
1650 String *val_str(String *str) override;
1652 bool val_json(Json_wrapper *result) override;
1653 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1654 bool get_time(MYSQL_TIME *ltime) override;
1655 const char *func_name() const override { return "rollup_group_item"; }
1656 table_map used_tables() const override {
1657 /*
1658 If underlying item is non-constant, return its used_tables value.
1659 Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1660 */
1661 return args[0]->const_for_execution()
1662 ? (args[0]->used_tables() | RAND_TABLE_BIT)
1663 : args[0]->used_tables();
1664 }
1665 void update_used_tables() override {
1668 }
1669 Item_result result_type() const override { return args[0]->result_type(); }
1670 bool resolve_type(THD *) override {
1671 // needn't handle dynamic parameter as its const_item() is false.
1673
1674 // The item could be a NULL constant.
1675 null_value = args[0]->is_null();
1676 return false;
1677 }
1678 Item *inner_item() { return args[0]; }
1679 const Item *inner_item() const { return args[0]; }
1680 bool rollup_null() const {
1682 }
1683 enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1684 void print(const THD *thd, String *str,
1685 enum_query_type query_type) const override;
1686 bool eq(const Item *item, bool binary_cmp) const override;
1687
1688 // Used by AggregateIterator.
1690
1691 // Used when cloning the item only.
1692 int min_rollup_level() const { return m_min_rollup_level; }
1693
1694 private:
1697};
1698
1701
1702 public:
1703 Item_func_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1704 longlong val_int() override;
1705 const char *func_name() const override { return "length"; }
1706 bool resolve_type(THD *thd) override {
1707 if (param_type_is_default(thd, 0, 1)) return true;
1708 max_length = 10;
1709 return false;
1710 }
1711};
1712
1714 public:
1715 Item_func_bit_length(const POS &pos, Item *a) : Item_func_length(pos, a) {}
1716 longlong val_int() override {
1717 assert(fixed);
1718 return Item_func_length::val_int() * 8;
1719 }
1720 const char *func_name() const override { return "bit_length"; }
1721};
1722
1725
1726 public:
1728 Item_func_char_length(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1729 longlong val_int() override;
1730 const char *func_name() const override { return "char_length"; }
1731 bool resolve_type(THD *thd) override {
1732 max_length = 10;
1733 return Item_int_func::resolve_type(thd);
1734 }
1735};
1736
1738 public:
1739 Item_func_coercibility(const POS &pos, Item *a) : Item_int_func(pos, a) {
1740 null_on_null = false;
1741 }
1742 longlong val_int() override;
1743 const char *func_name() const override { return "coercibility"; }
1744 bool resolve_type(THD *thd) override {
1745 max_length = 10;
1746 set_nullable(false);
1747 return Item_int_func::resolve_type(thd);
1748 }
1749};
1750
1753
1754 public:
1756 Item_func_locate(const POS &pos, Item *a, Item *b)
1757 : Item_int_func(pos, a, b) {}
1758 Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1759 : Item_int_func(pos, a, b, c) {}
1760
1761 const char *func_name() const override { return "locate"; }
1762 longlong val_int() override;
1763 bool resolve_type(THD *thd) override;
1764 void print(const THD *thd, String *str,
1765 enum_query_type query_type) const override;
1766};
1767
1768class Item_func_instr final : public Item_func_locate {
1769 public:
1770 Item_func_instr(const POS &pos, Item *a, Item *b)
1771 : Item_func_locate(pos, a, b) {}
1772
1773 const char *func_name() const override { return "instr"; }
1774};
1775
1777 public:
1779 : Item_int_func(pos, a) {}
1780 longlong val_int() override;
1781 const char *func_name() const override {
1782 return "validate_password_strength";
1783 }
1784 bool resolve_type(THD *thd) override {
1785 max_length = 10;
1786 set_nullable(true);
1787 return Item_int_func::resolve_type(thd);
1788 }
1789};
1790
1791class Item_func_field final : public Item_int_func {
1794
1795 public:
1796 Item_func_field(const POS &pos, PT_item_list *opt_list)
1797 : Item_int_func(pos, opt_list) {}
1798 longlong val_int() override;
1799 const char *func_name() const override { return "field"; }
1800 bool resolve_type(THD *thd) override;
1801};
1802
1803class Item_func_ascii final : public Item_int_func {
1805
1806 public:
1807 Item_func_ascii(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1808 longlong val_int() override;
1809 const char *func_name() const override { return "ascii"; }
1810 bool resolve_type(THD *thd) override {
1811 max_length = 3;
1812 return Item_int_func::resolve_type(thd);
1813 }
1814};
1815
1816class Item_func_ord final : public Item_int_func {
1818
1819 public:
1820 Item_func_ord(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1821 longlong val_int() override;
1822 const char *func_name() const override { return "ord"; }
1823};
1824
1827 /*
1828 if m_enum_value is non-zero, it indicates the index of the value of
1829 argument 0 in the set in argument 1, given that argument 0 is
1830 a constant value and argument 1 is a field of type SET.
1831 */
1834
1835 public:
1837 : Item_int_func(pos, a, b) {}
1838 longlong val_int() override;
1839 const char *func_name() const override { return "find_in_set"; }
1840 bool resolve_type(THD *) override;
1841 const CHARSET_INFO *compare_collation() const override {
1842 return cmp_collation.collation;
1843 }
1844};
1845
1846/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1847
1848class Item_func_bit : public Item_func {
1849 protected:
1850 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1852 /// Buffer storing the determined value
1854 /**
1855 @returns true if the second argument should be of binary type for the
1856 result to be of binary type.
1857 */
1859
1860 public:
1861 Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1862 Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1863
1864 bool resolve_type(THD *) override;
1865 enum Item_result result_type() const override { return hybrid_type; }
1866
1867 longlong val_int() override;
1868 String *val_str(String *str) override;
1869 double val_real() override;
1870 my_decimal *val_decimal(my_decimal *decimal_value) override;
1871
1872 void print(const THD *thd, String *str,
1873 enum_query_type query_type) const override {
1874 print_op(thd, str, query_type);
1875 }
1876 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1877 if (hybrid_type == INT_RESULT)
1878 return get_date_from_int(ltime, fuzzydate);
1879 else
1880 return get_date_from_string(ltime, fuzzydate);
1881 }
1882 bool get_time(MYSQL_TIME *ltime) override {
1883 if (hybrid_type == INT_RESULT)
1884 return get_time_from_int(ltime);
1885 else
1886 return get_time_from_string(ltime);
1887 }
1888
1889 private:
1890 /**
1891 @brief Performs the operation on integers to produce a result of type
1892 INT_RESULT.
1893 @return The result of the operation.
1894 */
1895 virtual longlong int_op() = 0;
1896
1897 /**
1898 @brief Performs the operation on binary strings to produce a result of
1899 type STRING_RESULT.
1900 @return The result of the operation.
1901 */
1902 virtual String *str_op(String *) = 0;
1903};
1904
1905/**
1906 Base class for all the bit functions that work with two binary
1907 arguments: '&', '|', '^'.
1908*/
1909
1911 protected:
1913 return true;
1914 }
1915 template <class Char_func, class Int_func>
1916 String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1917 template <class Int_func>
1918 longlong eval_int_op(Int_func int_func);
1919
1920 public:
1922 : Item_func_bit(pos, a, b) {}
1923};
1924
1926 public:
1927 Item_func_bit_or(const POS &pos, Item *a, Item *b)
1928 : Item_func_bit_two_param(pos, a, b) {}
1929 const char *func_name() const override { return "|"; }
1930
1931 private:
1932 longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1933 String *str_op(String *str) override {
1934 return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1935 }
1936};
1937
1939 public:
1940 Item_func_bit_and(const POS &pos, Item *a, Item *b)
1941 : Item_func_bit_two_param(pos, a, b) {}
1942 const char *func_name() const override { return "&"; }
1943
1944 private:
1945 longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1946 String *str_op(String *str) override {
1947 return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1948 }
1949};
1950
1952 public:
1953 Item_func_bit_xor(const POS &pos, Item *a, Item *b)
1954 : Item_func_bit_two_param(pos, a, b) {}
1955 const char *func_name() const override { return "^"; }
1956
1957 private:
1958 longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1959 String *str_op(String *str) override {
1960 return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1961 }
1962};
1963
1965 public:
1966 Item_func_bit_count(const POS &pos, Item *a) : Item_int_func(pos, a) {}
1967 longlong val_int() override;
1968 const char *func_name() const override { return "bit_count"; }
1969 bool resolve_type(THD *thd) override {
1970 // Default: binary string; reprepare if integer
1971 if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
1972 args[0]->propagate_type(
1974 return true;
1976 return false;
1977 }
1978};
1979
1981 protected:
1983 return false;
1984 }
1985 template <bool to_left>
1987 template <bool to_left>
1989
1990 public:
1991 Item_func_shift(const POS &pos, Item *a, Item *b)
1992 : Item_func_bit(pos, a, b) {}
1993};
1994
1996 public:
1997 Item_func_shift_left(const POS &pos, Item *a, Item *b)
1998 : Item_func_shift(pos, a, b) {}
1999 const char *func_name() const override { return "<<"; }
2000
2001 private:
2002 longlong int_op() override { return eval_int_op<true>(); }
2003 String *str_op(String *str) override { return eval_str_op<true>(str); }
2004};
2005
2007 public:
2009 : Item_func_shift(pos, a, b) {}
2010 const char *func_name() const override { return ">>"; }
2011
2012 private:
2013 longlong int_op() override { return eval_int_op<false>(); }
2014 String *str_op(String *str) override { return eval_str_op<false>(str); }
2015};
2016
2017class Item_func_bit_neg final : public Item_func_bit {
2018 protected:
2020 return false;
2021 }
2022
2023 public:
2024 Item_func_bit_neg(const POS &pos, Item *a) : Item_func_bit(pos, a) {}
2025 const char *func_name() const override { return "~"; }
2026 void print(const THD *thd, String *str,
2027 enum_query_type query_type) const override {
2028 Item_func::print(thd, str, query_type);
2029 }
2030
2031 private:
2032 longlong int_op() override;
2033 String *str_op(String *str) override;
2034};
2035
2038
2039 public:
2041 explicit Item_func_last_insert_id(const POS &pos) : Item_int_func(pos) {}
2042 Item_func_last_insert_id(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2043
2044 bool do_itemize(Parse_context *pc, Item **res) override;
2045 longlong val_int() override;
2046 const char *func_name() const override { return "last_insert_id"; }
2047
2049 return INNER_TABLE_BIT;
2050 }
2051
2052 bool resolve_type(THD *thd) override {
2053 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2054 unsigned_flag = true;
2055 return false;
2056 }
2057 bool check_function_as_value_generator(uchar *checker_args) override {
2059 pointer_cast<Check_function_as_value_generator_parameters *>(
2060 checker_args);
2061 func_arg->banned_function_name = func_name();
2062 return true;
2063 }
2064};
2065
2068
2069 public:
2070 Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
2071 : Item_int_func(pos, count_expr, expr) {}
2072
2073 /// Ensure that "benchmark()" is never optimized away
2075 return RAND_TABLE_BIT;
2076 }
2077
2078 bool do_itemize(Parse_context *pc, Item **res) override;
2079 longlong val_int() override;
2080 const char *func_name() const override { return "benchmark"; }
2081 bool resolve_type(THD *thd) override {
2082 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2083 if (param_type_is_default(thd, 1, 2)) return true;
2084 max_length = 1;
2085 set_nullable(true);
2086 return false;
2087 }
2088 void print(const THD *thd, String *str,
2089 enum_query_type query_type) const override;
2090 bool check_function_as_value_generator(uchar *checker_args) override {
2092 pointer_cast<Check_function_as_value_generator_parameters *>(
2093 checker_args);
2094 func_arg->banned_function_name = func_name();
2095 return true;
2096 }
2097};
2098
2101
2102class Item_func_sleep final : public Item_int_func {
2104
2105 public:
2106 Item_func_sleep(const POS &pos, Item *a) : Item_int_func(pos, a) {}
2107
2108 bool do_itemize(Parse_context *pc, Item **res) override;
2109 const char *func_name() const override { return "sleep"; }
2110 /**
2111 This function is non-deterministic and hence depends on the
2112 'RAND' pseudo-table.
2113
2114 @retval RAND_TABLE_BIT
2115 */
2117 return RAND_TABLE_BIT;
2118 }
2119 bool check_function_as_value_generator(uchar *checker_args) override {
2121 pointer_cast<Check_function_as_value_generator_parameters *>(
2122 checker_args);
2123 func_arg->banned_function_name = func_name();
2124 return true;
2125 }
2126 bool resolve_type(THD *thd) override {
2127 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2128 return Item_int_func::resolve_type(thd);
2129 }
2130 longlong val_int() override;
2131};
2132
2133class Item_udf_func : public Item_func {
2135
2136 protected:
2138 bool is_expensive_processor(uchar *) override { return true; }
2139
2140 public:
2141 Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2142 : Item_func(pos, opt_list), udf(udf_arg) {
2143 null_on_null = false;
2144 }
2145 ~Item_udf_func() override = default;
2146
2147 bool do_itemize(Parse_context *pc, Item **res) override;
2148 const char *func_name() const override { return udf.name(); }
2149 enum Functype functype() const override { return UDF_FUNC; }
2152 }
2153 bool fix_fields(THD *thd, Item **ref) override;
2154 void cleanup() override;
2155 Item_result result_type() const override { return udf.result_type(); }
2156 bool is_expensive() override { return true; }
2157 void print(const THD *thd, String *str,
2158 enum_query_type query_type) const override;
2159
2160 bool check_function_as_value_generator(uchar *checker_args) override {
2162 pointer_cast<Check_function_as_value_generator_parameters *>(
2163 checker_args);
2164 func_arg->banned_function_name = func_name();
2165 return true;
2166 }
2167
2168 protected:
2169 bool may_have_named_parameters() const override { return true; }
2170
2171 private:
2172 /**
2173 This member is set during resolving and is used by update_used_tables() and
2174 fix_after_pullout() to preserve the non-deterministic property.
2175 */
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 const 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 do_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 do_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 do_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 do_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 EVENT_TRACKING_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 protected:
3421 void add_json_info(Json_object *obj) override;
3422
3423 public:
3425 uint key, flags;
3426 /// True if we are doing a full-text index scan with this MATCH function as a
3427 /// predicate, and the score can be retrieved with get_relevance(). If it is
3428 /// false, the score of the document must be retrieved with find_relevance().
3433 /**
3434 Master item means that if identical items are present in the
3435 statement, they use the same FT handler. FT handler is initialized
3436 only for master item and slave items just use it. FT hints initialized
3437 for master only, slave items HINTS are not accessed.
3438 */
3440 Item *concat_ws; // Item_func_concat_ws
3441 String value; // value of concat_ws
3442 String search_value; // key_item()'s value converted to cmp_collation
3443
3444 /**
3445 Constructor for Item_func_match class.
3446
3447 @param pos Position of token in the parser.
3448 @param a List of arguments.
3449 @param against_arg Expression to match against.
3450 @param b FT Flags.
3451 */
3452 Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3453 : Item_real_func(pos, a),
3454 against(against_arg),
3455 key(0),
3456 flags(b),
3459 master(nullptr),
3461 hints(nullptr),
3462 simple_expression(false),
3463 used_in_where_only(false) {
3464 null_on_null = false;
3465 }
3466
3467 bool do_itemize(Parse_context *pc, Item **res) override;
3468
3469 void cleanup() override {
3470 DBUG_TRACE;
3472 if (master == nullptr && ft_handler != nullptr) {
3474 }
3475 score_from_index_scan = false;
3476 ft_handler = nullptr;
3477 concat_ws = nullptr;
3478 return;
3479 }
3480 Item *key_item() const override { return against; }
3481 enum Functype functype() const override { return FT_FUNC; }
3482 const char *func_name() const override { return "match"; }
3483 bool fix_fields(THD *thd, Item **ref) override;
3484 bool eq(const Item *, bool binary_cmp) const override;
3485 /* The following should be safe, even if we compare doubles */
3486 longlong val_int() override {
3487 assert(fixed);
3488 return val_real() != 0.0;
3489 }
3490 double val_real() override;
3491 void print(const THD *thd, String *str,
3492 enum_query_type query_type) const override;
3493
3494 bool fix_index(const THD *thd);
3495 bool init_search(THD *thd);
3496 bool check_function_as_value_generator(uchar *checker_args) override {
3498 pointer_cast<Check_function_as_value_generator_parameters *>(
3499 checker_args);
3500 func_arg->banned_function_name = func_name();
3501 return true;
3502 }
3503
3504 /**
3505 Get number of matching rows from FT handler.
3506
3507 @note Requires that FT handler supports the extended API
3508
3509 @return Number of matching rows in result
3510 */
3512 assert(ft_handler);
3514
3515 return ((FT_INFO_EXT *)ft_handler)
3516 ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3517 }
3518
3519 /**
3520 Check whether FT result is ordered on rank
3521
3522 @return true if result is ordered
3523 @return false otherwise
3524 */
3526 assert(!master);
3527 if (hints->get_flags() & FT_SORTED) return true;
3528
3530 return false;
3531
3532 assert(ft_handler);
3533 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3535 }
3536
3537 /**
3538 Check whether FT result contains the document ID
3539
3540 @return true if document ID is available
3541 @return false otherwise
3542 */
3544 assert(ft_handler);
3545
3547 return false;
3548
3549 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3551 }
3552
3553 float get_filtering_effect(THD *thd, table_map filter_for_table,
3554 table_map read_tables,
3555 const MY_BITMAP *fields_to_ignore,
3556 double rows_in_table) override;
3557
3558 /**
3559 Returns master MATCH function.
3560
3561 @return pointer to master MATCH function.
3562 */
3564 if (master) return master->get_master();
3565 return this;
3566 }
3567
3568 /**
3569 Set master MATCH function and adjust used_in_where_only value.
3570
3571 @param item item for which master should be set.
3572 */
3575 item->master = this;
3576 }
3577
3578 /**
3579 Returns pointer to Ft_hints object belonging to master MATCH function.
3580
3581 @return pointer to Ft_hints object
3582 */
3584 assert(!master);
3585 return hints;
3586 }
3587
3588 /**
3589 Set comparison operation type and and value for master MATCH function.
3590
3591 @param type comparison operation type
3592 @param value_arg comparison operation value
3593 */
3594 void set_hints_op(enum ft_operation type, double value_arg) {
3595 assert(!master);
3596 hints->set_hint_op(type, value_arg);
3597 }
3598
3599 /**
3600 Set FT hints.
3601 */
3602 void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3603
3604 /**
3605 Check if ranking is not needed.
3606
3607 @return true if ranking is not needed
3608 @return false otherwise
3609 */
3611 assert(!master);
3612 return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3613 used_in_where_only && // MATCH result is not used
3614 // in expression
3615 hints->get_op_type() == FT_OP_NO); // MATCH is single function
3616 }
3617
3618 /**
3619 Set flag that the function is a simple expression.
3620
3621 @param val true if the function is a simple expression, false otherwise
3622 */
3623 void set_simple_expression(bool val) {
3624 assert(!master);
3625 simple_expression = val;
3626 }
3627
3628 /**
3629 Check if this MATCH function is a simple expression in WHERE condition.
3630
3631 @return true if simple expression
3632 @return false otherwise
3633 */
3635 assert(!master);
3636 return simple_expression;
3637 }
3638
3639 private:
3640 /**
3641 Fulltext index hints, initialized for master MATCH function only.
3642 */
3644 /**
3645 Flag is true when MATCH function is used as a simple expression in
3646 WHERE condition, i.e. there is no AND/OR combinations, just simple
3647 MATCH function or [MATCH, rank] comparison operation.
3648 */
3650 /**
3651 true if MATCH function is used in WHERE condition only.
3652 Used to determine what hints can be used for FT handler.
3653 Note that only master MATCH function has valid value.
3654 it's ok since only master function is involved in the hint processing.
3655 */
3657 /**
3658 Check whether storage engine for given table,
3659 allows FTS Boolean search on non-indexed columns.
3660
3661 @todo A flag should be added to the extended fulltext API so that
3662 it may be checked whether search on non-indexed columns are
3663 supported. Currently, it is not possible to check for such a
3664 flag since @c this->ft_handler is not yet set when this function is
3665 called. The current hack is to assume that search on non-indexed
3666 columns are supported for engines that does not support the extended
3667 fulltext API (e.g., MyISAM), while it is not supported for other
3668 engines (e.g., InnoDB)
3669
3670 @param tr Table for which storage engine to check
3671
3672 @retval true if BOOLEAN search on non-indexed columns is supported
3673 @retval false otherwise
3674 */
3676 // Only Boolean search may support non_indexed columns
3677 if (!(flags & FT_BOOL)) return false;
3678
3679 assert(tr && tr->file);
3680
3681 // Assume that if extended fulltext API is not supported,
3682 // non-indexed columns are allowed. This will be true for MyISAM.
3683 if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3684
3685 return false;
3686 }
3687};
3688
3689/**
3690 A visitor that calls the specified function on every non-aggregated full-text
3691 search function (Item_func_match) it encounters when it is used in a
3692 PREFIX+POSTFIX walk with WalkItem(). It skips every item that is wrapped in an
3693 aggregate function, and also every item wrapped in a reference, as the items
3694 behind the reference are already handled elsewhere (in another query block or
3695 in another element of the SELECT list).
3696 */
3698 public:
3700 std::function<bool(Item_func_match *)> func);
3701 bool operator()(Item *item);
3702
3703 private:
3704 std::function<bool(Item_func_match *)> m_func;
3705};
3706
3709
3711
3712 public:
3713 Item_func_is_free_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
3714
3715 bool do_itemize(Parse_context *pc, Item **res) override;
3716 longlong val_int() override;
3717 const char *func_name() const override { return "is_free_lock"; }
3718 bool resolve_type(THD *thd) override {
3719 if (param_type_is_default(thd, 0, 1)) return true;
3720 max_length = 1;
3721 set_nullable(true);
3722 return false;
3723 }
3724 bool is_non_const_over_literals(uchar *) override { return true; }
3725 bool check_function_as_value_generator(uchar *checker_args) override {
3727 pointer_cast<Check_function_as_value_generator_parameters *>(
3728 checker_args);
3729 func_arg->banned_function_name = func_name();
3730 return true;
3731 }
3732};
3733
3736
3738
3739 public:
3740 Item_func_is_used_lock(const POS &pos, Item *a) : Item_int_func(pos, a) {}
3741
3742 bool do_itemize(Parse_context *pc, Item **res) override;
3743 longlong val_int() override;
3744 const char *func_name() const override { return "is_used_lock"; }
3745 bool resolve_type(THD *thd) override {
3746 if (param_type_is_default(thd, 0, 1)) return true;
3747 unsigned_flag = true;
3748 set_nullable(true);
3749 return false;
3750 }
3751 bool is_non_const_over_literals(uchar *) override { return true; }
3752 bool check_function_as_value_generator(uchar *checker_args) override {
3754 pointer_cast<Check_function_as_value_generator_parameters *>(
3755 checker_args);
3756 func_arg->banned_function_name = func_name();
3757 return true;
3758 }
3759};
3760
3763
3764 public:
3765 explicit Item_func_row_count(const POS &pos) : Item_int_func(pos) {}
3766
3767 bool do_itemize(Parse_context *pc, Item **res) override;
3768
3769 longlong val_int() override;
3770 const char *func_name() const override { return "row_count"; }
3771 bool resolve_type(THD *) override {
3772 set_nullable(false);
3773 return false;
3774 }
3775 bool check_function_as_value_generator(uchar *checker_args) override {
3777 pointer_cast<Check_function_as_value_generator_parameters *>(
3778 checker_args);
3779 func_arg->banned_function_name = func_name();
3780 return true;
3781 }
3782};
3783
3784/*
3785 *
3786 * Stored FUNCTIONs
3787 *
3788 */
3789
3790class sp_head;
3791class sp_name;
3792
3793class Item_func_sp final : public Item_func {
3795
3796 private:
3798 /// The name of the stored function
3799 sp_name *m_name{nullptr};
3800 /// Pointer to actual function instance (null when not resolved or executing)
3801 sp_head *m_sp{nullptr};
3802 /// The result field of the concrete stored function.
3804 /// true when function execution is deterministic
3805 bool m_deterministic{false};
3806
3807 bool execute();
3808 bool execute_impl(THD *thd);
3809 bool init_result_field(THD *thd);
3810
3811 protected:
3812 bool is_expensive_processor(uchar *) override { return true; }
3813
3814 public:
3815 Item_func_sp(const POS &pos, const LEX_STRING &db_name,
3816 const LEX_STRING &fn_name, bool use_explicit_name,
3817 PT_item_list *opt_list);
3818
3819 bool do_itemize(Parse_context *pc, Item **res) override;
3820 /**
3821 Must not be called before the procedure is resolved,
3822 i.e. @c init_result_field().
3823 */
3824 table_map get_initial_pseudo_tables() const override;
3825 void update_used_tables() override;
3826 void fix_after_pullout(Query_block *parent_query_block,
3827 Query_block *removed_query_block) override;
3828 void cleanup() override;
3829
3830 const char *func_name() const override;
3831
3832 Field *tmp_table_field(TABLE *t_arg) override;
3833
3834 void make_field(Send_field *tmp_field) override;
3835
3836 Item_result result_type() const override;
3837
3838 longlong val_int() override;
3839 double val_real() override;
3840 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
3841 bool get_time(MYSQL_TIME *ltime) override;
3842 my_decimal *val_decimal(my_decimal *dec_buf) override;
3843 String *val_str(String *str) override;