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