MySQL 8.0.30
Source Code Documentation
item_func.h
Go to the documentation of this file.
1#ifndef ITEM_FUNC_INCLUDED
2#define ITEM_FUNC_INCLUDED
3
4/* Copyright (c) 2000, 2022, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26#include <sys/types.h>
27
28#include <climits>
29#include <cmath> // isfinite
30#include <cstddef>
31#include <functional>
32
33#include "decimal.h"
34#include "field_types.h"
35#include "ft_global.h"
36#include "lex_string.h"
37#include "m_ctype.h"
38#include "my_alloc.h"
39#include "my_base.h"
40#include "my_compiler.h"
41#include "my_dbug.h"
42#include "my_inttypes.h"
44#include "my_table_map.h"
45#include "my_thread_local.h"
46#include "my_time.h"
49#include "mysql_com.h"
50#include "mysql_time.h"
51#include "mysqld_error.h"
52#include "sql/enum_query_type.h"
53#include "sql/field.h"
54#include "sql/handler.h"
55#include "sql/item.h" // Item_result_field
56#include "sql/my_decimal.h" // str2my_decimal
57#include "sql/parse_location.h" // POS
58#include "sql/set_var.h" // enum_var_type
59#include "sql/sql_const.h"
60#include "sql/sql_udf.h" // udf_handler
61#include "sql/table.h"
62#include "sql/thr_malloc.h"
63#include "sql_string.h"
64#include "template_utils.h"
65
66class Json_wrapper;
67class PT_item_list;
68class Protocol;
69class Query_block;
70class THD;
71class sp_rcontext;
72struct MY_BITMAP;
73struct Parse_context;
74
75template <class T>
76class List;
77
78/* Function items used by mysql */
79
80extern bool reject_geometry_args(uint arg_count, Item **args,
82void unsupported_json_comparison(size_t arg_count, Item **args,
83 const char *msg);
84
85void report_conversion_error(const CHARSET_INFO *to_cs, const char *from,
86 size_t from_length, const CHARSET_INFO *from_cs);
87
88bool simplify_string_args(THD *thd, const DTCollation &c, Item **items,
89 uint nitems);
90
92
94 protected:
95 /**
96 Array of pointers to arguments. If there are max 2 arguments, this array
97 is often just m_embedded_arguments; otherwise it's explicitly allocated in
98 the constructor.
99 */
101
102 private:
104
105 /// Allocates space for the given number of arguments, if needed. Uses
106 /// #m_embedded_arguments if it's big enough.
107 bool alloc_args(MEM_ROOT *mem_root, unsigned num_args) {
108 if (num_args <= array_elements(m_embedded_arguments)) {
110 } else {
111 args = mem_root->ArrayAlloc<Item *>(num_args);
112 if (args == nullptr) {
113 // OOM
114 arg_count = 0;
115 return true;
116 }
117 }
118 arg_count = num_args;
119 return false;
120 }
121
122 public:
123 uint arg_count; ///< How many arguments in 'args'
124 /// Changes argument and maintains any necessary invariants.
125 virtual void set_arg_resolve(THD *, uint i, Item *arg) { args[i] = arg; }
126 virtual uint argument_count() const { return arg_count; }
127 inline Item **arguments() const {
128 return (argument_count() > 0) ? args : nullptr;
129 }
130
131 protected:
132 /*
133 These decide of types of arguments which are prepared-statement
134 parameters.
135 */
138 bool param_type_is_default(THD *thd, uint start, uint end, uint step,
139 enum_field_types def);
142 return param_type_is_default(thd, start, end, 1, def);
143 }
145
146 /**
147 Affects how to determine that NULL argument implies a NULL function return.
148 Default behaviour in this class is:
149 - if true, any NULL argument means the function returns NULL.
150 - if false, no such assumption is made and not_null_tables_cache is thus
151 set to 0.
152 null_on_null is true for all Item_func derived classes, except Item_func_sp,
153 all CASE derived functions and a few other functions.
154 RETURNS NULL ON NULL INPUT can be implemented for stored functions by
155 modifying this member in class Item_func_sp.
156 */
157 bool null_on_null{true};
158 /*
159 Allowed numbers of columns in result (usually 1, which means scalar value)
160 0 means get this number from first argument
161 */
163 /// Value used in calculation of result of used_tables()
165 /// Value used in calculation of result of not_null_tables()
167
168 public:
169 /*
170 When updating Functype with new spatial functions,
171 is_spatial_operator() should also be updated.
172
173 DD_INTERNAL_FUNC:
174 Some of the internal functions introduced for the INFORMATION_SCHEMA views
175 opens data-dictionary tables. DD_INTERNAL_FUNC is used for the such type
176 of functions.
177 */
178 enum Functype {
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)
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)
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)
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)
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 virtual Item *get_arg(uint i) { return args[i]; }
436 virtual Item *set_arg(THD *, uint, Item *) {
437 assert(0);
438 return nullptr;
439 }
440
441 bool itemize(Parse_context *pc, Item **res) override;
442
443 bool fix_fields(THD *, Item **ref) override;
444 bool fix_func_arg(THD *, Item **arg);
445 void fix_after_pullout(Query_block *parent_query_block,
446 Query_block *removed_query_block) override;
447 /**
448 Resolve type of function after all arguments have had their data types
449 resolved. Called from resolve_type() when no dynamic parameters
450 are used and from propagate_type() otherwise.
451 */
452 virtual bool resolve_type_inner(THD *) {
453 assert(false);
454 return false;
455 }
456 bool propagate_type(THD *thd, const Type_properties &type) override;
457 /**
458 Returns the pseudo tables depended upon in order to evaluate this
459 function expression. The default implementation returns the empty
460 set.
461 */
462 virtual table_map get_initial_pseudo_tables() const { return 0; }
463 table_map used_tables() const override { return used_tables_cache; }
465 void update_used_tables() override;
467 bool eq(const Item *item, bool binary_cmp) const override;
468 virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
469 virtual bool have_rev_func() const { return false; }
470 virtual Item *key_item() const { return args[0]; }
471 /**
472 Copy arguments from list to args array
473
474 @param list function argument list
475 @param context_free true: for use in context-independent
476 constructors (Item_func(POS,...)) i.e. for use
477 in the parser
478 @return true on OOM, false otherwise
479 */
480 bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
481 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
482 mem_root_deque<Item *> *fields) override;
483 void print(const THD *thd, String *str,
484 enum_query_type query_type) const override;
485 void print_op(const THD *thd, String *str, enum_query_type query_type) const;
486 void print_args(const THD *thd, String *str, uint from,
487 enum_query_type query_type) const;
488 virtual void fix_num_length_and_dec();
489 virtual bool is_deprecated() const { return false; }
490 bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
491 return (null_value = args[0]->get_date(ltime, fuzzy_date));
492 }
493 inline bool get_arg0_time(MYSQL_TIME *ltime) {
494 return (null_value = args[0]->get_time(ltime));
495 }
496 bool is_null() override { return update_null_value() || null_value; }
499 friend class udf_handler;
500 Field *tmp_table_field(TABLE *t_arg) override;
501 Item *get_tmp_table_item(THD *thd) override;
502
503 my_decimal *val_decimal(my_decimal *) override;
504
505 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
506 int item_sep) {
507 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
508 false);
509 }
510 /*
511 Aggregate arguments for string result, e.g: CONCAT(a,b)
512 - convert to @@character_set_connection if all arguments are numbers
513 - allow DERIVATION_NONE
514 */
516 uint nitems, int item_sep = 1) {
517 return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
518 item_sep);
519 }
520 /*
521 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
522 - don't convert to @@character_set_connection if all arguments are numbers
523 - don't allow DERIVATION_NONE
524 */
526 uint nitems, int item_sep = 1) {
527 return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
528 item_sep);
529 }
530
531 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
532 Item *transform(Item_transformer transformer, uchar *arg) override;
533 Item *compile(Item_analyzer analyzer, uchar **arg_p,
534 Item_transformer transformer, uchar *arg_t) override;
535 void traverse_cond(Cond_traverser traverser, void *arg,
536 traverse_order order) override;
537
538 /**
539 Throw an error if the input double number is not finite, i.e. is either
540 +/-INF or NAN.
541 */
542 inline double check_float_overflow(double value) {
543 return std::isfinite(value) ? value : raise_float_overflow();
544 }
545 /**
546 Throw an error if the input BIGINT value represented by the
547 (longlong value, bool unsigned flag) pair cannot be returned by the
548 function, i.e. is not compatible with this Item's unsigned_flag.
549 */
550 inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
551 if ((unsigned_flag && !val_unsigned && value < 0) ||
552 (!unsigned_flag && val_unsigned &&
553 (ulonglong)value > (ulonglong)LLONG_MAX))
554 return raise_integer_overflow();
555 return value;
556 }
557 /**
558 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
559 */
562 }
563
565 assert(fixed);
566 for (uint i = 0; i < arg_count; i++) {
567 if (args[i]->type() == Item::FIELD_ITEM &&
569 return true;
570 }
571 return false;
572 }
573
575 assert(fixed);
576 for (uint i = 0; i < arg_count; i++) {
577 if (args[i]->type() == Item::FIELD_ITEM &&
578 (args[i]->data_type() == MYSQL_TYPE_DATE ||
580 return true;
581 }
582 return false;
583 }
584
586 assert(fixed);
587 for (uint i = 0; i < arg_count; i++) {
588 if (args[i]->type() == Item::FIELD_ITEM &&
589 (args[i]->data_type() == MYSQL_TYPE_TIME ||
591 return true;
592 }
593 return false;
594 }
595
597 assert(fixed);
598 for (uint i = 0; i < arg_count; i++) {
599 if (args[i]->type() == Item::FIELD_ITEM &&
601 return true;
602 }
603 return false;
604 }
605
606 /*
607 We assume the result of any function that has a TIMESTAMP argument to be
608 timezone-dependent, since a TIMESTAMP value in both numeric and string
609 contexts is interpreted according to the current timezone.
610 The only exception is UNIX_TIMESTAMP() which returns the internal
611 representation of a TIMESTAMP argument verbatim, and thus does not depend on
612 the timezone.
613 */
615 return has_timestamp_args();
616 }
617
618 Item *gc_subst_transformer(uchar *arg) override;
619
620 bool resolve_type(THD *thd) override {
621 // By default, pick PS-param's type from other arguments, or VARCHAR
622 return param_type_uses_non_param(thd);
623 }
624
625 /**
626 Whether an arg of a JSON function can be cached to avoid repetitive
627 string->JSON conversion. This function returns true only for those args,
628 which are the source of JSON data. JSON path args are cached independently
629 and for them this function returns false. Same as for all other type of
630 args.
631
632 @param arg the arg to cache
633
634 @retval true arg can be cached
635 @retval false otherwise
636 */
637 virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
638 return CACHE_NONE;
639 }
640
641 /// Whether this Item is an equi-join condition. If this Item is a compound
642 /// item (i.e. multiple condition AND'ed together), it will only return true
643 /// if the Item contains only equi-join conditions AND'ed together. This is
644 /// used to determine whether the condition can be used as a join condition
645 /// for hash join (join conditions in hash join must be equi-join conditions),
646 /// or if it should be placed as a filter after the join.
647 virtual bool contains_only_equi_join_condition() const { return false; }
648
649 protected:
650 /**
651 Whether or not an item should contribute to the filtering effect
652 (@see get_filtering_effect()). First it verifies that table
653 requirements are satisfied as follows:
654
655 1) The item must refer to a field in 'filter_for_table' in some
656 way. This reference may be indirect through any number of
657 intermediate items. For example, this item may be an
658 Item_cond_and which refers to an Item_func_eq which refers to
659 the field.
660 2) The item must not refer to other tables than those already
661 read and the table in 'filter_for_table'
662
663 Then it contines to other properties as follows:
664
665 Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
666 Item_func is to contribute to the filtering effect, then
667
668 1) one of the operands must be a field from 'filter_for_table' that is not
669 in 'fields_to_ignore', and
670 2) depending on the Item_func type filtering effect is calculated
671 for, one or all [1] of the other operand(s) must be an available
672 value, i.e.:
673 - a constant, or
674 - a constant subquery, or
675 - a field value read from a table in 'read_tables', or
676 - a second field in 'filter_for_table', or
677 - a function that only refers to constants or tables in
678 'read_tables', or
679 - special case: an implicit value like NULL in the case of
680 "field IS NULL". Such Item_funcs have arg_count==1.
681
682 [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
683 for the rest (e.g. BETWEEN)
684
685 @param read_tables Tables earlier in the join sequence.
686 Predicates for table 'filter_for_table' that
687 rely on values from these tables can be part of
688 the filter effect.
689 @param filter_for_table The table we are calculating filter effect for
690 @param fields_to_ignore Columns that should be ignored.
691
692
693 @return Item_field that participates in the predicate if none of the
694 requirements are broken, NULL otherwise
695
696 @note: This function only applies to items doing comparison, i.e.
697 boolean predicates. Unfortunately, some of those items do not
698 inherit from Item_bool_func so the member function has to be
699 placed in Item_func.
700 */
702 table_map read_tables, table_map filter_for_table,
703 const MY_BITMAP *fields_to_ignore) const;
704 /**
705 Named parameters are allowed in a parameter list
706
707 The syntax to name parameters in a function call is as follow:
708 <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
709 where "AS" is optional.
710 Only UDF function support that syntax.
711
712 @return true if the function item can have named parameters
713 */
714 virtual bool may_have_named_parameters() const { return false; }
715 bool is_non_const_over_literals(uchar *) override { return false; }
716
717 bool check_function_as_value_generator(uchar *checker_args) override {
718 if (is_deprecated()) {
720 pointer_cast<Check_function_as_value_generator_parameters *>(
721 checker_args);
722 func_arg->banned_function_name = func_name();
723 return true;
724 }
725 return false;
726 }
727 bool is_valid_for_pushdown(uchar *arg) override;
728 bool check_column_in_window_functions(uchar *arg) override;
729 bool check_column_in_group_by(uchar *arg) override;
730
732};
733
734class Item_real_func : public Item_func {
735 public:
737 explicit Item_real_func(const POS &pos) : Item_func(pos) {
739 }
740
744 }
745
747
748 Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
750 }
751
754 }
755
758 }
759
760 String *val_str(String *str) override;
761 my_decimal *val_decimal(my_decimal *decimal_value) override;
762 longlong val_int() override {
763 assert(fixed);
765 }
766 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
767 return get_date_from_real(ltime, fuzzydate);
768 }
769 bool get_time(MYSQL_TIME *ltime) override {
770 return get_time_from_real(ltime);
771 }
772 enum Item_result result_type() const override { return REAL_RESULT; }
773};
774
776 protected:
778
779 public:
782 }
786 }
787
791 }
795 }
796
800 }
804 }
805
806 enum Item_result result_type() const override { return hybrid_type; }
808 return MYSQL_TYPE_DOUBLE;
809 }
810 bool resolve_type(THD *thd) override;
811 bool resolve_type_inner(THD *thd) override;
812 void fix_num_length_and_dec() override;
813 virtual void set_numeric_type() = 0; // To be called from resolve_type()
814
815 double val_real() override;
816 longlong val_int() override;
817 my_decimal *val_decimal(my_decimal *) override;
818 String *val_str(String *str) override;
819 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
820 bool get_time(MYSQL_TIME *ltime) override;
821 /**
822 @brief Performs the operation that this functions implements when the
823 result type is INT.
824
825 @return The result of the operation.
826 */
827 virtual longlong int_op() = 0;
828
829 /**
830 @brief Performs the operation that this functions implements when the
831 result type is REAL.
832
833 @return The result of the operation.
834 */
835 virtual double real_op() = 0;
836
837 /**
838 @brief Performs the operation that this functions implements when the
839 result type is DECIMAL.
840
841 @param decimal_value A pointer where the DECIMAL value will be allocated.
842 @return
843 - 0 If the result is NULL
844 - The same pointer it was given, with the area initialized to the
845 result of the operation.
846 */
847 virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
848
849 /**
850 @brief Performs the operation that this functions implements when the
851 result type is a string type.
852
853 @return The result of the operation.
854 */
855 virtual String *str_op(String *) = 0;
856 /**
857 @brief Performs the operation that this functions implements when the
858 result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
859
860 @return The result of the operation.
861 */
862 virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
863 virtual bool time_op(MYSQL_TIME *ltime) = 0;
864 bool is_null() override { return update_null_value() || null_value; }
865};
866
867/* function where type of result detected by first argument */
869 public:
872
875 : Item_func_numhybrid(pos, a, b) {}
876
877 void fix_num_length_and_dec() override;
878 void set_numeric_type() override;
879 String *str_op(String *) override {
880 assert(0);
881 return nullptr;
882 }
884 assert(0);
885 return false;
886 }
887 bool time_op(MYSQL_TIME *) override {
888 assert(0);
889 return false;
890 }
891};
892
893/* Base class for operations like '+', '-', '*' */
895 public:
897 Item_num_op(const POS &pos, Item *a, Item *b)
898 : Item_func_numhybrid(pos, a, b) {}
899
900 virtual void result_precision() = 0;
901
902 void print(const THD *thd, String *str,
903 enum_query_type query_type) const override {
904 print_op(thd, str, query_type);
905 }
906
907 void set_numeric_type() override;
908 String *str_op(String *) override {
909 assert(0);
910 return nullptr;
911 }
913 assert(0);
914 return false;
915 }
916 bool time_op(MYSQL_TIME *) override {
917 assert(0);
918 return false;
919 }
920};
921
922class Item_int_func : public Item_func {
923 public:
925 explicit Item_int_func(const POS &pos) : Item_func(pos) {
927 }
928
930 Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
932 }
933
936 }
937 Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
939 }
940
941 Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
943 }
944 Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
945 : Item_func(pos, a, b, c) {
947 }
948
949 Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
951 }
952 Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
953 : Item_func(pos, a, b, c, d) {
955 }
956
959 }
961 : Item_func(pos, opt_list) {
963 }
964
965 Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
967 }
968 double val_real() override;
969 String *val_str(String *str) override;
970 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
971 return get_date_from_int(ltime, fuzzydate);
972 }
973 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
974 enum Item_result result_type() const override { return INT_RESULT; }
975 /*
976 Concerning PS-param types,
977 resolve_type(THD *) is not overridden here, as experience shows that for
978 most child classes of this class, VARCHAR is the best default
979 */
980};
981
984
985 public:
987
989 return INNER_TABLE_BIT;
990 }
991 bool itemize(Parse_context *pc, Item **res) override;
992 const char *func_name() const override { return "connection_id"; }
993 bool resolve_type(THD *thd) override;
994 bool fix_fields(THD *thd, Item **ref) override;
995 longlong val_int() override;
996 bool check_function_as_value_generator(uchar *checker_args) override {
998 pointer_cast<Check_function_as_value_generator_parameters *>(
999 checker_args);
1000 func_arg->banned_function_name = func_name();
1001 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1002 (func_arg->source == VGS_CHECK_CONSTRAINT));
1003 }
1004};
1005
1007 public:
1009 unsigned_flag = false;
1010 }
1011 const char *func_name() const override { return "cast_as_signed"; }
1012 longlong val_int() override;
1013 bool resolve_type(THD *thd) override;
1014 void print(const THD *thd, String *str,
1015 enum_query_type query_type) const override;
1016 enum Functype functype() const override { return TYPECAST_FUNC; }
1017};
1018
1020 public:
1022 unsigned_flag = true;
1023 }
1024 const char *func_name() const override { return "cast_as_unsigned"; }
1025 longlong val_int() override;
1026 bool resolve_type(THD *thd) override;
1027 void print(const THD *thd, String *str,
1028 enum_query_type query_type) const override;
1029 enum Functype functype() const override { return TYPECAST_FUNC; }
1030};
1031
1032class Item_typecast_decimal final : public Item_func {
1033 public:
1034 Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1035 : Item_func(pos, a) {
1037 }
1038 String *val_str(String *str) override;
1039 double val_real() override;
1040 longlong val_int() override;
1041 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1042 return get_date_from_decimal(ltime, fuzzydate);
1043 }
1044 bool get_time(MYSQL_TIME *ltime) override {
1045 return get_time_from_decimal(ltime);
1046 }
1047 my_decimal *val_decimal(my_decimal *) override;
1048 enum Item_result result_type() const override { return DECIMAL_RESULT; }
1049 bool resolve_type(THD *thd) override {
1050 if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1051 return true;
1052 return false;
1053 }
1054 const char *func_name() const override { return "cast_as_decimal"; }
1055 enum Functype functype() const override { return TYPECAST_FUNC; }
1056 void print(const THD *thd, String *str,
1057 enum_query_type query_type) const override;
1058};
1059
1060/**
1061 Class used to implement CAST to floating-point data types.
1062*/
1063class Item_typecast_real final : public Item_func {
1064 public:
1065 Item_typecast_real(const POS &pos, Item *a, bool as_double)
1066 : Item_func(pos, a) {
1067 if (as_double)
1069 else
1071 }
1073 String *val_str(String *str) override;
1074 double val_real() override;
1075 longlong val_int() override { return val_int_from_real(); }
1076 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1077 bool get_time(MYSQL_TIME *ltime) override;
1078 my_decimal *val_decimal(my_decimal *decimal_value) override;
1079 enum Item_result result_type() const override { return REAL_RESULT; }
1080 bool resolve_type(THD *thd) override {
1081 return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1082 }
1083 const char *func_name() const override { return "cast_as_real"; }
1084 enum Functype functype() const override { return TYPECAST_FUNC; }
1085 void print(const THD *thd, String *str,
1086 enum_query_type query_type) const override;
1087};
1088
1090 public:
1093 : Item_num_op(pos, a, b) {}
1094
1095 void result_precision() override;
1096 bool check_partition_func_processor(uchar *) override { return false; }
1097 bool check_function_as_value_generator(uchar *) override { return false; }
1098};
1099
1101 public:
1104 : Item_func_additive_op(pos, a, b) {}
1105
1106 const char *func_name() const override { return "+"; }
1107
1108 // SUPPRESS_UBSAN: signed integer overflow
1109 longlong int_op() override SUPPRESS_UBSAN;
1110
1111 double real_op() override;
1112 my_decimal *decimal_op(my_decimal *) override;
1113 enum Functype functype() const override { return PLUS_FUNC; }
1114};
1115
1117 public:
1120 : Item_func_additive_op(pos, a, b) {}
1121
1122 const char *func_name() const override { return "-"; }
1123
1124 // SUPPRESS_UBSAN: signed integer overflow
1125 longlong int_op() override SUPPRESS_UBSAN;
1126
1127 double real_op() override;
1128 my_decimal *decimal_op(my_decimal *) override;
1129 bool resolve_type(THD *thd) override;
1130 enum Functype functype() const override { return MINUS_FUNC; }
1131};
1132
1133class Item_func_mul final : public Item_num_op {
1134 public:
1136 Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1137
1138 const char *func_name() const override { return "*"; }
1139 longlong int_op() override;
1140 double real_op() override;
1141 my_decimal *decimal_op(my_decimal *) override;
1142 void result_precision() override;
1143 bool check_partition_func_processor(uchar *) override { return false; }
1144 bool check_function_as_value_generator(uchar *) override { return false; }
1145 enum Functype functype() const override { return MUL_FUNC; }
1146};
1147
1149 public:
1151 : Item_num_op(pos, a, b) {}
1153 longlong int_op() override;
1154 double real_op() override;
1155 my_decimal *decimal_op(my_decimal *) override;
1156 enum Functype functype() const override { return DIV_FUNC; }
1157
1158 protected:
1160};
1161
1162class Item_func_div final : public Item_func_div_base {
1163 public:
1165 : Item_func_div_base(pos, a, b) {}
1166 const char *func_name() const override { return "/"; }
1167 bool resolve_type(THD *thd) override;
1168 void result_precision() override;
1169};
1170
1172 public:
1175 : Item_func_div_base(pos, a, b) {}
1176 const char *func_name() const override { return "DIV"; }
1178 return MYSQL_TYPE_LONGLONG;
1179 }
1180 bool resolve_type(THD *thd) override;
1181 void result_precision() override;
1182 void set_numeric_type() override;
1183 bool check_partition_func_processor(uchar *) override { return false; }
1184 bool check_function_as_value_generator(uchar *) override { return false; }
1185};
1186
1187class Item_func_mod final : public Item_num_op {
1188 public:
1190 Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1191
1192 longlong int_op() override;
1193 double real_op() override;
1194 my_decimal *decimal_op(my_decimal *) override;
1195 const char *func_name() const override { return "%"; }
1196 void result_precision() override;
1197 bool resolve_type(THD *thd) override;
1198 bool check_partition_func_processor(uchar *) override { return false; }
1199 bool check_function_as_value_generator(uchar *) override { return false; }
1200 enum Functype functype() const override { return MOD_FUNC; }
1201};
1202
1203class Item_func_neg final : public Item_func_num1 {
1204 public:
1207
1208 double real_op() override;
1209 longlong int_op() override;
1210 my_decimal *decimal_op(my_decimal *) override;
1211 const char *func_name() const override { return "-"; }
1212 enum Functype functype() const override { return NEG_FUNC; }
1213 bool resolve_type(THD *thd) override;
1214 void fix_num_length_and_dec() override;
1215 bool check_partition_func_processor(uchar *) override { return false; }
1216 bool check_function_as_value_generator(uchar *) override { return false; }
1217};
1218
1219class Item_func_abs final : public Item_func_num1 {
1220 public:
1222 double real_op() override;
1223 longlong int_op() override;
1224 my_decimal *decimal_op(my_decimal *) override;
1225 const char *func_name() const override { return "abs"; }
1226 bool resolve_type(THD *) override;
1227 bool check_partition_func_processor(uchar *) override { return false; }
1228 bool check_function_as_value_generator(uchar *) override { return false; }
1229 enum Functype functype() const override { return ABS_FUNC; }
1230};
1231
1232// A class to handle logarithmic and trigonometric functions
1233
1235 public:
1238
1239 Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1240 bool resolve_type(THD *thd) override;
1241};
1242
1243class Item_func_exp final : public Item_dec_func {
1244 public:
1246 double val_real() override;
1247 const char *func_name() const override { return "exp"; }
1248 enum Functype functype() const override { return EXP_FUNC; }
1249};
1250
1251class Item_func_ln final : public Item_dec_func {
1252 public:
1254 double val_real() override;
1255 const char *func_name() const override { return "ln"; }
1256 enum Functype functype() const override { return LN_FUNC; }
1257};
1258
1259class Item_func_log final : public Item_dec_func {
1260 public:
1262 Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1263 double val_real() override;
1264 const char *func_name() const override { return "log"; }
1265 enum Functype functype() const override { return LOG_FUNC; }
1266};
1267
1268class Item_func_log2 final : public Item_dec_func {
1269 public:
1271 double val_real() override;
1272 const char *func_name() const override { return "log2"; }
1273};
1274
1275class Item_func_log10 final : public Item_dec_func {
1276 public:
1278 double val_real() override;
1279 const char *func_name() const override { return "log10"; }
1280 enum Functype functype() const override { return LOG10_FUNC; }
1281};
1282
1283class Item_func_sqrt final : public Item_dec_func {
1284 public:
1286 double val_real() override;
1287 const char *func_name() const override { return "sqrt"; }
1288 enum Functype functype() const override { return SQRT_FUNC; }
1289};
1290
1291class Item_func_pow final : public Item_dec_func {
1292 public:
1293 Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1294 double val_real() override;
1295 const char *func_name() const override { return "pow"; }
1296};
1297
1298class Item_func_acos final : public Item_dec_func {
1299 public:
1301 double val_real() override;
1302 const char *func_name() const override { return "acos"; }
1303 enum Functype functype() const override { return ACOS_FUNC; }
1304};
1305
1306class Item_func_asin final : public Item_dec_func {
1307 public:
1309 double val_real() override;
1310 const char *func_name() const override { return "asin"; }
1311 enum Functype functype() const override { return ASIN_FUNC; }
1312};
1313
1314class Item_func_atan final : public Item_dec_func {
1315 public:
1317 Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1318 double val_real() override;
1319 const char *func_name() const override { return "atan"; }
1320 enum Functype functype() const override { return ATAN_FUNC; }
1321};
1322
1323class Item_func_cos final : public Item_dec_func {
1324 public:
1326 double val_real() override;
1327 const char *func_name() const override { return "cos"; }
1328 enum Functype functype() const override { return COS_FUNC; }
1329};
1330
1331class Item_func_sin final : public Item_dec_func {
1332 public:
1334 double val_real() override;
1335 const char *func_name() const override { return "sin"; }
1336 enum Functype functype() const override { return SIN_FUNC; }
1337};
1338
1339class Item_func_tan final : public Item_dec_func {
1340 public:
1342 double val_real() override;
1343 const char *func_name() const override { return "tan"; }
1344 enum Functype functype() const override { return TAN_FUNC; }
1345};
1346
1347class Item_func_cot final : public Item_dec_func {
1348 public:
1350 double val_real() override;
1351 const char *func_name() const override { return "cot"; }
1352 enum Functype functype() const override { return COT_FUNC; }
1353};
1354
1356 public:
1359 bool resolve_type_inner(THD *thd) override;
1360};
1361
1363 public:
1366 const char *func_name() const override { return "ceiling"; }
1367 longlong int_op() override;
1368 double real_op() override;
1369 my_decimal *decimal_op(my_decimal *) override;
1370 bool check_partition_func_processor(uchar *) override { return false; }
1371 bool check_function_as_value_generator(uchar *) override { return false; }
1372 enum Functype functype() const override { return CEILING_FUNC; }
1373};
1374
1376 public:
1379 const char *func_name() const override { return "floor"; }
1380 longlong int_op() override;
1381 double real_op() override;
1382 my_decimal *decimal_op(my_decimal *) override;
1383 bool check_partition_func_processor(uchar *) override { return false; }
1384 bool check_function_as_value_generator(uchar *) override { return false; }
1385 enum Functype functype() const override { return FLOOR_FUNC; }
1386};
1387
1388/* This handles round and truncate */
1389
1390class Item_func_round final : public Item_func_num1 {
1392
1393 public:
1394 Item_func_round(Item *a, Item *b, bool trunc_arg)
1395 : Item_func_num1(a, b), truncate(trunc_arg) {}
1396 Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1397 : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1398
1399 const char *func_name() const override {
1400 return truncate ? "truncate" : "round";
1401 }
1402 double real_op() override;
1403 longlong int_op() override;
1404 my_decimal *decimal_op(my_decimal *) override;
1405 bool resolve_type(THD *) override;
1406 enum Functype functype() const override {
1408 }
1409};
1410
1411class Item_func_rand final : public Item_real_func {
1413
1415 bool first_eval{true}; // true if val_real() is called 1st time
1416 public:
1418 explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1419
1420 bool itemize(Parse_context *pc, Item **res) override;
1421 double val_real() override;
1422 const char *func_name() const override { return "rand"; }
1423 /**
1424 This function is non-deterministic and hence depends on the
1425 'RAND' pseudo-table.
1426
1427 @retval RAND_TABLE_BIT
1428 */
1430 return RAND_TABLE_BIT;
1431 }
1432 bool fix_fields(THD *thd, Item **ref) override;
1433 bool resolve_type(THD *thd) override;
1434 void cleanup() override {
1435 first_eval = true;
1437 }
1438 bool check_function_as_value_generator(uchar *checker_args) override {
1440 pointer_cast<Check_function_as_value_generator_parameters *>(
1441 checker_args);
1442 func_arg->banned_function_name = func_name();
1443 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1444 (func_arg->source == VGS_CHECK_CONSTRAINT));
1445 }
1446
1447 private:
1448 void seed_random(Item *val);
1449};
1450
1451class Item_func_sign final : public Item_int_func {
1452 public:
1454 const char *func_name() const override { return "sign"; }
1455 longlong val_int() override;
1456 bool resolve_type(THD *thd) override;
1457};
1458
1459// Common base class for the DEGREES and RADIANS functions.
1461 double mul, add;
1462
1463 protected:
1464 Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1465 : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1466
1467 public:
1468 double val_real() override;
1469 bool resolve_type(THD *thd) override;
1470};
1471
1473 public:
1475 : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1476 const char *func_name() const override { return "degrees"; }
1477 enum Functype functype() const override { return DEGREES_FUNC; }
1478};
1479
1481 public:
1483 : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1484 const char *func_name() const override { return "radians"; }
1485 enum Functype functype() const override { return RADIANS_FUNC; }
1486};
1488 public:
1489 Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1490 : Item_func_numhybrid(pos, opt_list),
1491 m_is_least_func(is_least_func),
1493
1494 longlong val_int() override;
1495 double val_real() override;
1496 my_decimal *val_decimal(my_decimal *) override;
1497 longlong int_op() override;
1498 double real_op() override;
1499 my_decimal *decimal_op(my_decimal *) override;
1500 String *str_op(String *) override;
1501 bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1502 bool time_op(MYSQL_TIME *ltime) override;
1504 return MYSQL_TYPE_VARCHAR;
1505 }
1506 bool resolve_type(THD *thd) override;
1507 bool resolve_type_inner(THD *thd) override;
1508 void set_numeric_type() override {}
1509 enum Item_result result_type() const override { return hybrid_type; }
1510
1511 /**
1512 Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1513 return a number in format YYMMDDhhmmss.
1514 */
1515 enum Item_result cast_to_int_type() const override {
1517 }
1518
1519 /// Returns true if arguments to this function should be compared as dates.
1520 bool compare_as_dates() const {
1521 return temporal_item != nullptr &&
1523 }
1524
1525 /// Returns true if at least one of the arguments was of temporal type.
1526 bool has_temporal_arg() const { return temporal_item; }
1527
1528 private:
1529 /// True if LEAST function, false if GREATEST.
1532 /*
1533 Used for determining whether one of the arguments is of temporal type and
1534 for converting arguments to a common output format if arguments are
1535 compared as dates and result type is character string. For example,
1536 LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1537 '95-05-05'.
1538 */
1540 /**
1541 Compare arguments as datetime values.
1542
1543 @param value Pointer to which the datetime value of the winning argument
1544 is written.
1545
1546 @return true if error, false otherwise.
1547 */
1548 bool cmp_datetimes(longlong *value);
1549
1550 /**
1551 Compare arguments as time values.
1552
1553 @param value Pointer to which the time value of the winning argument is
1554 written.
1555
1556 @return true if error, false otherwise.
1557 */
1558 bool cmp_times(longlong *value);
1559};
1560
1561class Item_func_min final : public Item_func_min_max {
1562 public:
1564 : Item_func_min_max(pos, opt_list, true) {}
1565 const char *func_name() const override { return "least"; }
1566 enum Functype functype() const override { return LEAST_FUNC; }
1567};
1568
1569class Item_func_max final : public Item_func_min_max {
1570 public:
1572 : Item_func_min_max(pos, opt_list, false) {}
1573 const char *func_name() const override { return "greatest"; }
1574 enum Functype functype() const override { return GREATEST_FUNC; }
1575};
1576
1577/**
1578 A wrapper Item that normally returns its parameter, but becomes NULL when
1579 processing rows for rollup. Rollup is implemented by AggregateIterator, and
1580 works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1581 where only one group level is active, and so on. E.g., for a query with GROUP
1582 BY a,b, the rows will look like this:
1583
1584 a b rollup level
1585 1 1 2
1586 1 2 2
1587 1 NULL 1
1588 2 1 2
1589 2 NULL 1
1590 NULL NULL 0
1591
1592 Each rollup group item has a minimum level for when it becomes NULL. In the
1593 example above, a would have minimum level 0 and b would have minimum level 1.
1594 For simplicity, the JOIN carries a list of all rollup group items, and they
1595 are being given the current rollup level when it changes. A rollup level of
1596 INT_MAX essentially always disables rollup, which is useful when there are
1597 leftover group items in places that are not relevant for rollup
1598 (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1599 tables that are created before grouping, which should then effectively be
1600 disabled).
1601 */
1602class Item_rollup_group_item final : public Item_func {
1603 public:
1608 // We're going to replace inner_item in the SELECT list, so copy its hidden
1609 // status. (We could have done this in the caller, but it fits naturally in
1610 // with all the other copying done here.)
1612 set_nullable(true);
1614 }
1615 double val_real() override;
1616 longlong val_int() override;
1617 String *val_str(String *str) override;
1619 bool val_json(Json_wrapper *result) override;
1620 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1621 bool get_time(MYSQL_TIME *ltime) override;
1622 const char *func_name() const override { return "rollup_group_item"; }
1623 table_map used_tables() const override {
1624 /*
1625 If underlying item is non-constant, return its used_tables value.
1626 Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1627 */
1628 return args[0]->const_for_execution()
1629 ? (args[0]->used_tables() | RAND_TABLE_BIT)
1630 : args[0]->used_tables();
1631 }
1632 Item_result result_type() const override { return args[0]->result_type(); }
1633 bool resolve_type(THD *) override {
1634 // needn't handle dynamic parameter as its const_item() is false.
1636
1637 // The item could be a NULL constant.
1638 null_value = args[0]->is_null();
1639 return false;
1640 }
1641 Item *inner_item() const { return args[0]; }
1642 bool rollup_null() const {
1644 }
1645 enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1646 void print(const THD *thd, String *str,
1647 enum_query_type query_type) const override;
1648 bool eq(const Item *item, bool binary_cmp) const override;
1649
1650 // Used by AggregateIterator.
1652
1653 // Used when cloning the item only.
1654 int min_rollup_level() const { return m_min_rollup_level; }
1655
1656 private:
1659};
1660
1663
1664 public:
1666 longlong val_int() override;
1667 const char *func_name() const override { return "length"; }
1668 bool resolve_type(THD *thd) override {
1669 if (param_type_is_default(thd, 0, 1)) return true;
1670 max_length = 10;
1671 return false;
1672 }
1673};
1674
1676 public:
1678 longlong val_int() override {
1679 assert(fixed);
1680 return Item_func_length::val_int() * 8;
1681 }
1682 const char *func_name() const override { return "bit_length"; }
1683};
1684
1687
1688 public:
1691 longlong val_int() override;
1692 const char *func_name() const override { return "char_length"; }
1693 bool resolve_type(THD *thd) override {
1694 max_length = 10;
1695 return Item_int_func::resolve_type(thd);
1696 }
1697};
1698
1700 public:
1702 null_on_null = false;
1703 }
1704 longlong val_int() override;
1705 const char *func_name() const override { return "coercibility"; }
1706 bool resolve_type(THD *thd) override {
1707 max_length = 10;
1708 set_nullable(false);
1709 return Item_int_func::resolve_type(thd);
1710 }
1711};
1712
1715
1716 public:
1719 : Item_int_func(pos, a, b) {}
1720 Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1721 : Item_int_func(pos, a, b, c) {}
1722
1723 const char *func_name() const override { return "locate"; }
1724 longlong val_int() override;
1725 bool resolve_type(THD *thd) override;
1726 void print(const THD *thd, String *str,
1727 enum_query_type query_type) const override;
1728};
1729
1730class Item_func_instr final : public Item_func_locate {
1731 public:
1733 : Item_func_locate(pos, a, b) {}
1734
1735 const char *func_name() const override { return "instr"; }
1736};
1737
1739 public:
1741 : Item_int_func(pos, a) {}
1742 longlong val_int() override;
1743 const char *func_name() const override {
1744 return "validate_password_strength";
1745 }
1746 bool resolve_type(THD *thd) override {
1747 max_length = 10;
1748 set_nullable(true);
1749 return Item_int_func::resolve_type(thd);
1750 }
1751};
1752
1753class Item_func_field final : public Item_int_func {
1756
1757 public:
1759 : Item_int_func(pos, opt_list) {}
1760 longlong val_int() override;
1761 const char *func_name() const override { return "field"; }
1762 bool resolve_type(THD *thd) override;
1763};
1764
1765class Item_func_ascii final : public Item_int_func {
1767
1768 public:
1770 longlong val_int() override;
1771 const char *func_name() const override { return "ascii"; }
1772 bool resolve_type(THD *thd) override {
1773 max_length = 3;
1774 return Item_int_func::resolve_type(thd);
1775 }
1776};
1777
1778class Item_func_ord final : public Item_int_func {
1780
1781 public:
1783 longlong val_int() override;
1784 const char *func_name() const override { return "ord"; }
1785};
1786
1789 /*
1790 if m_enum_value is non-zero, it indicates the index of the value of
1791 argument 0 in the set in argument 1, given that argument 0 is
1792 a constant value and argument 1 is a field of type SET.
1793 */
1796
1797 public:
1799 : Item_int_func(pos, a, b) {}
1800 longlong val_int() override;
1801 const char *func_name() const override { return "find_in_set"; }
1802 bool resolve_type(THD *) override;
1803 const CHARSET_INFO *compare_collation() const override {
1804 return cmp_collation.collation;
1805 }
1806};
1807
1808/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1809
1810class Item_func_bit : public Item_func {
1811 protected:
1812 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1814 /// Buffer storing the determined value
1816 /**
1817 @returns true if the second argument should be of binary type for the
1818 result to be of binary type.
1819 */
1821
1822 public:
1823 Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1824 Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1825
1826 bool resolve_type(THD *) override;
1827 enum Item_result result_type() const override { return hybrid_type; }
1828
1829 longlong val_int() override;
1830 String *val_str(String *str) override;
1831 double val_real() override;
1832 my_decimal *val_decimal(my_decimal *decimal_value) override;
1833
1834 void print(const THD *thd, String *str,
1835 enum_query_type query_type) const override {
1836 print_op(thd, str, query_type);
1837 }
1838 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1839 if (hybrid_type == INT_RESULT)
1840 return get_date_from_int(ltime, fuzzydate);
1841 else
1842 return get_date_from_string(ltime, fuzzydate);
1843 }
1844 bool get_time(MYSQL_TIME *ltime) override {
1845 if (hybrid_type == INT_RESULT)
1846 return get_time_from_int(ltime);
1847 else
1848 return get_time_from_string(ltime);
1849 }
1850
1851 private:
1852 /**
1853 @brief Performs the operation on integers to produce a result of type
1854 INT_RESULT.
1855 @return The result of the operation.
1856 */
1857 virtual longlong int_op() = 0;
1858
1859 /**
1860 @brief Performs the operation on binary strings to produce a result of
1861 type STRING_RESULT.
1862 @return The result of the operation.
1863 */
1864 virtual String *str_op(String *) = 0;
1865};
1866
1867/**
1868 Base class for all the bit functions that work with two binary
1869 arguments: '&', '|', '^'.
1870*/
1871
1873 protected:
1875 return true;
1876 }
1877 template <class Char_func, class Int_func>
1878 String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1879 template <class Int_func>
1880 longlong eval_int_op(Int_func int_func);
1881
1882 public:
1884 : Item_func_bit(pos, a, b) {}
1885};
1886
1888 public:
1890 : Item_func_bit_two_param(pos, a, b) {}
1891 const char *func_name() const override { return "|"; }
1892
1893 private:
1894 longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1895 String *str_op(String *str) override {
1896 return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1897 }
1898};
1899
1901 public:
1903 : Item_func_bit_two_param(pos, a, b) {}
1904 const char *func_name() const override { return "&"; }
1905
1906 private:
1907 longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1908 String *str_op(String *str) override {
1909 return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1910 }
1911};
1912
1914 public:
1916 : Item_func_bit_two_param(pos, a, b) {}
1917 const char *func_name() const override { return "^"; }
1918
1919 private:
1920 longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1921 String *str_op(String *str) override {
1922 return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1923 }
1924};
1925
1927 public:
1929 longlong val_int() override;
1930 const char *func_name() const override { return "bit_count"; }
1931 bool resolve_type(THD *thd) override {
1932 // Default: binary string; reprepare if integer
1933 if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
1934 args[0]->propagate_type(
1936 return true;
1938 return false;
1939 }
1940};
1941
1943 protected:
1945 return false;
1946 }
1947 template <bool to_left>
1949 template <bool to_left>
1951
1952 public:
1954 : Item_func_bit(pos, a, b) {}
1955};
1956
1958 public:
1960 : Item_func_shift(pos, a, b) {}
1961 const char *func_name() const override { return "<<"; }
1962
1963 private:
1964 longlong int_op() override { return eval_int_op<true>(); }
1965 String *str_op(String *str) override { return eval_str_op<true>(str); }
1966};
1967
1969 public:
1971 : Item_func_shift(pos, a, b) {}
1972 const char *func_name() const override { return ">>"; }
1973
1974 private:
1975 longlong int_op() override { return eval_int_op<false>(); }
1976 String *str_op(String *str) override { return eval_str_op<false>(str); }
1977};
1978
1979class Item_func_bit_neg final : public Item_func_bit {
1980 protected:
1982 return false;
1983 }
1984
1985 public:
1987 const char *func_name() const override { return "~"; }
1988 void print(const THD *thd, String *str,
1989 enum_query_type query_type) const override {
1990 Item_func::print(thd, str, query_type);
1991 }
1992
1993 private:
1994 longlong int_op() override;
1995 String *str_op(String *str) override;
1996};
1997
2000
2001 public:
2005
2006 bool itemize(Parse_context *pc, Item **res) override;
2007 longlong val_int() override;
2008 const char *func_name() const override { return "last_insert_id"; }
2009 bool resolve_type(THD *thd) override {
2010 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2011 unsigned_flag = true;
2012 return false;
2013 }
2014 bool check_function_as_value_generator(uchar *checker_args) override {
2016 pointer_cast<Check_function_as_value_generator_parameters *>(
2017 checker_args);
2018 func_arg->banned_function_name = func_name();
2019 return true;
2020 }
2021};
2022
2025
2026 public:
2027 Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
2028 : Item_int_func(pos, count_expr, expr) {}
2029
2030 /// Ensure that "benchmark()" is never optimized away
2032 return RAND_TABLE_BIT;
2033 }
2034
2035 bool itemize(Parse_context *pc, Item **res) override;
2036 longlong val_int() override;
2037 const char *func_name() const override { return "benchmark"; }
2038 bool resolve_type(THD *thd) override {
2039 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2040 if (param_type_is_default(thd, 1, 2)) return true;
2041 max_length = 1;
2042 set_nullable(true);
2043 return false;
2044 }
2045 void print(const THD *thd, String *str,
2046 enum_query_type query_type) const override;
2047 bool check_function_as_value_generator(uchar *checker_args) override {
2049 pointer_cast<Check_function_as_value_generator_parameters *>(
2050 checker_args);
2051 func_arg->banned_function_name = func_name();
2052 return true;
2053 }
2054};
2055
2058
2059class Item_func_sleep final : public Item_int_func {
2061
2062 public:
2064
2065 bool itemize(Parse_context *pc, Item **res) override;
2066 const char *func_name() const override { return "sleep"; }
2067 /**
2068 This function is non-deterministic and hence depends on the
2069 'RAND' pseudo-table.
2070
2071 @retval RAND_TABLE_BIT
2072 */
2074 return RAND_TABLE_BIT;
2075 }
2076 bool check_function_as_value_generator(uchar *checker_args) override {
2078 pointer_cast<Check_function_as_value_generator_parameters *>(
2079 checker_args);
2080 func_arg->banned_function_name = func_name();
2081 return true;
2082 }
2083 bool resolve_type(THD *thd) override {
2084 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2085 return Item_int_func::resolve_type(thd);
2086 }
2087 longlong val_int() override;
2088};
2089
2090class Item_udf_func : public Item_func {
2092
2093 protected:
2095 bool is_expensive_processor(uchar *) override { return true; }
2096
2097 public:
2098 Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2099 : Item_func(pos, opt_list), udf(udf_arg) {
2100 null_on_null = false;
2101 }
2102 ~Item_udf_func() override = default;
2103
2104 bool itemize(Parse_context *pc, Item **res) override;
2105 const char *func_name() const override { return udf.name(); }
2106 enum Functype functype() const override { return UDF_FUNC; }
2107 bool fix_fields(THD *thd, Item **ref) override;
2108 void update_used_tables() override {
2109 /*
2110 TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
2111 not.
2112 Currently UDF_INIT has a member (const_item) that is an in/out
2113 parameter to the init() call.
2114 The code in udf_handler::fix_fields also duplicates the arguments
2115 handling code in Item_func::fix_fields().
2116
2117 The lack of information if a UDF is deterministic makes writing
2118 a correct update_used_tables() for UDFs impossible.
2119 One solution to this would be :
2120 - Add a is_deterministic member of UDF_INIT
2121 - (optionally) deprecate the const_item member of UDF_INIT
2122 - Take away the duplicate code from udf_handler::fix_fields() and
2123 make Item_udf_func call Item_func::fix_fields() to process its
2124 arguments as for any other function.
2125 - Store the deterministic flag returned by <udf>_init into the
2126 udf_handler.
2127 - Don't implement Item_udf_func::fix_fields, implement
2128 Item_udf_func::resolve_type() instead (similar to non-UDF functions).
2129 - Override Item_func::update_used_tables to call
2130 Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
2131 result of Item_func::update_used_tables() if the UDF is
2132 non-deterministic.
2133 - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
2134 better describe its usage.
2135
2136 The above would require a change of the UDF API.
2137 Until that change is done here's how the current code works:
2138 We call Item_func::update_used_tables() only when we know that
2139 the function depends on real non-const tables and is deterministic.
2140 This can be done only because we know that the optimizer will
2141 call update_used_tables() only when there's possibly a new const
2142 table. So update_used_tables() can only make a Item_func more
2143 constant than it is currently.
2144 That's why we don't need to do anything if a function is guaranteed
2145 to return non-constant (it's non-deterministic) or is already a
2146 const.
2147 */
2151
2153 assert(!null_on_null); // no need to update not_null_tables_cache
2154 }
2155 void cleanup() override;
2156 Item_result result_type() const override { return udf.result_type(); }
2157 bool is_expensive() override { return true; }
2158 void print(const THD *thd, String *str,
2159 enum_query_type query_type) const override;
2160
2161 bool check_function_as_value_generator(uchar *checker_args) override {
2163 pointer_cast<Check_function_as_value_generator_parameters *>(
2164 checker_args);
2165 func_arg->banned_function_name = func_name();
2166 return true;
2167 }
2168
2169 protected:
2170 bool may_have_named_parameters() const override { return true; }
2171};
2172
2174 public:
2175 Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2176 : Item_udf_func(pos, udf_arg, opt_list) {}
2177 longlong val_int() override {
2178 assert(fixed == 1);
2180 }
2181 my_decimal *val_decimal(my_decimal *dec_buf) override {
2182 double res = val_real();
2183 if (null_value) return nullptr;
2184 double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2185 return dec_buf;
2186 }
2187 double val_real() override;
2188 String *val_str(String *str) override;
2189 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2190 return get_date_from_real(ltime, fuzzydate);
2191 }
2192 bool get_time(MYSQL_TIME *ltime) override {
2193 return get_time_from_real(ltime);
2194 }
2195 bool resolve_type(THD *) override {
2197 fix_num_length_and_dec(); // @todo - needed?
2198 return false;
2199 }
2200};
2201
2202class Item_func_udf_int final : public Item_udf_func {
2203 public:
2204 Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2205 : Item_udf_func(pos, udf_arg, opt_list) {}
2206 longlong val_int() override;
2207 double val_real() override {
2208 return static_cast<double>(Item_func_udf_int::val_int());
2209 }
2210 String *val_str(String *str) override;
2211 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2212 return get_date_from_int(ltime, fuzzydate);
2213 }
2214 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2215 enum Item_result result_type() const override { return INT_RESULT; }
2216 bool resolve_type(THD *) override {
2218 return false;
2219 }
2220};
2221
2223 public:
2225 PT_item_list *opt_list)
2226 : Item_udf_func(pos, udf_arg, opt_list) {}
2227 longlong val_int() override;
2228 double val_real() override;
2229 my_decimal *val_decimal(my_decimal *) override;
2230 String *val_str(String *str) override;
2231 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2232 return get_date_from_decimal(ltime, fuzzydate);
2233 }
2234 bool get_time(MYSQL_TIME *ltime) override {
2235 return get_time_from_decimal(ltime);
2236 }
2237 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2238 bool resolve_type(THD *thd) override;
2239};
2240
2242 public:
2243 Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2244 : Item_udf_func(pos, udf_arg, opt_list) {}
2245
2246 String *val_str(String *) override;
2247 double val_real() override {
2248 int err_not_used;
2249 const char *end_not_used;
2250 String *res;
2251 res = val_str(&str_value);
2252 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2253 &end_not_used, &err_not_used)
2254 : 0.0;
2255 }
2256 longlong val_int() override {
2257 int err_not_used;
2258 String *res;
2259 res = val_str(&str_value);
2260 return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2261 nullptr, &err_not_used)
2262 : (longlong)0;
2263 }
2264 my_decimal *val_decimal(my_decimal *dec_buf) override {
2265 String *res = val_str(&str_value);
2266 if (!res) return nullptr;
2267 str2my_decimal(E_DEC_FATAL_ERROR, res->ptr(), res->length(), res->charset(),
2268 dec_buf);
2269 return dec_buf;
2270 }
2271 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2272 return get_date_from_string(ltime, fuzzydate);
2273 }
2274 bool get_time(MYSQL_TIME *ltime) override {
2275 return get_time_from_string(ltime);
2276 }
2277 enum Item_result result_type() const override { return STRING_RESULT; }
2278 bool resolve_type(THD *thd) override;
2279};
2280
2281void mysql_ull_cleanup(THD *thd);
2283
2284class Item_func_get_lock final : public Item_int_func {
2286
2288
2289 public:
2291 : Item_int_func(pos, a, b) {}
2292
2293 bool itemize(Parse_context *pc, Item **res) override;
2294 longlong val_int() override;
2295 const char *func_name() const override { return "get_lock"; }
2296 bool resolve_type(THD *thd) override {
2297 if (param_type_is_default(thd, 0, 1)) return true;
2298 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
2299 max_length = 1;
2300 set_nullable(true);
2301 return false;
2302 }
2303 bool is_non_const_over_literals(uchar *) override { return true; }
2304 bool check_function_as_value_generator(uchar *checker_args) override {
2306 pointer_cast<Check_function_as_value_generator_parameters *>(
2307 checker_args);
2308 func_arg->banned_function_name = func_name();
2309 return true;
2310 }
2311};
2312
2315
2317
2318 public:
2320 bool itemize(Parse_context *pc, Item **res) override;
2321
2322 longlong val_int() override;
2323 const char *func_name() const override { return "release_lock"; }
2324 bool resolve_type(THD *thd) override {
2325 if (param_type_is_default(thd, 0, 1)) return true;
2326 max_length = 1;
2327 set_nullable(true);
2328 return false;
2329 }
2330 bool is_non_const_over_literals(uchar *) override { return true; }
2331 bool check_function_as_value_generator(uchar *checker_args) override {
2333 pointer_cast<Check_function_as_value_generator_parameters *>(
2334 checker_args);
2335 func_arg->banned_function_name = func_name();
2336 return true;
2337 }
2338};
2339
2342
2343 public:
2345 bool itemize(Parse_context *pc, Item **res) override;
2346
2347 longlong val_int() override;
2348 const char *func_name() const override { return "release_all_locks"; }
2349 bool resolve_type(THD *) override {
2350 unsigned_flag = true;
2351 return false;
2352 }
2353 bool is_non_const_over_literals(uchar *) override { return true; }
2354 bool check_function_as_value_generator(uchar *checker_args) override {
2356 pointer_cast<Check_function_as_value_generator_parameters *>(
2357 checker_args);
2358 func_arg->banned_function_name = func_name();
2359 return true;
2360 }
2361};
2362
2363/* replication functions */
2364
2368
2369 public:
2371 : Item_int_func(pos, a, b) {}
2373 : Item_int_func(pos, a, b, c) {}
2375 : Item_int_func(pos, a, b, c, d) {}
2376
2377 bool itemize(Parse_context *pc, Item **res) override;
2378 longlong val_int() override;
2379 const char *func_name() const override { return "source_pos_wait"; }
2380 bool resolve_type(THD *thd) override {
2381 if (param_type_is_default(thd, 0, 1)) return true;
2382 if (param_type_is_default(thd, 1, 3, MYSQL_TYPE_LONGLONG)) return true;
2383 if (param_type_is_default(thd, 3, 4)) return true;
2384 max_length = 21;
2385 set_nullable(true);
2386 return false;
2387 }
2388 bool check_function_as_value_generator(uchar *checker_args) override {
2390 pointer_cast<Check_function_as_value_generator_parameters *>(
2391 checker_args);
2392 func_arg->banned_function_name = func_name();
2393 return true;
2394 }
2395};
2396
2398 public:
2400 : Item_source_pos_wait(pos, a, b) {}
2402 : Item_source_pos_wait(pos, a, b, c) {}
2404 : Item_source_pos_wait(pos, a, b, c, d) {}
2405 longlong val_int() override;
2406};
2407
2408/**
2409 This class is used for implementing the new wait_for_executed_gtid_set
2410 function and the functions related to them. This new function is independent
2411 of the slave threads.
2412*/
2415
2417
2418 public:
2420 : Item_int_func(pos, a) {}
2422 : Item_int_func(pos, a, b) {}
2423
2424 bool itemize(Parse_context *pc, Item **res) override;
2425 longlong val_int() override;
2426 const char *func_name() const override {
2427 return "wait_for_executed_gtid_set";
2428 }
2429 bool resolve_type(THD *thd) override {
2430 if (param_type_is_default(thd, 0, 1)) return true;
2431 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2432 set_nullable(true);
2433 return false;
2434 }
2435};
2436
2439
2441
2442 public:
2444 Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b);
2445 Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b, Item *c);
2446
2447 bool itemize(Parse_context *pc, Item **res) override;
2448 longlong val_int() override;
2449 const char *func_name() const override {
2450 return "wait_until_sql_thread_after_gtids";
2451 }
2452 bool resolve_type(THD *thd) override {
2453 if (param_type_is_default(thd, 0, 1)) return true;
2454 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2455 if (param_type_is_default(thd, 2, 3)) return true;
2456 set_nullable(true);
2457 return false;
2458 }
2459};
2460
2464
2465 public:
2467 : Item_int_func(pos, a, b) {}
2468 longlong val_int() override;
2469 const char *func_name() const override { return "gtid_subset"; }
2470 bool resolve_type(THD *thd) override {
2471 if (param_type_is_default(thd, 0, ~0U)) return true;
2472 return false;
2473 }
2474 bool is_bool_func() const override { return true; }
2475};
2476
2477/**
2478 Internal functions used by INFORMATION_SCHEMA implementation to check
2479 if user have access to given database/table/column.
2480*/
2481
2483 public:
2485 : Item_int_func(pos, a) {}
2486 longlong val_int() override;
2487 const char *func_name() const override { return "can_access_database"; }
2488 bool resolve_type(THD *) override {
2489 set_nullable(true);
2490 return false;
2491 }
2492};
2493
2495 public:
2497 : Item_int_func(pos, a, b) {}
2498 longlong val_int() override;
2499 const char *func_name() const override { return "can_access_table"; }
2500 bool resolve_type(THD *) override {
2501 set_nullable(true);
2502 return false;
2503 }
2504};
2505
2507 public:
2509 : Item_int_func(pos, a, b) {}
2510 longlong val_int() override;
2511 const char *func_name() const override { return "can_access_user"; }
2512 bool resolve_type(THD *) override {
2513 set_nullable(true);
2514 return false;
2515 }
2516};
2517
2519 public:
2521 : Item_int_func(pos, a, b) {}
2522 longlong val_int() override;
2523 const char *func_name() const override { return "can_access_trigger"; }
2524 bool resolve_type(THD *) override {
2525 max_length = 4;
2526 set_nullable(true);
2527 return false;
2528 }
2529};
2530
2532 public:
2534 : Item_int_func(pos, list) {}
2535 longlong val_int() override;
2536 const char *func_name() const override { return "can_access_routine"; }
2537 bool resolve_type(THD *) override {
2538 max_length = 4;
2539 set_nullable(true);
2540 return false;
2541 }
2542};
2543
2545 public:
2547 longlong val_int() override;
2548 const char *func_name() const override { return "can_access_event"; }
2549 bool resolve_type(THD *) override {
2550 set_nullable(true);
2551 return false;
2552 }
2553};
2554
2556 public:
2558 : Item_int_func(pos, a) {}
2559 longlong val_int() override;
2560 const char *func_name() const override { return "can_access_resource_group"; }
2561 bool resolve_type(THD *) override {
2562 max_length = 1; // Function can return 0 or 1.
2563 set_nullable(true);
2564 return false;
2565 }
2566};
2567
2569 public:
2571 : Item_int_func(pos, a, b, c, d) {}
2572 longlong val_int() override;
2573 const char *func_name() const override { return "can_access_view"; }
2574 bool resolve_type(THD *) override {
2575 set_nullable(true);
2576 return false;
2577 }
2578};
2579
2581 public:
2583 : Item_int_func(pos, a, b, c) {}
2584 longlong val_int() override;
2585 const char *func_name() const override { return "can_access_column"; }
2586 bool resolve_type(THD *) override {
2587 set_nullable(true);
2588 return false;
2589 }
2590};
2591
2593 public:
2595 : Item_int_func(pos, a) {}
2597 : Item_int_func(pos, a, b) {}
2599 : Item_int_func(pos, a, b, c) {}
2600 longlong val_int() override;
2601 const char *func_name() const override { return "is_visible_dd_object"; }
2602 bool resolve_type(THD *) override {
2603 max_length = 1;
2604 set_nullable(true);
2605 return false;
2606 }
2607};
2608
2610 public:
2612 : Item_int_func(pos, list) {}
2613 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2614 longlong val_int() override;
2615 const char *func_name() const override { return "internal_table_rows"; }
2616 bool resolve_type(THD *) override {
2617 set_nullable(true);
2618 unsigned_flag = true;
2619 null_on_null = false;
2620 return false;
2621 }
2622};
2623
2625 public:
2627 : Item_int_func(pos, list) {}
2628 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2629 longlong val_int() override;
2630 const char *func_name() const override { return "internal_avg_row_length"; }
2631 bool resolve_type(THD *) override {
2632 set_nullable(true);
2633 unsigned_flag = true;
2634 null_on_null = false;
2635 return false;
2636 }
2637};
2638
2640 public:
2642 : Item_int_func(pos, list) {}
2643 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2644 longlong val_int() override;
2645 const char *func_name() const override { return "internal_data_length"; }
2646 bool resolve_type(THD *) override {
2647 set_nullable(true);
2648 unsigned_flag = true;
2649 null_on_null = false;
2650 return false;
2651 }
2652};
2653
2655 public:
2657 : Item_int_func(pos, list) {}
2658 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2659 longlong val_int() override;
2660 const char *func_name() const override { return "internal_max_data_length"; }
2661 bool resolve_type(THD *) override {
2662 set_nullable(true);
2663 unsigned_flag = true;
2664 null_on_null = false;
2665 return false;
2666 }
2667};
2668
2670 public:
2672 : Item_int_func(pos, list) {}
2673 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2674 longlong val_int() override;
2675 const char *func_name() const override { return "internal_index_length"; }
2676 bool resolve_type(THD *) override {
2677 set_nullable(true);
2678 unsigned_flag = true;
2679 null_on_null = false;
2680 return false;
2681 }
2682};
2683
2685 public:
2687 : Item_int_func(pos, list) {}
2688 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2689 longlong val_int() override;
2690 const char *func_name() const override { return "internal_data_free"; }
2691 bool resolve_type(THD *) override {
2692 set_nullable(true);
2693 unsigned_flag = true;
2694 null_on_null = false;
2695 return false;
2696 }
2697};
2698
2700 public:
2702 : Item_int_func(pos, list) {}
2703 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2704 longlong val_int() override;
2705 const char *func_name() const override { return "internal_auto_increment"; }
2706 bool resolve_type(THD *) override {
2707 set_nullable(true);
2708 unsigned_flag = true;
2709 null_on_null = false;
2710 return false;
2711 }
2712};
2713
2715 public:
2717 : Item_int_func(pos, list) {}
2718 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2719 longlong val_int() override;
2720 const char *func_name() const override { return "internal_checksum"; }
2721 bool resolve_type(THD *) override {
2722 set_nullable(true);
2723 null_on_null = false;
2724 return false;
2725 }
2726};
2727
2729 public:
2731 : Item_int_func(pos, a) {}
2732 longlong val_int() override;
2733 const char *func_name() const override { return "internal_keys_disabled"; }
2734 bool resolve_type(THD *) override {
2735 set_nullable(false);
2736 null_on_null = false;
2737 return false;
2738 }
2739};
2740
2742 public:
2745 : Item_int_func(pos, list) {}
2746 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2747 longlong val_int() override;
2748 const char *func_name() const override {
2749 return "internal_index_column_cardinality";
2750 }
2751 bool resolve_type(THD *) override {
2752 set_nullable(true);
2753 null_on_null = false;
2754 return false;
2755 }
2756};
2757
2759 public:
2761 Item *d)
2762 : Item_int_func(pos, a, b, c, d) {}
2763 longlong val_int() override;
2764 const char *func_name() const override { return "internal_dd_char_length"; }
2765 bool resolve_type(THD *) override {
2766 set_nullable(true);
2767 null_on_null = false;
2768 return false;
2769 }
2770};
2771
2773 : public Item_int_func {
2774 public:
2777 : Item_int_func(pos, list) {}
2778 longlong val_int() override;
2779 const char *func_name() const override {
2780 return "internal_get_view_warning_or_error";
2781 }
2782 bool resolve_type(THD *) override {
2783 max_length = 1;
2784 set_nullable(false);
2785 null_on_null = false;
2786 return false;
2787 }
2788};
2789
2791 public:
2793 : Item_int_func(pos, list) {}
2794 longlong val_int() override;
2795 bool resolve_type(THD *) override {
2796 set_nullable(true);
2797 null_on_null = false;
2798 return false;
2799 }
2800 const char *func_name() const override {
2801 return "get_dd_index_sub_part_length";
2802 }
2803};
2804
2806 public:
2808 Item *d)
2809 : Item_int_func(pos, a, b, c, d) {}
2810 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2811 longlong val_int() override;
2812 const char *func_name() const override { return "internal_tablespace_id"; }
2813 bool resolve_type(THD *) override {
2814 set_nullable(true);
2815 null_on_null = false;
2816 return false;
2817 }
2818};
2819
2821 : public Item_int_func {
2822 public:
2824 Item *b, Item *c, Item *d)
2825 : Item_int_func(pos, a, b, c, d) {}
2826
2827 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2828 longlong val_int() override;
2829
2830 const char *func_name() const override {
2831 return "internal_tablespace_logfile_group_number";
2832 }
2833
2834 bool resolve_type(THD *) override {
2835 set_nullable(true);
2836 null_on_null = false;
2837 return false;
2838 }
2839};
2840
2842 public:
2844 Item *c, Item *d)
2845 : Item_int_func(pos, a, b, c, d) {}
2846
2847 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2848 longlong val_int() override;
2849
2850 const char *func_name() const override {
2851 return "internal_tablespace_free_extents";
2852 }
2853
2854 bool resolve_type(THD *) override {
2855 set_nullable(true);
2856 null_on_null = false;
2857 return false;
2858 }
2859};
2860
2862 public:
2864 Item *c, Item *d)
2865 : Item_int_func(pos, a, b, c, d) {}
2866
2867 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2868 longlong val_int() override;
2869
2870 const char *func_name() const override {
2871 return "internal_tablespace_total_extents";
2872 }
2873
2874 bool resolve_type(THD *) override {
2875 set_nullable(true);
2876 null_on_null = false;
2877 return false;
2878 }
2879};
2880
2882 public:
2884 Item *c, Item *d)
2885 : Item_int_func(pos, a, b, c, d) {}
2886
2887 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2888 longlong val_int() override;
2889
2890 const char *func_name() const override {
2891 return "internal_tablespace_extent_size";
2892 }
2893
2894 bool resolve_type(THD *) override {
2895 set_nullable(true);
2896 null_on_null = false;
2897 return false;
2898 }
2899};
2900
2902 public:
2904 Item *c, Item *d)
2905 : Item_int_func(pos, a, b, c, d) {}
2906
2907 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2908 longlong val_int() override;
2909
2910 const char *func_name() const override {
2911 return "internal_tablespace_initial_size";
2912 }
2913
2914 bool resolve_type(THD *) override {
2915 set_nullable(true);
2916 null_on_null = false;
2917 return false;
2918 }
2919};
2920
2922 public:
2924 Item *c, Item *d)
2925 : Item_int_func(pos, a, b, c, d) {}
2926
2927 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2928 longlong val_int() override;
2929
2930 const char *func_name() const override {
2931 return "internal_tablespace_maximum_size";
2932 }
2933
2934 bool resolve_type(THD *) override {
2935 set_nullable(true);
2936 null_on_null = false;
2937 return false;
2938 }
2939};
2940
2942 public:
2944 Item *b, Item *c, Item *d)
2945 : Item_int_func(pos, a, b, c, d) {}
2946
2947 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2948 longlong val_int() override;
2949
2950 const char *func_name() const override {
2951 return "internal_tablespace_autoextend_size";
2952 }
2953
2954 bool resolve_type(THD *) override {
2955 set_nullable(true);
2956 null_on_null = false;
2957 return false;
2958 }
2959};
2960
2962 public:
2964 Item *c, Item *d)
2965 : Item_int_func(pos, a, b, c, d) {}
2966
2967 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2968 longlong val_int() override;
2969
2970 const char *func_name() const override {
2971 return "internal_tablespace_version";
2972 }
2973
2974 bool resolve_type(THD *) override {
2975 set_nullable(true);
2976 null_on_null = false;
2977 return false;
2978 }
2979};
2980
2982 public:
2984 Item *c, Item *d)
2985 : Item_int_func(pos, a, b, c, d) {}
2986
2987 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2988 longlong val_int() override;
2989
2990 const char *func_name() const override {
2991 return "internal_tablespace_data_free";
2992 }
2993
2994 bool resolve_type(THD *) override {
2995 set_nullable(true);
2996 null_on_null = false;
2997 return false;
2998 }
2999};
3000
3001/**
3002 Common class for:
3003 Item_func_get_system_var
3004 Item_func_get_user_var
3005 Item_func_set_user_var
3006*/
3007class Item_var_func : public Item_func {
3008 public:
3010 explicit Item_var_func(const POS &pos) : Item_func(pos) {}
3011
3012 Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
3013
3015 Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
3016
3017 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
3018 return get_date_from_non_temporal(ltime, fuzzydate);
3019 }
3020 bool get_time(MYSQL_TIME *ltime) override {
3021 return get_time_from_non_temporal(ltime);
3022 }
3023 bool check_function_as_value_generator(uchar *checker_args) override {
3025 pointer_cast<Check_function_as_value_generator_parameters *>(
3026 checker_args);
3027 func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
3028 ? ER_CHECK_CONSTRAINT_VARIABLES
3029 : ER_DEFAULT_VAL_GENERATED_VARIABLES;
3030 return true;
3031 }
3032};
3033
3034/* Handling of user definable variables */
3035
3036// this is needed for user_vars hash
3039 m_ptr = nullptr;
3040 m_length = 0;
3041 }
3042 void set_value(char *value, size_t length) {
3043 m_ptr = value;
3044 m_length = length;
3045 }
3046
3047 /**
3048 Position inside a user_var_entry where small values are stored:
3049 double values, longlong values and string values with length
3050 up to extra_size (should be 8 bytes on all platforms).
3051 String values with length longer than 8 are stored in a separate
3052 memory buffer, which is allocated when needed using the method realloc().
3053 */
3055 return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
3056 }
3057
3058 /**
3059 Position inside a user_var_entry where a null-terminates array
3060 of characters representing the variable name is stored.
3061 */
3063
3064 /**
3065 Initialize m_ptr to the internal buffer (if the value is small enough),
3066 or allocate a separate buffer.
3067 @param length - length of the value to be stored.
3068 */
3069 bool mem_realloc(size_t length);
3070
3071 /**
3072 Check if m_ptr point to an external buffer previously alloced by realloc().
3073 @retval true - an external buffer is alloced.
3074 @retval false - m_ptr is null, or points to the internal buffer.
3075 */
3076 bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
3077
3078 /**
3079 Free the external value buffer, if it's allocated.
3080 */
3081 void free_value() {
3082 if (alloced()) my_free(m_ptr);
3083 }
3084
3085 /**
3086 Copy the array of characters from the given name into the internal
3087 name buffer and initialize entry_name to point to it.
3088 */
3090 name.strcpy(name_ptr());
3091 entry_name = Name_string(name_ptr(), name.length());
3092 }
3093
3094 /**
3095 Initialize all members
3096
3097 @param thd Current session.
3098 @param name Name of the user_var_entry instance.
3099 @param cs charset information of the user_var_entry instance.
3100 */
3101 void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
3102
3103 /**
3104 Store a value of the given type into a user_var_entry instance.
3105 @param from Value
3106 @param length Size of the value
3107 @param type type
3108 @retval false on success
3109 @retval true on memory allocation error
3110 */
3111 bool store(const void *from, size_t length, Item_result type);
3112
3113 /**
3114 Assert the user variable is locked.
3115 This is debug code only.
3116 The thread LOCK_thd_data mutex protects:
3117 - the thd->user_vars hash itself
3118 - the values in the user variable itself.
3119 The protection is required for monitoring,
3120 as a different thread can inspect this session
3121 user variables, on a live session.
3122 */
3123 void assert_locked() const;
3124
3125 static const size_t extra_size = sizeof(double);
3126 char *m_ptr; ///< Value
3127 size_t m_length; ///< Value length
3128 Item_result m_type; ///< Value type
3130 /**
3131 Set to the id of the most recent query that has used the variable.
3132 Used in binlogging: When set, there is no need to add a reference to this
3133 variable to the binlog. Imagine it is this:
3134
3135 INSERT INTO t SELECT @a:=10, @a:=@a+1.
3136
3137 Then we have a Item_func_get_user_var (because of the `@a+1`) so we
3138 think we have to write the value of `@a` to the binlog. But before that,
3139 we have a Item_func_set_user_var to create `@a` (`@a:=10`), in this we mark
3140 the variable as "already logged" so that it won't be logged
3141 by Item_func_get_user_var (because that's not necessary).
3142 */
3144
3145 public:
3146 user_var_entry() = default; /* Remove gcc warning */
3147
3148 THD *owner_session() const { return m_owner; }
3149
3150 Simple_cstring entry_name; // Variable name
3151 DTCollation collation; // Collation with attributes
3152 bool unsigned_flag; // true if unsigned, false if signed
3153
3154 /**
3155 Set value to user variable.
3156
3157 @param ptr pointer to buffer with new value
3158 @param length length of new value
3159 @param type type of new value
3160 @param cs charset info for new value
3161 @param dv derivation for new value
3162 @param unsigned_arg indicates if a value of type INT_RESULT is unsigned
3163
3164 @note Sets error and fatal error if allocation fails.
3165
3166 @retval
3167 false success
3168 @retval
3169 true failure
3170 */
3171 bool store(const void *ptr, size_t length, Item_result type,
3172 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3173 /**
3174 Set type of to the given value.
3175 @param type Data type.
3176 */
3178 assert_locked();
3179 m_type = type;
3180 }
3181 /**
3182 Set value to NULL
3183 @param type Data type.
3184 */
3185
3187 assert_locked();
3188 free_value();
3189 reset_value();
3190 m_type = type;
3191 }
3192
3193 void set_used_query_id(query_id_t query_id) { m_used_query_id = query_id; }
3195
3196 /**
3197 Allocates and initializes a user variable instance.
3198
3199 @param thd Current session.
3200 @param name Name of the variable.
3201 @param cs Charset of the variable.
3202
3203 @return Address of the allocated and initialized user_var_entry instance.
3204 @retval NULL On allocation error.
3205 */
3206 static user_var_entry *create(THD *thd, const Name_string &name,
3207 const CHARSET_INFO *cs);
3208
3209 /**
3210 Free all memory used by a user_var_entry instance
3211 previously created by create().
3212 */
3213 void destroy() {
3214 assert_locked();
3215 free_value(); // Free the external value buffer
3216 my_free(this); // Free the instance itself
3217 }
3218
3219 void lock();
3220 void unlock();
3221
3222 /* Routines to access the value and its type */
3223 const char *ptr() const { return m_ptr; }
3224 size_t length() const { return m_length; }
3225 Item_result type() const { return m_type; }
3226 /* Item-alike routines to access the value */
3227 double val_real(bool *null_value) const;
3228 longlong val_int(bool *null_value) const;
3229 String *val_str(bool *null_value, String *str, uint decimals) const;
3230 my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
3231};
3232
3233/**
3234 This class is used to implement operations like
3235 SET \@variable or \@variable:= expression.
3236*/
3237
3244 union {
3246 double vreal;
3250
3251 public:
3252 Name_string name; // keep it public
3253
3256 : Item_var_func(pos, b), name(a) {}
3257
3259 : Item_var_func(thd, item),
3261 entry(item->entry),
3262 value(item->value),
3264 null_item(item->null_item),
3265 save_result(item->save_result),
3266 name(item->name) {}
3267 enum Functype functype() const override { return SUSERVAR_FUNC; }
3268 double val_real() override;
3269 longlong val_int() override;
3270 String *val_str(String *str) override;
3271 my_decimal *val_decimal(my_decimal *) override;
3272 bool update_hash(const void *ptr, uint length, enum Item_result type,
3273 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3274 bool send(Protocol *protocol, String *str_arg) override;
3275 void make_field(Send_field *tmp_field) override;
3276 bool check(bool use_result_field);
3277 void save_item_result(Item *item);
3278 bool update();
3279 enum Item_result result_type() const override { return cached_result_type; }
3280 bool fix_fields(THD *thd, Item **ref) override;
3281 bool resolve_type(THD *) override;
3282 void print(const THD *thd, String *str,
3283 enum_query_type query_type) const override;
3284 void print_assignment(const THD *thd, String *str,
3285 enum_query_type query_type) const;
3286 const char *func_name() const override { return "set_user_var"; }
3287
3288 type_conversion_status save_in_field(Field *field, bool no_conversions,
3289 bool can_use_result_field);
3290
3291 void save_org_in_field(Field *field) override {
3292 save_in_field(field, true, false);
3293 }
3294
3295 bool set_entry(THD *thd, bool create_if_not_exists);
3296 void cleanup() override;
3297
3298 protected:
3300 bool no_conversions) override {
3301 return save_in_field(field, no_conversions, true);
3302 }
3303};
3304
3309
3310 public:
3311 Name_string name; // keep it public
3312
3317
3318 enum Functype functype() const override { return GUSERVAR_FUNC; }
3319 double val_real() override;
3320 longlong val_int() override;
3321 my_decimal *val_decimal(my_decimal *) override;
3322 String *val_str(String *str) override;
3323 const CHARSET_INFO *charset_for_protocol() override;
3324 bool resolve_type(THD *) override;
3325 bool propagate_type(THD *thd, const Type_properties &type) override;
3326 void cleanup() override;
3327 void update_used_tables() override {} // Keep existing used tables
3328 void print(const THD *thd, String *str,
3329 enum_query_type query_type) const override;
3330 enum Item_result result_type() const override;
3331 /*
3332 We must always return variables as strings to guard against selects of type
3333 select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3334 */
3335 const char *func_name() const override { return "get_user_var"; }
3336 bool is_non_const_over_literals(uchar *) override { return true; }
3337 bool eq(const Item *item, bool binary_cmp) const override;
3338
3339 private:
3340 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3341
3342 public:
3344 return this;
3345 }
3346};
3347
3348/*
3349 This item represents user variable used as out parameter (e.g in LOAD DATA),
3350 and it is supposed to be used only for this purprose. So it is simplified
3351 a lot. Actually you should never obtain its value.
3352
3353 The only two reasons for this thing being an Item is possibility to store it
3354 in const mem_root_deque<Item> and desire to place this code somewhere near
3355 other functions working with user variables.
3356*/
3360
3361 public:
3363 : Item(pos), name(a) {
3364 item_name.copy(a);
3365 }
3366 /* We should return something different from FIELD_ITEM here */
3367 enum Type type() const override { return STRING_ITEM; }
3368 double val_real() override;
3369 longlong val_int() override;
3370 String *val_str(String *str) override;
3371 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3373 assert(0);
3374 return true;
3375 }
3376 bool get_time(MYSQL_TIME *) override {
3377 assert(0);
3378 return true;
3379 }
3380
3381 /* fix_fields() binds variable name with its entry structure */
3382 bool fix_fields(THD *thd, Item **ref) override;
3383 void print(const THD *thd, String *str,
3384 enum_query_type query_type) const override;
3385 void set_null_value(const CHARSET_INFO *cs);
3386 void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3387};
3388
3389/* A system variable */
3390
3391#define GET_SYS_VAR_CACHE_LONG 1
3392#define GET_SYS_VAR_CACHE_DOUBLE 2
3393#define GET_SYS_VAR_CACHE_STRING 4
3394
3396
3397/** Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET. */
3399 public:
3403
3404 private:
3405 // Thread handle.
3407
3408 // Item_func_get_system_var instance.
3410
3411 /*
3412 Value conversion type.
3413 Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3414 member while creating the object. While converting value if there are any
3415 intermediate conversions in the same query then this member is used to avoid
3416 auditing more than once.
3417 */
3419
3420 /*
3421 To indicate event auditing is required or not. Event is not audited if
3422 * scope of the variable is *not* GLOBAL.
3423 * or the event is already audited for global variable for the same query.
3424 */
3426};
3427
3437
3438 template <typename T>
3440
3442
3443 public:
3445 enum_var_type scope);
3446 enum Functype functype() const override { return GSYSVAR_FUNC; }
3448 return INNER_TABLE_BIT;
3449 }
3450 bool resolve_type(THD *) override;
3451 void print(const THD *thd, String *str,
3452 enum_query_type query_type) const override;
3453 bool is_non_const_over_literals(uchar *) override { return true; }
3454 enum Item_result result_type() const override {
3455 assert(fixed);
3456 return type_to_result(data_type());
3457 }
3458 double val_real() override;
3459 longlong val_int() override;
3460 String *val_str(String *) override;
3461 my_decimal *val_decimal(my_decimal *dec_buf) override {
3462 return val_decimal_from_real(dec_buf);
3463 }
3464 /* TODO: fix to support views */
3465 const char *func_name() const override { return "get_system_var"; }
3466 bool eq(const Item *item, bool binary_cmp) const override;
3467 bool is_valid_for_pushdown(uchar *arg [[maybe_unused]]) override {
3468 // Expressions which have system variables cannot be pushed as of
3469 // now because Item_func_get_system_var::print does not print the
3470 // original expression which leads to an incorrect clone.
3471 return true;
3472 }
3473
3474 void cleanup() override;
3475};
3476
3477class JOIN;
3478
3479class Item_func_match final : public Item_real_func {
3481
3482 public:
3485 /// True if we are doing a full-text index scan with this MATCH function as a
3486 /// predicate, and the score can be retrieved with get_relevance(). If it is
3487 /// false, the score of the document must be retrieved with find_relevance().
3492 /**
3493 Master item means that if identical items are present in the
3494 statement, they use the same FT handler. FT handler is initialized
3495 only for master item and slave items just use it. FT hints initialized
3496 for master only, slave items HINTS are not accessed.
3497 */
3499 Item *concat_ws; // Item_func_concat_ws
3500 String value; // value of concat_ws
3501 String search_value; // key_item()'s value converted to cmp_collation
3502
3503 /**
3504 Constructor for Item_func_match class.
3505
3506 @param pos Position of token in the parser.
3507 @param a List of arguments.
3508 @param against_arg Expression to match against.
3509 @param b FT Flags.
3510 */
3511 Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3512 : Item_real_func(pos, a),
3513 against(against_arg),
3514 key(0),
3515 flags(b),
3518 master(nullptr),
3520 hints(nullptr),
3521 simple_expression(false),
3522 used_in_where_only(false) {
3523 null_on_null = false;
3524 }
3525
3526 bool itemize(Parse_context *pc, Item **res) override;
3527
3528 void cleanup() override {
3529 DBUG_TRACE;
3531 if (master == nullptr && ft_handler != nullptr) {
3533 }
3534 score_from_index_scan = false;
3535 ft_handler = nullptr;
3536 concat_ws = nullptr;
3537 return;
3538 }
3539 Item *key_item() const override { return against; }
3540 enum Functype functype() const override { return FT_FUNC; }
3541 const char *func_name() const override { return "match"; }
3542 bool fix_fields(THD *thd, Item **ref) override;
3543 bool eq(const Item *, bool binary_cmp) const override;
3544 /* The following should be safe, even if we compare doubles */
3545 longlong val_int() override {
3546 assert(fixed);
3547 return val_real() != 0.0;
3548 }
3549 double val_real() override;
3550 void print(const THD *thd, String *str,
3551 enum_query_type query_type) const override;
3552
3553 bool fix_index(const THD *thd);
3554 bool init_search(THD *thd);
3555 bool check_function_as_value_generator(uchar *checker_args) override {
3557 pointer_cast<Check_function_as_value_generator_parameters *>(
3558 checker_args);
3559 func_arg->banned_function_name = func_name();
3560 return true;
3561 }
3562
3563 /**
3564 Get number of matching rows from FT handler.
3565
3566 @note Requires that FT handler supports the extended API
3567
3568 @return Number of matching rows in result
3569 */
3571 assert(ft_handler);
3573
3574 return ((FT_INFO_EXT *)ft_handler)
3575 ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3576 }
3577
3578 /**
3579 Check whether FT result is ordered on rank
3580
3581 @return true if result is ordered
3582 @return false otherwise
3583 */
3585 assert(!master);
3586 if (hints->get_flags() & FT_SORTED) return true;
3587
3589 return false;
3590
3591 assert(ft_handler);
3592 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3594 }
3595
3596 /**
3597 Check whether FT result contains the document ID
3598
3599 @return true if document ID is available
3600 @return false otherwise
3601 */
3603 assert(ft_handler);
3604
3606 return false;
3607
3608 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3610 }
3611
3612 float get_filtering_effect(THD *thd, table_map filter_for_table,
3613 table_map read_tables,
3614 const MY_BITMAP *fields_to_ignore,
3615 double rows_in_table) override;
3616
3617 /**
3618 Returns master MATCH function.
3619
3620 @return pointer to master MATCH function.
3621 */
3623 if (master) return master->get_master();
3624 return this;
3625 }
3626
3627 /**
3628 Set master MATCH function and adjust used_in_where_only value.
3629
3630 @param item item for which master should be set.
3631 */
3634 item->master = this;
3635 }
3636
3637 /**
3638 Returns pointer to Ft_hints object belonging to master MATCH function.
3639
3640 @return pointer to Ft_hints object
3641 */
3643 assert(!master);
3644 return hints;
3645 }
3646
3647 /**
3648 Set comparison operation type and and value for master MATCH function.
3649
3650 @param type comparison operation type
3651 @param value_arg comparison operation value
3652 */
3653 void set_hints_op(enum ft_operation type, double value_arg) {
3654 assert(!master);
3655 hints->set_hint_op(type, value_arg);
3656 }
3657
3658 /**
3659 Set FT hints.
3660 */
3661 void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3662
3663 /**
3664 Check if ranking is not needed.
3665
3666 @return true if ranking is not needed
3667 @return false otherwise
3668 */
3670 assert(!master);
3671 return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3672 used_in_where_only && // MATCH result is not used
3673 // in expression
3674 hints->get_op_type() == FT_OP_NO); // MATCH is single function
3675 }
3676
3677 /**
3678 Set flag that the function is a simple expression.
3679
3680 @param val true if the function is a simple expression, false otherwise
3681 */
3682 void set_simple_expression(bool val) {
3683 assert(!master);
3684 simple_expression = val;
3685 }
3686
3687 /**
3688 Check if this MATCH function is a simple expression in WHERE condition.
3689
3690 @return true if simple expression
3691 @return false otherwise
3692 */
3694 assert(!master);
3695 return simple_expression;
3696 }
3697
3698 private:
3699 /**
3700 Fulltext index hints, initialized for master MATCH function only.
3701 */
3703 /**
3704 Flag is true when MATCH function is used as a simple expression in
3705 WHERE condition, i.e. there is no AND/OR combinations, just simple
3706 MATCH function or [MATCH, rank] comparison operation.
3707 */
3709 /**
3710 true if MATCH function is used in WHERE condition only.
3711 Used to determine what hints can be used for FT handler.
3712 Note that only master MATCH function has valid value.
3713 it's ok since only master function is involved in the hint processing.
3714 */
3716 /**
3717 Check whether storage engine for given table,
3718 allows FTS Boolean search on non-indexed columns.
3719
3720 @todo A flag should be added to the extended fulltext API so that
3721 it may be checked whether search on non-indexed columns are
3722 supported. Currently, it is not possible to check for such a
3723 flag since @c this->ft_handler is not yet set when this function is
3724 called. The current hack is to assume that search on non-indexed
3725 columns are supported for engines that does not support the extended
3726 fulltext API (e.g., MyISAM), while it is not supported for other
3727 engines (e.g., InnoDB)
3728
3729 @param tr Table for which storage engine to check
3730
3731 @retval true if BOOLEAN search on non-indexed columns is supported
3732 @retval false otherwise
3733 */
3734 bool