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