MySQL 8.0.29
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 {
297 };
304 };
305 enum Type type() const override { return FUNC_ITEM; }
306 virtual enum Functype functype() const { return UNKNOWN_FUNC; }
308
309 explicit Item_func(const POS &pos)
311
313 args[0] = a;
315 }
316 Item_func(const POS &pos, Item *a)
318 args[0] = a;
319 }
320
322 args[0] = a;
323 args[1] = b;
327 }
328 Item_func(const POS &pos, Item *a, Item *b)
330 args[0] = a;
331 args[1] = b;
332 }
333
334 Item_func(Item *a, Item *b, Item *c) {
335 if (alloc_args(*THR_MALLOC, 3)) return;
336 args[0] = a;
337 args[1] = b;
338 args[2] = c;
343 }
344
345 Item_func(const POS &pos, Item *a, Item *b, Item *c)
347 if (alloc_args(*THR_MALLOC, 3)) return;
348 args[0] = a;
349 args[1] = b;
350 args[2] = c;
351 }
352
353 Item_func(Item *a, Item *b, Item *c, Item *d) {
354 if (alloc_args(*THR_MALLOC, 4)) return;
355 args[0] = a;
356 args[1] = b;
357 args[2] = c;
358 args[3] = d;
364 }
365
366 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
368 if (alloc_args(*THR_MALLOC, 4)) return;
369 args[0] = a;
370 args[1] = b;
371 args[2] = c;
372 args[3] = d;
373 }
374 Item_func(Item *a, Item *b, Item *c, Item *d, Item *e) {
375 if (alloc_args(*THR_MALLOC, 5)) return;
376 args[0] = a;
377 args[1] = b;
378 args[2] = c;
379 args[3] = d;
380 args[4] = e;
387 }
388 Item_func(const POS &pos, Item *a, Item *b, Item *c, Item *d, Item *e)
390 if (alloc_args(*THR_MALLOC, 5)) return;
391 args[0] = a;
392 args[1] = b;
393 args[2] = c;
394 args[3] = d;
395 args[4] = e;
396 }
398 set_arguments(list, false);
399 }
400
401 Item_func(const POS &pos, PT_item_list *opt_list);
402
403 // Constructor used for Item_cond_and/or (see Item comment)
404 Item_func(THD *thd, const Item_func *item);
405
406 virtual Item *get_arg(uint i) { return args[i]; }
407 virtual Item *set_arg(THD *, uint, Item *) {
408 assert(0);
409 return nullptr;
410 }
411
412 bool itemize(Parse_context *pc, Item **res) override;
413
414 bool fix_fields(THD *, Item **ref) override;
415 bool fix_func_arg(THD *, Item **arg);
416 void fix_after_pullout(Query_block *parent_query_block,
417 Query_block *removed_query_block) override;
418 /**
419 Resolve type of function after all arguments have had their data types
420 resolved. Called from resolve_type() when no dynamic parameters
421 are used and from propagate_type() otherwise.
422 */
423 virtual bool resolve_type_inner(THD *) {
424 assert(false);
425 return false;
426 }
427 bool propagate_type(THD *thd, const Type_properties &type) override;
428 /**
429 Returns the pseudo tables depended upon in order to evaluate this
430 function expression. The default implementation returns the empty
431 set.
432 */
433 virtual table_map get_initial_pseudo_tables() const { return 0; }
434 table_map used_tables() const override { return used_tables_cache; }
436 void update_used_tables() override;
438 bool eq(const Item *item, bool binary_cmp) const override;
439 virtual optimize_type select_optimize(const THD *) { return OPTIMIZE_NONE; }
440 virtual bool have_rev_func() const { return false; }
441 virtual Item *key_item() const { return args[0]; }
442 /**
443 Copy arguments from list to args array
444
445 @param list function argument list
446 @param context_free true: for use in context-independent
447 constructors (Item_func(POS,...)) i.e. for use
448 in the parser
449 @return true on OOM, false otherwise
450 */
451 bool set_arguments(mem_root_deque<Item *> *list, bool context_free);
452 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
453 mem_root_deque<Item *> *fields) override;
454 void print(const THD *thd, String *str,
455 enum_query_type query_type) const override;
456 void print_op(const THD *thd, String *str, enum_query_type query_type) const;
457 void print_args(const THD *thd, String *str, uint from,
458 enum_query_type query_type) const;
459 virtual void fix_num_length_and_dec();
460 virtual bool is_deprecated() const { return false; }
461 bool get_arg0_date(MYSQL_TIME *ltime, my_time_flags_t fuzzy_date) {
462 return (null_value = args[0]->get_date(ltime, fuzzy_date));
463 }
464 inline bool get_arg0_time(MYSQL_TIME *ltime) {
465 return (null_value = args[0]->get_time(ltime));
466 }
467 bool is_null() override { return update_null_value() || null_value; }
470 friend class udf_handler;
471 Field *tmp_table_field(TABLE *t_arg) override;
472 Item *get_tmp_table_item(THD *thd) override;
473
474 my_decimal *val_decimal(my_decimal *) override;
475
476 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems, uint flags,
477 int item_sep) {
478 return agg_item_charsets(c, func_name(), items, nitems, flags, item_sep,
479 false);
480 }
481 /*
482 Aggregate arguments for string result, e.g: CONCAT(a,b)
483 - convert to @@character_set_connection if all arguments are numbers
484 - allow DERIVATION_NONE
485 */
487 uint nitems, int item_sep = 1) {
488 return agg_item_charsets_for_string_result(c, func_name(), items, nitems,
489 item_sep);
490 }
491 /*
492 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
493 - don't convert to @@character_set_connection if all arguments are numbers
494 - don't allow DERIVATION_NONE
495 */
497 uint nitems, int item_sep = 1) {
498 return agg_item_charsets_for_comparison(c, func_name(), items, nitems,
499 item_sep);
500 }
501
502 bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
503 Item *transform(Item_transformer transformer, uchar *arg) override;
504 Item *compile(Item_analyzer analyzer, uchar **arg_p,
505 Item_transformer transformer, uchar *arg_t) override;
506 void traverse_cond(Cond_traverser traverser, void *arg,
507 traverse_order order) override;
508
509 /**
510 Throw an error if the input double number is not finite, i.e. is either
511 +/-INF or NAN.
512 */
513 inline double check_float_overflow(double value) {
514 return std::isfinite(value) ? value : raise_float_overflow();
515 }
516 /**
517 Throw an error if the input BIGINT value represented by the
518 (longlong value, bool unsigned flag) pair cannot be returned by the
519 function, i.e. is not compatible with this Item's unsigned_flag.
520 */
521 inline longlong check_integer_overflow(longlong value, bool val_unsigned) {
522 if ((unsigned_flag && !val_unsigned && value < 0) ||
523 (!unsigned_flag && val_unsigned &&
524 (ulonglong)value > (ulonglong)LLONG_MAX))
525 return raise_integer_overflow();
526 return value;
527 }
528 /**
529 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW.
530 */
533 }
534
536 assert(fixed);
537 for (uint i = 0; i < arg_count; i++) {
538 if (args[i]->type() == Item::FIELD_ITEM &&
540 return true;
541 }
542 return false;
543 }
544
546 assert(fixed);
547 for (uint i = 0; i < arg_count; i++) {
548 if (args[i]->type() == Item::FIELD_ITEM &&
549 (args[i]->data_type() == MYSQL_TYPE_DATE ||
551 return true;
552 }
553 return false;
554 }
555
557 assert(fixed);
558 for (uint i = 0; i < arg_count; i++) {
559 if (args[i]->type() == Item::FIELD_ITEM &&
560 (args[i]->data_type() == MYSQL_TYPE_TIME ||
562 return true;
563 }
564 return false;
565 }
566
568 assert(fixed);
569 for (uint i = 0; i < arg_count; i++) {
570 if (args[i]->type() == Item::FIELD_ITEM &&
572 return true;
573 }
574 return false;
575 }
576
577 /*
578 We assume the result of any function that has a TIMESTAMP argument to be
579 timezone-dependent, since a TIMESTAMP value in both numeric and string
580 contexts is interpreted according to the current timezone.
581 The only exception is UNIX_TIMESTAMP() which returns the internal
582 representation of a TIMESTAMP argument verbatim, and thus does not depend on
583 the timezone.
584 */
586 return has_timestamp_args();
587 }
588
589 Item *gc_subst_transformer(uchar *arg) override;
590
591 bool resolve_type(THD *thd) override {
592 // By default, pick PS-param's type from other arguments, or VARCHAR
593 return param_type_uses_non_param(thd);
594 }
595
596 /**
597 Whether an arg of a JSON function can be cached to avoid repetitive
598 string->JSON conversion. This function returns true only for those args,
599 which are the source of JSON data. JSON path args are cached independently
600 and for them this function returns false. Same as for all other type of
601 args.
602
603 @param arg the arg to cache
604
605 @retval true arg can be cached
606 @retval false otherwise
607 */
608 virtual enum_const_item_cache can_cache_json_arg(Item *arg [[maybe_unused]]) {
609 return CACHE_NONE;
610 }
611
612 /// Whether this Item is an equi-join condition. If this Item is a compound
613 /// item (i.e. multiple condition AND'ed together), it will only return true
614 /// if the Item contains only equi-join conditions AND'ed together. This is
615 /// used to determine whether the condition can be used as a join condition
616 /// for hash join (join conditions in hash join must be equi-join conditions),
617 /// or if it should be placed as a filter after the join.
618 virtual bool contains_only_equi_join_condition() const { return false; }
619
621
622 protected:
623 /**
624 Whether or not an item should contribute to the filtering effect
625 (@see get_filtering_effect()). First it verifies that table
626 requirements are satisfied as follows:
627
628 1) The item must refer to a field in 'filter_for_table' in some
629 way. This reference may be indirect through any number of
630 intermediate items. For example, this item may be an
631 Item_cond_and which refers to an Item_func_eq which refers to
632 the field.
633 2) The item must not refer to other tables than those already
634 read and the table in 'filter_for_table'
635
636 Then it contines to other properties as follows:
637
638 Item_funcs represent "<operand1> OP <operand2> [OP ...]". If the
639 Item_func is to contribute to the filtering effect, then
640
641 1) one of the operands must be a field from 'filter_for_table' that is not
642 in 'fields_to_ignore', and
643 2) depending on the Item_func type filtering effect is calculated
644 for, one or all [1] of the other operand(s) must be an available
645 value, i.e.:
646 - a constant, or
647 - a constant subquery, or
648 - a field value read from a table in 'read_tables', or
649 - a second field in 'filter_for_table', or
650 - a function that only refers to constants or tables in
651 'read_tables', or
652 - special case: an implicit value like NULL in the case of
653 "field IS NULL". Such Item_funcs have arg_count==1.
654
655 [1] "At least one" for multiple equality (X = Y = Z = ...), "all"
656 for the rest (e.g. BETWEEN)
657
658 @param read_tables Tables earlier in the join sequence.
659 Predicates for table 'filter_for_table' that
660 rely on values from these tables can be part of
661 the filter effect.
662 @param filter_for_table The table we are calculating filter effect for
663 @param fields_to_ignore Columns that should be ignored.
664
665
666 @return Item_field that participates in the predicate if none of the
667 requirements are broken, NULL otherwise
668
669 @note: This function only applies to items doing comparison, i.e.
670 boolean predicates. Unfortunately, some of those items do not
671 inherit from Item_bool_func so the member function has to be
672 placed in Item_func.
673 */
675 table_map read_tables, table_map filter_for_table,
676 const MY_BITMAP *fields_to_ignore) const;
677 /**
678 Named parameters are allowed in a parameter list
679
680 The syntax to name parameters in a function call is as follow:
681 <code>foo(expr AS named, expr named, expr AS "named", expr "named")</code>
682 where "AS" is optional.
683 Only UDF function support that syntax.
684
685 @return true if the function item can have named parameters
686 */
687 virtual bool may_have_named_parameters() const { return false; }
688 bool is_non_const_over_literals(uchar *) override { return false; }
689
690 bool check_function_as_value_generator(uchar *checker_args) override {
691 if (is_deprecated()) {
693 pointer_cast<Check_function_as_value_generator_parameters *>(
694 checker_args);
695 func_arg->banned_function_name = func_name();
696 return true;
697 }
698 return false;
699 }
700 bool is_valid_for_pushdown(uchar *arg) override;
701 bool check_column_in_window_functions(uchar *arg) override;
702 bool check_column_in_group_by(uchar *arg) override;
703
705};
706
707class Item_real_func : public Item_func {
708 public:
710 explicit Item_real_func(const POS &pos) : Item_func(pos) {
712 }
713
717 }
718
720
721 Item_real_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
723 }
724
727 }
728
729 Item_real_func(const POS &pos, PT_item_list *list) : Item_func(pos, list) {
731 }
732
733 String *val_str(String *str) override;
734 my_decimal *val_decimal(my_decimal *decimal_value) override;
735 longlong val_int() override {
736 assert(fixed);
738 }
739 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
740 return get_date_from_real(ltime, fuzzydate);
741 }
742 bool get_time(MYSQL_TIME *ltime) override {
743 return get_time_from_real(ltime);
744 }
745 enum Item_result result_type() const override { return REAL_RESULT; }
746};
747
749 protected:
751
752 public:
755 }
759 }
760
764 }
768 }
769
773 }
777 }
778
779 enum Item_result result_type() const override { return hybrid_type; }
781 return MYSQL_TYPE_DOUBLE;
782 }
783 bool resolve_type(THD *thd) override;
784 bool resolve_type_inner(THD *thd) override;
785 void fix_num_length_and_dec() override;
786 virtual void set_numeric_type() = 0; // To be called from resolve_type()
787
788 double val_real() override;
789 longlong val_int() override;
790 my_decimal *val_decimal(my_decimal *) override;
791 String *val_str(String *str) override;
792 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
793 bool get_time(MYSQL_TIME *ltime) override;
794 /**
795 @brief Performs the operation that this functions implements when the
796 result type is INT.
797
798 @return The result of the operation.
799 */
800 virtual longlong int_op() = 0;
801
802 /**
803 @brief Performs the operation that this functions implements when the
804 result type is REAL.
805
806 @return The result of the operation.
807 */
808 virtual double real_op() = 0;
809
810 /**
811 @brief Performs the operation that this functions implements when the
812 result type is DECIMAL.
813
814 @param decimal_value A pointer where the DECIMAL value will be allocated.
815 @return
816 - 0 If the result is NULL
817 - The same pointer it was given, with the area initialized to the
818 result of the operation.
819 */
820 virtual my_decimal *decimal_op(my_decimal *decimal_value) = 0;
821
822 /**
823 @brief Performs the operation that this functions implements when the
824 result type is a string type.
825
826 @return The result of the operation.
827 */
828 virtual String *str_op(String *) = 0;
829 /**
830 @brief Performs the operation that this functions implements when the
831 result type is MYSQL_TYPE_DATE or MYSQL_TYPE_DATETIME.
832
833 @return The result of the operation.
834 */
835 virtual bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) = 0;
836 virtual bool time_op(MYSQL_TIME *ltime) = 0;
837 bool is_null() override { return update_null_value() || null_value; }
838};
839
840/* function where type of result detected by first argument */
842 public:
845
848 : Item_func_numhybrid(pos, a, b) {}
849
850 void fix_num_length_and_dec() override;
851 void set_numeric_type() override;
852 String *str_op(String *) override {
853 assert(0);
854 return nullptr;
855 }
857 assert(0);
858 return false;
859 }
860 bool time_op(MYSQL_TIME *) override {
861 assert(0);
862 return false;
863 }
864};
865
866/* Base class for operations like '+', '-', '*' */
868 public:
870 Item_num_op(const POS &pos, Item *a, Item *b)
871 : Item_func_numhybrid(pos, a, b) {}
872
873 virtual void result_precision() = 0;
874
875 void print(const THD *thd, String *str,
876 enum_query_type query_type) const override {
877 print_op(thd, str, query_type);
878 }
879
880 void set_numeric_type() override;
881 String *str_op(String *) override {
882 assert(0);
883 return nullptr;
884 }
886 assert(0);
887 return false;
888 }
889 bool time_op(MYSQL_TIME *) override {
890 assert(0);
891 return false;
892 }
893};
894
895class Item_int_func : public Item_func {
896 public:
898 explicit Item_int_func(const POS &pos) : Item_func(pos) {
900 }
901
903 Item_int_func(const POS &pos, Item *a) : Item_func(pos, a) {
905 }
906
909 }
910 Item_int_func(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {
912 }
913
914 Item_int_func(Item *a, Item *b, Item *c) : Item_func(a, b, c) {
916 }
917 Item_int_func(const POS &pos, Item *a, Item *b, Item *c)
918 : Item_func(pos, a, b, c) {
920 }
921
922 Item_int_func(Item *a, Item *b, Item *c, Item *d) : Item_func(a, b, c, d) {
924 }
925 Item_int_func(const POS &pos, Item *a, Item *b, Item *c, Item *d)
926 : Item_func(pos, a, b, c, d) {
928 }
929
932 }
934 : Item_func(pos, opt_list) {
936 }
937
938 Item_int_func(THD *thd, Item_int_func *item) : Item_func(thd, item) {
940 }
941 double val_real() override;
942 String *val_str(String *str) override;
943 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
944 return get_date_from_int(ltime, fuzzydate);
945 }
946 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
947 enum Item_result result_type() const override { return INT_RESULT; }
948 /*
949 Concerning PS-param types,
950 resolve_type(THD *) is not overidden here, as experience shows that for
951 most child classes of this class, VARCHAR is the best default
952 */
953};
954
957
959
960 public:
962
963 bool itemize(Parse_context *pc, Item **res) override;
964 const char *func_name() const override { return "connection_id"; }
965 bool resolve_type(THD *thd) override;
966 bool fix_fields(THD *thd, Item **ref) override;
967 longlong val_int() override {
968 assert(fixed);
969 return value;
970 }
971 bool check_function_as_value_generator(uchar *checker_args) override {
973 pointer_cast<Check_function_as_value_generator_parameters *>(
974 checker_args);
975 func_arg->banned_function_name = func_name();
976 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
977 (func_arg->source == VGS_CHECK_CONSTRAINT));
978 }
979};
980
982 public:
984 unsigned_flag = false;
985 }
986 const char *func_name() const override { return "cast_as_signed"; }
987 longlong val_int() override;
988 bool resolve_type(THD *thd) override;
989 void print(const THD *thd, String *str,
990 enum_query_type query_type) const override;
991 enum Functype functype() const override { return TYPECAST_FUNC; }
992};
993
995 public:
997 unsigned_flag = true;
998 }
999 const char *func_name() const override { return "cast_as_unsigned"; }
1000 longlong val_int() override;
1001 bool resolve_type(THD *thd) override;
1002 void print(const THD *thd, String *str,
1003 enum_query_type query_type) const override;
1004 enum Functype functype() const override { return TYPECAST_FUNC; }
1005};
1006
1007class Item_typecast_decimal final : public Item_func {
1008 public:
1009 Item_typecast_decimal(const POS &pos, Item *a, int len, int dec)
1010 : Item_func(pos, a) {
1012 }
1013 String *val_str(String *str) override;
1014 double val_real() override;
1015 longlong val_int() override;
1016 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1017 return get_date_from_decimal(ltime, fuzzydate);
1018 }
1019 bool get_time(MYSQL_TIME *ltime) override {
1020 return get_time_from_decimal(ltime);
1021 }
1022 my_decimal *val_decimal(my_decimal *) override;
1023 enum Item_result result_type() const override { return DECIMAL_RESULT; }
1024 bool resolve_type(THD *thd) override {
1025 if (args[0]->propagate_type(thd, MYSQL_TYPE_NEWDECIMAL, false, true))
1026 return true;
1027 return false;
1028 }
1029 const char *func_name() const override { return "cast_as_decimal"; }
1030 enum Functype functype() const override { return TYPECAST_FUNC; }
1031 void print(const THD *thd, String *str,
1032 enum_query_type query_type) const override;
1033};
1034
1035/**
1036 Class used to implement CAST to floating-point data types.
1037*/
1038class Item_typecast_real final : public Item_func {
1039 public:
1040 Item_typecast_real(const POS &pos, Item *a, bool as_double)
1041 : Item_func(pos, a) {
1042 if (as_double)
1044 else
1046 }
1048 String *val_str(String *str) override;
1049 double val_real() override;
1050 longlong val_int() override { return val_int_from_real(); }
1051 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1052 bool get_time(MYSQL_TIME *ltime) override;
1053 my_decimal *val_decimal(my_decimal *decimal_value) override;
1054 enum Item_result result_type() const override { return REAL_RESULT; }
1055 bool resolve_type(THD *thd) override {
1056 return args[0]->propagate_type(thd, MYSQL_TYPE_DOUBLE, false, true);
1057 }
1058 const char *func_name() const override { return "cast_as_real"; }
1059 enum Functype functype() const override { return TYPECAST_FUNC; }
1060 void print(const THD *thd, String *str,
1061 enum_query_type query_type) const override;
1062};
1063
1065 public:
1068 : Item_num_op(pos, a, b) {}
1069
1070 void result_precision() override;
1071 bool check_partition_func_processor(uchar *) override { return false; }
1072 bool check_function_as_value_generator(uchar *) override { return false; }
1073};
1074
1076 public:
1079 : Item_func_additive_op(pos, a, b) {}
1080
1081 const char *func_name() const override { return "+"; }
1082
1083 // SUPPRESS_UBSAN: signed integer overflow
1084 longlong int_op() override SUPPRESS_UBSAN;
1085
1086 double real_op() override;
1087 my_decimal *decimal_op(my_decimal *) override;
1088 enum Functype functype() const override { return PLUS_FUNC; }
1089};
1090
1092 public:
1095 : Item_func_additive_op(pos, a, b) {}
1096
1097 const char *func_name() const override { return "-"; }
1098
1099 // SUPPRESS_UBSAN: signed integer overflow
1100 longlong int_op() override SUPPRESS_UBSAN;
1101
1102 double real_op() override;
1103 my_decimal *decimal_op(my_decimal *) override;
1104 bool resolve_type(THD *thd) override;
1105 enum Functype functype() const override { return MINUS_FUNC; }
1106};
1107
1108class Item_func_mul final : public Item_num_op {
1109 public:
1111 Item_func_mul(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1112
1113 const char *func_name() const override { return "*"; }
1114 longlong int_op() override;
1115 double real_op() override;
1116 my_decimal *decimal_op(my_decimal *) override;
1117 void result_precision() override;
1118 bool check_partition_func_processor(uchar *) override { return false; }
1119 bool check_function_as_value_generator(uchar *) override { return false; }
1120 enum Functype functype() const override { return MUL_FUNC; }
1121};
1122
1124 public:
1126 : Item_num_op(pos, a, b) {}
1128 longlong int_op() override;
1129 double real_op() override;
1130 my_decimal *decimal_op(my_decimal *) override;
1131 enum Functype functype() const override { return DIV_FUNC; }
1132
1133 protected:
1135};
1136
1137class Item_func_div final : public Item_func_div_base {
1138 public:
1140 : Item_func_div_base(pos, a, b) {}
1141 const char *func_name() const override { return "/"; }
1142 bool resolve_type(THD *thd) override;
1143 void result_precision() override;
1144};
1145
1147 public:
1150 : Item_func_div_base(pos, a, b) {}
1151 const char *func_name() const override { return "DIV"; }
1153 return MYSQL_TYPE_LONGLONG;
1154 }
1155 bool resolve_type(THD *thd) override;
1156 void result_precision() override;
1157 void set_numeric_type() override;
1158 bool check_partition_func_processor(uchar *) override { return false; }
1159 bool check_function_as_value_generator(uchar *) override { return false; }
1160};
1161
1162class Item_func_mod final : public Item_num_op {
1163 public:
1165 Item_func_mod(const POS &pos, Item *a, Item *b) : Item_num_op(pos, a, b) {}
1166
1167 longlong int_op() override;
1168 double real_op() override;
1169 my_decimal *decimal_op(my_decimal *) override;
1170 const char *func_name() const override { return "%"; }
1171 void result_precision() override;
1172 bool resolve_type(THD *thd) override;
1173 bool check_partition_func_processor(uchar *) override { return false; }
1174 bool check_function_as_value_generator(uchar *) override { return false; }
1175 enum Functype functype() const override { return MOD_FUNC; }
1176};
1177
1178class Item_func_neg final : public Item_func_num1 {
1179 public:
1182
1183 double real_op() override;
1184 longlong int_op() override;
1185 my_decimal *decimal_op(my_decimal *) override;
1186 const char *func_name() const override { return "-"; }
1187 enum Functype functype() const override { return NEG_FUNC; }
1188 bool resolve_type(THD *thd) override;
1189 void fix_num_length_and_dec() override;
1190 bool check_partition_func_processor(uchar *) override { return false; }
1191 bool check_function_as_value_generator(uchar *) override { return false; }
1192};
1193
1194class Item_func_abs final : public Item_func_num1 {
1195 public:
1197 double real_op() override;
1198 longlong int_op() override;
1199 my_decimal *decimal_op(my_decimal *) override;
1200 const char *func_name() const override { return "abs"; }
1201 bool resolve_type(THD *) override;
1202 bool check_partition_func_processor(uchar *) override { return false; }
1203 bool check_function_as_value_generator(uchar *) override { return false; }
1204 enum Functype functype() const override { return ABS_FUNC; }
1205};
1206
1207// A class to handle logarithmic and trigonometric functions
1208
1210 public:
1213
1214 Item_dec_func(const POS &pos, Item *a, Item *b) : Item_real_func(pos, a, b) {}
1215 bool resolve_type(THD *thd) override;
1216};
1217
1218class Item_func_exp final : public Item_dec_func {
1219 public:
1221 double val_real() override;
1222 const char *func_name() const override { return "exp"; }
1223 enum Functype functype() const override { return EXP_FUNC; }
1224};
1225
1226class Item_func_ln final : public Item_dec_func {
1227 public:
1229 double val_real() override;
1230 const char *func_name() const override { return "ln"; }
1231 enum Functype functype() const override { return LN_FUNC; }
1232};
1233
1234class Item_func_log final : public Item_dec_func {
1235 public:
1237 Item_func_log(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1238 double val_real() override;
1239 const char *func_name() const override { return "log"; }
1240 enum Functype functype() const override { return LOG_FUNC; }
1241};
1242
1243class Item_func_log2 final : public Item_dec_func {
1244 public:
1246 double val_real() override;
1247 const char *func_name() const override { return "log2"; }
1248};
1249
1250class Item_func_log10 final : public Item_dec_func {
1251 public:
1253 double val_real() override;
1254 const char *func_name() const override { return "log10"; }
1255 enum Functype functype() const override { return LOG10_FUNC; }
1256};
1257
1258class Item_func_sqrt final : public Item_dec_func {
1259 public:
1261 double val_real() override;
1262 const char *func_name() const override { return "sqrt"; }
1263 enum Functype functype() const override { return SQRT_FUNC; }
1264};
1265
1266class Item_func_pow final : public Item_dec_func {
1267 public:
1268 Item_func_pow(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1269 double val_real() override;
1270 const char *func_name() const override { return "pow"; }
1271};
1272
1273class Item_func_acos final : public Item_dec_func {
1274 public:
1276 double val_real() override;
1277 const char *func_name() const override { return "acos"; }
1278 enum Functype functype() const override { return ACOS_FUNC; }
1279};
1280
1281class Item_func_asin final : public Item_dec_func {
1282 public:
1284 double val_real() override;
1285 const char *func_name() const override { return "asin"; }
1286 enum Functype functype() const override { return ASIN_FUNC; }
1287};
1288
1289class Item_func_atan final : public Item_dec_func {
1290 public:
1292 Item_func_atan(const POS &pos, Item *a, Item *b) : Item_dec_func(pos, a, b) {}
1293 double val_real() override;
1294 const char *func_name() const override { return "atan"; }
1295 enum Functype functype() const override { return ATAN_FUNC; }
1296};
1297
1298class Item_func_cos final : public Item_dec_func {
1299 public:
1301 double val_real() override;
1302 const char *func_name() const override { return "cos"; }
1303 enum Functype functype() const override { return COS_FUNC; }
1304};
1305
1306class Item_func_sin final : public Item_dec_func {
1307 public:
1309 double val_real() override;
1310 const char *func_name() const override { return "sin"; }
1311 enum Functype functype() const override { return SIN_FUNC; }
1312};
1313
1314class Item_func_tan final : public Item_dec_func {
1315 public:
1317 double val_real() override;
1318 const char *func_name() const override { return "tan"; }
1319 enum Functype functype() const override { return TAN_FUNC; }
1320};
1321
1322class Item_func_cot final : public Item_dec_func {
1323 public:
1325 double val_real() override;
1326 const char *func_name() const override { return "cot"; }
1327 enum Functype functype() const override { return COT_FUNC; }
1328};
1329
1331 public:
1334 bool resolve_type_inner(THD *thd) override;
1335};
1336
1338 public:
1341 const char *func_name() const override { return "ceiling"; }
1342 longlong int_op() override;
1343 double real_op() override;
1344 my_decimal *decimal_op(my_decimal *) override;
1345 bool check_partition_func_processor(uchar *) override { return false; }
1346 bool check_function_as_value_generator(uchar *) override { return false; }
1347 enum Functype functype() const override { return CEILING_FUNC; }
1348};
1349
1351 public:
1354 const char *func_name() const override { return "floor"; }
1355 longlong int_op() override;
1356 double real_op() override;
1357 my_decimal *decimal_op(my_decimal *) override;
1358 bool check_partition_func_processor(uchar *) override { return false; }
1359 bool check_function_as_value_generator(uchar *) override { return false; }
1360 enum Functype functype() const override { return FLOOR_FUNC; }
1361};
1362
1363/* This handles round and truncate */
1364
1365class Item_func_round final : public Item_func_num1 {
1367
1368 public:
1369 Item_func_round(Item *a, Item *b, bool trunc_arg)
1370 : Item_func_num1(a, b), truncate(trunc_arg) {}
1371 Item_func_round(const POS &pos, Item *a, Item *b, bool trunc_arg)
1372 : Item_func_num1(pos, a, b), truncate(trunc_arg) {}
1373
1374 const char *func_name() const override {
1375 return truncate ? "truncate" : "round";
1376 }
1377 double real_op() override;
1378 longlong int_op() override;
1379 my_decimal *decimal_op(my_decimal *) override;
1380 bool resolve_type(THD *) override;
1381 enum Functype functype() const override {
1383 }
1384};
1385
1386class Item_func_rand final : public Item_real_func {
1388
1390 bool first_eval{true}; // true if val_real() is called 1st time
1391 public:
1393 explicit Item_func_rand(const POS &pos) : Item_real_func(pos) {}
1394
1395 bool itemize(Parse_context *pc, Item **res) override;
1396 double val_real() override;
1397 const char *func_name() const override { return "rand"; }
1398 /**
1399 This function is non-deterministic and hence depends on the
1400 'RAND' pseudo-table.
1401
1402 @retval RAND_TABLE_BIT
1403 */
1405 return RAND_TABLE_BIT;
1406 }
1407 bool fix_fields(THD *thd, Item **ref) override;
1408 bool resolve_type(THD *thd) override;
1409 void cleanup() override {
1410 first_eval = true;
1412 }
1413 bool check_function_as_value_generator(uchar *checker_args) override {
1415 pointer_cast<Check_function_as_value_generator_parameters *>(
1416 checker_args);
1417 func_arg->banned_function_name = func_name();
1418 return ((func_arg->source == VGS_GENERATED_COLUMN) ||
1419 (func_arg->source == VGS_CHECK_CONSTRAINT));
1420 }
1421
1422 private:
1423 void seed_random(Item *val);
1424};
1425
1426class Item_func_sign final : public Item_int_func {
1427 public:
1429 const char *func_name() const override { return "sign"; }
1430 longlong val_int() override;
1431 bool resolve_type(THD *thd) override;
1432};
1433
1434// Common base class for the DEGREES and RADIANS functions.
1436 double mul, add;
1437
1438 protected:
1439 Item_func_units(const POS &pos, Item *a, double mul_arg, double add_arg)
1440 : Item_real_func(pos, a), mul(mul_arg), add(add_arg) {}
1441
1442 public:
1443 double val_real() override;
1444 bool resolve_type(THD *thd) override;
1445};
1446
1448 public:
1450 : Item_func_units(pos, a, 180.0 / M_PI, 0.0) {}
1451 const char *func_name() const override { return "degrees"; }
1452 enum Functype functype() const override { return DEGREES_FUNC; }
1453};
1454
1456 public:
1458 : Item_func_units(pos, a, M_PI / 180.0, 0.0) {}
1459 const char *func_name() const override { return "radians"; }
1460 enum Functype functype() const override { return RADIANS_FUNC; }
1461};
1463 public:
1464 Item_func_min_max(const POS &pos, PT_item_list *opt_list, bool is_least_func)
1465 : Item_func_numhybrid(pos, opt_list),
1466 m_is_least_func(is_least_func),
1468
1469 longlong val_int() override;
1470 double val_real() override;
1471 my_decimal *val_decimal(my_decimal *) override;
1472 longlong int_op() override;
1473 double real_op() override;
1474 my_decimal *decimal_op(my_decimal *) override;
1475 String *str_op(String *) override;
1476 bool date_op(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1477 bool time_op(MYSQL_TIME *ltime) override;
1479 return MYSQL_TYPE_VARCHAR;
1480 }
1481 bool resolve_type(THD *thd) override;
1482 bool resolve_type_inner(THD *thd) override;
1483 void set_numeric_type() override {}
1484 enum Item_result result_type() const override { return hybrid_type; }
1485
1486 /**
1487 Make CAST(LEAST_OR_GREATEST(datetime_expr, varchar_expr))
1488 return a number in format YYMMDDhhmmss.
1489 */
1490 enum Item_result cast_to_int_type() const override {
1492 }
1493
1494 /// Returns true if arguments to this function should be compared as dates.
1495 bool compare_as_dates() const {
1496 return temporal_item != nullptr &&
1498 }
1499
1500 /// Returns true if at least one of the arguments was of temporal type.
1501 bool has_temporal_arg() const { return temporal_item; }
1502
1503 private:
1504 /// True if LEAST function, false if GREATEST.
1507 /*
1508 Used for determining whether one of the arguments is of temporal type and
1509 for converting arguments to a common output format if arguments are
1510 compared as dates and result type is character string. For example,
1511 LEAST('95-05-05', date '10-10-10') should return '1995-05-05', not
1512 '95-05-05'.
1513 */
1515 /**
1516 Compare arguments as datetime values.
1517
1518 @param value Pointer to which the datetime value of the winning argument
1519 is written.
1520
1521 @return true if error, false otherwise.
1522 */
1523 bool cmp_datetimes(longlong *value);
1524
1525 /**
1526 Compare arguments as time values.
1527
1528 @param value Pointer to which the time value of the winning argument is
1529 written.
1530
1531 @return true if error, false otherwise.
1532 */
1533 bool cmp_times(longlong *value);
1534};
1535
1536class Item_func_min final : public Item_func_min_max {
1537 public:
1539 : Item_func_min_max(pos, opt_list, true) {}
1540 const char *func_name() const override { return "least"; }
1541 enum Functype functype() const override { return LEAST_FUNC; }
1542};
1543
1544class Item_func_max final : public Item_func_min_max {
1545 public:
1547 : Item_func_min_max(pos, opt_list, false) {}
1548 const char *func_name() const override { return "greatest"; }
1549 enum Functype functype() const override { return GREATEST_FUNC; }
1550};
1551
1552/**
1553 A wrapper Item that normally returns its parameter, but becomes NULL when
1554 processing rows for rollup. Rollup is implemented by AggregateIterator, and
1555 works by means of hierarchical levels -- 0 is the “grand totals” phase, 1 is
1556 where only one group level is active, and so on. E.g., for a query with GROUP
1557 BY a,b, the rows will look like this:
1558
1559 a b rollup level
1560 1 1 2
1561 1 2 2
1562 1 NULL 1
1563 2 1 2
1564 2 NULL 1
1565 NULL NULL 0
1566
1567 Each rollup group item has a minimum level for when it becomes NULL. In the
1568 example above, a would have minimum level 0 and b would have minimum level 1.
1569 For simplicity, the JOIN carries a list of all rollup group items, and they
1570 are being given the current rollup level when it changes. A rollup level of
1571 INT_MAX essentially always disables rollup, which is useful when there are
1572 leftover group items in places that are not relevant for rollup
1573 (e.g., sometimes resolving can leave rollup wrappers in place for temporary
1574 tables that are created before grouping, which should then effectively be
1575 disabled).
1576 */
1577class Item_rollup_group_item final : public Item_func {
1578 public:
1583 // We're going to replace inner_item in the SELECT list, so copy its hidden
1584 // status. (We could have done this in the caller, but it fits naturally in
1585 // with all the other copying done here.)
1587 set_nullable(true);
1589 }
1590 double val_real() override;
1591 longlong val_int() override;
1592 String *val_str(String *str) override;
1594 bool val_json(Json_wrapper *result) override;
1595 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1596 bool get_time(MYSQL_TIME *ltime) override;
1597 const char *func_name() const override { return "rollup_group_item"; }
1598 table_map used_tables() const override {
1599 /*
1600 If underlying item is non-constant, return its used_tables value.
1601 Otherwise, ensure it is non-constant by adding RAND_TABLE_BIT.
1602 */
1603 return args[0]->const_for_execution()
1604 ? (args[0]->used_tables() | RAND_TABLE_BIT)
1605 : args[0]->used_tables();
1606 }
1607 Item_result result_type() const override { return args[0]->result_type(); }
1608 bool resolve_type(THD *) override {
1609 // needn't handle dynamic parameter as its const_item() is false.
1611
1612 // The item could be a NULL constant.
1613 null_value = args[0]->is_null();
1614 return false;
1615 }
1616 Item *inner_item() const { return args[0]; }
1617 bool rollup_null() const {
1619 }
1620 enum Functype functype() const override { return ROLLUP_GROUP_ITEM_FUNC; }
1621 void print(const THD *thd, String *str,
1622 enum_query_type query_type) const override;
1623 bool eq(const Item *item, bool binary_cmp) const override;
1624
1625 // Used by AggregateIterator.
1627
1628 // Used when cloning the item only.
1629 int min_rollup_level() const { return m_min_rollup_level; }
1630
1631 private:
1634};
1635
1638
1639 public:
1641 longlong val_int() override;
1642 const char *func_name() const override { return "length"; }
1643 bool resolve_type(THD *thd) override {
1644 if (param_type_is_default(thd, 0, 1)) return true;
1645 max_length = 10;
1646 return false;
1647 }
1648};
1649
1651 public:
1653 longlong val_int() override {
1654 assert(fixed);
1655 return Item_func_length::val_int() * 8;
1656 }
1657 const char *func_name() const override { return "bit_length"; }
1658};
1659
1662
1663 public:
1666 longlong val_int() override;
1667 const char *func_name() const override { return "char_length"; }
1668 bool resolve_type(THD *thd) override {
1669 max_length = 10;
1670 return Item_int_func::resolve_type(thd);
1671 }
1672};
1673
1675 public:
1677 null_on_null = false;
1678 }
1679 longlong val_int() override;
1680 const char *func_name() const override { return "coercibility"; }
1681 bool resolve_type(THD *thd) override {
1682 max_length = 10;
1683 set_nullable(false);
1684 return Item_int_func::resolve_type(thd);
1685 }
1686};
1687
1690
1691 public:
1694 : Item_int_func(pos, a, b) {}
1695 Item_func_locate(const POS &pos, Item *a, Item *b, Item *c)
1696 : Item_int_func(pos, a, b, c) {}
1697
1698 const char *func_name() const override { return "locate"; }
1699 longlong val_int() override;
1700 bool resolve_type(THD *thd) override;
1701 void print(const THD *thd, String *str,
1702 enum_query_type query_type) const override;
1703};
1704
1705class Item_func_instr final : public Item_func_locate {
1706 public:
1708 : Item_func_locate(pos, a, b) {}
1709
1710 const char *func_name() const override { return "instr"; }
1711};
1712
1714 public:
1716 : Item_int_func(pos, a) {}
1717 longlong val_int() override;
1718 const char *func_name() const override {
1719 return "validate_password_strength";
1720 }
1721 bool resolve_type(THD *thd) override {
1722 max_length = 10;
1723 set_nullable(true);
1724 return Item_int_func::resolve_type(thd);
1725 }
1726};
1727
1728class Item_func_field final : public Item_int_func {
1731
1732 public:
1734 : Item_int_func(pos, opt_list) {}
1735 longlong val_int() override;
1736 const char *func_name() const override { return "field"; }
1737 bool resolve_type(THD *thd) override;
1738};
1739
1740class Item_func_ascii final : public Item_int_func {
1742
1743 public:
1745 longlong val_int() override;
1746 const char *func_name() const override { return "ascii"; }
1747 bool resolve_type(THD *thd) override {
1748 max_length = 3;
1749 return Item_int_func::resolve_type(thd);
1750 }
1751};
1752
1753class Item_func_ord final : public Item_int_func {
1755
1756 public:
1758 longlong val_int() override;
1759 const char *func_name() const override { return "ord"; }
1760};
1761
1764 /*
1765 if m_enum_value is non-zero, it indicates the index of the value of
1766 argument 0 in the set in argument 1, given that argument 0 is
1767 a constant value and argument 1 is a field of type SET.
1768 */
1771
1772 public:
1774 : Item_int_func(pos, a, b) {}
1775 longlong val_int() override;
1776 const char *func_name() const override { return "find_in_set"; }
1777 bool resolve_type(THD *) override;
1778 const CHARSET_INFO *compare_collation() const override {
1779 return cmp_collation.collation;
1780 }
1781};
1782
1783/* Base class for all bit functions: '~', '|', '^', '&', '>>', '<<' */
1784
1785class Item_func_bit : public Item_func {
1786 protected:
1787 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1789 /// Buffer storing the determined value
1791 /**
1792 @returns true if the second argument should be of binary type for the
1793 result to be of binary type.
1794 */
1796
1797 public:
1798 Item_func_bit(const POS &pos, Item *a, Item *b) : Item_func(pos, a, b) {}
1799 Item_func_bit(const POS &pos, Item *a) : Item_func(pos, a) {}
1800
1801 bool resolve_type(THD *) override;
1802 enum Item_result result_type() const override { return hybrid_type; }
1803
1804 longlong val_int() override;
1805 String *val_str(String *str) override;
1806 double val_real() override;
1807 my_decimal *val_decimal(my_decimal *decimal_value) override;
1808
1809 void print(const THD *thd, String *str,
1810 enum_query_type query_type) const override {
1811 print_op(thd, str, query_type);
1812 }
1813 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1814 if (hybrid_type == INT_RESULT)
1815 return get_date_from_int(ltime, fuzzydate);
1816 else
1817 return get_date_from_string(ltime, fuzzydate);
1818 }
1819 bool get_time(MYSQL_TIME *ltime) override {
1820 if (hybrid_type == INT_RESULT)
1821 return get_time_from_int(ltime);
1822 else
1823 return get_time_from_string(ltime);
1824 }
1825
1826 private:
1827 /**
1828 @brief Performs the operation on integers to produce a result of type
1829 INT_RESULT.
1830 @return The result of the operation.
1831 */
1832 virtual longlong int_op() = 0;
1833
1834 /**
1835 @brief Performs the operation on binary strings to produce a result of
1836 type STRING_RESULT.
1837 @return The result of the operation.
1838 */
1839 virtual String *str_op(String *) = 0;
1840};
1841
1842/**
1843 Base class for all the bit functions that work with two binary
1844 arguments: '&', '|', '^'.
1845*/
1846
1848 protected:
1850 return true;
1851 }
1852 template <class Char_func, class Int_func>
1853 String *eval_str_op(String *, Char_func char_func, Int_func int_func);
1854 template <class Int_func>
1855 longlong eval_int_op(Int_func int_func);
1856
1857 public:
1859 : Item_func_bit(pos, a, b) {}
1860};
1861
1863 public:
1865 : Item_func_bit_two_param(pos, a, b) {}
1866 const char *func_name() const override { return "|"; }
1867
1868 private:
1869 longlong int_op() override { return eval_int_op(std::bit_or<ulonglong>()); }
1870 String *str_op(String *str) override {
1871 return eval_str_op(str, std::bit_or<char>(), std::bit_or<ulonglong>());
1872 }
1873};
1874
1876 public:
1878 : Item_func_bit_two_param(pos, a, b) {}
1879 const char *func_name() const override { return "&"; }
1880
1881 private:
1882 longlong int_op() override { return eval_int_op(std::bit_and<ulonglong>()); }
1883 String *str_op(String *str) override {
1884 return eval_str_op(str, std::bit_and<char>(), std::bit_and<ulonglong>());
1885 }
1886};
1887
1889 public:
1891 : Item_func_bit_two_param(pos, a, b) {}
1892 const char *func_name() const override { return "^"; }
1893
1894 private:
1895 longlong int_op() override { return eval_int_op(std::bit_xor<ulonglong>()); }
1896 String *str_op(String *str) override {
1897 return eval_str_op(str, std::bit_xor<char>(), std::bit_xor<ulonglong>());
1898 }
1899};
1900
1902 public:
1904 longlong val_int() override;
1905 const char *func_name() const override { return "bit_count"; }
1906 bool resolve_type(THD *thd) override {
1907 // Default: binary string; reprepare if integer
1908 if (args[0]->data_type() == MYSQL_TYPE_INVALID &&
1909 args[0]->propagate_type(
1911 return true;
1913 return false;
1914 }
1915};
1916
1918 protected:
1920 return false;
1921 }
1922 template <bool to_left>
1924 template <bool to_left>
1926
1927 public:
1929 : Item_func_bit(pos, a, b) {}
1930};
1931
1933 public:
1935 : Item_func_shift(pos, a, b) {}
1936 const char *func_name() const override { return "<<"; }
1937
1938 private:
1939 longlong int_op() override { return eval_int_op<true>(); }
1940 String *str_op(String *str) override { return eval_str_op<true>(str); }
1941};
1942
1944 public:
1946 : Item_func_shift(pos, a, b) {}
1947 const char *func_name() const override { return ">>"; }
1948
1949 private:
1950 longlong int_op() override { return eval_int_op<false>(); }
1951 String *str_op(String *str) override { return eval_str_op<false>(str); }
1952};
1953
1954class Item_func_bit_neg final : public Item_func_bit {
1955 protected:
1957 return false;
1958 }
1959
1960 public:
1962 const char *func_name() const override { return "~"; }
1963 void print(const THD *thd, String *str,
1964 enum_query_type query_type) const override {
1965 Item_func::print(thd, str, query_type);
1966 }
1967
1968 private:
1969 longlong int_op() override;
1970 String *str_op(String *str) override;
1971};
1972
1975
1976 public:
1980
1981 bool itemize(Parse_context *pc, Item **res) override;
1982 longlong val_int() override;
1983 const char *func_name() const override { return "last_insert_id"; }
1984 bool resolve_type(THD *thd) override {
1985 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
1986 unsigned_flag = true;
1987 return false;
1988 }
1989 bool check_function_as_value_generator(uchar *checker_args) override {
1991 pointer_cast<Check_function_as_value_generator_parameters *>(
1992 checker_args);
1993 func_arg->banned_function_name = func_name();
1994 return true;
1995 }
1996};
1997
2000
2001 public:
2002 Item_func_benchmark(const POS &pos, Item *count_expr, Item *expr)
2003 : Item_int_func(pos, count_expr, expr) {}
2004
2005 /// Ensure that "benchmark()" is never optimized away
2007 return RAND_TABLE_BIT;
2008 }
2009
2010 bool itemize(Parse_context *pc, Item **res) override;
2011 longlong val_int() override;
2012 const char *func_name() const override { return "benchmark"; }
2013 bool resolve_type(THD *thd) override {
2014 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_LONGLONG)) return true;
2015 if (param_type_is_default(thd, 1, 2)) return true;
2016 max_length = 1;
2017 set_nullable(true);
2018 return false;
2019 }
2020 void print(const THD *thd, String *str,
2021 enum_query_type query_type) const override;
2022 bool check_function_as_value_generator(uchar *checker_args) override {
2024 pointer_cast<Check_function_as_value_generator_parameters *>(
2025 checker_args);
2026 func_arg->banned_function_name = func_name();
2027 return true;
2028 }
2029};
2030
2033
2034class Item_func_sleep final : public Item_int_func {
2036
2037 public:
2039
2040 bool itemize(Parse_context *pc, Item **res) override;
2041 const char *func_name() const override { return "sleep"; }
2042 /**
2043 This function is non-deterministic and hence depends on the
2044 'RAND' pseudo-table.
2045
2046 @retval RAND_TABLE_BIT
2047 */
2049 return RAND_TABLE_BIT;
2050 }
2051 bool check_function_as_value_generator(uchar *checker_args) override {
2053 pointer_cast<Check_function_as_value_generator_parameters *>(
2054 checker_args);
2055 func_arg->banned_function_name = func_name();
2056 return true;
2057 }
2058 bool resolve_type(THD *thd) override {
2059 if (param_type_is_default(thd, 0, 1, MYSQL_TYPE_DOUBLE)) return true;
2060 return Item_int_func::resolve_type(thd);
2061 }
2062 longlong val_int() override;
2063};
2064
2065class Item_udf_func : public Item_func {
2067
2068 protected:
2070 bool is_expensive_processor(uchar *) override { return true; }
2071
2072 public:
2073 Item_udf_func(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2074 : Item_func(pos, opt_list), udf(udf_arg) {
2075 null_on_null = false;
2076 }
2077 ~Item_udf_func() override = default;
2078
2079 bool itemize(Parse_context *pc, Item **res) override;
2080 const char *func_name() const override { return udf.name(); }
2081 enum Functype functype() const override { return UDF_FUNC; }
2082 bool fix_fields(THD *thd, Item **ref) override;
2083 void update_used_tables() override {
2084 /*
2085 TODO: Make a member in UDF_INIT and return if a UDF is deterministic or
2086 not.
2087 Currently UDF_INIT has a member (const_item) that is an in/out
2088 parameter to the init() call.
2089 The code in udf_handler::fix_fields also duplicates the arguments
2090 handling code in Item_func::fix_fields().
2091
2092 The lack of information if a UDF is deterministic makes writing
2093 a correct update_used_tables() for UDFs impossible.
2094 One solution to this would be :
2095 - Add a is_deterministic member of UDF_INIT
2096 - (optionally) deprecate the const_item member of UDF_INIT
2097 - Take away the duplicate code from udf_handler::fix_fields() and
2098 make Item_udf_func call Item_func::fix_fields() to process its
2099 arguments as for any other function.
2100 - Store the deterministic flag returned by <udf>_init into the
2101 udf_handler.
2102 - Don't implement Item_udf_func::fix_fields, implement
2103 Item_udf_func::resolve_type() instead (similar to non-UDF functions).
2104 - Override Item_func::update_used_tables to call
2105 Item_func::update_used_tables() and add a RAND_TABLE_BIT to the
2106 result of Item_func::update_used_tables() if the UDF is
2107 non-deterministic.
2108 - (optionally) rename RAND_TABLE_BIT to NONDETERMINISTIC_BIT to
2109 better describe its usage.
2110
2111 The above would require a change of the UDF API.
2112 Until that change is done here's how the current code works:
2113 We call Item_func::update_used_tables() only when we know that
2114 the function depends on real non-const tables and is deterministic.
2115 This can be done only because we know that the optimizer will
2116 call update_used_tables() only when there's possibly a new const
2117 table. So update_used_tables() can only make a Item_func more
2118 constant than it is currently.
2119 That's why we don't need to do anything if a function is guaranteed
2120 to return non-constant (it's non-deterministic) or is already a
2121 const.
2122 */
2126
2128 assert(!null_on_null); // no need to update not_null_tables_cache
2129 }
2130 void cleanup() override;
2131 Item_result result_type() const override { return udf.result_type(); }
2132 bool is_expensive() override { return true; }
2133 void print(const THD *thd, String *str,
2134 enum_query_type query_type) const override;
2135
2136 bool check_function_as_value_generator(uchar *checker_args) override {
2138 pointer_cast<Check_function_as_value_generator_parameters *>(
2139 checker_args);
2140 func_arg->banned_function_name = func_name();
2141 return true;
2142 }
2143
2144 protected:
2145 bool may_have_named_parameters() const override { return true; }
2146};
2147
2149 public:
2150 Item_func_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2151 : Item_udf_func(pos, udf_arg, opt_list) {}
2152 longlong val_int() override {
2153 assert(fixed == 1);
2155 }
2156 my_decimal *val_decimal(my_decimal *dec_buf) override {
2157 double res = val_real();
2158 if (null_value) return nullptr;
2159 double2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf);
2160 return dec_buf;
2161 }
2162 double val_real() override;
2163 String *val_str(String *str) override;
2164 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2165 return get_date_from_real(ltime, fuzzydate);
2166 }
2167 bool get_time(MYSQL_TIME *ltime) override {
2168 return get_time_from_real(ltime);
2169 }
2170 bool resolve_type(THD *) override {
2172 fix_num_length_and_dec(); // @todo - needed?
2173 return false;
2174 }
2175};
2176
2177class Item_func_udf_int final : public Item_udf_func {
2178 public:
2179 Item_func_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2180 : Item_udf_func(pos, udf_arg, opt_list) {}
2181 longlong val_int() override;
2182 double val_real() override {
2183 return static_cast<double>(Item_func_udf_int::val_int());
2184 }
2185 String *val_str(String *str) override;
2186 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2187 return get_date_from_int(ltime, fuzzydate);
2188 }
2189 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2190 enum Item_result result_type() const override { return INT_RESULT; }
2191 bool resolve_type(THD *) override {
2193 return false;
2194 }
2195};
2196
2198 public:
2200 PT_item_list *opt_list)
2201 : Item_udf_func(pos, udf_arg, opt_list) {}
2202 longlong val_int() override;
2203 double val_real() override;
2204 my_decimal *val_decimal(my_decimal *) override;
2205 String *val_str(String *str) override;
2206 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2207 return get_date_from_decimal(ltime, fuzzydate);
2208 }
2209 bool get_time(MYSQL_TIME *ltime) override {
2210 return get_time_from_decimal(ltime);
2211 }
2212 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2213 bool resolve_type(THD *thd) override;
2214};
2215
2217 public:
2218 Item_func_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2219 : Item_udf_func(pos, udf_arg, opt_list) {}
2220
2221 String *val_str(String *) override;
2222 double val_real() override {
2223 int err_not_used;
2224 const char *end_not_used;
2225 String *res;
2226 res = val_str(&str_value);
2227 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2228 &end_not_used, &err_not_used)
2229 : 0.0;
2230 }
2231 longlong val_int() override {
2232 int err_not_used;
2233 String *res;
2234 res = val_str(&str_value);
2235 return res ? my_strntoll(res->charset(), res->ptr(), res->length(), 10,
2236 nullptr, &err_not_used)
2237 : (longlong)0;
2238 }
2239 my_decimal *val_decimal(my_decimal *dec_buf) override {
2240 String *res = val_str(&str_value);
2241 if (!res) return nullptr;
2242 str2my_decimal(E_DEC_FATAL_ERROR, res->ptr(), res->length(), res->charset(),
2243 dec_buf);
2244 return dec_buf;
2245 }
2246 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2247 return get_date_from_string(ltime, fuzzydate);
2248 }
2249 bool get_time(MYSQL_TIME *ltime) override {
2250 return get_time_from_string(ltime);
2251 }
2252 enum Item_result result_type() const override { return STRING_RESULT; }
2253 bool resolve_type(THD *thd) override;
2254};
2255
2256void mysql_ull_cleanup(THD *thd);
2258
2259class Item_func_get_lock final : public Item_int_func {
2261
2263
2264 public:
2266 : Item_int_func(pos, a, b) {}
2267
2268 bool itemize(Parse_context *pc, Item **res) override;
2269 longlong val_int() override;
2270 const char *func_name() const override { return "get_lock"; }
2271 bool resolve_type(THD *thd) override {
2272 if (param_type_is_default(thd, 0, 1)) return true;
2273 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_LONGLONG)) return true;
2274 max_length = 1;
2275 set_nullable(true);
2276 return false;
2277 }
2278 bool is_non_const_over_literals(uchar *) override { return true; }
2279 bool check_function_as_value_generator(uchar *checker_args) override {
2281 pointer_cast<Check_function_as_value_generator_parameters *>(
2282 checker_args);
2283 func_arg->banned_function_name = func_name();
2284 return true;
2285 }
2286};
2287
2290
2292
2293 public:
2295 bool itemize(Parse_context *pc, Item **res) override;
2296
2297 longlong val_int() override;
2298 const char *func_name() const override { return "release_lock"; }
2299 bool resolve_type(THD *thd) override {
2300 if (param_type_is_default(thd, 0, 1)) return true;
2301 max_length = 1;
2302 set_nullable(true);
2303 return false;
2304 }
2305 bool is_non_const_over_literals(uchar *) override { return true; }
2306 bool check_function_as_value_generator(uchar *checker_args) override {
2308 pointer_cast<Check_function_as_value_generator_parameters *>(
2309 checker_args);
2310 func_arg->banned_function_name = func_name();
2311 return true;
2312 }
2313};
2314
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_all_locks"; }
2324 bool resolve_type(THD *) override {
2325 unsigned_flag = true;
2326 return false;
2327 }
2328 bool is_non_const_over_literals(uchar *) override { return true; }
2329 bool check_function_as_value_generator(uchar *checker_args) override {
2331 pointer_cast<Check_function_as_value_generator_parameters *>(
2332 checker_args);
2333 func_arg->banned_function_name = func_name();
2334 return true;
2335 }
2336};
2337
2338/* replication functions */
2339
2343
2344 public:
2346 : Item_int_func(pos, a, b) {}
2348 : Item_int_func(pos, a, b, c) {}
2350 : Item_int_func(pos, a, b, c, d) {}
2351
2352 bool itemize(Parse_context *pc, Item **res) override;
2353 longlong val_int() override;
2354 const char *func_name() const override { return "source_pos_wait"; }
2355 bool resolve_type(THD *thd) override {
2356 if (param_type_is_default(thd, 0, 1)) return true;
2357 if (param_type_is_default(thd, 1, 3, MYSQL_TYPE_LONGLONG)) return true;
2358 if (param_type_is_default(thd, 3, 4)) return true;
2359 max_length = 21;
2360 set_nullable(true);
2361 return false;
2362 }
2363 bool check_function_as_value_generator(uchar *checker_args) override {
2365 pointer_cast<Check_function_as_value_generator_parameters *>(
2366 checker_args);
2367 func_arg->banned_function_name = func_name();
2368 return true;
2369 }
2370};
2371
2373 public:
2375 : Item_source_pos_wait(pos, a, b) {}
2377 : Item_source_pos_wait(pos, a, b, c) {}
2379 : Item_source_pos_wait(pos, a, b, c, d) {}
2380 longlong val_int() override;
2381};
2382
2383/**
2384 This class is used for implementing the new wait_for_executed_gtid_set
2385 function and the functions related to them. This new function is independent
2386 of the slave threads.
2387*/
2390
2392
2393 public:
2395 : Item_int_func(pos, a) {}
2397 : Item_int_func(pos, a, b) {}
2398
2399 bool itemize(Parse_context *pc, Item **res) override;
2400 longlong val_int() override;
2401 const char *func_name() const override {
2402 return "wait_for_executed_gtid_set";
2403 }
2404 bool resolve_type(THD *thd) override {
2405 if (param_type_is_default(thd, 0, 1)) return true;
2406 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2407 set_nullable(true);
2408 return false;
2409 }
2410};
2411
2414
2416
2417 public:
2419 Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b);
2420 Item_master_gtid_set_wait(const POS &pos, Item *a, Item *b, Item *c);
2421
2422 bool itemize(Parse_context *pc, Item **res) override;
2423 longlong val_int() override;
2424 const char *func_name() const override {
2425 return "wait_until_sql_thread_after_gtids";
2426 }
2427 bool resolve_type(THD *thd) override {
2428 if (param_type_is_default(thd, 0, 1)) return true;
2429 if (param_type_is_default(thd, 1, 2, MYSQL_TYPE_DOUBLE)) return true;
2430 if (param_type_is_default(thd, 2, 3)) return true;
2431 set_nullable(true);
2432 return false;
2433 }
2434};
2435
2439
2440 public:
2442 : Item_int_func(pos, a, b) {}
2443 longlong val_int() override;
2444 const char *func_name() const override { return "gtid_subset"; }
2445 bool resolve_type(THD *thd) override {
2446 if (param_type_is_default(thd, 0, ~0U)) return true;
2447 set_nullable(false);
2448 return false;
2449 }
2450 bool is_bool_func() const override { return true; }
2451};
2452
2453/**
2454 Internal functions used by INFORMATION_SCHEMA implementation to check
2455 if user have access to given database/table/column.
2456*/
2457
2459 public:
2461 : Item_int_func(pos, a) {}
2462 longlong val_int() override;
2463 const char *func_name() const override { return "can_access_database"; }
2464 bool resolve_type(THD *) override {
2465 set_nullable(true);
2466 return false;
2467 }
2468};
2469
2471 public:
2473 : Item_int_func(pos, a, b) {}
2474 longlong val_int() override;
2475 const char *func_name() const override { return "can_access_table"; }
2476 bool resolve_type(THD *) override {
2477 set_nullable(true);
2478 return false;
2479 }
2480};
2481
2483 public:
2485 : Item_int_func(pos, a, b) {}
2486 longlong val_int() override;
2487 const char *func_name() const override { return "can_access_user"; }
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_trigger"; }
2500 bool resolve_type(THD *) override {
2501 max_length = 4;
2502 set_nullable(true);
2503 return false;
2504 }
2505};
2506
2508 public:
2510 : Item_int_func(pos, list) {}
2511 longlong val_int() override;
2512 const char *func_name() const override { return "can_access_routine"; }
2513 bool resolve_type(THD *) override {
2514 max_length = 4;
2515 set_nullable(true);
2516 return false;
2517 }
2518};
2519
2521 public:
2523 longlong val_int() override;
2524 const char *func_name() const override { return "can_access_event"; }
2525 bool resolve_type(THD *) override {
2526 set_nullable(true);
2527 return false;
2528 }
2529};
2530
2532 public:
2534 : Item_int_func(pos, a) {}
2535 longlong val_int() override;
2536 const char *func_name() const override { return "can_access_resource_group"; }
2537 bool resolve_type(THD *) override {
2538 max_length = 1; // Function can return 0 or 1.
2539 set_nullable(true);
2540 return false;
2541 }
2542};
2543
2545 public:
2547 : Item_int_func(pos, a, b, c, d) {}
2548 longlong val_int() override;
2549 const char *func_name() const override { return "can_access_view"; }
2550 bool resolve_type(THD *) override {
2551 set_nullable(true);
2552 return false;
2553 }
2554};
2555
2557 public:
2559 : Item_int_func(pos, a, b, c) {}
2560 longlong val_int() override;
2561 const char *func_name() const override { return "can_access_column"; }
2562 bool resolve_type(THD *) override {
2563 set_nullable(true);
2564 return false;
2565 }
2566};
2567
2569 public:
2571 : Item_int_func(pos, a) {}
2573 : Item_int_func(pos, a, b) {}
2574 longlong val_int() override;
2575 const char *func_name() const override { return "is_visible_dd_object"; }
2576 bool resolve_type(THD *) override {
2577 max_length = 1;
2578 set_nullable(true);
2579 return false;
2580 }
2581};
2582
2584 public:
2586 : Item_int_func(pos, list) {}
2587 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2588 longlong val_int() override;
2589 const char *func_name() const override { return "internal_table_rows"; }
2590 bool resolve_type(THD *) override {
2591 set_nullable(true);
2592 unsigned_flag = true;
2593 null_on_null = false;
2594 return false;
2595 }
2596};
2597
2599 public:
2601 : Item_int_func(pos, list) {}
2602 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2603 longlong val_int() override;
2604 const char *func_name() const override { return "internal_avg_row_length"; }
2605 bool resolve_type(THD *) override {
2606 set_nullable(true);
2607 unsigned_flag = true;
2608 null_on_null = false;
2609 return false;
2610 }
2611};
2612
2614 public:
2616 : Item_int_func(pos, list) {}
2617 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2618 longlong val_int() override;
2619 const char *func_name() const override { return "internal_data_length"; }
2620 bool resolve_type(THD *) override {
2621 set_nullable(true);
2622 unsigned_flag = true;
2623 null_on_null = false;
2624 return false;
2625 }
2626};
2627
2629 public:
2631 : Item_int_func(pos, list) {}
2632 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2633 longlong val_int() override;
2634 const char *func_name() const override { return "internal_max_data_length"; }
2635 bool resolve_type(THD *) override {
2636 set_nullable(true);
2637 unsigned_flag = true;
2638 null_on_null = false;
2639 return false;
2640 }
2641};
2642
2644 public:
2646 : Item_int_func(pos, list) {}
2647 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2648 longlong val_int() override;
2649 const char *func_name() const override { return "internal_index_length"; }
2650 bool resolve_type(THD *) override {
2651 set_nullable(true);
2652 unsigned_flag = true;
2653 null_on_null = false;
2654 return false;
2655 }
2656};
2657
2659 public:
2661 : Item_int_func(pos, list) {}
2662 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2663 longlong val_int() override;
2664 const char *func_name() const override { return "internal_data_free"; }
2665 bool resolve_type(THD *) override {
2666 set_nullable(true);
2667 unsigned_flag = true;
2668 null_on_null = false;
2669 return false;
2670 }
2671};
2672
2674 public:
2676 : Item_int_func(pos, list) {}
2677 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2678 longlong val_int() override;
2679 const char *func_name() const override { return "internal_auto_increment"; }
2680 bool resolve_type(THD *) override {
2681 set_nullable(true);
2682 unsigned_flag = true;
2683 null_on_null = false;
2684 return false;
2685 }
2686};
2687
2689 public:
2691 : Item_int_func(pos, list) {}
2692 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2693 longlong val_int() override;
2694 const char *func_name() const override { return "internal_checksum"; }
2695 bool resolve_type(THD *) override {
2696 set_nullable(true);
2697 null_on_null = false;
2698 return false;
2699 }
2700};
2701
2703 public:
2705 : Item_int_func(pos, a) {}
2706 longlong val_int() override;
2707 const char *func_name() const override { return "internal_keys_disabled"; }
2708 bool resolve_type(THD *) override {
2709 set_nullable(false);
2710 null_on_null = false;
2711 return false;
2712 }
2713};
2714
2716 public:
2718 PT_item_list *list)
2719 : Item_int_func(pos, list) {}
2720 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2721 longlong val_int() override;
2722 const char *func_name() const override {
2723 return "internal_index_column_cardinality";
2724 }
2725 bool resolve_type(THD *) override {
2726 set_nullable(true);
2727 null_on_null = false;
2728 return false;
2729 }
2730};
2731
2733 public:
2735 Item *d)
2736 : Item_int_func(pos, a, b, c, d) {}
2737 longlong val_int() override;
2738 const char *func_name() const override { return "internal_dd_char_length"; }
2739 bool resolve_type(THD *) override {
2740 set_nullable(true);
2741 null_on_null = false;
2742 return false;
2743 }
2744};
2745
2747 : public Item_int_func {
2748 public:
2750 PT_item_list *list)
2751 : Item_int_func(pos, list) {}
2752 longlong val_int() override;
2753 const char *func_name() const override {
2754 return "internal_get_view_warning_or_error";
2755 }
2756 bool resolve_type(THD *) override {
2757 max_length = 1;
2758 set_nullable(false);
2759 null_on_null = false;
2760 return false;
2761 }
2762};
2763
2765 public:
2767 : Item_int_func(pos, list) {}
2768 longlong val_int() override;
2769 bool resolve_type(THD *) override {
2770 set_nullable(true);
2771 null_on_null = false;
2772 return false;
2773 }
2774 const char *func_name() const override {
2775 return "get_dd_index_sub_part_length";
2776 }
2777};
2778
2780 public:
2782 Item *d)
2783 : Item_int_func(pos, a, b, c, d) {}
2784 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2785 longlong val_int() override;
2786 const char *func_name() const override { return "internal_tablespace_id"; }
2787 bool resolve_type(THD *) override {
2788 set_nullable(true);
2789 null_on_null = false;
2790 return false;
2791 }
2792};
2793
2795 : public Item_int_func {
2796 public:
2798 Item *b, Item *c, Item *d)
2799 : Item_int_func(pos, a, b, c, d) {}
2800
2801 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2802 longlong val_int() override;
2803
2804 const char *func_name() const override {
2805 return "internal_tablespace_logfile_group_number";
2806 }
2807
2808 bool resolve_type(THD *) override {
2809 set_nullable(true);
2810 null_on_null = false;
2811 return false;
2812 }
2813};
2814
2816 public:
2818 Item *c, Item *d)
2819 : Item_int_func(pos, a, b, c, d) {}
2820
2821 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2822 longlong val_int() override;
2823
2824 const char *func_name() const override {
2825 return "internal_tablespace_free_extents";
2826 }
2827
2828 bool resolve_type(THD *) override {
2829 set_nullable(true);
2830 null_on_null = false;
2831 return false;
2832 }
2833};
2834
2836 public:
2838 Item *c, Item *d)
2839 : Item_int_func(pos, a, b, c, d) {}
2840
2841 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2842 longlong val_int() override;
2843
2844 const char *func_name() const override {
2845 return "internal_tablespace_total_extents";
2846 }
2847
2848 bool resolve_type(THD *) override {
2849 set_nullable(true);
2850 null_on_null = false;
2851 return false;
2852 }
2853};
2854
2856 public:
2858 Item *c, Item *d)
2859 : Item_int_func(pos, a, b, c, d) {}
2860
2861 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2862 longlong val_int() override;
2863
2864 const char *func_name() const override {
2865 return "internal_tablespace_extent_size";
2866 }
2867
2868 bool resolve_type(THD *) override {
2869 set_nullable(true);
2870 null_on_null = false;
2871 return false;
2872 }
2873};
2874
2876 public:
2878 Item *c, Item *d)
2879 : Item_int_func(pos, a, b, c, d) {}
2880
2881 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2882 longlong val_int() override;
2883
2884 const char *func_name() const override {
2885 return "internal_tablespace_initial_size";
2886 }
2887
2888 bool resolve_type(THD *) override {
2889 set_nullable(true);
2890 null_on_null = false;
2891 return false;
2892 }
2893};
2894
2896 public:
2898 Item *c, Item *d)
2899 : Item_int_func(pos, a, b, c, d) {}
2900
2901 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2902 longlong val_int() override;
2903
2904 const char *func_name() const override {
2905 return "internal_tablespace_maximum_size";
2906 }
2907
2908 bool resolve_type(THD *) override {
2909 set_nullable(true);
2910 null_on_null = false;
2911 return false;
2912 }
2913};
2914
2916 public:
2918 Item *b, Item *c, Item *d)
2919 : Item_int_func(pos, a, b, c, d) {}
2920
2921 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2922 longlong val_int() override;
2923
2924 const char *func_name() const override {
2925 return "internal_tablespace_autoextend_size";
2926 }
2927
2928 bool resolve_type(THD *) override {
2929 set_nullable(true);
2930 null_on_null = false;
2931 return false;
2932 }
2933};
2934
2936 public:
2938 Item *c, Item *d)
2939 : Item_int_func(pos, a, b, c, d) {}
2940
2941 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2942 longlong val_int() override;
2943
2944 const char *func_name() const override {
2945 return "internal_tablespace_version";
2946 }
2947
2948 bool resolve_type(THD *) override {
2949 set_nullable(true);
2950 null_on_null = false;
2951 return false;
2952 }
2953};
2954
2956 public:
2958 Item *c, Item *d)
2959 : Item_int_func(pos, a, b, c, d) {}
2960
2961 enum Functype functype() const override { return DD_INTERNAL_FUNC; }
2962 longlong val_int() override;
2963
2964 const char *func_name() const override {
2965 return "internal_tablespace_data_free";
2966 }
2967
2968 bool resolve_type(THD *) override {
2969 set_nullable(true);
2970 null_on_null = false;
2971 return false;
2972 }
2973};
2974
2975/**
2976 Common class for:
2977 Item_func_get_system_var
2978 Item_func_get_user_var
2979 Item_func_set_user_var
2980*/
2981class Item_var_func : public Item_func {
2982 public:
2984 explicit Item_var_func(const POS &pos) : Item_func(pos) {}
2985
2986 Item_var_func(THD *thd, Item_var_func *item) : Item_func(thd, item) {}
2987
2989 Item_var_func(const POS &pos, Item *a) : Item_func(pos, a) {}
2990
2991 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2992 return get_date_from_non_temporal(ltime, fuzzydate);
2993 }
2994 bool get_time(MYSQL_TIME *ltime) override {
2995 return get_time_from_non_temporal(ltime);
2996 }
2997 bool check_function_as_value_generator(uchar *checker_args) override {
2999 pointer_cast<Check_function_as_value_generator_parameters *>(
3000 checker_args);
3001 func_arg->err_code = (func_arg->source == VGS_CHECK_CONSTRAINT)
3002 ? ER_CHECK_CONSTRAINT_VARIABLES
3003 : ER_DEFAULT_VAL_GENERATED_VARIABLES;
3004 return true;
3005 }
3006};
3007
3008/* Handling of user definable variables */
3009
3010// this is needed for user_vars hash
3013 m_ptr = nullptr;
3014 m_length = 0;
3015 }
3016 void set_value(char *value, size_t length) {
3017 m_ptr = value;
3018 m_length = length;
3019 }
3020
3021 /**
3022 Position inside a user_var_entry where small values are stored:
3023 double values, longlong values and string values with length
3024 up to extra_size (should be 8 bytes on all platforms).
3025 String values with length longer than 8 are stored in a separate
3026 memory buffer, which is allocated when needed using the method realloc().
3027 */
3029 return pointer_cast<char *>(this) + ALIGN_SIZE(sizeof(user_var_entry));
3030 }
3031
3032 /**
3033 Position inside a user_var_entry where a null-terminates array
3034 of characters representing the variable name is stored.
3035 */
3037
3038 /**
3039 Initialize m_ptr to the internal buffer (if the value is small enough),
3040 or allocate a separate buffer.
3041 @param length - length of the value to be stored.
3042 */
3043 bool mem_realloc(size_t length);
3044
3045 /**
3046 Check if m_ptr point to an external buffer previously alloced by realloc().
3047 @retval true - an external buffer is alloced.
3048 @retval false - m_ptr is null, or points to the internal buffer.
3049 */
3050 bool alloced() { return m_ptr && m_ptr != internal_buffer_ptr(); }
3051
3052 /**
3053 Free the external value buffer, if it's allocated.
3054 */
3055 void free_value() {
3056 if (alloced()) my_free(m_ptr);
3057 }
3058
3059 /**
3060 Copy the array of characters from the given name into the internal
3061 name buffer and initialize entry_name to point to it.
3062 */
3064 name.strcpy(name_ptr());
3065 entry_name = Name_string(name_ptr(), name.length());
3066 }
3067
3068 /**
3069 Initialize all members
3070
3071 @param thd Current session.
3072 @param name Name of the user_var_entry instance.
3073 @param cs charset information of the user_var_entry instance.
3074 */
3075 void init(THD *thd, const Simple_cstring &name, const CHARSET_INFO *cs);
3076
3077 /**
3078 Store a value of the given type into a user_var_entry instance.
3079 @param from Value
3080 @param length Size of the value
3081 @param type type
3082 @retval false on success
3083 @retval true on memory allocation error
3084 */
3085 bool store(const void *from, size_t length, Item_result type);
3086
3087 /**
3088 Assert the user variable is locked.
3089 This is debug code only.
3090 The thread LOCK_thd_data mutex protects:
3091 - the thd->user_vars hash itself
3092 - the values in the user variable itself.
3093 The protection is required for monitoring,
3094 as a different thread can inspect this session
3095 user variables, on a live session.
3096 */
3097 void assert_locked() const;
3098
3099 static const size_t extra_size = sizeof(double);
3100 char *m_ptr; ///< Value
3101 size_t m_length; ///< Value length
3102 Item_result m_type; ///< Value type
3104 /**
3105 Set to the id of the most recent query that has used the variable.
3106 Used in binlogging: When set, there is no need to add a reference to this
3107 variable to the binlog. Imagine it is this:
3108
3109 INSERT INTO t SELECT @a:=10, @a:=@a+1.
3110
3111 Then we have a Item_func_get_user_var (because of the `@a+1`) so we
3112 think we have to write the value of `@a` to the binlog. But before that,
3113 we have a Item_func_set_user_var to create `@a` (`@a:=10`), in this we mark
3114 the variable as "already logged" so that it won't be logged
3115 by Item_func_get_user_var (because that's not necessary).
3116 */
3118
3119 public:
3120 user_var_entry() = default; /* Remove gcc warning */
3121
3122 THD *owner_session() const { return m_owner; }
3123
3124 Simple_cstring entry_name; // Variable name
3125 DTCollation collation; // Collation with attributes
3126 bool unsigned_flag; // true if unsigned, false if signed
3127
3128 /**
3129 Set value to user variable.
3130
3131 @param ptr pointer to buffer with new value
3132 @param length length of new value
3133 @param type type of new value
3134 @param cs charset info for new value
3135 @param dv derivation for new value
3136 @param unsigned_arg indiates if a value of type INT_RESULT is unsigned
3137
3138 @note Sets error and fatal error if allocation fails.
3139
3140 @retval
3141 false success
3142 @retval
3143 true failure
3144 */
3145 bool store(const void *ptr, size_t length, Item_result type,
3146 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3147 /**
3148 Set type of to the given value.
3149 @param type Data type.
3150 */
3152 assert_locked();
3153 m_type = type;
3154 }
3155 /**
3156 Set value to NULL
3157 @param type Data type.
3158 */
3159
3161 assert_locked();
3162 free_value();
3163 reset_value();
3164 m_type = type;
3165 }
3166
3167 void set_used_query_id(query_id_t query_id) { m_used_query_id = query_id; }
3169
3170 /**
3171 Allocates and initializes a user variable instance.
3172
3173 @param thd Current session.
3174 @param name Name of the variable.
3175 @param cs Charset of the variable.
3176
3177 @return Address of the allocated and initialized user_var_entry instance.
3178 @retval NULL On allocation error.
3179 */
3180 static user_var_entry *create(THD *thd, const Name_string &name,
3181 const CHARSET_INFO *cs);
3182
3183 /**
3184 Free all memory used by a user_var_entry instance
3185 previously created by create().
3186 */
3187 void destroy() {
3188 assert_locked();
3189 free_value(); // Free the external value buffer
3190 my_free(this); // Free the instance itself
3191 }
3192
3193 void lock();
3194 void unlock();
3195
3196 /* Routines to access the value and its type */
3197 const char *ptr() const { return m_ptr; }
3198 size_t length() const { return m_length; }
3199 Item_result type() const { return m_type; }
3200 /* Item-alike routines to access the value */
3201 double val_real(bool *null_value) const;
3202 longlong val_int(bool *null_value) const;
3203 String *val_str(bool *null_value, String *str, uint decimals) const;
3204 my_decimal *val_decimal(bool *null_value, my_decimal *result) const;
3205};
3206
3207/**
3208 This class is used to implement operations like
3209 SET \@variable or \@variable:= expression.
3210*/
3211
3218 union {
3220 double vreal;
3224
3225 public:
3226 Name_string name; // keep it public
3227
3230 : Item_var_func(pos, b), name(a) {}
3231
3233 : Item_var_func(thd, item),
3235 entry(item->entry),
3236 value(item->value),
3238 null_item(item->null_item),
3239 save_result(item->save_result),
3240 name(item->name) {}
3241 enum Functype functype() const override { return SUSERVAR_FUNC; }
3242 double val_real() override;
3243 longlong val_int() override;
3244 String *val_str(String *str) override;
3245 my_decimal *val_decimal(my_decimal *) override;
3246 bool update_hash(const void *ptr, uint length, enum Item_result type,
3247 const CHARSET_INFO *cs, Derivation dv, bool unsigned_arg);
3248 bool send(Protocol *protocol, String *str_arg) override;
3249 void make_field(Send_field *tmp_field) override;
3250 bool check(bool use_result_field);
3251 void save_item_result(Item *item);
3252 bool update();
3253 enum Item_result result_type() const override { return cached_result_type; }
3254 bool fix_fields(THD *thd, Item **ref) override;
3255 bool resolve_type(THD *) override;
3256 void print(const THD *thd, String *str,
3257 enum_query_type query_type) const override;
3258 void print_assignment(const THD *thd, String *str,
3259 enum_query_type query_type) const;
3260 const char *func_name() const override { return "set_user_var"; }
3261
3262 type_conversion_status save_in_field(Field *field, bool no_conversions,
3263 bool can_use_result_field);
3264
3265 void save_org_in_field(Field *field) override {
3266 save_in_field(field, true, false);
3267 }
3268
3269 bool set_entry(THD *thd, bool create_if_not_exists);
3270 void cleanup() override;
3271
3272 protected:
3274 bool no_conversions) override {
3275 return save_in_field(field, no_conversions, true);
3276 }
3277};
3278
3283
3284 public:
3285 Name_string name; // keep it public
3286
3291
3292 enum Functype functype() const override { return GUSERVAR_FUNC; }
3293 double val_real() override;
3294 longlong val_int() override;
3295 my_decimal *val_decimal(my_decimal *) override;
3296 String *val_str(String *str) override;
3297 const CHARSET_INFO *charset_for_protocol() override;
3298 bool resolve_type(THD *) override;
3299 bool propagate_type(THD *thd, const Type_properties &type) override;
3300 void cleanup() override;
3301 void update_used_tables() override {} // Keep existing used tables
3302 void print(const THD *thd, String *str,
3303 enum_query_type query_type) const override;
3304 enum Item_result result_type() const override;
3305 /*
3306 We must always return variables as strings to guard against selects of type
3307 select @t1:=1,@t1,@t:="hello",@t from foo where (@t1:= t2.b)
3308 */
3309 const char *func_name() const override { return "get_user_var"; }
3310 bool is_non_const_over_literals(uchar *) override { return true; }
3311 bool eq(const Item *item, bool binary_cmp) const override;
3312
3313 private:
3314 bool set_value(THD *thd, sp_rcontext *ctx, Item **it) override;
3315
3316 public:
3318 return this;
3319 }
3320};
3321
3322/*
3323 This item represents user variable used as out parameter (e.g in LOAD DATA),
3324 and it is supposed to be used only for this purprose. So it is simplified
3325 a lot. Actually you should never obtain its value.
3326
3327 The only two reasons for this thing being an Item is possibility to store it
3328 in const mem_root_deque<Item> and desire to place this code somewhere near
3329 other functions working with user variables.
3330*/
3334
3335 public:
3337 : Item(pos), name(a) {
3338 item_name.copy(a);
3339 }
3340 /* We should return something different from FIELD_ITEM here */
3341 enum Type type() const override { return STRING_ITEM; }
3342 double val_real() override;
3343 longlong val_int() override;
3344 String *val_str(String *str) override;
3345 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
3347 assert(0);
3348 return true;
3349 }
3350 bool get_time(MYSQL_TIME *) override {
3351 assert(0);
3352 return true;
3353 }
3354
3355 /* fix_fields() binds variable name with its entry structure */
3356 bool fix_fields(THD *thd, Item **ref) override;
3357 void print(const THD *thd, String *str,
3358 enum_query_type query_type) const override;
3359 void set_null_value(const CHARSET_INFO *cs);
3360 void set_value(const char *str, size_t length, const CHARSET_INFO *cs);
3361};
3362
3363/* A system variable */
3364
3365#define GET_SYS_VAR_CACHE_LONG 1
3366#define GET_SYS_VAR_CACHE_DOUBLE 2
3367#define GET_SYS_VAR_CACHE_STRING 4
3368
3370
3371/** Class to log audit event MYSQL_AUDIT_GLOBAL_VARIABLE_GET. */
3373 public:
3377
3378 private:
3379 // Thread handle.
3381
3382 // Item_func_get_system_var instance.
3384
3385 /*
3386 Value conversion type.
3387 Depending on the value conversion type GET_SYS_VAR_CACHE_* is stored in this
3388 member while creating the object. While converting value if there are any
3389 intermediate conversions in the same query then this member is used to avoid
3390 auditing more than once.
3391 */
3393
3394 /*
3395 To indicate event auditing is required or not. Event is not audited if
3396 * scope of the variable is *not* GLOBAL.
3397 * or the event is already audited for global variable for the same query.
3398 */
3400};
3401
3411
3412 template <typename T>
3414
3416
3417 public:
3419 enum_var_type scope);
3420 enum Functype functype() const override { return GSYSVAR_FUNC; }
3422 return INNER_TABLE_BIT;
3423 }
3424 bool resolve_type(THD *) override;
3425 void print(const THD *thd, String *str,
3426 enum_query_type query_type) const override;
3427 bool is_non_const_over_literals(uchar *) override { return true; }
3428 enum Item_result result_type() const override {
3429 assert(fixed);
3430 return type_to_result(data_type());
3431 }
3432 double val_real() override;
3433 longlong val_int() override;
3434 String *val_str(String *) override;
3435 my_decimal *val_decimal(my_decimal *dec_buf) override {
3436 return val_decimal_from_real(dec_buf);
3437 }
3438 /* TODO: fix to support views */
3439 const char *func_name() const override { return "get_system_var"; }
3440 bool eq(const Item *item, bool binary_cmp) const override;
3441
3442 void cleanup() override;
3443};
3444
3445class JOIN;
3446
3447class Item_func_match final : public Item_real_func {
3449
3450 public:
3453 /// True if we are doing a full-text index scan with this MATCH function as a
3454 /// predicate, and the score can be retrieved with get_relevance(). If it is
3455 /// false, the score of the document must be retrieved with find_relevance().
3460 /**
3461 Master item means that if identical items are present in the
3462 statement, they use the same FT handler. FT handler is initialized
3463 only for master item and slave items just use it. FT hints initialized
3464 for master only, slave items HINTS are not accessed.
3465 */
3467 Item *concat_ws; // Item_func_concat_ws
3468 String value; // value of concat_ws
3469 String search_value; // key_item()'s value converted to cmp_collation
3470
3471 /**
3472 Constructor for Item_func_match class.
3473
3474 @param pos Position of token in the parser.
3475 @param a List of arguments.
3476 @param against_arg Expression to match against.
3477 @param b FT Flags.
3478 */
3479 Item_func_match(const POS &pos, PT_item_list *a, Item *against_arg, uint b)
3480 : Item_real_func(pos, a),
3481 against(against_arg),
3482 key(0),
3483 flags(b),
3486 master(nullptr),
3488 hints(nullptr),
3489 simple_expression(false),
3490 used_in_where_only(false) {
3491 null_on_null = false;
3492 }
3493
3494 bool itemize(Parse_context *pc, Item **res) override;
3495
3496 void cleanup() override {
3497 DBUG_TRACE;
3499 if (master == nullptr && ft_handler != nullptr) {
3501 }
3502 score_from_index_scan = false;
3503 ft_handler = nullptr;
3504 concat_ws = nullptr;
3505 return;
3506 }
3507 Item *key_item() const override { return against; }
3508 enum Functype functype() const override { return FT_FUNC; }
3509 const char *func_name() const override { return "match"; }
3510 bool fix_fields(THD *thd, Item **ref) override;
3511 bool eq(const Item *, bool binary_cmp) const override;
3512 /* The following should be safe, even if we compare doubles */
3513 longlong val_int() override {
3514 assert(fixed);
3515 return val_real() != 0.0;
3516 }
3517 double val_real() override;
3518 void print(const THD *thd, String *str,
3519 enum_query_type query_type) const override;
3520
3521 bool fix_index(const THD *thd);
3522 bool init_search(THD *thd);
3523 bool check_function_as_value_generator(uchar *checker_args) override {
3525 pointer_cast<Check_function_as_value_generator_parameters *>(
3526 checker_args);
3527 func_arg->banned_function_name = func_name();
3528 return true;
3529 }
3530
3531 /**
3532 Get number of matching rows from FT handler.
3533
3534 @note Requires that FT handler supports the extended API
3535
3536 @return Number of matching rows in result
3537 */
3539 assert(ft_handler);
3541
3542 return ((FT_INFO_EXT *)ft_handler)
3543 ->could_you->count_matches((FT_INFO_EXT *)ft_handler);
3544 }
3545
3546 /**
3547 Check whether FT result is ordered on rank
3548
3549 @return true if result is ordered
3550 @return false otherwise
3551 */
3553 assert(!master);
3554 if (hints->get_flags() & FT_SORTED) return true;
3555
3557 return false;
3558
3559 assert(ft_handler);
3560 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3562 }
3563
3564 /**
3565 Check whether FT result contains the document ID
3566
3567 @return true if document ID is available
3568 @return false otherwise
3569 */
3571 assert(ft_handler);
3572
3574 return false;
3575
3576 return ((FT_INFO_EXT *)ft_handler)->could_you->get_flags() &
3578 }
3579
3580 float get_filtering_effect(THD *thd, table_map filter_for_table,
3581 table_map read_tables,
3582 const MY_BITMAP *fields_to_ignore,
3583 double rows_in_table) override;
3584
3585 /**
3586 Returns master MATCH function.
3587
3588 @return pointer to master MATCH function.
3589 */
3591 if (master) return master->get_master();
3592 return this;
3593 }
3594
3595 /**
3596 Set master MATCH function and adjust used_in_where_only value.
3597
3598 @param item item for which master should be set.
3599 */
3602 item->master = this;
3603 }
3604
3605 /**
3606 Returns pointer to Ft_hints object belonging to master MATCH function.
3607
3608 @return pointer to Ft_hints object
3609 */
3611 assert(!master);
3612 return hints;
3613 }
3614
3615 /**
3616 Set comparison operation type and and value for master MATCH function.
3617
3618 @param type comparison operation type
3619 @param value_arg comparison operation value
3620 */
3621 void set_hints_op(enum ft_operation type, double value_arg) {
3622 assert(!master);
3623 hints->set_hint_op(type, value_arg);
3624 }
3625
3626 /**
3627 Set FT hints.
3628 */
3629 void set_hints(JOIN *join, uint ft_flag, ha_rows ft_limit, bool no_cond);
3630
3631 /**
3632 Check if ranking is not needed.
3633
3634 @return true if ranking is not needed
3635 @return false otherwise
3636 */
3638 assert(!master);
3639 return (!(hints->get_flags() & FT_SORTED) && // FT_SORTED is no set
3640 used_in_where_only && // MATCH result is not used
3641 // in expression
3642 hints->get_op_type() == FT_OP_NO); // MATCH is single function
3643 }
3644
3645 /**
3646 Set flag that the function is a simple expression.
3647
3648 @param val true if the function is a simple expression, false otherwise
3649 */
3650 void set_simple_expression(bool val) {
3651 assert(!master);
3652 simple_expression = val;
3653 }
3654
3655 /**
3656 Check if this MATCH function is a simple expression in WHERE condition.
3657
3658 @return true if simple expression
3659 @return false otherwise
3660 */
3662 assert(!master);
3663 return simple_expression;
3664 }
3665
3666 private:
3667 /**
3668 Fulltext index hints, initialized for master MATCH function only.
3669 */
3671 /**
3672 Flag is true when MATCH function is used as a simple expression in
3673 WHERE condition, i.e. there is no AND/OR combinations, just simple
3674 MATCH function or [MATCH, rank] comparison operation.
3675 */
3677 /**
3678 true if MATCH function is used in WHERE condition only.
3679 Used to dermine what hints can be used for FT handler.
3680 Note that only master MATCH function has valid value.
3681 it's ok since only master function is involved in the hint processing.
3682 */
3684 /**
3685 Check whether storage engine for given table,
3686 allows FTS Boolean search on non-indexed columns.
3687
3688 @todo A flag should be added to the extended fulltext API so that
3689 it may be checked whether search on non-indexed columns are
3690 supported. Currently, it is not possible to check for such a
3691 flag since @c this->ft_handler is not yet set when this function is
3692 called. The current hack is to assume that search on non-indexed
3693 columns are supported for engines that does not support the extended
3694 fulltext API (e.g., MyISAM), while it is not supported for other
3695 engines (e.g., InnoDB)
3696
3697 @param tr Table for which storage engine to check
3698
3699 @retval true if BOOLEAN search on non-indexed columns is supported
3700 @retval false otherwise
3701 */
3703 // Only Boolean search may support non_indexed columns
3704 if (!(flags & FT_BOOL)) return false;
3705
3706 assert(tr && tr->file);
3707
3708 // Assume that if extended fulltext API is not supported,
3709 // non-indexed columns are allowed. This will be true for MyISAM.
3710 if ((tr->file->ha_table_flags() & HA_CAN_FULLTEXT_EXT) == 0) return true;
3711
3712 return false;
3713 }
3714};
3715
3718
3720
3721 public:
3723
3724 bool itemize(Parse_context *pc, Item **res) override;
3725 longlong val_int() override;
3726 const char *func_name() const override { return "is_free_lock"; }
3727 bool resolve_type(THD *thd) override {
3728 if (param_type_is_default(thd, 0, 1)) return true;
3729 max_length = 1;
3730 set_nullable(true);
3731 return false;
3732 }
3733 bool is_non_const_over_literals(uchar *) override { return true; }
3734 bool check_function_as_value_generator(uchar *checker_args) override {
3736 pointer_cast<Check_function_as_value_generator_parameters *>(
3737 checker_args);
3738 func_arg->banned_function_name = func_name();
3739 return true;
3740 }
3741};
3742
3745
3747
3748 public:
3750
3751 bool itemize(Parse_context *pc, Item **res) override;
3752 longlong val_int() override;
3753 const char *func_name() const override { return "is_used_lock"; }
3754 bool resolve_type(THD *thd) override {
3755 if (param_type_is_default(thd, 0, 1)) return true;
3756 unsigned_flag = true;
3757 set_nullable(true);
3758 return false;
3759 }
3760 bool is_non_const_over_literals(uchar *) override { return true; }