1#ifndef SQL_ITEM_INCLUDED
2#define SQL_ITEM_INCLUDED
3
4/* Copyright (c) 2000, 2017, Oracle and/or its affiliates.
5 Copyright (c) 2009, 2018, MariaDB Corporation
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 of the License.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
19
20
21#ifdef USE_PRAGMA_INTERFACE
22#pragma interface /* gcc class implementation */
23#endif
24
25#include "sql_priv.h" /* STRING_BUFFER_USUAL_SIZE */
26#include "unireg.h"
27#include "sql_const.h" /* RAND_TABLE_BIT, MAX_FIELD_NAME */
28#include "field.h" /* Derivation */
29#include "sql_type.h"
30#include "sql_time.h"
31
32C_MODE_START
33#include <ma_dyncol.h>
34
35/*
36 A prototype for a C-compatible structure to store a value of any data type.
37 Currently it has to stay in /sql, as it depends on String and my_decimal.
38 We'll do the following changes:
39 1. add pure C "struct st_string" and "struct st_my_decimal"
40 2. change type of m_string to struct st_string and move inside the union
41 3. change type of m_decmal to struct st_my_decimal and move inside the union
42 4. move the definition to some file in /include
43*/
44struct st_value
45{
46 enum enum_dynamic_column_type m_type;
47 union
48 {
49 longlong m_longlong;
50 double m_double;
51 MYSQL_TIME m_time;
52 } value;
53 String m_string;
54 my_decimal m_decimal;
55};
56
57C_MODE_END
58
59
60class Value: public st_value
61{
62public:
63 bool is_null() const { return m_type == DYN_COL_NULL; }
64 bool is_longlong() const
65 {
66 return m_type == DYN_COL_UINT || m_type == DYN_COL_INT;
67 }
68 bool is_double() const { return m_type == DYN_COL_DOUBLE; }
69 bool is_temporal() const { return m_type == DYN_COL_DATETIME; }
70 bool is_string() const { return m_type == DYN_COL_STRING; }
71 bool is_decimal() const { return m_type == DYN_COL_DECIMAL; }
72};
73
74
75template<size_t buffer_size>
76class ValueBuffer: public Value
77{
78 char buffer[buffer_size];
79 void reset_buffer()
80 {
81 m_string.set(buffer, buffer_size, &my_charset_bin);
82 }
83public:
84 ValueBuffer()
85 {
86 reset_buffer();
87 }
88};
89
90
91#ifdef DBUG_OFF
92static inline const char *dbug_print_item(Item *item) { return NULL; }
93#else
94const char *dbug_print_item(Item *item);
95#endif
96
97class Virtual_tmp_table;
98class sp_head;
99class Protocol;
100struct TABLE_LIST;
101void item_init(void); /* Init item functions */
102class Item_field;
103class Item_param;
104class user_var_entry;
105class JOIN;
106struct KEY_FIELD;
107struct SARGABLE_PARAM;
108class RANGE_OPT_PARAM;
109class SEL_TREE;
110
111enum precedence {
112 LOWEST_PRECEDENCE,
113 ASSIGN_PRECEDENCE, // :=
114 OR_PRECEDENCE, // OR, || (unless PIPES_AS_CONCAT)
115 XOR_PRECEDENCE, // XOR
116 AND_PRECEDENCE, // AND, &&
117 NOT_PRECEDENCE, // NOT (unless HIGH_NOT_PRECEDENCE)
118 BETWEEN_PRECEDENCE, // BETWEEN, CASE, WHEN, THEN, ELSE
119 CMP_PRECEDENCE, // =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
120 BITOR_PRECEDENCE, // |
121 BITAND_PRECEDENCE, // &
122 SHIFT_PRECEDENCE, // <<, >>
123 ADDINTERVAL_PRECEDENCE, // first argument in +INTERVAL
124 ADD_PRECEDENCE, // +, -
125 MUL_PRECEDENCE, // *, /, DIV, %, MOD
126 BITXOR_PRECEDENCE, // ^
127 PIPES_PRECEDENCE, // || (if PIPES_AS_CONCAT)
128 NEG_PRECEDENCE, // unary -, ~
129 BANG_PRECEDENCE, // !, NOT (if HIGH_NOT_PRECEDENCE)
130 COLLATE_PRECEDENCE, // BINARY, COLLATE
131 INTERVAL_PRECEDENCE, // INTERVAL
132 DEFAULT_PRECEDENCE,
133 HIGHEST_PRECEDENCE
134};
135
136bool mark_unsupported_function(const char *where, void *store, uint result);
137
138/* convenience helper for mark_unsupported_function() above */
139bool mark_unsupported_function(const char *w1, const char *w2,
140 void *store, uint result);
141
142/* Bits for the split_sum_func() function */
143#define SPLIT_SUM_SKIP_REGISTERED 1 /* Skip registered funcs */
144#define SPLIT_SUM_SELECT 2 /* SELECT item; Split all parts */
145
146
147#define NO_EXTRACTION_FL (1 << 6)
148#define FULL_EXTRACTION_FL (1 << 7)
149#define EXTRACTION_MASK (NO_EXTRACTION_FL | FULL_EXTRACTION_FL)
150
151extern const char *item_empty_name;
152
153void dummy_error_processor(THD *thd, void *data);
154
155void view_error_processor(THD *thd, void *data);
156
157/*
158 Instances of Name_resolution_context store the information necesary for
159 name resolution of Items and other context analysis of a query made in
160 fix_fields().
161
162 This structure is a part of SELECT_LEX, a pointer to this structure is
163 assigned when an item is created (which happens mostly during parsing
164 (sql_yacc.yy)), but the structure itself will be initialized after parsing
165 is complete
166
167 TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
168 separate SELECT_LEX which allow to remove tricks of changing this
169 structure before and after INSERT/CREATE and its SELECT to make correct
170 field name resolution.
171*/
172struct Name_resolution_context: Sql_alloc
173{
174 /*
175 The name resolution context to search in when an Item cannot be
176 resolved in this context (the context of an outer select)
177 */
178 Name_resolution_context *outer_context;
179
180 /*
181 List of tables used to resolve the items of this context. Usually these
182 are tables from the FROM clause of SELECT statement. The exceptions are
183 INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
184 subquery is not moved to a separate SELECT_LEX. For these types of
185 statements we have to change this member dynamically to ensure correct
186 name resolution of different parts of the statement.
187 */
188 TABLE_LIST *table_list;
189 /*
190 In most cases the two table references below replace 'table_list' above
191 for the purpose of name resolution. The first and last name resolution
192 table references allow us to search only in a sub-tree of the nested
193 join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
194 and JOIN ... ON.
195 */
196 TABLE_LIST *first_name_resolution_table;
197 /*
198 Last table to search in the list of leaf table references that begins
199 with first_name_resolution_table.
200 */
201 TABLE_LIST *last_name_resolution_table;
202
203 /* Cache first_name_resolution_table in setup_natural_join_row_types */
204 TABLE_LIST *natural_join_first_table;
205 /*
206 SELECT_LEX item belong to, in case of merged VIEW it can differ from
207 SELECT_LEX where item was created, so we can't use table_list/field_list
208 from there
209 */
210 st_select_lex *select_lex;
211
212 /*
213 Processor of errors caused during Item name resolving, now used only to
214 hide underlying tables in errors about views (i.e. it substitute some
215 errors for views)
216 */
217 void (*error_processor)(THD *, void *);
218 void *error_processor_data;
219
220 /*
221 When TRUE items are resolved in this context both against the
222 SELECT list and this->table_list. If FALSE, items are resolved
223 only against this->table_list.
224 */
225 bool resolve_in_select_list;
226
227 /*
228 Security context of this name resolution context. It's used for views
229 and is non-zero only if the view is defined with SQL SECURITY DEFINER.
230 */
231 Security_context *security_ctx;
232
233 Name_resolution_context()
234 :outer_context(0), table_list(0), select_lex(0),
235 error_processor_data(0),
236 security_ctx(0)
237 {}
238
239 void init()
240 {
241 resolve_in_select_list= FALSE;
242 error_processor= &dummy_error_processor;
243 first_name_resolution_table= NULL;
244 last_name_resolution_table= NULL;
245 }
246
247 void resolve_in_table_list_only(TABLE_LIST *tables)
248 {
249 table_list= first_name_resolution_table= tables;
250 resolve_in_select_list= FALSE;
251 }
252
253 void process_error(THD *thd)
254 {
255 (*error_processor)(thd, error_processor_data);
256 }
257 st_select_lex *outer_select()
258 {
259 return (outer_context ?
260 outer_context->select_lex :
261 NULL);
262 }
263};
264
265
266/*
267 Store and restore the current state of a name resolution context.
268*/
269
270class Name_resolution_context_state
271{
272private:
273 TABLE_LIST *save_table_list;
274 TABLE_LIST *save_first_name_resolution_table;
275 TABLE_LIST *save_next_name_resolution_table;
276 bool save_resolve_in_select_list;
277 TABLE_LIST *save_next_local;
278
279public:
280 Name_resolution_context_state() {} /* Remove gcc warning */
281
282public:
283 /* Save the state of a name resolution context. */
284 void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
285 {
286 save_table_list= context->table_list;
287 save_first_name_resolution_table= context->first_name_resolution_table;
288 save_resolve_in_select_list= context->resolve_in_select_list;
289 save_next_local= table_list->next_local;
290 save_next_name_resolution_table= table_list->next_name_resolution_table;
291 }
292
293 /* Restore a name resolution context from saved state. */
294 void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
295 {
296 table_list->next_local= save_next_local;
297 table_list->next_name_resolution_table= save_next_name_resolution_table;
298 context->table_list= save_table_list;
299 context->first_name_resolution_table= save_first_name_resolution_table;
300 context->resolve_in_select_list= save_resolve_in_select_list;
301 }
302
303 TABLE_LIST *get_first_name_resolution_table()
304 {
305 return save_first_name_resolution_table;
306 }
307};
308
309class Name_resolution_context_backup
310{
311 Name_resolution_context &ctx;
312 TABLE_LIST &table_list;
313 table_map save_map;
314 Name_resolution_context_state ctx_state;
315
316public:
317 Name_resolution_context_backup(Name_resolution_context &_ctx, TABLE_LIST &_table_list)
318 : ctx(_ctx), table_list(_table_list), save_map(_table_list.map)
319 {
320 ctx_state.save_state(&ctx, &table_list);
321 ctx.table_list= &table_list;
322 ctx.first_name_resolution_table= &table_list;
323 }
324 ~Name_resolution_context_backup()
325 {
326 ctx_state.restore_state(&ctx, &table_list);
327 table_list.map= save_map;
328 }
329};
330
331
332/*
333 This enum is used to report information about monotonicity of function
334 represented by Item* tree.
335 Monotonicity is defined only for Item* trees that represent table
336 partitioning expressions (i.e. have no subselects/user vars/PS parameters
337 etc etc). An Item* tree is assumed to have the same monotonicity properties
338 as its correspoinding function F:
339
340 [signed] longlong F(field1, field2, ...) {
341 put values of field_i into table record buffer;
342 return item->val_int();
343 }
344
345 NOTE
346 At the moment function monotonicity is not well defined (and so may be
347 incorrect) for Item trees with parameters/return types that are different
348 from INT_RESULT, may be NULL, or are unsigned.
349 It will be possible to address this issue once the related partitioning bugs
350 (BUG#16002, BUG#15447, BUG#13436) are fixed.
351
352 The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
353 NULL which puts those rows into the NULL partition, but
354 '2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
355 for this (see Bug#20577).
356*/
357
358typedef enum monotonicity_info
359{
360 NON_MONOTONIC, /* none of the below holds */
361 MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */
362 MONOTONIC_INCREASING_NOT_NULL, /* But only for valid/real x and y */
363 MONOTONIC_STRICT_INCREASING,/* F() is unary and (x < y) => (F(x) < F(y)) */
364 MONOTONIC_STRICT_INCREASING_NOT_NULL /* But only for valid/real x and y */
365} enum_monotonicity_info;
366
367/*************************************************************************/
368
369class sp_rcontext;
370
371/**
372 A helper class to collect different behavior of various kinds of SP variables:
373 - local SP variables and SP parameters
374 - PACKAGE BODY routine variables
375 - (there will be more kinds in the future)
376*/
377
378class Sp_rcontext_handler
379{
380public:
381 virtual ~Sp_rcontext_handler() {}
382 /**
383 A prefix used for SP variable names in queries:
384 - EXPLAIN EXTENDED
385 - SHOW PROCEDURE CODE
386 Local variables and SP parameters have empty prefixes.
387 Package body variables are marked with a special prefix.
388 This improves readability of the output of these queries,
389 especially when a local variable or a parameter has the same
390 name with a package body variable.
391 */
392 virtual const LEX_CSTRING *get_name_prefix() const= 0;
393 /**
394 At execution time THD->spcont points to the run-time context (sp_rcontext)
395 of the currently executed routine.
396 Local variables store their data in the sp_rcontext pointed by thd->spcont.
397 Package body variables store data in separate sp_rcontext that belongs
398 to the package.
399 This method provides access to the proper sp_rcontext structure,
400 depending on the SP variable kind.
401 */
402 virtual sp_rcontext *get_rcontext(sp_rcontext *ctx) const= 0;
403};
404
405
406class Sp_rcontext_handler_local: public Sp_rcontext_handler
407{
408public:
409 const LEX_CSTRING *get_name_prefix() const;
410 sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
411};
412
413
414class Sp_rcontext_handler_package_body: public Sp_rcontext_handler
415{
416public:
417 const LEX_CSTRING *get_name_prefix() const;
418 sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
419};
420
421
422extern MYSQL_PLUGIN_IMPORT
423 Sp_rcontext_handler_local sp_rcontext_handler_local;
424
425
426extern MYSQL_PLUGIN_IMPORT
427 Sp_rcontext_handler_package_body sp_rcontext_handler_package_body;
428
429
430
431class Item_equal;
432
433struct st_join_table* const NO_PARTICULAR_TAB= (struct st_join_table*)0x1;
434
435typedef struct replace_equal_field_arg
436{
437 Item_equal *item_equal;
438 struct st_join_table *context_tab;
439} REPLACE_EQUAL_FIELD_ARG;
440
441class Settable_routine_parameter
442{
443public:
444 /*
445 Set required privileges for accessing the parameter.
446
447 SYNOPSIS
448 set_required_privilege()
449 rw if 'rw' is true then we are going to read and set the
450 parameter, so SELECT and UPDATE privileges might be
451 required, otherwise we only reading it and SELECT
452 privilege might be required.
453 */
454 Settable_routine_parameter() {}
455 virtual ~Settable_routine_parameter() {}
456 virtual void set_required_privilege(bool rw) {};
457
458 /*
459 Set parameter value.
460
461 SYNOPSIS
462 set_value()
463 thd thread handle
464 ctx context to which parameter belongs (if it is local
465 variable).
466 it item which represents new value
467
468 RETURN
469 FALSE if parameter value has been set,
470 TRUE if error has occurred.
471 */
472 virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0;
473
474 virtual void set_out_param_info(Send_field *info) {}
475
476 virtual const Send_field *get_out_param_info() const
477 { return NULL; }
478
479 virtual Item_param *get_item_param() { return 0; }
480};
481
482
483/*
484 A helper class to calculate offset and length of a query fragment
485 - outside of SP
486 - inside an SP
487 - inside a compound block
488*/
489class Query_fragment
490{
491 uint m_pos;
492 uint m_length;
493 void set(size_t pos, size_t length)
494 {
495 DBUG_ASSERT(pos < UINT_MAX32);
496 DBUG_ASSERT(length < UINT_MAX32);
497 m_pos= (uint) pos;
498 m_length= (uint) length;
499 }
500public:
501 Query_fragment(THD *thd, sp_head *sphead, const char *start, const char *end);
502 uint pos() const { return m_pos; }
503 uint length() const { return m_length; }
504};
505
506
507/**
508 This is used for items in the query that needs to be rewritten
509 before binlogging
510
511 At the moment this applies to Item_param and Item_splocal
512*/
513class Rewritable_query_parameter
514{
515 public:
516 /*
517 Offset inside the query text.
518 Value of 0 means that this object doesn't have to be replaced
519 (for example SP variables in control statements)
520 */
521 uint pos_in_query;
522
523 /*
524 Byte length of parameter name in the statement. This is not
525 Item::name.length because name.length contains byte length of UTF8-encoded
526 name, but the query string is in the client charset.
527 */
528 uint len_in_query;
529
530 bool limit_clause_param;
531
532 Rewritable_query_parameter(uint pos_in_q= 0, uint len_in_q= 0)
533 : pos_in_query(pos_in_q), len_in_query(len_in_q),
534 limit_clause_param(false)
535 { }
536
537 virtual ~Rewritable_query_parameter() { }
538
539 virtual bool append_for_log(THD *thd, String *str) = 0;
540};
541
542class Copy_query_with_rewrite
543{
544 THD *thd;
545 const char *src;
546 size_t src_len, from;
547 String *dst;
548
549 bool copy_up_to(size_t bytes)
550 {
551 DBUG_ASSERT(bytes >= from);
552 return dst->append(src + from, uint32(bytes - from));
553 }
554
555public:
556
557 Copy_query_with_rewrite(THD *t, const char *s, size_t l, String *d)
558 :thd(t), src(s), src_len(l), from(0), dst(d) { }
559
560 bool append(Rewritable_query_parameter *p)
561 {
562 if (copy_up_to(p->pos_in_query) || p->append_for_log(thd, dst))
563 return true;
564 from= p->pos_in_query + p->len_in_query;
565 return false;
566 }
567
568 bool finalize()
569 { return copy_up_to(src_len); }
570};
571
572struct st_dyncall_create_def
573{
574 Item *key, *value;
575 CHARSET_INFO *cs;
576 uint len, frac;
577 DYNAMIC_COLUMN_TYPE type;
578};
579
580typedef struct st_dyncall_create_def DYNCALL_CREATE_DEF;
581
582
583typedef bool (Item::*Item_processor) (void *arg);
584/*
585 Analyzer function
586 SYNOPSIS
587 argp in/out IN: Analysis parameter
588 OUT: Parameter to be passed to the transformer
589
590 RETURN
591 TRUE Invoke the transformer
592 FALSE Don't do it
593
594*/
595typedef bool (Item::*Item_analyzer) (uchar **argp);
596typedef Item* (Item::*Item_transformer) (THD *thd, uchar *arg);
597typedef void (*Cond_traverser) (const Item *item, void *arg);
598
599struct st_cond_statistic;
600
601struct find_selective_predicates_list_processor_data
602{
603 TABLE *table;
604 List<st_cond_statistic> list;
605};
606
607class MY_LOCALE;
608
609class Item_equal;
610class COND_EQUAL;
611
612class st_select_lex_unit;
613
614class Item_func_not;
615class Item_splocal;
616
617/**
618 String_copier that sends Item specific warnings.
619*/
620class String_copier_for_item: public String_copier
621{
622 THD *m_thd;
623public:
624 bool copy_with_warn(CHARSET_INFO *dstcs, String *dst,
625 CHARSET_INFO *srccs, const char *src,
626 uint32 src_length, uint32 nchars);
627 String_copier_for_item(THD *thd): m_thd(thd) { }
628};
629
630class Item: public Value_source,
631 public Type_all_attributes
632{
633 void operator=(Item &);
634 /**
635 The index in the JOIN::join_tab array of the JOIN_TAB this Item is attached
636 to. Items are attached (or 'pushed') to JOIN_TABs during optimization by the
637 make_cond_for_table procedure. During query execution, this item is
638 evaluated when the join loop reaches the corresponding JOIN_TAB.
639
640 If the value of join_tab_idx >= MAX_TABLES, this means that there is no
641 corresponding JOIN_TAB.
642 */
643 uint join_tab_idx;
644
645 static void *operator new(size_t size);
646
647public:
648 static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
649 { return alloc_root(mem_root, size); }
650 static void operator delete(void *ptr,size_t size) { TRASH_FREE(ptr, size); }
651 static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
652
653 enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM,
654 WINDOW_FUNC_ITEM, STRING_ITEM,
655 INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
656 COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
657 PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
658 FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
659 SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
660 PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM,
661 XPATH_NODESET, XPATH_NODESET_CMP,
662 VIEW_FIXER_ITEM, EXPR_CACHE_ITEM,
663 DATE_ITEM};
664
665 enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
666
667 enum traverse_order { POSTFIX, PREFIX };
668
669 /* Cache of the result of is_expensive(). */
670 int8 is_expensive_cache;
671
672 /* Reuse size, only used by SP local variable assignment, otherwize 0 */
673 uint rsize;
674
675protected:
676 /*
677 str_values's main purpose is to be used to cache the value in
678 save_in_field
679 */
680 String str_value;
681
682 SEL_TREE *get_mm_tree_for_const(RANGE_OPT_PARAM *param);
683
684 /**
685 Create a field based on the exact data type handler.
686 */
687 Field *create_table_field_from_handler(TABLE *table)
688 {
689 const Type_handler *h= type_handler();
690 return h->make_and_init_table_field(&name, Record_addr(maybe_null),
691 *this, table);
692 }
693 /**
694 Create a field based on field_type of argument.
695 This is used to create a field for
696 - IFNULL(x,something)
697 - time functions
698 - prepared statement placeholders
699 - SP variables with data type references: DECLARE a TYPE OF t1.a;
700 @retval NULL error
701 @retval !NULL on success
702 */
703 Field *tmp_table_field_from_field_type(TABLE *table)
704 {
705 const Type_handler *h= type_handler()->type_handler_for_tmp_table(this);
706 return h->make_and_init_table_field(&name, Record_addr(maybe_null),
707 *this, table);
708 }
709 Field *create_tmp_field_int(TABLE *table, uint convert_int_length);
710
711 void push_note_converted_to_negative_complement(THD *thd);
712 void push_note_converted_to_positive_complement(THD *thd);
713
714 /* Helper methods, to get an Item value from another Item */
715 double val_real_from_item(Item *item)
716 {
717 DBUG_ASSERT(fixed == 1);
718 double value= item->val_real();
719 null_value= item->null_value;
720 return value;
721 }
722 longlong val_int_from_item(Item *item)
723 {
724 DBUG_ASSERT(fixed == 1);
725 longlong value= item->val_int();
726 null_value= item->null_value;
727 return value;
728 }
729 String *val_str_from_item(Item *item, String *str)
730 {
731 DBUG_ASSERT(fixed == 1);
732 String *res= item->val_str(str);
733 if (res)
734 res->set_charset(collation.collation);
735 if ((null_value= item->null_value))
736 res= NULL;
737 return res;
738 }
739 my_decimal *val_decimal_from_item(Item *item, my_decimal *decimal_value)
740 {
741 DBUG_ASSERT(fixed == 1);
742 my_decimal *value= item->val_decimal(decimal_value);
743 if ((null_value= item->null_value))
744 value= NULL;
745 return value;
746 }
747 bool get_date_from_item(Item *item, MYSQL_TIME *ltime, ulonglong fuzzydate)
748 {
749 bool rc= item->get_date(ltime, fuzzydate);
750 null_value= MY_TEST(rc || item->null_value);
751 return rc;
752 }
753 /*
754 This method is used if the item was not null but convertion to
755 TIME/DATE/DATETIME failed. We return a zero date if allowed,
756 otherwise - null.
757 */
758 bool make_zero_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
759
760public:
761 /*
762 Cache val_str() into the own buffer, e.g. to evaluate constant
763 expressions with subqueries in the ORDER/GROUP clauses.
764 */
765 String *val_str() { return val_str(&str_value); }
766
767 const MY_LOCALE *locale_from_val_str();
768
769 LEX_CSTRING name; /* Name of item */
770 /* Original item name (if it was renamed)*/
771 const char *orig_name;
772 /**
773 Intrusive list pointer for free list. If not null, points to the next
774 Item on some Query_arena's free list. For instance, stored procedures
775 have their own Query_arena's.
776
777 @see Query_arena::free_list
778 */
779 Item *next;
780 int marker;
781 bool maybe_null; /* If item may be null */
782 bool in_rollup; /* If used in GROUP BY list
783 of a query with ROLLUP */
784 bool null_value; /* if item is null */
785 bool with_sum_func; /* True if item contains a sum func */
786 bool with_param; /* True if contains an SP parameter */
787 bool with_window_func; /* True if item contains a window func */
788 /**
789 True if any item except Item_sum contains a field. Set during parsing.
790 */
791 bool with_field;
792 bool fixed; /* If item fixed with fix_fields */
793 bool is_autogenerated_name; /* indicate was name of this Item
794 autogenerated or set by user */
795 // alloc & destruct is done as start of select on THD::mem_root
796 Item(THD *thd);
797 /*
798 Constructor used by Item_field, Item_ref & aggregate (sum) functions.
799 Used for duplicating lists in processing queries with temporary
800 tables
801 Also it used for Item_cond_and/Item_cond_or for creating
802 top AND/OR structure of WHERE clause to protect it of
803 optimisation changes in prepared statements
804 */
805 Item(THD *thd, Item *item);
806 virtual ~Item()
807 {
808#ifdef EXTRA_DEBUG
809 name.str= 0;
810 name.length= 0;
811#endif
812 } /*lint -e1509 */
813 void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs);
814 void set_name_no_truncate(THD *thd, const char *str, uint length,
815 CHARSET_INFO *cs);
816 void init_make_send_field(Send_field *tmp_field,enum enum_field_types type);
817 virtual void cleanup();
818 virtual void make_send_field(THD *thd, Send_field *field);
819 virtual bool fix_fields(THD *, Item **);
820 /*
821 Fix after some tables has been pulled out. Basically re-calculate all
822 attributes that are dependent on the tables.
823 */
824 virtual void fix_after_pullout(st_select_lex *new_parent, Item **ref,
825 bool merge)
826 {};
827
828 /*
829 This method should be used in case where we are sure that we do not need
830 complete fix_fields() procedure.
831 Usually this method is used by the optimizer when it has to create a new
832 item out of other already fixed items. For example, if the optimizer has
833 to create a new Item_func for an inferred equality whose left and right
834 parts are already fixed items. In some cases the optimizer cannot use
835 directly fixed items as the arguments of the created functional item,
836 but rather uses intermediate type conversion items. Then the method is
837 supposed to be applied recursively.
838 */
839 virtual inline void quick_fix_field() { fixed= 1; }
840
841 bool save_in_value(struct st_value *value)
842 {
843 return type_handler()->Item_save_in_value(this, value);
844 }
845
846 /* Function returns 1 on overflow and -1 on fatal errors */
847 int save_in_field_no_warnings(Field *field, bool no_conversions);
848 virtual int save_in_field(Field *field, bool no_conversions);
849 virtual bool save_in_param(THD *thd, Item_param *param);
850 virtual void save_org_in_field(Field *field,
851 fast_field_copier data
852 __attribute__ ((__unused__)))
853 { (void) save_in_field(field, 1); }
854 virtual fast_field_copier setup_fast_field_copier(Field *field)
855 { return NULL; }
856 virtual int save_safe_in_field(Field *field)
857 { return save_in_field(field, 1); }
858 virtual bool send(Protocol *protocol, st_value *buffer)
859 {
860 return type_handler()->Item_send(this, protocol, buffer);
861 }
862 virtual bool eq(const Item *, bool binary_cmp) const;
863 enum_field_types field_type() const
864 {
865 return type_handler()->field_type();
866 }
867 virtual const Type_handler *type_handler() const= 0;
868 const Type_handler *type_handler_for_comparison() const
869 {
870 return type_handler()->type_handler_for_comparison();
871 }
872 virtual const Type_handler *real_type_handler() const
873 {
874 return type_handler();
875 }
876 virtual const Type_handler *cast_to_int_type_handler() const
877 {
878 return type_handler();
879 }
880 virtual const Type_handler *type_handler_for_system_time() const
881 {
882 return real_type_handler();
883 }
884 /* result_type() of an item specifies how the value should be returned */
885 Item_result result_type() const
886 {
887 return type_handler()->result_type();
888 }
889 /* ... while cmp_type() specifies how it should be compared */
890 Item_result cmp_type() const
891 {
892 return type_handler()->cmp_type();
893 }
894 const Type_handler *string_type_handler() const
895 {
896 return Type_handler::string_type_handler(max_length);
897 }
898 /*
899 Calculate the maximum length of an expression.
900 This method is used in data type aggregation for UNION, e.g.:
901 SELECT 'b' UNION SELECT COALESCE(double_10_3_field) FROM t1;
902
903 The result is usually equal to max_length, except for some numeric types.
904 In case of the INT, FLOAT, DOUBLE data types Item::max_length and
905 Item::decimals are ignored, so the returned value depends only on the
906 data type itself. E.g. for an expression of the DOUBLE(10,3) data type,
907 the result is always 53 (length 10 and precision 3 do not matter).
908
909 max_length is ignored for these numeric data types because the length limit
910 means only "expected maximum length", it is not a hard limit, so it does
911 not impose any data truncation. E.g. a column of the type INT(4) can
912 normally store big values up to 2147483647 without truncation. When we're
913 aggregating such column for UNION it's important to create a long enough
914 result column, not to lose any data.
915
916 For detailed behaviour of various data types see implementations of
917 the corresponding Type_handler_xxx::max_display_length().
918
919 Note, Item_field::max_display_length() overrides this to get
920 max_display_length() from the underlying field.
921 */
922 virtual uint32 max_display_length() const
923 {
924 return type_handler()->max_display_length(this);
925 }
926 TYPELIB *get_typelib() const { return NULL; }
927 void set_maybe_null(bool maybe_null_arg) { maybe_null= maybe_null_arg; }
928 void set_typelib(TYPELIB *typelib)
929 {
930 // Non-field Items (e.g. hybrid functions) never have ENUM/SET types yet.
931 DBUG_ASSERT(0);
932 }
933 Item_cache* get_cache(THD *thd) const
934 {
935 return type_handler()->Item_get_cache(thd, this);
936 }
937 virtual enum Type type() const =0;
938 /*
939 real_type() is the type of base item. This is same as type() for
940 most items, except Item_ref() and Item_cache_wrapper() where it
941 shows the type for the underlaying item.
942 */
943 virtual enum Type real_type() const { return type(); }
944
945 /*
946 Return information about function monotonicity. See comment for
947 enum_monotonicity_info for details. This function can only be called
948 after fix_fields() call.
949 */
950 virtual enum_monotonicity_info get_monotonicity_info() const
951 { return NON_MONOTONIC; }
952
953 /*
954 Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
955
956 SYNOPSIS
957 val_int_endpoint()
958 left_endp FALSE <=> The interval is "x < const" or "x <= const"
959 TRUE <=> The interval is "x > const" or "x >= const"
960
961 incl_endp IN FALSE <=> the comparison is '<' or '>'
962 TRUE <=> the comparison is '<=' or '>='
963 OUT The same but for the "F(x) $CMP$ F(const)" comparison
964
965 DESCRIPTION
966 This function is defined only for unary monotonic functions. The caller
967 supplies the source half-interval
968
969 x $CMP$ const
970
971 The value of const is supplied implicitly as the value this item's
972 argument, the form of $CMP$ comparison is specified through the
973 function's arguments. The calle returns the result interval
974
975 F(x) $CMP2$ F(const)
976
977 passing back F(const) as the return value, and the form of $CMP2$
978 through the out parameter. NULL values are assumed to be comparable and
979 be less than any non-NULL values.
980
981 RETURN
982 The output range bound, which equal to the value of val_int()
983 - If the value of the function is NULL then the bound is the
984 smallest possible value of LONGLONG_MIN
985 */
986 virtual longlong val_int_endpoint(bool left_endp, bool *incl_endp)
987 { DBUG_ASSERT(0); return 0; }
988
989
990 /* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
991 /*
992 Return double precision floating point representation of item.
993
994 SYNOPSIS
995 val_real()
996
997 RETURN
998 In case of NULL value return 0.0 and set null_value flag to TRUE.
999 If value is not null null_value flag will be reset to FALSE.
1000 */
1001 virtual double val_real()=0;
1002 /*
1003 Return integer representation of item.
1004
1005 SYNOPSIS
1006 val_int()
1007
1008 RETURN
1009 In case of NULL value return 0 and set null_value flag to TRUE.
1010 If value is not null null_value flag will be reset to FALSE.
1011 */
1012 virtual longlong val_int()=0;
1013 /**
1014 Get a value for CAST(x AS SIGNED).
1015 Too large positive unsigned integer values are converted
1016 to negative complements.
1017 Values of non-integer data types are adjusted to the SIGNED range.
1018 */
1019 virtual longlong val_int_signed_typecast()
1020 {
1021 return cast_to_int_type_handler()->Item_val_int_signed_typecast(this);
1022 }
1023 longlong val_int_signed_typecast_from_str();
1024 /**
1025 Get a value for CAST(x AS UNSIGNED).
1026 Negative signed integer values are converted
1027 to positive complements.
1028 Values of non-integer data types are adjusted to the UNSIGNED range.
1029 */
1030 virtual longlong val_int_unsigned_typecast()
1031 {
1032 return cast_to_int_type_handler()->Item_val_int_unsigned_typecast(this);
1033 }
1034 longlong val_int_unsigned_typecast_from_decimal();
1035 longlong val_int_unsigned_typecast_from_int();
1036 longlong val_int_unsigned_typecast_from_str();
1037 /*
1038 This is just a shortcut to avoid the cast. You should still use
1039 unsigned_flag to check the sign of the item.
1040 */
1041 inline ulonglong val_uint() { return (ulonglong) val_int(); }
1042 /*
1043 Adjust the result of val_int() to an unsigned number:
1044 - NULL value is converted to 0. The caller can check "null_value"
1045 to distinguish between 0 and NULL when necessary.
1046 - Negative numbers are converted to 0.
1047 - Positive numbers bigger than upper_bound are converted to upper_bound.
1048 - Other numbers are returned as is.
1049 */
1050 ulonglong val_uint_from_val_int(ulonglong upper_bound)
1051 {
1052 longlong nr= val_int();
1053 return (null_value || (nr < 0 && !unsigned_flag)) ? 0 :
1054 (ulonglong) nr > upper_bound ? upper_bound : (ulonglong) nr;
1055 }
1056
1057 /*
1058 Return string representation of this item object.
1059
1060 SYNOPSIS
1061 val_str()
1062 str an allocated buffer this or any nested Item object can use to
1063 store return value of this method.
1064
1065 NOTE
1066 The caller can modify the returned String, if it's not marked
1067 "const" (with the String::mark_as_const() method). That means that
1068 if the item returns its own internal buffer (e.g. tmp_value), it
1069 *must* be marked "const" [1]. So normally it's preferrable to
1070 return the result value in the String, that was passed as an
1071 argument. But, for example, SUBSTR() returns a String that simply
1072 points into the buffer of SUBSTR()'s args[0]->val_str(). Such a
1073 String is always "const", so it's ok to use tmp_value for that and
1074 avoid reallocating/copying of the argument String.
1075
1076 [1] consider SELECT CONCAT(f, ":", f) FROM (SELECT func() AS f);
1077 here the return value of f() is used twice in the top-level
1078 select, and if they share the same tmp_value buffer, modifying the
1079 first one will implicitly modify the second too.
1080
1081 RETURN
1082 In case of NULL value return 0 (NULL pointer) and set null_value flag
1083 to TRUE.
1084 If value is not null null_value flag will be reset to FALSE.
1085 */
1086 virtual String *val_str(String *str)=0;
1087
1088 /*
1089 Returns string representation of this item in ASCII format.
1090
1091 SYNOPSIS
1092 val_str_ascii()
1093 str - similar to val_str();
1094
1095 NOTE
1096 This method is introduced for performance optimization purposes.
1097
1098 1. val_str() result of some Items in string context
1099 depends on @@character_set_results.
1100 @@character_set_results can be set to a "real multibyte" character
1101 set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
1102 below for convenience.)
1103
1104 So the default string result of such functions
1105 in these circumstances is real multi-byte character set, like UTF32.
1106
1107 For example, all numbers in string context
1108 return result in @@character_set_results:
1109
1110 SELECT CONCAT(20010101); -> UTF32
1111
1112 We do sprintf() first (to get ASCII representation)
1113 and then convert to UTF32;
1114
1115 So these kind "data sources" can use ASCII representation
1116 internally, but return multi-byte data only because
1117 @@character_set_results wants so.
1118 Therefore, conversion from ASCII to UTF32 is applied internally.
1119
1120
1121 2. Some other functions need in fact ASCII input.
1122
1123 For example,
1124 inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
1125
1126 Similar, fields of certain type, like DATE, TIME,
1127 when you insert string data into them, expect in fact ASCII input.
1128 If they get non-ASCII input, for example UTF32, they
1129 convert input from UTF32 to ASCII, and then use ASCII
1130 representation to do further processing.
1131
1132
1133 3. Now imagine we pass result of a data source of the first type
1134 to a data destination of the second type.
1135
1136 What happens:
1137 a. data source converts data from ASCII to UTF32, because
1138 @@character_set_results wants so and passes the result to
1139 data destination.
1140 b. data destination gets UTF32 string.
1141 c. data destination converts UTF32 string to ASCII,
1142 because it needs ASCII representation to be able to handle data
1143 correctly.
1144
1145 As a result we get two steps of unnecessary conversion:
1146 From ASCII to UTF32, then from UTF32 to ASCII.
1147
1148 A better way to handle these situations is to pass ASCII
1149 representation directly from the source to the destination.
1150
1151 This is why val_str_ascii() introduced.
1152
1153 RETURN
1154 Similar to val_str()
1155 */
1156 virtual String *val_str_ascii(String *str);
1157
1158 /*
1159 Returns the val_str() value converted to the given character set.
1160 */
1161 String *val_str(String *str, String *converter, CHARSET_INFO *to);
1162
1163 virtual String *val_json(String *str) { return val_str(str); }
1164 /*
1165 Return decimal representation of item with fixed point.
1166
1167 SYNOPSIS
1168 val_decimal()
1169 decimal_buffer buffer which can be used by Item for returning value
1170 (but can be not)
1171
1172 NOTE
1173 Returned value should not be changed if it is not the same which was
1174 passed via argument.
1175
1176 RETURN
1177 Return pointer on my_decimal (it can be other then passed via argument)
1178 if value is not NULL (null_value flag will be reset to FALSE).
1179 In case of NULL value it return 0 pointer and set null_value flag
1180 to TRUE.
1181 */
1182 virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
1183 /*
1184 Return boolean value of item.
1185
1186 RETURN
1187 FALSE value is false or NULL
1188 TRUE value is true (not equal to 0)
1189 */
1190 virtual bool val_bool()
1191 {
1192 return type_handler()->Item_val_bool(this);
1193 }
1194 virtual String *val_nodeset(String*) { return 0; }
1195
1196 /*
1197 save_val() is method of val_* family which stores value in the given
1198 field.
1199 */
1200 virtual void save_val(Field *to) { save_org_in_field(to, NULL); }
1201 /*
1202 save_result() is method of val*result() family which stores value in
1203 the given field.
1204 */
1205 virtual void save_result(Field *to) { save_val(to); }
1206 /* Helper functions, see item_sum.cc */
1207 String *val_string_from_real(String *str);
1208 String *val_string_from_int(String *str);
1209 String *val_string_from_decimal(String *str);
1210 String *val_string_from_date(String *str);
1211 my_decimal *val_decimal_from_real(my_decimal *decimal_value);
1212 my_decimal *val_decimal_from_int(my_decimal *decimal_value);
1213 my_decimal *val_decimal_from_string(my_decimal *decimal_value);
1214 my_decimal *val_decimal_from_date(my_decimal *decimal_value);
1215 my_decimal *val_decimal_from_time(my_decimal *decimal_value);
1216 longlong val_int_from_decimal();
1217 longlong val_int_from_date();
1218 longlong val_int_from_real()
1219 {
1220 DBUG_ASSERT(fixed == 1);
1221 return Converter_double_to_longlong_with_warn(val_real(), false).result();
1222 }
1223 longlong val_int_from_str(int *error);
1224 double val_real_from_decimal();
1225 double val_real_from_date();
1226
1227 // Get TIME, DATE or DATETIME using proper sql_mode flags for the field type
1228 bool get_temporal_with_sql_mode(MYSQL_TIME *ltime);
1229 // Check NULL value for a TIME, DATE or DATETIME expression
1230 bool is_null_from_temporal();
1231
1232 int save_time_in_field(Field *field, bool no_conversions);
1233 int save_date_in_field(Field *field, bool no_conversions);
1234 int save_str_in_field(Field *field, bool no_conversions);
1235 int save_real_in_field(Field *field, bool no_conversions);
1236 int save_int_in_field(Field *field, bool no_conversions);
1237 int save_decimal_in_field(Field *field, bool no_conversions);
1238
1239 int save_str_value_in_field(Field *field, String *result);
1240
1241 virtual Field *get_tmp_table_field() { return 0; }
1242 virtual Field *create_field_for_create_select(TABLE *table);
1243 virtual Field *create_field_for_schema(THD *thd, TABLE *table);
1244 virtual const char *full_name() const { return name.str ? name.str : "???"; }
1245 const char *field_name_or_null()
1246 { return real_item()->type() == Item::FIELD_ITEM ? name.str : NULL; }
1247
1248 /*
1249 *result* family of methods is analog of *val* family (see above) but
1250 return value of result_field of item if it is present. If Item have not
1251 result field, it return val(). This methods set null_value flag in same
1252 way as *val* methods do it.
1253 */
1254 virtual double val_result() { return val_real(); }
1255 virtual longlong val_int_result() { return val_int(); }
1256 virtual String *str_result(String* tmp) { return val_str(tmp); }
1257 virtual my_decimal *val_decimal_result(my_decimal *val)
1258 { return val_decimal(val); }
1259 virtual bool val_bool_result() { return val_bool(); }
1260 virtual bool is_null_result() { return is_null(); }
1261 /*
1262 Returns 1 if result type and collation for val_str() can change between
1263 calls
1264 */
1265 virtual bool dynamic_result() { return 0; }
1266 /*
1267 Bitmap of tables used by item
1268 (note: if you need to check dependencies on individual columns, check out
1269 class Field_enumerator)
1270 */
1271 virtual table_map used_tables() const { return (table_map) 0L; }
1272 virtual table_map all_used_tables() const { return used_tables(); }
1273 /*
1274 Return table map of tables that can't be NULL tables (tables that are
1275 used in a context where if they would contain a NULL row generated
1276 by a LEFT or RIGHT join, the item would not be true).
1277 This expression is used on WHERE item to determinate if a LEFT JOIN can be
1278 converted to a normal join.
1279 Generally this function should return used_tables() if the function
1280 would return null if any of the arguments are null
1281 As this is only used in the beginning of optimization, the value don't
1282 have to be updated in update_used_tables()
1283 */
1284 virtual table_map not_null_tables() const { return used_tables(); }
1285 /*
1286 Returns true if this is a simple constant item like an integer, not
1287 a constant expression. Used in the optimizer to propagate basic constants.
1288 */
1289 virtual bool basic_const_item() const { return 0; }
1290 /* cloning of constant items (0 if it is not const) */
1291 virtual Item *clone_item(THD *thd) { return 0; }
1292 virtual Item* build_clone(THD *thd) { return get_copy(thd); }
1293 virtual cond_result eq_cmp_result() const { return COND_OK; }
1294 inline uint float_length(uint decimals_par) const
1295 { return decimals < FLOATING_POINT_DECIMALS ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;}
1296 /* Returns total number of decimal digits */
1297 virtual uint decimal_precision() const
1298 {
1299 return type_handler()->Item_decimal_precision(this);
1300 }
1301 /* Returns the number of integer part digits only */
1302 inline int decimal_int_part() const
1303 { return my_decimal_int_part(decimal_precision(), decimals); }
1304 /*
1305 Returns the number of fractional digits only.
1306 NOT_FIXED_DEC is replaced to the maximum possible number
1307 of fractional digits, taking into account the data type.
1308 */
1309 uint decimal_scale() const
1310 {
1311 return type_handler()->Item_decimal_scale(this);
1312 }
1313 /*
1314 Returns how many digits a divisor adds into a division result.
1315 This is important when the integer part of the divisor can be 0.
1316 In this example:
1317 SELECT 1 / 0.000001; -> 1000000.0000
1318 the divisor adds 5 digits into the result precision.
1319
1320 Currently this method only replaces NOT_FIXED_DEC to
1321 TIME_SECOND_PART_DIGITS for temporal data types.
1322 This method can be made virtual, to create more efficient (smaller)
1323 data types for division results.
1324 For example, in
1325 SELECT 1/1.000001;
1326 the divisor could provide no additional precision into the result,
1327 so could any other items that are know to return a result
1328 with non-zero integer part.
1329 */
1330 uint divisor_precision_increment() const
1331 {
1332 return type_handler()->Item_divisor_precision_increment(this);
1333 }
1334 /**
1335 TIME or DATETIME precision of the item: 0..6
1336 */
1337 uint time_precision()
1338 {
1339 return const_item() ? type_handler()->Item_time_precision(this) :
1340 MY_MIN(decimals, TIME_SECOND_PART_DIGITS);
1341 }
1342 uint datetime_precision()
1343 {
1344 return const_item() ? type_handler()->Item_datetime_precision(this) :
1345 MY_MIN(decimals, TIME_SECOND_PART_DIGITS);
1346 }
1347 virtual longlong val_int_min() const
1348 {
1349 return LONGLONG_MIN;
1350 }
1351 /*
1352 Returns true if this is constant (during query execution, i.e. its value
1353 will not change until next fix_fields) and its value is known.
1354 */
1355 virtual bool const_item() const { return used_tables() == 0; }
1356 /*
1357 Returns true if this is constant but its value may be not known yet.
1358 (Can be used for parameters of prep. stmts or of stored procedures.)
1359 */
1360 virtual bool const_during_execution() const
1361 { return (used_tables() & ~PARAM_TABLE_BIT) == 0; }
1362
1363 /**
1364 This method is used for to:
1365 - to generate a view definition query (SELECT-statement);
1366 - to generate a SQL-query for EXPLAIN EXTENDED;
1367 - to generate a SQL-query to be shown in INFORMATION_SCHEMA;
1368 - debug.
1369
1370 For more information about view definition query, INFORMATION_SCHEMA
1371 query and why they should be generated from the Item-tree, @see
1372 mysql_register_view().
1373 */
1374 virtual enum precedence precedence() const { return DEFAULT_PRECEDENCE; }
1375 void print_parenthesised(String *str, enum_query_type query_type,
1376 enum precedence parent_prec);
1377 /**
1378 This helper is used to print expressions as a part of a table definition,
1379 in particular for
1380 - generated columns
1381 - check constraints
1382 - default value expressions
1383 - partitioning expressions
1384 */
1385 void print_for_table_def(String *str)
1386 {
1387 print_parenthesised(str,
1388 (enum_query_type)(QT_ITEM_ORIGINAL_FUNC_NULLIF |
1389 QT_ITEM_IDENT_SKIP_DB_NAMES |
1390 QT_ITEM_IDENT_SKIP_TABLE_NAMES |
1391 QT_NO_DATA_EXPANSION |
1392 QT_TO_SYSTEM_CHARSET),
1393 LOWEST_PRECEDENCE);
1394 }
1395 virtual void print(String *str, enum_query_type query_type);
1396 void print_item_w_name(String *str, enum_query_type query_type);
1397 void print_value(String *str);
1398
1399 virtual void update_used_tables() {}
1400 virtual COND *build_equal_items(THD *thd, COND_EQUAL *inheited,
1401 bool link_item_fields,
1402 COND_EQUAL **cond_equal_ref)
1403 {
1404 update_used_tables();
1405 DBUG_ASSERT(!cond_equal_ref || !cond_equal_ref[0]);
1406 return this;
1407 }
1408 virtual COND *remove_eq_conds(THD *thd, Item::cond_result *cond_value,
1409 bool top_level);
1410 virtual void add_key_fields(JOIN *join, KEY_FIELD **key_fields,
1411 uint *and_level,
1412 table_map usable_tables,
1413 SARGABLE_PARAM **sargables)
1414 {
1415 return;
1416 }
1417 /*
1418 Make a select tree for all keys in a condition or a condition part
1419 @param param Context
1420 @param cond_ptr[OUT] Store a replacement item here if the condition
1421 can be simplified, e.g.:
1422 WHERE part1 OR part2 OR part3
1423 with one of the partN evalutating to SEL_TREE::ALWAYS.
1424 */
1425 virtual SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr);
1426 /*
1427 Checks whether the item is:
1428 - a simple equality (field=field_item or field=constant_item), or
1429 - a row equality
1430 and form multiple equality predicates.
1431 */
1432 virtual bool check_equality(THD *thd, COND_EQUAL *cond, List<Item> *eq_list)
1433 {
1434 return false;
1435 }
1436 virtual void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
1437 List<Item> &fields, uint flags) {}
1438 /* Called for items that really have to be split */
1439 void split_sum_func2(THD *thd, Ref_ptr_array ref_pointer_array,
1440 List<Item> &fields,
1441 Item **ref, uint flags);
1442 virtual bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)= 0;
1443 bool get_date_from_int(MYSQL_TIME *ltime, ulonglong fuzzydate);
1444 bool get_date_from_year(MYSQL_TIME *ltime, ulonglong fuzzydate);
1445 bool get_date_from_real(MYSQL_TIME *ltime, ulonglong fuzzydate);
1446 bool get_date_from_decimal(MYSQL_TIME *ltime, ulonglong fuzzydate);
1447 bool get_date_from_string(MYSQL_TIME *ltime, ulonglong fuzzydate);
1448 bool get_time(MYSQL_TIME *ltime)
1449 { return get_date(ltime, Time::flags_for_get_date()); }
1450 /*
1451 Get time with automatic DATE/DATETIME to TIME conversion,
1452 by subtracting CURRENT_DATE.
1453
1454 Performce a reverse operation to CAST(time AS DATETIME)
1455 Suppose:
1456 - we have a set of items (typically with the native MYSQL_TYPE_TIME type)
1457 whose item->get_date() return TIME1 value, and
1458 - CAST(AS DATETIME) for the same Items return DATETIME1,
1459 after applying time-to-datetime conversion to TIME1.
1460
1461 then all items (typically of the native MYSQL_TYPE_{DATE|DATETIME} types)
1462 whose get_date() return DATETIME1 must also return TIME1 from
1463 get_time_with_conversion()
1464
1465 @param thd - the thread, its variables.old_mode is checked
1466 to decide if use simple YYYYMMDD truncation (old mode),
1467 or perform full DATETIME-to-TIME conversion with
1468 CURRENT_DATE subtraction.
1469 @param[out] ltime - store the result here
1470 @param fuzzydate - flags to be used for the get_date() call.
1471 Normally, should include TIME_TIME_ONLY, to let
1472 the called low-level routines, e.g. str_to_date(),
1473 know that we prefer TIME rather that DATE/DATETIME
1474 and do less conversion outside of the low-level
1475 routines.
1476
1477 @returns true - on error, e.g. get_date() returned NULL value,
1478 or get_date() returned DATETIME/DATE with non-zero
1479 YYYYMMDD part.
1480 @returns false - on success
1481 */
1482 bool get_time_with_conversion(THD *thd, MYSQL_TIME *ltime,
1483 ulonglong fuzzydate);
1484 // Get a DATE or DATETIME value in numeric packed format for comparison
1485 virtual longlong val_datetime_packed()
1486 {
1487 ulonglong fuzzydate= TIME_FUZZY_DATES | TIME_INVALID_DATES;
1488 Datetime dt(current_thd, this, fuzzydate);
1489 return dt.is_valid_datetime() ? pack_time(dt.get_mysql_time()) : 0;
1490 }
1491 // Get a TIME value in numeric packed format for comparison
1492 virtual longlong val_time_packed()
1493 {
1494 Time tm(this, Time::comparison_flags_for_get_date());
1495 return tm.is_valid_time() ? pack_time(tm.get_mysql_time()) : 0;
1496 }
1497 longlong val_datetime_packed_result();
1498 longlong val_time_packed_result()
1499 {
1500 MYSQL_TIME ltime;
1501 ulonglong fuzzydate= Time::comparison_flags_for_get_date();
1502 return get_date_result(&ltime, fuzzydate) ? 0 : pack_time(&ltime);
1503 }
1504
1505 // Get a temporal value in packed DATE/DATETIME or TIME format
1506 longlong val_temporal_packed(enum_field_types f_type)
1507 {
1508 return f_type == MYSQL_TYPE_TIME ? val_time_packed() :
1509 val_datetime_packed();
1510 }
1511 bool get_seconds(ulonglong *sec, ulong *sec_part);
1512 virtual bool get_date_result(MYSQL_TIME *ltime, ulonglong fuzzydate)
1513 { return get_date(ltime,fuzzydate); }
1514 /*
1515 The method allows to determine nullness of a complex expression
1516 without fully evaluating it, instead of calling val/result*() then
1517 checking null_value. Used in Item_func_isnull/Item_func_isnotnull
1518 and Item_sum_count.
1519 Any new item which can be NULL must implement this method.
1520 */
1521 virtual bool is_null() { return 0; }
1522
1523 /*
1524 Make sure the null_value member has a correct value.
1525 */
1526 virtual void update_null_value ()
1527 {
1528 switch (cmp_type()) {
1529 case INT_RESULT:
1530 (void) val_int();
1531 break;
1532 case REAL_RESULT:
1533 (void) val_real();
1534 break;
1535 case DECIMAL_RESULT:
1536 {
1537 my_decimal tmp;
1538 (void) val_decimal(&tmp);
1539 }
1540 break;
1541 case TIME_RESULT:
1542 {
1543 MYSQL_TIME ltime;
1544 (void) get_temporal_with_sql_mode(&ltime);
1545 }
1546 break;
1547 case STRING_RESULT:
1548 {
1549 StringBuffer<MAX_FIELD_WIDTH> tmp;
1550 (void) val_str(&tmp);
1551 }
1552 break;
1553 case ROW_RESULT:
1554 DBUG_ASSERT(0);
1555 null_value= true;
1556 }
1557 }
1558
1559 /*
1560 Inform the item that there will be no distinction between its result
1561 being FALSE or NULL.
1562
1563 NOTE
1564 This function will be called for eg. Items that are top-level AND-parts
1565 of the WHERE clause. Items implementing this function (currently
1566 Item_cond_and and subquery-related item) enable special optimizations
1567 when they are "top level".
1568 */
1569 virtual void top_level_item() {}
1570 /*
1571 set field of temporary table for Item which can be switched on temporary
1572 table during query processing (grouping and so on)
1573 */
1574 virtual void set_result_field(Field *field) {}
1575 virtual bool is_result_field() { return 0; }
1576 virtual bool is_bool_type() { return false; }
1577 virtual bool is_json_type() { return false; }
1578 /* This is to handle printing of default values */
1579 virtual bool need_parentheses_in_default() { return false; }
1580 virtual void save_in_result_field(bool no_conversions) {}
1581 /*
1582 set value of aggregate function in case of no rows for grouping were found
1583 */
1584 virtual void no_rows_in_result() {}
1585 virtual void restore_to_before_no_rows_in_result() {}
1586 virtual Item *copy_or_same(THD *thd) { return this; }
1587 virtual Item *copy_andor_structure(THD *thd) { return this; }
1588 virtual Item *real_item() { return this; }
1589 virtual Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
1590 virtual Item *make_odbc_literal(THD *thd, const LEX_CSTRING *typestr)
1591 {
1592 return this;
1593 }
1594
1595 static CHARSET_INFO *default_charset();
1596
1597 CHARSET_INFO *charset_for_protocol(void) const
1598 {
1599 return type_handler()->charset_for_protocol(this);
1600 };
1601
1602 virtual bool walk(Item_processor processor, bool walk_subquery, void *arg)
1603 {
1604 return (this->*processor)(arg);
1605 }
1606
1607 virtual Item* transform(THD *thd, Item_transformer transformer, uchar *arg);
1608
1609 /*
1610 This function performs a generic "compilation" of the Item tree.
1611 The process of compilation is assumed to go as follows:
1612
1613 compile()
1614 {
1615 if (this->*some_analyzer(...))
1616 {
1617 compile children if any;
1618 this->*some_transformer(...);
1619 }
1620 }
1621
1622 i.e. analysis is performed top-down while transformation is done
1623 bottom-up.
1624 */
1625 virtual Item* compile(THD *thd, Item_analyzer analyzer, uchar **arg_p,
1626 Item_transformer transformer, uchar *arg_t)
1627 {
1628 if ((this->*analyzer) (arg_p))
1629 return ((this->*transformer) (thd, arg_t));
1630 return 0;
1631 }
1632
1633 virtual void traverse_cond(Cond_traverser traverser,
1634 void *arg, traverse_order order)
1635 {
1636 (*traverser)(this, arg);
1637 }
1638
1639 /*========= Item processors, to be used with Item::walk() ========*/
1640 virtual bool remove_dependence_processor(void *arg) { return 0; }
1641 virtual bool cleanup_processor(void *arg);
1642 virtual bool cleanup_excluding_fields_processor(void *arg) { return cleanup_processor(arg); }
1643 virtual bool cleanup_excluding_const_fields_processor(void *arg) { return cleanup_processor(arg); }
1644 virtual bool collect_item_field_processor(void *arg) { return 0; }
1645 virtual bool collect_outer_ref_processor(void *arg) {return 0; }
1646 virtual bool check_inner_refs_processor(void *arg) { return 0; }
1647 virtual bool find_item_in_field_list_processor(void *arg) { return 0; }
1648 virtual bool find_item_processor(void *arg);
1649 virtual bool change_context_processor(void *arg) { return 0; }
1650 virtual bool reset_query_id_processor(void *arg) { return 0; }
1651 virtual bool is_expensive_processor(void *arg) { return 0; }
1652
1653 // FIXME reduce the number of "add field to bitmap" processors
1654 virtual bool add_field_to_set_processor(void *arg) { return 0; }
1655 virtual bool register_field_in_read_map(void *arg) { return 0; }
1656 virtual bool register_field_in_write_map(void *arg) { return 0; }
1657 virtual bool register_field_in_bitmap(void *arg) { return 0; }
1658 virtual bool update_table_bitmaps_processor(void *arg) { return 0; }
1659
1660 virtual bool enumerate_field_refs_processor(void *arg) { return 0; }
1661 virtual bool mark_as_eliminated_processor(void *arg) { return 0; }
1662 virtual bool eliminate_subselect_processor(void *arg) { return 0; }
1663 virtual bool set_fake_select_as_master_processor(void *arg) { return 0; }
1664 virtual bool view_used_tables_processor(void *arg) { return 0; }
1665 virtual bool eval_not_null_tables(void *arg) { return 0; }
1666 virtual bool is_subquery_processor(void *arg) { return 0; }
1667 virtual bool count_sargable_conds(void *arg) { return 0; }
1668 virtual bool limit_index_condition_pushdown_processor(void *arg) { return 0; }
1669 virtual bool exists2in_processor(void *arg) { return 0; }
1670 virtual bool find_selective_predicates_list_processor(void *arg) { return 0; }
1671
1672 /*
1673 TRUE if the expression depends only on the table indicated by tab_map
1674 or can be converted to such an exression using equalities.
1675 Not to be used for AND/OR formulas.
1676 */
1677 virtual bool excl_dep_on_table(table_map tab_map) { return false; }
1678 /*
1679 TRUE if the expression depends only on grouping fields of sel
1680 or can be converted to such an exression using equalities.
1681 Not to be used for AND/OR formulas.
1682 */
1683 virtual bool excl_dep_on_grouping_fields(st_select_lex *sel) { return false; }
1684
1685 virtual bool switch_to_nullable_fields_processor(void *arg) { return 0; }
1686 virtual bool find_function_processor (void *arg) { return 0; }
1687 /*
1688 Check if a partition function is allowed
1689 SYNOPSIS
1690 check_partition_func_processor()
1691 int_arg Ignored
1692 RETURN VALUE
1693 TRUE Partition function not accepted
1694 FALSE Partition function accepted
1695
1696 DESCRIPTION
1697 check_partition_func_processor is used to check if a partition function
1698 uses an allowed function. An allowed function will always ensure that
1699 X=Y guarantees that also part_function(X)=part_function(Y) where X is
1700 a set of partition fields and so is Y. The problems comes mainly from
1701 character sets where two equal strings can be quite unequal. E.g. the
1702 german character for double s is equal to 2 s.
1703
1704 The default is that an item is not allowed
1705 in a partition function. Allowed functions
1706 can never depend on server version, they cannot depend on anything
1707 related to the environment. They can also only depend on a set of
1708 fields in the table itself. They cannot depend on other tables and
1709 cannot contain any queries and cannot contain udf's or similar.
1710 If a new Item class is defined and it inherits from a class that is
1711 allowed in a partition function then it is very important to consider
1712 whether this should be inherited to the new class. If not the function
1713 below should be defined in the new Item class.
1714
1715 The general behaviour is that most integer functions are allowed.
1716 If the partition function contains any multi-byte collations then
1717 the function check_part_func_fields will report an error on the
1718 partition function independent of what functions are used. So the
1719 only character sets allowed are single character collation and
1720 even for those only a limited set of functions are allowed. The
1721 problem with multi-byte collations is that almost every string
1722 function has the ability to change things such that two strings
1723 that are equal will not be equal after manipulated by a string
1724 function. E.g. two strings one contains a double s, there is a
1725 special german character that is equal to two s. Now assume a
1726 string function removes one character at this place, then in
1727 one the double s will be removed and in the other there will
1728 still be one s remaining and the strings are no longer equal
1729 and thus the partition function will not sort equal strings into
1730 the same partitions.
1731
1732 So the check if a partition function is valid is two steps. First
1733 check that the field types are valid, next check that the partition
1734 function is valid. The current set of partition functions valid
1735 assumes that there are no multi-byte collations amongst the partition
1736 fields.
1737 */
1738 virtual bool check_partition_func_processor(void *arg) { return 1;}
1739 virtual bool post_fix_fields_part_expr_processor(void *arg) { return 0; }
1740 virtual bool rename_fields_processor(void *arg) { return 0; }
1741 /** Processor used to check acceptability of an item in the defining
1742 expression for a virtual column
1743
1744 @param arg always ignored
1745
1746 @retval 0 the item is accepted in the definition of a virtual column
1747 @retval 1 otherwise
1748 */
1749 struct vcol_func_processor_result
1750 {
1751 uint errors; /* Bits of possible errors */
1752 const char *name; /* Not supported function */
1753 };
1754 struct func_processor_rename
1755 {
1756 LEX_CSTRING db_name;
1757 LEX_CSTRING table_name;
1758 List<Create_field> fields;
1759 };
1760 virtual bool check_vcol_func_processor(void *arg)
1761 {
1762 return mark_unsupported_function(full_name(), arg, VCOL_IMPOSSIBLE);
1763 }
1764 virtual bool check_field_expression_processor(void *arg) { return 0; }
1765 virtual bool check_func_default_processor(void *arg) { return 0; }
1766 /*
1767 Check if an expression value has allowed arguments, like DATE/DATETIME
1768 for date functions. Also used by partitioning code to reject
1769 timezone-dependent expressions in a (sub)partitioning function.
1770 */
1771 virtual bool check_valid_arguments_processor(void *arg) { return 0; }
1772 virtual bool update_vcol_processor(void *arg) { return 0; }
1773 virtual bool set_fields_as_dependent_processor(void *arg) { return 0; }
1774 /*============== End of Item processor list ======================*/
1775
1776 virtual Item *get_copy(THD *thd)=0;
1777
1778 bool cache_const_expr_analyzer(uchar **arg);
1779 Item* cache_const_expr_transformer(THD *thd, uchar *arg);
1780
1781 virtual Item* propagate_equal_fields(THD*, const Context &, COND_EQUAL *)
1782 {
1783 return this;
1784 };
1785
1786 Item* propagate_equal_fields_and_change_item_tree(THD *thd,
1787 const Context &ctx,
1788 COND_EQUAL *cond,
1789 Item **place);
1790
1791 /* arg points to REPLACE_EQUAL_FIELD_ARG object */
1792 virtual Item *replace_equal_field(THD *thd, uchar *arg) { return this; }
1793
1794 struct Collect_deps_prm
1795 {
1796 List<Item> *parameters;
1797 /* unit from which we count nest_level */
1798 st_select_lex_unit *nest_level_base;
1799 uint count;
1800 int nest_level;
1801 bool collect;
1802 };
1803
1804 /*
1805 For SP local variable returns pointer to Item representing its
1806 current value and pointer to current Item otherwise.
1807 */
1808 virtual Item *this_item() { return this; }
1809 virtual const Item *this_item() const { return this; }
1810
1811 /*
1812 For SP local variable returns address of pointer to Item representing its
1813 current value and pointer passed via parameter otherwise.
1814 */
1815 virtual Item **this_item_addr(THD *thd, Item **addr_arg) { return addr_arg; }
1816
1817 // Row emulation
1818 virtual uint cols() const { return 1; }
1819 virtual Item* element_index(uint i) { return this; }
1820 virtual Item** addr(uint i) { return 0; }
1821 virtual bool check_cols(uint c);
1822 bool check_type_traditional_scalar(const char *opname) const;
1823 bool check_type_scalar(const char *opname) const;
1824 bool check_type_or_binary(const char *opname, const Type_handler *handler) const;
1825 bool check_type_general_purpose_string(const char *opname) const;
1826 bool check_type_can_return_int(const char *opname) const;
1827 bool check_type_can_return_decimal(const char *opname) const;
1828 bool check_type_can_return_real(const char *opname) const;
1829 bool check_type_can_return_str(const char *opname) const;
1830 bool check_type_can_return_text(const char *opname) const;
1831 bool check_type_can_return_date(const char *opname) const;
1832 bool check_type_can_return_time(const char *opname) const;
1833 // It is not row => null inside is impossible
1834 virtual bool null_inside() { return 0; }
1835 // used in row subselects to get value of elements
1836 virtual void bring_value() {}
1837
1838 const Type_handler *type_handler_long_or_longlong() const
1839 {
1840 return Type_handler::type_handler_long_or_longlong(max_char_length());
1841 }
1842
1843 virtual Field *create_tmp_field(bool group, TABLE *table)
1844 {
1845 return tmp_table_field_from_field_type(table);
1846 }
1847
1848 virtual Item_field *field_for_view_update() { return 0; }
1849
1850 virtual Item *neg_transformer(THD *thd) { return NULL; }
1851 virtual Item *update_value_transformer(THD *thd, uchar *select_arg)
1852 { return this; }
1853 virtual Item *expr_cache_insert_transformer(THD *thd, uchar *unused)
1854 { return this; }
1855 virtual Item *derived_field_transformer_for_having(THD *thd, uchar *arg)
1856 { return this; }
1857 virtual Item *derived_field_transformer_for_where(THD *thd, uchar *arg)
1858 { return this; }
1859 virtual Item *derived_grouping_field_transformer_for_where(THD *thd,
1860 uchar *arg)
1861 { return this; }
1862 virtual Item *in_predicate_to_in_subs_transformer(THD *thd, uchar *arg)
1863 { return this; }
1864 virtual bool expr_cache_is_needed(THD *) { return FALSE; }
1865 virtual Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
1866 bool needs_charset_converter(uint32 length, CHARSET_INFO *tocs) const
1867 {
1868 /*
1869 This will return "true" if conversion happens:
1870 - between two non-binary different character sets
1871 - from "binary" to "unsafe" character set
1872 (those that can have non-well-formed string)
1873 - from "binary" to UCS2-alike character set with mbminlen>1,
1874 when prefix left-padding is needed for an incomplete character:
1875 binary 0xFF -> ucs2 0x00FF)
1876 */
1877 if (!String::needs_conversion_on_storage(length,
1878 collation.collation, tocs))
1879 return false;
1880 /*
1881 No needs to add converter if an "arg" is NUMERIC or DATETIME
1882 value (which is pure ASCII) and at the same time target DTCollation
1883 is ASCII-compatible. For example, no needs to rewrite:
1884 SELECT * FROM t1 WHERE datetime_field = '2010-01-01';
1885 to
1886 SELECT * FROM t1 WHERE CONVERT(datetime_field USING cs) = '2010-01-01';
1887
1888 TODO: avoid conversion of any values with
1889 repertoire ASCII and 7bit-ASCII-compatible,
1890 not only numeric/datetime origin.
1891 */
1892 if (collation.derivation == DERIVATION_NUMERIC &&
1893 collation.repertoire == MY_REPERTOIRE_ASCII &&
1894 !(collation.collation->state & MY_CS_NONASCII) &&
1895 !(tocs->state & MY_CS_NONASCII))
1896 return false;
1897 return true;
1898 }
1899 bool needs_charset_converter(CHARSET_INFO *tocs)
1900 {
1901 // Pass 1 as length to force conversion if tocs->mbminlen>1.
1902 return needs_charset_converter(1, tocs);
1903 }
1904 Item *const_charset_converter(THD *thd, CHARSET_INFO *tocs, bool lossless,
1905 const char *func_name);
1906 Item *const_charset_converter(THD *thd, CHARSET_INFO *tocs, bool lossless)
1907 { return const_charset_converter(thd, tocs, lossless, NULL); }
1908 void delete_self()
1909 {
1910 cleanup();
1911 delete this;
1912 }
1913
1914 virtual Item_splocal *get_item_splocal() { return 0; }
1915 virtual Rewritable_query_parameter *get_rewritable_query_parameter()
1916 { return 0; }
1917
1918 /*
1919 Return Settable_routine_parameter interface of the Item. Return 0
1920 if this Item is not Settable_routine_parameter.
1921 */
1922 virtual Settable_routine_parameter *get_settable_routine_parameter()
1923 {
1924 return 0;
1925 }
1926
1927 virtual Load_data_outvar *get_load_data_outvar()
1928 {
1929 return 0;
1930 }
1931 Load_data_outvar *get_load_data_outvar_or_error()
1932 {
1933 Load_data_outvar *dst= get_load_data_outvar();
1934 if (dst)
1935 return dst;
1936 my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), name.str);
1937 return NULL;
1938 }
1939
1940 /**
1941 Test whether an expression is expensive to compute. Used during
1942 optimization to avoid computing expensive expressions during this
1943 phase. Also used to force temp tables when sorting on expensive
1944 functions.
1945 @todo
1946 Normally we should have a method:
1947 cost Item::execution_cost(),
1948 where 'cost' is either 'double' or some structure of various cost
1949 parameters.
1950
1951 @note
1952 This function is now used to prevent evaluation of expensive subquery
1953 predicates during the optimization phase. It also prevents evaluation
1954 of predicates that are not computable at this moment.
1955 */
1956 virtual bool is_expensive()
1957 {
1958 if (is_expensive_cache < 0)
1959 is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
1960 return MY_TEST(is_expensive_cache);
1961 }
1962 virtual Field::geometry_type get_geometry_type() const
1963 { return Field::GEOM_GEOMETRY; };
1964 uint uint_geometry_type() const
1965 { return get_geometry_type(); }
1966 void set_geometry_type(uint type)
1967 {
1968 DBUG_ASSERT(0);
1969 }
1970 String *check_well_formed_result(String *str, bool send_error= 0);
1971 bool eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs);
1972 bool too_big_for_varchar() const
1973 { return max_char_length() > CONVERT_IF_BIGGER_TO_BLOB; }
1974 void fix_length_and_charset(uint32 max_char_length_arg, CHARSET_INFO *cs)
1975 {
1976 max_length= char_to_byte_length_safe(max_char_length_arg, cs->mbmaxlen);
1977 collation.collation= cs;
1978 }
1979 void fix_char_length(size_t max_char_length_arg)
1980 {
1981 max_length= char_to_byte_length_safe(max_char_length_arg,
1982 collation.collation->mbmaxlen);
1983 }
1984 /*
1985 Return TRUE if the item points to a column of an outer-joined table.
1986 */
1987 virtual bool is_outer_field() const { DBUG_ASSERT(fixed); return FALSE; }
1988
1989 /**
1990 Checks if this item or any of its decendents contains a subquery. This is a
1991 replacement of the former Item::has_subquery() and Item::with_subselect.
1992 */
1993 virtual bool with_subquery() const { DBUG_ASSERT(fixed); return false; }
1994
1995 Item* set_expr_cache(THD *thd);
1996
1997 virtual Item_equal *get_item_equal() { return NULL; }
1998 virtual void set_item_equal(Item_equal *item_eq) {};
1999 virtual Item_equal *find_item_equal(COND_EQUAL *cond_equal) { return NULL; }
2000 /**
2001 Set the join tab index to the minimal (left-most) JOIN_TAB to which this
2002 Item is attached. The number is an index is depth_first_tab() traversal
2003 order.
2004 */
2005 virtual void set_join_tab_idx(uint join_tab_idx_arg)
2006 {
2007 if (join_tab_idx_arg < join_tab_idx)
2008 join_tab_idx= join_tab_idx_arg;
2009 }
2010 virtual uint get_join_tab_idx() { return join_tab_idx; }
2011
2012 table_map view_used_tables(TABLE_LIST *view)
2013 {
2014 view->view_used_tables= 0;
2015 walk(&Item::view_used_tables_processor, 0, view);
2016 return view->view_used_tables;
2017 }
2018
2019 /**
2020 Collect and add to the list cache parameters for this Item.
2021
2022 @note Now implemented only for subqueries and in_optimizer,
2023 if we need it for general function then this method should
2024 be defined for Item_func.
2025 */
2026 virtual void get_cache_parameters(List<Item> &parameters) { };
2027
2028 virtual void mark_as_condition_AND_part(TABLE_LIST *embedding) {};
2029
2030 /* how much position should be reserved for Exists2In transformation */
2031 virtual uint exists2in_reserved_items() { return 0; };
2032
2033 virtual Item *neg(THD *thd);
2034
2035 /**
2036 Inform the item that it is located under a NOT, which is a top-level item.
2037 */
2038 virtual void under_not(Item_func_not * upper
2039 __attribute__((unused))) {};
2040
2041
2042 void register_in(THD *thd);
2043
2044 bool depends_only_on(table_map view_map)
2045 { return marker & FULL_EXTRACTION_FL; }
2046 int get_extraction_flag()
2047 { return marker & EXTRACTION_MASK; }
2048 void set_extraction_flag(int flags)
2049 {
2050 marker &= ~EXTRACTION_MASK;
2051 marker|= flags;
2052 }
2053 void clear_extraction_flag()
2054 {
2055 marker &= ~EXTRACTION_MASK;
2056 }
2057};
2058
2059MEM_ROOT *get_thd_memroot(THD *thd);
2060
2061template <class T>
2062inline Item* get_item_copy (THD *thd, T* item)
2063{
2064 Item *copy= new (get_thd_memroot(thd)) T(*item);
2065 if (likely(copy))
2066 copy->register_in(thd);
2067 return copy;
2068}
2069
2070
2071/*
2072 This class is a replacement for the former member Item::with_subselect.
2073 Determines if the descendant Item is a subselect or some of
2074 its arguments is or contains a subselect.
2075*/
2076class With_subquery_cache
2077{
2078protected:
2079 bool m_with_subquery;
2080public:
2081 With_subquery_cache(): m_with_subquery(false) { }
2082 void join(const Item *item) { m_with_subquery|= item->with_subquery(); }
2083};
2084
2085
2086class Type_geometry_attributes
2087{
2088 uint m_geometry_type;
2089 static const uint m_geometry_type_unknown= Field::GEOM_GEOMETRYCOLLECTION + 1;
2090 void copy(const Type_handler *handler, const Type_all_attributes *gattr)
2091 {
2092 // Ignore implicit NULLs
2093 m_geometry_type= handler == &type_handler_geometry ?
2094 gattr->uint_geometry_type() :
2095 m_geometry_type_unknown;
2096 }
2097public:
2098 Type_geometry_attributes()
2099 :m_geometry_type(m_geometry_type_unknown)
2100 { }
2101 Type_geometry_attributes(const Type_handler *handler,
2102 const Type_all_attributes *gattr)
2103 :m_geometry_type(m_geometry_type_unknown)
2104 {
2105 copy(handler, gattr);
2106 }
2107 void join(const Item *item)
2108 {
2109 // Ignore implicit NULLs
2110 if (m_geometry_type == m_geometry_type_unknown)
2111 copy(item->type_handler(), item);
2112 else if (item->type_handler() == &type_handler_geometry)
2113 {
2114 m_geometry_type=
2115 Field_geom::geometry_type_merge((Field_geom::geometry_type)
2116 m_geometry_type,
2117 (Field_geom::geometry_type)
2118 item->uint_geometry_type());
2119 }
2120 }
2121 Field::geometry_type get_geometry_type() const
2122 {
2123 return m_geometry_type == m_geometry_type_unknown ?
2124 Field::GEOM_GEOMETRY :
2125 (Field::geometry_type) m_geometry_type;
2126 }
2127 void set_geometry_type(uint type)
2128 {
2129 DBUG_ASSERT(type <= m_geometry_type_unknown);
2130 m_geometry_type= type;
2131 }
2132};
2133
2134
2135
2136/**
2137 Compare two Items for List<Item>::add_unique()
2138*/
2139
2140bool cmp_items(Item *a, Item *b);
2141
2142
2143/**
2144 Array of items, e.g. function or aggerate function arguments.
2145*/
2146class Item_args
2147{
2148protected:
2149 Item **args, *tmp_arg[2];
2150 uint arg_count;
2151 void set_arguments(THD *thd, List<Item> &list);
2152 bool walk_args(Item_processor processor, bool walk_subquery, void *arg)
2153 {
2154 for (uint i= 0; i < arg_count; i++)
2155 {
2156 if (args[i]->walk(processor, walk_subquery, arg))
2157 return true;
2158 }
2159 return false;
2160 }
2161 bool transform_args(THD *thd, Item_transformer transformer, uchar *arg);
2162 void propagate_equal_fields(THD *, const Item::Context &, COND_EQUAL *);
2163 bool excl_dep_on_table(table_map tab_map)
2164 {
2165 for (uint i= 0; i < arg_count; i++)
2166 {
2167 if (args[i]->const_item())
2168 continue;
2169 if (!args[i]->excl_dep_on_table(tab_map))
2170 return false;
2171 }
2172 return true;
2173 }
2174 bool excl_dep_on_grouping_fields(st_select_lex *sel)
2175 {
2176 for (uint i= 0; i < arg_count; i++)
2177 {
2178 if (args[i]->const_item())
2179 continue;
2180 if (!args[i]->excl_dep_on_grouping_fields(sel))
2181 return false;
2182 }
2183 return true;
2184 }
2185public:
2186 Item_args(void)
2187 :args(NULL), arg_count(0)
2188 { }
2189 Item_args(Item *a)
2190 :args(tmp_arg), arg_count(1)
2191 {
2192 args[0]= a;
2193 }
2194 Item_args(Item *a, Item *b)
2195 :args(tmp_arg), arg_count(2)
2196 {
2197 args[0]= a; args[1]= b;
2198 }
2199 Item_args(THD *thd, Item *a, Item *b, Item *c)
2200 {
2201 arg_count= 0;
2202 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 3))))
2203 {
2204 arg_count= 3;
2205 args[0]= a; args[1]= b; args[2]= c;
2206 }
2207 }
2208 Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d)
2209 {
2210 arg_count= 0;
2211 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 4))))
2212 {
2213 arg_count= 4;
2214 args[0]= a; args[1]= b; args[2]= c; args[3]= d;
2215 }
2216 }
2217 Item_args(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e)
2218 {
2219 arg_count= 5;
2220 if (likely((args= (Item**) thd_alloc(thd, sizeof(Item*) * 5))))
2221 {
2222 arg_count= 5;
2223 args[0]= a; args[1]= b; args[2]= c; args[3]= d; args[4]= e;
2224 }
2225 }
2226 Item_args(THD *thd, List<Item> &list)
2227 {
2228 set_arguments(thd, list);
2229 }
2230 Item_args(THD *thd, const Item_args *other);
2231 bool alloc_arguments(THD *thd, uint count);
2232 void add_argument(Item *item)
2233 {
2234 args[arg_count++]= item;
2235 }
2236 inline Item **arguments() const { return args; }
2237 inline uint argument_count() const { return arg_count; }
2238 inline void remove_arguments() { arg_count=0; }
2239};
2240
2241
2242/*
2243 Class to be used to enumerate all field references in an item tree. This
2244 includes references to outside but not fields of the tables within a
2245 subquery.
2246 Suggested usage:
2247
2248 class My_enumerator : public Field_enumerator
2249 {
2250 virtual void visit_field() { ... your actions ...}
2251 }
2252
2253 My_enumerator enumerator;
2254 item->walk(Item::enumerate_field_refs_processor, ...,&enumerator);
2255
2256 This is similar to Visitor pattern.
2257*/
2258
2259class Field_enumerator
2260{
2261public:
2262 virtual void visit_field(Item_field *field)= 0;
2263 virtual ~Field_enumerator() {}; /* purecov: inspected */
2264 Field_enumerator() {} /* Remove gcc warning */
2265};
2266
2267class Item_string;
2268
2269
2270/**
2271 A common class for Item_basic_constant and Item_param
2272*/
2273class Item_basic_value :public Item
2274{
2275 bool is_basic_value(const Item *item, Type type_arg) const
2276 {
2277 return item->basic_const_item() && item->type() == type_arg;
2278 }
2279 bool is_basic_value(Type type_arg) const
2280 {
2281 return basic_const_item() && type() == type_arg;
2282 }
2283 bool str_eq(const String *value,
2284 const String *other, CHARSET_INFO *cs, bool binary_cmp) const
2285 {
2286 return binary_cmp ?
2287 value->bin_eq(other) :
2288 collation.collation == cs && value->eq(other, collation.collation);
2289 }
2290
2291protected:
2292 // Value metadata, e.g. to make string processing easier
2293 class Metadata: private MY_STRING_METADATA
2294 {
2295 public:
2296 Metadata(const String *str)
2297 {
2298 my_string_metadata_get(this, str->charset(), str->ptr(), str->length());
2299 }
2300 Metadata(const String *str, uint repertoire_arg)
2301 {
2302 MY_STRING_METADATA::repertoire= repertoire_arg;
2303 MY_STRING_METADATA::char_length= str->numchars();
2304 }
2305 uint repertoire() const { return MY_STRING_METADATA::repertoire; }
2306 size_t char_length() const { return MY_STRING_METADATA::char_length; }
2307 };
2308 void fix_charset_and_length(CHARSET_INFO *cs,
2309 Derivation dv, Metadata metadata)
2310 {
2311 /*
2312 We have to have a different max_length than 'length' here to
2313 ensure that we get the right length if we do use the item
2314 to create a new table. In this case max_length must be the maximum
2315 number of chars for a string of this type because we in Create_field::
2316 divide the max_length with mbmaxlen).
2317 */
2318 collation.set(cs, dv, metadata.repertoire());
2319 fix_char_length(metadata.char_length());
2320 decimals= NOT_FIXED_DEC;
2321 }
2322 void fix_charset_and_length_from_str_value(const String &str, Derivation dv)
2323 {
2324 fix_charset_and_length(str.charset(), dv, Metadata(&str));
2325 }
2326 Item_basic_value(THD *thd): Item(thd) {}
2327 /*
2328 In the xxx_eq() methods below we need to cast off "const" to
2329 call val_xxx(). This is OK for Item_basic_constant and Item_param.
2330 */
2331 bool null_eq(const Item *item) const
2332 {
2333 DBUG_ASSERT(is_basic_value(NULL_ITEM));
2334 return item->type() == NULL_ITEM;
2335 }
2336 bool str_eq(const String *value, const Item *item, bool binary_cmp) const
2337 {
2338 DBUG_ASSERT(is_basic_value(STRING_ITEM));
2339 return is_basic_value(item, STRING_ITEM) &&
2340 str_eq(value, ((Item_basic_value*)item)->val_str(NULL),
2341 item->collation.collation, binary_cmp);
2342 }
2343 bool real_eq(double value, const Item *item) const
2344 {
2345 DBUG_ASSERT(is_basic_value(REAL_ITEM));
2346 return is_basic_value(item, REAL_ITEM) &&
2347 value == ((Item_basic_value*)item)->val_real();
2348 }
2349 bool int_eq(longlong value, const Item *item) const
2350 {
2351 DBUG_ASSERT(is_basic_value(INT_ITEM));
2352 return is_basic_value(item, INT_ITEM) &&
2353 value == ((Item_basic_value*)item)->val_int() &&
2354 (value >= 0 || item->unsigned_flag == unsigned_flag);
2355 }
2356};
2357
2358
2359class Item_basic_constant :public Item_basic_value
2360{
2361 table_map used_table_map;
2362public:
2363 Item_basic_constant(THD *thd): Item_basic_value(thd), used_table_map(0) {};
2364 void set_used_tables(table_map map) { used_table_map= map; }
2365 table_map used_tables() const { return used_table_map; }
2366 bool check_vcol_func_processor(void *arg) { return FALSE;}
2367 virtual Item_basic_constant *make_string_literal_concat(THD *thd,
2368 const LEX_CSTRING *)
2369 {
2370 DBUG_ASSERT(0);
2371 return this;
2372 }
2373 /* to prevent drop fixed flag (no need parent cleanup call) */
2374 void cleanup()
2375 {
2376 /*
2377 Restore the original field name as it might not have been allocated
2378 in the statement memory. If the name is auto generated, it must be
2379 done again between subsequent executions of a prepared statement.
2380 */
2381 if (orig_name)
2382 {
2383 name.str= orig_name;
2384 name.length= strlen(orig_name);
2385 }
2386 }
2387};
2388
2389
2390/*****************************************************************************
2391 The class is a base class for representation of stored routine variables in
2392 the Item-hierarchy. There are the following kinds of SP-vars:
2393 - local variables (Item_splocal);
2394 - CASE expression (Item_case_expr);
2395*****************************************************************************/
2396
2397class Item_sp_variable :public Item
2398{
2399protected:
2400 /*
2401 THD, which is stored in fix_fields() and is used in this_item() to avoid
2402 current_thd use.
2403 */
2404 THD *m_thd;
2405
2406 bool fix_fields_from_item(THD *thd, Item **, const Item *);
2407public:
2408 LEX_CSTRING m_name;
2409
2410public:
2411#ifdef DBUG_ASSERT_EXISTS
2412 /*
2413 Routine to which this Item_splocal belongs. Used for checking if correct
2414 runtime context is used for variable handling.
2415 */
2416 const sp_head *m_sp;
2417#endif
2418
2419public:
2420 Item_sp_variable(THD *thd, const LEX_CSTRING *sp_var_name);
2421
2422public:
2423 bool fix_fields(THD *thd, Item **)= 0;
2424
2425 double val_real();
2426 longlong val_int();
2427 String *val_str(String *sp);
2428 my_decimal *val_decimal(my_decimal *decimal_value);
2429 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2430 bool is_null();
2431
2432public:
2433 void make_send_field(THD *thd, Send_field *field);
2434
2435 inline bool const_item() const;
2436
2437 inline int save_in_field(Field *field, bool no_conversions);
2438 inline bool send(Protocol *protocol, st_value *buffer);
2439 bool check_vcol_func_processor(void *arg)
2440 {
2441 return mark_unsupported_function(m_name.str, arg, VCOL_IMPOSSIBLE);
2442 }
2443};
2444
2445/*****************************************************************************
2446 Item_sp_variable inline implementation.
2447*****************************************************************************/
2448
2449inline bool Item_sp_variable::const_item() const
2450{
2451 return TRUE;
2452}
2453
2454inline int Item_sp_variable::save_in_field(Field *field, bool no_conversions)
2455{
2456 return this_item()->save_in_field(field, no_conversions);
2457}
2458
2459inline bool Item_sp_variable::send(Protocol *protocol, st_value *buffer)
2460{
2461 return this_item()->send(protocol, buffer);
2462}
2463
2464
2465/*****************************************************************************
2466 A reference to local SP variable (incl. reference to SP parameter), used in
2467 runtime.
2468*****************************************************************************/
2469
2470class Item_splocal :public Item_sp_variable,
2471 private Settable_routine_parameter,
2472 public Rewritable_query_parameter,
2473 public Type_handler_hybrid_field_type
2474{
2475protected:
2476 const Sp_rcontext_handler *m_rcontext_handler;
2477
2478 uint m_var_idx;
2479
2480 Type m_type;
2481
2482 bool append_value_for_log(THD *thd, String *str);
2483
2484 sp_rcontext *get_rcontext(sp_rcontext *local_ctx) const;
2485 Item_field *get_variable(sp_rcontext *ctx) const;
2486
2487public:
2488 Item_splocal(THD *thd, const Sp_rcontext_handler *rh,
2489 const LEX_CSTRING *sp_var_name, uint sp_var_idx,
2490 const Type_handler *handler,
2491 uint pos_in_q= 0, uint len_in_q= 0);
2492
2493 bool fix_fields(THD *, Item **);
2494 Item *this_item();
2495 const Item *this_item() const;
2496 Item **this_item_addr(THD *thd, Item **);
2497
2498 virtual void print(String *str, enum_query_type query_type);
2499
2500public:
2501 inline const LEX_CSTRING *my_name() const;
2502
2503 inline uint get_var_idx() const;
2504
2505 inline enum Type type() const;
2506 const Type_handler *type_handler() const
2507 { return Type_handler_hybrid_field_type::type_handler(); }
2508 uint cols() const { return this_item()->cols(); }
2509 Item* element_index(uint i) { return this_item()->element_index(i); }
2510 Item** addr(uint i) { return this_item()->addr(i); }
2511 bool check_cols(uint c);
2512
2513private:
2514 bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2515
2516public:
2517 Item_splocal *get_item_splocal() { return this; }
2518
2519 Rewritable_query_parameter *get_rewritable_query_parameter()
2520 { return this; }
2521
2522 Settable_routine_parameter *get_settable_routine_parameter()
2523 { return this; }
2524
2525 bool append_for_log(THD *thd, String *str);
2526
2527 Item *get_copy(THD *thd) { return 0; }
2528
2529 /*
2530 Override the inherited create_field_for_create_select(),
2531 because we want to preserve the exact data type for:
2532 DECLARE a1 INT;
2533 DECLARE a2 TYPE OF t1.a2;
2534 CREATE TABLE t1 AS SELECT a1, a2;
2535 The inherited implementation would create a column
2536 based on result_type(), which is less exact.
2537 */
2538 Field *create_field_for_create_select(TABLE *table)
2539 { return create_table_field_from_handler(table); }
2540};
2541
2542
2543/**
2544 An Item_splocal variant whose data type becomes known only at
2545 sp_rcontext creation time, e.g. "DECLARE var1 t1.col1%TYPE".
2546*/
2547class Item_splocal_with_delayed_data_type: public Item_splocal
2548{
2549public:
2550 Item_splocal_with_delayed_data_type(THD *thd,
2551 const Sp_rcontext_handler *rh,
2552 const LEX_CSTRING *sp_var_name,
2553 uint sp_var_idx,
2554 uint pos_in_q, uint len_in_q)
2555 :Item_splocal(thd, rh, sp_var_name, sp_var_idx, &type_handler_null,
2556 pos_in_q, len_in_q)
2557 { }
2558};
2559
2560
2561/**
2562 SP variables that are fields of a ROW.
2563 DELCARE r ROW(a INT,b INT);
2564 SELECT r.a; -- This is handled by Item_splocal_row_field
2565*/
2566class Item_splocal_row_field :public Item_splocal
2567{
2568protected:
2569 LEX_CSTRING m_field_name;
2570 uint m_field_idx;
2571 bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2572public:
2573 Item_splocal_row_field(THD *thd,
2574 const Sp_rcontext_handler *rh,
2575 const LEX_CSTRING *sp_var_name,
2576 const LEX_CSTRING *sp_field_name,
2577 uint sp_var_idx, uint sp_field_idx,
2578 const Type_handler *handler,
2579 uint pos_in_q= 0, uint len_in_q= 0)
2580 :Item_splocal(thd, rh, sp_var_name, sp_var_idx, handler, pos_in_q, len_in_q),
2581 m_field_name(*sp_field_name),
2582 m_field_idx(sp_field_idx)
2583 { }
2584 bool fix_fields(THD *thd, Item **);
2585 Item *this_item();
2586 const Item *this_item() const;
2587 Item **this_item_addr(THD *thd, Item **);
2588 bool append_for_log(THD *thd, String *str);
2589 void print(String *str, enum_query_type query_type);
2590};
2591
2592
2593class Item_splocal_row_field_by_name :public Item_splocal_row_field
2594{
2595 bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
2596public:
2597 Item_splocal_row_field_by_name(THD *thd,
2598 const Sp_rcontext_handler *rh,
2599 const LEX_CSTRING *sp_var_name,
2600 const LEX_CSTRING *sp_field_name,
2601 uint sp_var_idx,
2602 const Type_handler *handler,
2603 uint pos_in_q= 0, uint len_in_q= 0)
2604 :Item_splocal_row_field(thd, rh, sp_var_name, sp_field_name,
2605 sp_var_idx, 0 /* field index will be set later */,
2606 handler, pos_in_q, len_in_q)
2607 { }
2608 bool fix_fields(THD *thd, Item **it);
2609 void print(String *str, enum_query_type query_type);
2610};
2611
2612
2613/*****************************************************************************
2614 Item_splocal inline implementation.
2615*****************************************************************************/
2616
2617inline const LEX_CSTRING *Item_splocal::my_name() const
2618{
2619 return &m_name;
2620}
2621
2622inline uint Item_splocal::get_var_idx() const
2623{
2624 return m_var_idx;
2625}
2626
2627inline enum Item::Type Item_splocal::type() const
2628{
2629 return m_type;
2630}
2631
2632/*****************************************************************************
2633 A reference to case expression in SP, used in runtime.
2634*****************************************************************************/
2635
2636class Item_case_expr :public Item_sp_variable
2637{
2638public:
2639 Item_case_expr(THD *thd, uint case_expr_id);
2640
2641public:
2642 bool fix_fields(THD *thd, Item **);
2643 Item *this_item();
2644 const Item *this_item() const;
2645 Item **this_item_addr(THD *thd, Item **);
2646
2647 inline enum Type type() const;
2648 const Type_handler *type_handler() const { return this_item()->type_handler(); }
2649
2650public:
2651 /*
2652 NOTE: print() is intended to be used from views and for debug.
2653 Item_case_expr can not occur in views, so here it is only for debug
2654 purposes.
2655 */
2656 virtual void print(String *str, enum_query_type query_type);
2657 Item *get_copy(THD *thd) { return 0; }
2658
2659private:
2660 uint m_case_expr_id;
2661};
2662
2663/*****************************************************************************
2664 Item_case_expr inline implementation.
2665*****************************************************************************/
2666
2667inline enum Item::Type Item_case_expr::type() const
2668{
2669 return this_item()->type();
2670}
2671
2672/*
2673 NAME_CONST(given_name, const_value).
2674 This 'function' has all properties of the supplied const_value (which is
2675 assumed to be a literal constant), and the name given_name.
2676
2677 This is used to replace references to SP variables when we write PROCEDURE
2678 statements into the binary log.
2679
2680 TODO
2681 Together with Item_splocal and Item::this_item() we can actually extract
2682 common a base of this class and Item_splocal. Maybe it is possible to
2683 extract a common base with class Item_ref, too.
2684*/
2685
2686class Item_name_const : public Item
2687{
2688 Item *value_item;
2689 Item *name_item;
2690 bool valid_args;
2691public:
2692 Item_name_const(THD *thd, Item *name_arg, Item *val);
2693
2694 bool fix_fields(THD *, Item **);
2695
2696 enum Type type() const;
2697 double val_real();
2698 longlong val_int();
2699 String *val_str(String *sp);
2700 my_decimal *val_decimal(my_decimal *);
2701 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2702 bool is_null();
2703 virtual void print(String *str, enum_query_type query_type);
2704
2705 const Type_handler *type_handler() const
2706 {
2707 return value_item->type_handler();
2708 }
2709
2710 bool const_item() const
2711 {
2712 return TRUE;
2713 }
2714
2715 int save_in_field(Field *field, bool no_conversions)
2716 {
2717 return value_item->save_in_field(field, no_conversions);
2718 }
2719
2720 bool send(Protocol *protocol, st_value *buffer)
2721 {
2722 return value_item->send(protocol, buffer);
2723 }
2724 bool check_vcol_func_processor(void *arg)
2725 {
2726 return mark_unsupported_function("name_const()", arg, VCOL_IMPOSSIBLE);
2727 }
2728 Item *get_copy(THD *thd)
2729 { return get_item_copy<Item_name_const>(thd, this); }
2730};
2731
2732class Item_num: public Item_basic_constant
2733{
2734public:
2735 Item_num(THD *thd): Item_basic_constant(thd) { collation.set_numeric(); }
2736 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
2737 bool check_partition_func_processor(void *int_arg) { return FALSE;}
2738 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2739 {
2740 return type_handler()->Item_get_date(this, ltime, fuzzydate);
2741 }
2742};
2743
2744#define NO_CACHED_FIELD_INDEX ((uint)(-1))
2745
2746class st_select_lex;
2747
2748
2749class Item_result_field :public Item /* Item with result field */
2750{
2751public:
2752 Field *result_field; /* Save result here */
2753 Item_result_field(THD *thd): Item(thd), result_field(0) {}
2754 // Constructor used for Item_sum/Item_cond_and/or (see Item comment)
2755 Item_result_field(THD *thd, Item_result_field *item):
2756 Item(thd, item), result_field(item->result_field)
2757 {}
2758 ~Item_result_field() {} /* Required with gcc 2.95 */
2759 Field *get_tmp_table_field() { return result_field; }
2760 /*
2761 This implementation of used_tables() used by Item_avg_field and
2762 Item_variance_field which work when only temporary table left, so theu
2763 return table map of the temporary table.
2764 */
2765 table_map used_tables() const { return 1; }
2766 void set_result_field(Field *field) { result_field= field; }
2767 bool is_result_field() { return true; }
2768 void save_in_result_field(bool no_conversions)
2769 {
2770 save_in_field(result_field, no_conversions);
2771 }
2772 void cleanup();
2773 bool check_vcol_func_processor(void *arg) { return FALSE;}
2774};
2775
2776
2777class Item_ident :public Item_result_field
2778{
2779protected:
2780 /*
2781 We have to store initial values of db_name, table_name and field_name
2782 to be able to restore them during cleanup() because they can be
2783 updated during fix_fields() to values from Field object and life-time
2784 of those is shorter than life-time of Item_field.
2785 */
2786 const char *orig_db_name;
2787 const char *orig_table_name;
2788 LEX_CSTRING orig_field_name;
2789
2790public:
2791 Name_resolution_context *context;
2792 const char *db_name;
2793 const char *table_name;
2794 LEX_CSTRING field_name;
2795 bool alias_name_used; /* true if item was resolved against alias */
2796 /*
2797 Cached value of index for this field in table->field array, used by prep.
2798 stmts for speeding up their re-execution. Holds NO_CACHED_FIELD_INDEX
2799 if index value is not known.
2800 */
2801 uint cached_field_index;
2802 /*
2803 Cached pointer to table which contains this field, used for the same reason
2804 by prep. stmt. too in case then we have not-fully qualified field.
2805 0 - means no cached value.
2806 */
2807 TABLE_LIST *cached_table;
2808 st_select_lex *depended_from;
2809 /*
2810 Some Items resolved in another select should not be marked as dependency
2811 of the subquery where they are. During normal name resolution, we check
2812 this. Stored procedures and prepared statements first try to resolve an
2813 ident item using a cached table reference and field position from the
2814 previous query execution (cached_table/cached_field_index). If the
2815 tables were not changed, the ident matches the table/field, and we have
2816 faster resolution of the ident without looking through all tables and
2817 fields in the query. But in this case, we can not check all conditions
2818 about this ident item dependency, so we should cache the condition in
2819 this variable.
2820 */
2821 bool can_be_depended;
2822 Item_ident(THD *thd, Name_resolution_context *context_arg,
2823 const char *db_name_arg, const char *table_name_arg,
2824 const LEX_CSTRING *field_name_arg);
2825 Item_ident(THD *thd, Item_ident *item);
2826 Item_ident(THD *thd, TABLE_LIST *view_arg, const LEX_CSTRING *field_name_arg);
2827 const char *full_name() const;
2828 void cleanup();
2829 st_select_lex *get_depended_from() const;
2830 bool remove_dependence_processor(void * arg);
2831 virtual void print(String *str, enum_query_type query_type);
2832 virtual bool change_context_processor(void *cntx)
2833 { context= (Name_resolution_context *)cntx; return FALSE; }
2834 /**
2835 Collect outer references
2836 */
2837 virtual bool collect_outer_ref_processor(void *arg);
2838 friend bool insert_fields(THD *thd, Name_resolution_context *context,
2839 const char *db_name,
2840 const char *table_name, List_iterator<Item> *it,
2841 bool any_privileges);
2842};
2843
2844
2845class Item_ident_for_show :public Item
2846{
2847public:
2848 Field *field;
2849 const char *db_name;
2850 const char *table_name;
2851
2852 Item_ident_for_show(THD *thd, Field *par_field, const char *db_arg,
2853 const char *table_name_arg):
2854 Item(thd), field(par_field), db_name(db_arg), table_name(table_name_arg)
2855 {
2856 Type_std_attributes::set(par_field->type_std_attributes());
2857 }
2858 enum Type type() const { return FIELD_ITEM; }
2859 double val_real() { return field->val_real(); }
2860 longlong val_int() { return field->val_int(); }
2861 String *val_str(String *str) { return field->val_str(str); }
2862 my_decimal *val_decimal(my_decimal *dec) { return field->val_decimal(dec); }
2863 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
2864 {
2865 return field->get_date(ltime, fuzzydate);
2866 }
2867 void make_send_field(THD *thd, Send_field *tmp_field);
2868 const Type_handler *type_handler() const
2869 {
2870 const Type_handler *handler= field->type_handler();
2871 return handler->type_handler_for_item_field();
2872 }
2873 Item* get_copy(THD *thd)
2874 { return get_item_copy<Item_ident_for_show>(thd, this); }
2875};
2876
2877
2878class Item_field :public Item_ident,
2879 public Load_data_outvar
2880{
2881protected:
2882 void set_field(Field *field);
2883public:
2884 Field *field;
2885 Item_equal *item_equal;
2886 /*
2887 if any_privileges set to TRUE then here real effective privileges will
2888 be stored
2889 */
2890 uint have_privileges;
2891 /* field need any privileges (for VIEW creation) */
2892 bool any_privileges;
2893 Item_field(THD *thd, Name_resolution_context *context_arg,
2894 const char *db_arg,const char *table_name_arg,
2895 const LEX_CSTRING *field_name_arg);
2896 /*
2897 Constructor needed to process subselect with temporary tables (see Item)
2898 */
2899 Item_field(THD *thd, Item_field *item);
2900 /*
2901 Constructor used inside setup_wild(), ensures that field, table,
2902 and database names will live as long as Item_field (this is important
2903 in prepared statements).
2904 */
2905 Item_field(THD *thd, Name_resolution_context *context_arg, Field *field);
2906 /*
2907 If this constructor is used, fix_fields() won't work, because
2908 db_name, table_name and column_name are unknown. It's necessary to call
2909 reset_field() before fix_fields() for all fields created this way.
2910 */
2911 Item_field(THD *thd, Field *field);
2912 enum Type type() const { return FIELD_ITEM; }
2913 bool eq(const Item *item, bool binary_cmp) const;
2914 double val_real();
2915 longlong val_int();
2916 my_decimal *val_decimal(my_decimal *);
2917 String *val_str(String*);
2918 void save_result(Field *to);
2919 double val_result();
2920 longlong val_int_result();
2921 String *str_result(String* tmp);
2922 my_decimal *val_decimal_result(my_decimal *);
2923 bool val_bool_result();
2924 bool is_null_result();
2925 bool send(Protocol *protocol, st_value *buffer);
2926 Load_data_outvar *get_load_data_outvar()
2927 {
2928 return this;
2929 }
2930 bool load_data_set_null(THD *thd, const Load_data_param *param)
2931 {
2932 return field->load_data_set_null(thd);
2933 }
2934 bool load_data_set_value(THD *thd, const char *pos, uint length,
2935 const Load_data_param *param)
2936 {
2937 field->load_data_set_value(pos, length, param->charset());
2938 return false;
2939 }
2940 bool load_data_set_no_data(THD *thd, const Load_data_param *param);
2941 void load_data_print_for_log_event(THD *thd, String *to) const;
2942 bool load_data_add_outvar(THD *thd, Load_data_param *param) const
2943 {
2944 return param->add_outvar_field(thd, field);
2945 }
2946 uint load_data_fixed_length() const
2947 {
2948 return field->field_length;
2949 }
2950 void reset_field(Field *f);
2951 bool fix_fields(THD *, Item **);
2952 void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge);
2953 void make_send_field(THD *thd, Send_field *tmp_field);
2954 int save_in_field(Field *field,bool no_conversions);
2955 void save_org_in_field(Field *field, fast_field_copier optimizer_data);
2956 fast_field_copier setup_fast_field_copier(Field *field);
2957 table_map used_tables() const;
2958 table_map all_used_tables() const;
2959 const Type_handler *type_handler() const
2960 {
2961 const Type_handler *handler= field->type_handler();
2962 return handler->type_handler_for_item_field();
2963 }
2964 const Type_handler *cast_to_int_type_handler() const
2965 {
2966 return field->type_handler()->cast_to_int_type_handler();
2967 }
2968 const Type_handler *real_type_handler() const
2969 {
2970 if (field->is_created_from_null_item)
2971 return &type_handler_null;
2972 return field->type_handler();
2973 }
2974 TYPELIB *get_typelib() const { return field->get_typelib(); }
2975 enum_monotonicity_info get_monotonicity_info() const
2976 {
2977 return MONOTONIC_STRICT_INCREASING;
2978 }
2979 longlong val_int_endpoint(bool left_endp, bool *incl_endp);
2980 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
2981 bool get_date_result(MYSQL_TIME *ltime,ulonglong fuzzydate);
2982 bool is_null() { return field->is_null(); }
2983 void update_null_value();
2984 void update_table_bitmaps()
2985 {
2986 if (field && field->table)
2987 {
2988 TABLE *tab= field->table;
2989 tab->covering_keys.intersect(field->part_of_key);
2990 if (tab->read_set)
2991 bitmap_fast_test_and_set(tab->read_set, field->field_index);
2992 /*
2993 Do not mark a self-referecing virtual column.
2994 Such virtual columns are reported as invalid.
2995 */
2996 if (field->vcol_info && tab->vcol_set)
2997 tab->mark_virtual_col(field);
2998 }
2999 }
3000 void update_used_tables()
3001 {
3002 update_table_bitmaps();
3003 }
3004 COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
3005 bool link_item_fields,
3006 COND_EQUAL **cond_equal_ref)
3007 {
3008 /*
3009 normilize_cond() replaced all conditions of type
3010 WHERE/HAVING field
3011 to:
3012 WHERE/HAVING field<>0
3013 By the time of a build_equal_items() call, all such conditions should
3014 already be replaced. No Item_field are possible.
3015 Note, some Item_field derivants are still possible.
3016 Item_insert_value:
3017 SELECT * FROM t1 WHERE VALUES(a);
3018 Item_default_value:
3019 SELECT * FROM t1 WHERE DEFAULT(a);
3020 */
3021 DBUG_ASSERT(type() != FIELD_ITEM);
3022 return Item_ident::build_equal_items(thd, inherited, link_item_fields,
3023 cond_equal_ref);
3024 }
3025 bool is_result_field() { return false; }
3026 void save_in_result_field(bool no_conversions);
3027 Item *get_tmp_table_item(THD *thd);
3028 bool collect_item_field_processor(void * arg);
3029 bool add_field_to_set_processor(void * arg);
3030 bool find_item_in_field_list_processor(void *arg);
3031 bool register_field_in_read_map(void *arg);
3032 bool register_field_in_write_map(void *arg);
3033 bool register_field_in_bitmap(void *arg);
3034 bool check_partition_func_processor(void *int_arg) {return FALSE;}
3035 bool post_fix_fields_part_expr_processor(void *bool_arg);
3036 bool check_valid_arguments_processor(void *bool_arg);
3037 bool check_field_expression_processor(void *arg);
3038 bool enumerate_field_refs_processor(void *arg);
3039 bool update_table_bitmaps_processor(void *arg);
3040 bool switch_to_nullable_fields_processor(void *arg);
3041 bool update_vcol_processor(void *arg);
3042 bool rename_fields_processor(void *arg);
3043 bool check_vcol_func_processor(void *arg)
3044 {
3045 context= 0;
3046 if (field && (field->unireg_check == Field::NEXT_NUMBER))
3047 {
3048 // Auto increment fields are unsupported
3049 return mark_unsupported_function(field_name.str, arg, VCOL_FIELD_REF | VCOL_AUTO_INC);
3050 }
3051 return mark_unsupported_function(field_name.str, arg, VCOL_FIELD_REF);
3052 }
3053 bool set_fields_as_dependent_processor(void *arg)
3054 {
3055 if (!(used_tables() & OUTER_REF_TABLE_BIT))
3056 {
3057 depended_from= (st_select_lex *) arg;
3058 item_equal= NULL;
3059 }
3060 return 0;
3061 }
3062 void cleanup();
3063 Item_equal *get_item_equal() { return item_equal; }
3064 void set_item_equal(Item_equal *item_eq) { item_equal= item_eq; }
3065 Item_equal *find_item_equal(COND_EQUAL *cond_equal);
3066 Item* propagate_equal_fields(THD *, const Context &, COND_EQUAL *);
3067 Item *replace_equal_field(THD *thd, uchar *arg);
3068 uint32 max_display_length() const { return field->max_display_length(); }
3069 Item_field *field_for_view_update() { return this; }
3070 int fix_outer_field(THD *thd, Field **field, Item **reference);
3071 virtual Item *update_value_transformer(THD *thd, uchar *select_arg);
3072 Item *derived_field_transformer_for_having(THD *thd, uchar *arg);
3073 Item *derived_field_transformer_for_where(THD *thd, uchar *arg);
3074 Item *derived_grouping_field_transformer_for_where(THD *thd, uchar *arg);
3075 virtual void print(String *str, enum_query_type query_type);
3076 bool excl_dep_on_table(table_map tab_map);
3077 bool excl_dep_on_grouping_fields(st_select_lex *sel);
3078 bool cleanup_excluding_fields_processor(void *arg)
3079 { return field ? 0 : cleanup_processor(arg); }
3080 bool cleanup_excluding_const_fields_processor(void *arg)
3081 { return field && const_item() ? 0 : cleanup_processor(arg); }
3082
3083 Item *get_copy(THD *thd)
3084 { return get_item_copy<Item_field>(thd, this); }
3085 bool is_outer_field() const
3086 {
3087 DBUG_ASSERT(fixed);
3088 return field->table->pos_in_table_list->outer_join;
3089 }
3090 Field::geometry_type get_geometry_type() const
3091 {
3092 DBUG_ASSERT(field_type() == MYSQL_TYPE_GEOMETRY);
3093 return field->get_geometry_type();
3094 }
3095 friend class Item_default_value;
3096 friend class Item_insert_value;
3097 friend class st_select_lex_unit;
3098};
3099
3100
3101/**
3102 Item_field for the ROW data type
3103*/
3104class Item_field_row: public Item_field,
3105 public Item_args
3106{
3107public:
3108 Item_field_row(THD *thd, Field *field)
3109 :Item_field(thd, field),
3110 Item_args()
3111 { }
3112 Item *get_copy(THD *thd)
3113 { return get_item_copy<Item_field_row>(thd, this); }
3114
3115 const Type_handler *type_handler() const { return &type_handler_row; }
3116 uint cols() const { return arg_count; }
3117 Item* element_index(uint i) { return arg_count ? args[i] : this; }
3118 Item** addr(uint i) { return arg_count ? args + i : NULL; }
3119 bool check_cols(uint c)
3120 {
3121 if (cols() != c)
3122 {
3123 my_error(ER_OPERAND_COLUMNS, MYF(0), c);
3124 return true;
3125 }
3126 return false;
3127 }
3128 bool row_create_items(THD *thd, List<Spvar_definition> *list);
3129};
3130
3131
3132/*
3133 @brief
3134 Item_temptable_field is the same as Item_field, except that print()
3135 continues to work even if the table has been dropped.
3136
3137 @detail
3138
3139 We need this item for "ANALYZE statement" feature. Query execution has
3140 these steps:
3141
3142 1. Run the query.
3143 2. Cleanup starts. Temporary tables are destroyed
3144 3. print "ANALYZE statement" output, if needed
3145 4. Call close_thread_table() for regular tables.
3146
3147 Step #4 is done after step #3, so "ANALYZE stmt" has no problem printing
3148 Item_field objects that refer to regular tables.
3149
3150 However, Step #3 is done after Step #2. Attempt to print Item_field objects
3151 that refer to temporary tables will cause access to freed memory.
3152
3153 To resolve this, we use Item_temptable_field to refer to items in temporary
3154 (work) tables.
3155*/
3156
3157class Item_temptable_field :public Item_field
3158{
3159public:
3160 Item_temptable_field(THD *thd, Name_resolution_context *context_arg, Field *field)
3161 : Item_field(thd, context_arg, field) {}
3162
3163 Item_temptable_field(THD *thd, Field *field)
3164 : Item_field(thd, field) {}
3165
3166 Item_temptable_field(THD *thd, Item_field *item) : Item_field(thd, item) {};
3167
3168 virtual void print(String *str, enum_query_type query_type);
3169};
3170
3171
3172class Item_null :public Item_basic_constant
3173{
3174public:
3175 Item_null(THD *thd, const char *name_par=0, CHARSET_INFO *cs= &my_charset_bin):
3176 Item_basic_constant(thd)
3177 {
3178 maybe_null= null_value= TRUE;
3179 max_length= 0;
3180 name.str= name_par ? name_par : "NULL";
3181 name.length= strlen(name.str);
3182 fixed= 1;
3183 collation.set(cs, DERIVATION_IGNORABLE, MY_REPERTOIRE_ASCII);
3184 }
3185 enum Type type() const { return NULL_ITEM; }
3186 bool eq(const Item *item, bool binary_cmp) const { return null_eq(item); }
3187 double val_real();
3188 longlong val_int();
3189 String *val_str(String *str);
3190 my_decimal *val_decimal(my_decimal *);
3191 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
3192 int save_in_field(Field *field, bool no_conversions);
3193 int save_safe_in_field(Field *field);
3194 bool send(Protocol *protocol, st_value *buffer);
3195 const Type_handler *type_handler() const { return &type_handler_null; }
3196 bool basic_const_item() const { return 1; }
3197 Item *clone_item(THD *thd);
3198 bool is_null() { return 1; }
3199
3200 virtual inline void print(String *str, enum_query_type query_type)
3201 {
3202 str->append(STRING_WITH_LEN("NULL"));
3203 }
3204
3205 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
3206 bool check_partition_func_processor(void *int_arg) {return FALSE;}
3207 Item_basic_constant *make_string_literal_concat(THD *thd,
3208 const LEX_CSTRING *);
3209 Item *get_copy(THD *thd)
3210 { return get_item_copy<Item_null>(thd, this); }
3211};
3212
3213class Item_null_result :public Item_null
3214{
3215public:
3216 Field *result_field;
3217 Item_null_result(THD *thd): Item_null(thd), result_field(0) {}
3218 bool is_result_field() { return result_field != 0; }
3219 enum_field_types field_type() const
3220 {
3221 return result_field->type();
3222 }
3223 void save_in_result_field(bool no_conversions)
3224 {
3225 save_in_field(result_field, no_conversions);
3226 }
3227 bool check_partition_func_processor(void *int_arg) {return TRUE;}
3228 bool check_vcol_func_processor(void *arg)
3229 {
3230 return mark_unsupported_function(full_name(), arg, VCOL_IMPOSSIBLE);
3231 }
3232};
3233
3234/*
3235 Item represents one placeholder ('?') of prepared statement
3236
3237 Notes:
3238 Item_param::field_type() is used when this item is in a temporary table.
3239 This is NOT placeholder metadata sent to client, as this value
3240 is assigned after sending metadata (in setup_one_conversion_function).
3241 For example in case of 'SELECT ?' you'll get MYSQL_TYPE_STRING both
3242 in result set and placeholders metadata, no matter what type you will
3243 supply for this placeholder in mysql_stmt_execute.
3244
3245 Item_param has two Type_handler pointers,
3246 which can point to different handlers:
3247
3248 1. In the Type_handler_hybrid_field_type member
3249 It's initialized in:
3250 - Item_param::setup_conversion(), for client-server PS protocol,
3251 according to the bind type.
3252 - Item_param::set_from_item(), for EXECUTE and EXECUTE IMMEDIATE,
3253 according to the actual parameter data type.
3254
3255 2. In the "value" member.
3256 It's initialized in:
3257 - Item_param::set_param_func(), for client-server PS protocol.
3258 - Item_param::set_from_item(), for EXECUTE and EXECUTE IMMEDIATE.
3259*/
3260
3261class Item_param :public Item_basic_value,
3262 private Settable_routine_parameter,
3263 public Rewritable_query_parameter,
3264 private Type_handler_hybrid_field_type,
3265 public Type_geometry_attributes
3266{
3267 /*
3268 NO_VALUE is a special value meaning that the parameter has not been
3269 assigned yet. Item_param::state is assigned to NO_VALUE in constructor
3270 and is used at prepare time.
3271
3272 1. At prepare time
3273 Item_param::fix_fields() sets "fixed" to true,
3274 but as Item_param::state is still NO_VALUE,
3275 Item_param::basic_const_item() returns false. This prevents various
3276 optimizations to happen at prepare time fix_fields().
3277 For example, in this query:
3278 PREPARE stmt FROM 'SELECT FORMAT(10000,2,?)';
3279 Item_param::basic_const_item() is tested from
3280 Item_func_format::fix_length_and_dec().
3281
3282 2. At execute time:
3283 When Item_param gets a value
3284 (or a pseudo-value like DEFAULT_VALUE or IGNORE_VALUE):
3285 - Item_param::state changes from NO_VALUE to something else
3286 - Item_param::fixed is changed to true
3287 All Item_param::set_xxx() make sure to do so.
3288 In the state with an assigned value:
3289 - Item_param::basic_const_item() returns true
3290 - Item::type() returns NULL_ITEM, INT_ITEM, REAL_ITEM, DECIMAL_ITEM,
3291 DATE_ITEM, STRING_ITEM, depending on the value assigned.
3292 So in this state Item_param behaves in many cases like a literal.
3293
3294 When Item_param::cleanup() is called:
3295 - Item_param::state does not change
3296 - Item_param::fixed changes to false
3297 Note, this puts Item_param into an inconsistent state:
3298 - Item_param::basic_const_item() still returns "true"
3299 - Item_param::type() still pretends to be a basic constant Item
3300 Both are not expected in combination with fixed==false.
3301 However, these methods are not really called in this state,
3302 see asserts in Item_param::basic_const_item() and Item_param::type().
3303
3304 When Item_param::reset() is called:
3305 - Item_param::state changes to NO_VALUE
3306 - Item_param::fixed changes to false
3307 */
3308 enum enum_item_param_state
3309 {
3310 NO_VALUE, NULL_VALUE, SHORT_DATA_VALUE, LONG_DATA_VALUE,
3311 DEFAULT_VALUE, IGNORE_VALUE
3312 } state;
3313
3314 enum Type item_type;
3315
3316 void fix_type(Type type)
3317 {
3318 item_type= type;
3319 fixed= true;
3320 }
3321
3322 void fix_temporal(uint32 max_length_arg, uint decimals_arg);
3323
3324 struct CONVERSION_INFO
3325 {
3326 /*
3327 Character sets conversion info for string values.
3328 Character sets of client and connection defined at bind time are used
3329 for all conversions, even if one of them is later changed (i.e.
3330 between subsequent calls to mysql_stmt_execute).
3331 */
3332 CHARSET_INFO *character_set_client;
3333 CHARSET_INFO *character_set_of_placeholder;
3334 /*
3335 This points at character set of connection if conversion
3336 to it is required (i. e. if placeholder typecode is not BLOB).
3337 Otherwise it's equal to character_set_client (to simplify
3338 check in convert_str_value()).
3339 */
3340 CHARSET_INFO *final_character_set_of_str_value;
3341 private:
3342 bool needs_conversion() const
3343 {
3344 return final_character_set_of_str_value !=
3345 character_set_of_placeholder;
3346 }
3347 bool convert(THD *thd, String *str);
3348 public:
3349 void set(THD *thd, CHARSET_INFO *cs);
3350 bool convert_if_needed(THD *thd, String *str)
3351 {
3352 /*
3353 Check is so simple because all charsets were set up properly
3354 in setup_one_conversion_function, where typecode of
3355 placeholder was also taken into account: the variables are different
3356 here only if conversion is really necessary.
3357 */
3358 if (needs_conversion())
3359 return convert(thd, str);
3360 str->set_charset(final_character_set_of_str_value);
3361 return false;
3362 }
3363 };
3364
3365 bool m_empty_string_is_null;
3366
3367 class PValue_simple
3368 {
3369 public:
3370 union
3371 {
3372 longlong integer;
3373 double real;
3374 CONVERSION_INFO cs_info;
3375 MYSQL_TIME time;
3376 };
3377 void swap(PValue_simple &other)
3378 {
3379 swap_variables(PValue_simple, *this, other);
3380 }
3381 };
3382
3383 class PValue: public Type_handler_hybrid_field_type,
3384 public PValue_simple,
3385 public Value_source
3386 {
3387 public:
3388 PValue(): Type_handler_hybrid_field_type(&type_handler_null) {}
3389 my_decimal m_decimal;
3390 String m_string;
3391 /*
3392 A buffer for string and long data values. Historically all allocated
3393 values returned from val_str() were treated as eligible to
3394 modification. I. e. in some cases Item_func_concat can append it's
3395 second argument to return value of the first one. Because of that we
3396 can't return the original buffer holding string data from val_str(),
3397 and have to have one buffer for data and another just pointing to
3398 the data. This is the latter one and it's returned from val_str().
3399 Can not be declared inside the union as it's not a POD type.
3400 */
3401 String m_string_ptr;
3402
3403 void swap(PValue &other)
3404 {
3405 Type_handler_hybrid_field_type::swap(other);
3406 PValue_simple::swap(other);
3407 m_decimal.swap(other.m_decimal);
3408 m_string.swap(other.m_string);
3409 m_string_ptr.swap(other.m_string_ptr);
3410 }
3411 double val_real() const;
3412 longlong val_int(const Type_std_attributes *attr) const;
3413 my_decimal *val_decimal(my_decimal *dec, const Type_std_attributes *attr);
3414 String *val_str(String *str, const Type_std_attributes *attr);
3415 };
3416
3417 PValue value;
3418
3419 const String *value_query_val_str(THD *thd, String* str) const;
3420 bool value_eq(const Item *item, bool binary_cmp) const;
3421 Item *value_clone_item(THD *thd);
3422 bool can_return_value() const;
3423
3424public:
3425 /*
3426 Used for bulk protocol only.
3427 */
3428 enum enum_indicator_type indicator;
3429
3430 const Type_handler *type_handler() const
3431 { return Type_handler_hybrid_field_type::type_handler(); }
3432
3433 Field::geometry_type get_geometry_type() const
3434 { return Type_geometry_attributes::get_geometry_type(); };
3435
3436 void set_geometry_type(uint type)
3437 { Type_geometry_attributes::set_geometry_type(type); }
3438
3439 Item_param(THD *thd, const LEX_CSTRING *name_arg,
3440 uint pos_in_query_arg, uint len_in_query_arg);
3441
3442 enum Type type() const
3443 {
3444 DBUG_ASSERT(fixed || state == NO_VALUE);
3445 return item_type;
3446 }
3447
3448 double val_real()
3449 {
3450 return can_return_value() ? value.val_real() : 0e0;
3451 }
3452 longlong val_int()
3453 {
3454 return can_return_value() ? value.val_int(this) : 0;
3455 }
3456 my_decimal *val_decimal(my_decimal *dec)
3457 {
3458 return can_return_value() ? value.val_decimal(dec, this) : NULL;
3459 }
3460 String *val_str(String *str)
3461 {
3462 return can_return_value() ? value.val_str(str, this) : NULL;
3463 }
3464 bool get_date(MYSQL_TIME *tm, ulonglong fuzzydate);
3465 int save_in_field(Field *field, bool no_conversions);
3466
3467 void set_default();
3468 void set_ignore();
3469 void set_null();
3470 void set_int(longlong i, uint32 max_length_arg);
3471 void set_double(double i);
3472 void set_decimal(const char *str, ulong length);
3473 void set_decimal(const my_decimal *dv, bool unsigned_arg);
3474 bool set_str(const char *str, ulong length,
3475 CHARSET_INFO *fromcs, CHARSET_INFO *tocs);
3476 bool set_longdata(const char *str, ulong length);
3477 void set_time(MYSQL_TIME *tm, timestamp_type type, uint32 max_length_arg);
3478 void set_time(const MYSQL_TIME *tm, uint32 max_length_arg, uint decimals_arg);
3479 bool set_from_item(THD *thd, Item *item);
3480 void reset();
3481
3482 void set_param_tiny(uchar **pos, ulong len);
3483 void set_param_short(uchar **pos, ulong len);
3484 void set_param_int32(uchar **pos, ulong len);
3485 void set_param_int64(uchar **pos, ulong len);
3486 void set_param_float(uchar **pos, ulong len);
3487 void set_param_double(uchar **pos, ulong len);
3488 void set_param_decimal(uchar **pos, ulong len);
3489 void set_param_time(uchar **pos, ulong len);
3490 void set_param_datetime(uchar **pos, ulong len);
3491 void set_param_date(uchar **pos, ulong len);
3492 void set_param_str(uchar **pos, ulong len);
3493
3494 void setup_conversion(THD *thd, uchar param_type);
3495 void setup_conversion_blob(THD *thd);
3496 void setup_conversion_string(THD *thd, CHARSET_INFO *fromcs);
3497
3498 /*
3499 Assign placeholder value from bind data.
3500 Note, that 'len' has different semantics in embedded library (as we
3501 don't need to check that packet is not broken there). See
3502 sql_prepare.cc for details.
3503 */
3504 void set_param_func(uchar **pos, ulong len)
3505 {
3506 /*
3507 To avoid Item_param::set_xxx() asserting on data type mismatch,
3508 we set the value type handler here:
3509 - It can not be initialized yet after Item_param::setup_conversion().
3510 - Also, for LIMIT clause parameters, the value type handler might have
3511 changed from the real type handler to type_handler_longlong.
3512 So here we'll restore it.
3513 */
3514 const Type_handler *h= Item_param::type_handler();
3515 value.set_handler(h);
3516 h->Item_param_set_param_func(this, pos, len);
3517 }
3518
3519 bool set_value(THD *thd, const Type_all_attributes *attr,
3520 const st_value *val, const Type_handler *h)
3521 {
3522 value.set_handler(h); // See comments in set_param_func()
3523 return h->Item_param_set_from_value(thd, this, attr, val);
3524 }
3525
3526 bool set_limit_clause_param(longlong nr)
3527 {
3528 value.set_handler(&type_handler_longlong);
3529 set_int(nr, MY_INT64_NUM_DECIMAL_DIGITS);
3530 return !unsigned_flag && value.integer < 0;
3531 }
3532 const String *query_val_str(THD *thd, String *str) const;
3533
3534 bool convert_str_value(THD *thd);
3535
3536 /*
3537 If value for parameter was not set we treat it as non-const
3538 so no one will use parameters value in fix_fields still
3539 parameter is constant during execution.
3540 */
3541 virtual table_map used_tables() const
3542 { return state != NO_VALUE ? (table_map)0 : PARAM_TABLE_BIT; }
3543 virtual void print(String *str, enum_query_type query_type);
3544 bool is_null()
3545 { DBUG_ASSERT(state != NO_VALUE); return state == NULL_VALUE; }
3546 bool basic_const_item() const;
3547 bool has_no_value() const
3548 {
3549 return state == NO_VALUE;
3550 }
3551 bool has_long_data_value() const
3552 {
3553 return state == LONG_DATA_VALUE;
3554 }
3555 bool has_int_value() const
3556 {
3557 return state == SHORT_DATA_VALUE &&
3558 value.type_handler()->cmp_type() == INT_RESULT;
3559 }
3560 /*
3561 This method is used to make a copy of a basic constant item when
3562 propagating constants in the optimizer. The reason to create a new
3563 item and not use the existing one is not precisely known (2005/04/16).
3564 Probably we are trying to preserve tree structure of items, in other
3565 words, avoid pointing at one item from two different nodes of the tree.
3566 Return a new basic constant item if parameter value is a basic
3567 constant, assert otherwise. This method is called only if
3568 basic_const_item returned TRUE.
3569 */
3570 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
3571 Item *clone_item(THD *thd);
3572 /*
3573 Implement by-value equality evaluation if parameter value
3574 is set and is a basic constant (integer, real or string).
3575 Otherwise return FALSE.
3576 */
3577 bool eq(const Item *item, bool binary_cmp) const;
3578 void set_param_type_and_swap_value(Item_param *from);
3579
3580 Rewritable_query_parameter *get_rewritable_query_parameter()
3581 { return this; }
3582 Settable_routine_parameter *get_settable_routine_parameter()
3583 { return m_is_settable_routine_parameter ? this : NULL; }
3584
3585 bool append_for_log(THD *thd, String *str);
3586 bool check_vcol_func_processor(void *int_arg) {return FALSE;}
3587 Item *get_copy(THD *thd) { return 0; }
3588
3589private:
3590 void invalid_default_param() const;
3591
3592 virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
3593
3594 virtual void set_out_param_info(Send_field *info);
3595
3596public:
3597 virtual const Send_field *get_out_param_info() const;
3598
3599 Item_param *get_item_param() { return this; }
3600
3601 virtual void make_send_field(THD *thd, Send_field *field);
3602
3603private:
3604 Send_field *m_out_param_info;
3605 bool m_is_settable_routine_parameter;
3606};
3607
3608
3609class Item_int :public Item_num
3610{
3611public:
3612 longlong value;
3613 Item_int(THD *thd, int32 i,size_t length= MY_INT32_NUM_DECIMAL_DIGITS):
3614 Item_num(thd), value((longlong) i)
3615 { max_length=(uint32)length; fixed= 1; }
3616 Item_int(THD *thd, longlong i,size_t length= MY_INT64_NUM_DECIMAL_DIGITS):
3617 Item_num(thd), value(i)
3618 { max_length=(uint32)length; fixed= 1; }
3619 Item_int(THD *thd, ulonglong i, size_t length= MY_INT64_NUM_DECIMAL_DIGITS):
3620 Item_num(thd), value((longlong)i)
3621 { max_length=(uint32)length; fixed= 1; unsigned_flag= 1; }
3622 Item_int(THD *thd, const char *str_arg,longlong i,size_t length):
3623 Item_num(thd), value(i)
3624 {
3625 max_length=(uint32)length;
3626 name.str= str_arg; name.length= safe_strlen(name.str);
3627 fixed= 1;
3628 }
3629 Item_int(THD *thd, const char *str_arg,longlong i,size_t length, bool flag):
3630 Item_num(thd), value(i)
3631 {
3632 max_length=(uint32)length;
3633 name.str= str_arg; name.length= safe_strlen(name.str);
3634 fixed= 1;
3635 unsigned_flag= flag;
3636 }
3637 Item_int(THD *thd, const char *str_arg, size_t length=64);
3638 enum Type type() const { return INT_ITEM; }
3639 const Type_handler *type_handler() const
3640 { return type_handler_long_or_longlong(); }
3641 Field *create_tmp_field(bool group, TABLE *table)
3642 { return tmp_table_field_from_field_type(table); }
3643 Field *create_field_for_create_select(TABLE *table)
3644 { return tmp_table_field_from_field_type(table); }
3645 longlong val_int() { DBUG_ASSERT(fixed == 1); return value; }
3646 longlong val_int_min() const { DBUG_ASSERT(fixed == 1); return value; }
3647 double val_real() { DBUG_ASSERT(fixed == 1); return (double) value; }
3648 my_decimal *val_decimal(my_decimal *);
3649 String *val_str(String*);
3650 int save_in_field(Field *field, bool no_conversions);
3651 bool basic_const_item() const { return 1; }
3652 Item *clone_item(THD *thd);
3653 virtual void print(String *str, enum_query_type query_type);
3654 Item *neg(THD *thd);
3655 uint decimal_precision() const
3656 { return (uint) (max_length - MY_TEST(value < 0)); }
3657 bool eq(const Item *item, bool binary_cmp) const
3658 { return int_eq(value, item); }
3659 Item *get_copy(THD *thd)
3660 { return get_item_copy<Item_int>(thd, this); }
3661};
3662
3663
3664/*
3665 We sometimes need to distinguish a number from a boolean:
3666 a[1] and a[true] are different things in XPath.
3667 Also in JSON boolean values should be treated differently.
3668*/
3669class Item_bool :public Item_int
3670{
3671public:
3672 Item_bool(THD *thd, const char *str_arg, longlong i):
3673 Item_int(thd, str_arg, i, 1) {}
3674 bool is_bool_type() { return true; }
3675 Item *neg_transformer(THD *thd);
3676};
3677
3678
3679class Item_uint :public Item_int
3680{
3681public:
3682 Item_uint(THD *thd, const char *str_arg, size_t length);
3683 Item_uint(THD *thd, ulonglong i): Item_int(thd, i, 10) {}
3684 Item_uint(THD *thd, const char *str_arg, longlong i, uint length);
3685 double val_real()
3686 { DBUG_ASSERT(fixed == 1); return ulonglong2double((ulonglong)value); }
3687 String *val_str(String*);
3688 Item *clone_item(THD *thd);
3689 virtual void print(String *str, enum_query_type query_type);
3690 Item *neg(THD *thd);
3691 uint decimal_precision() const { return max_length; }
3692 Item *get_copy(THD *thd)
3693 { return get_item_copy<Item_uint>(thd, this); }
3694};
3695
3696
3697class Item_datetime :public Item_int
3698{
3699protected:
3700 MYSQL_TIME ltime;
3701public:
3702 Item_datetime(THD *thd): Item_int(thd, 0) { unsigned_flag=0; }
3703 int save_in_field(Field *field, bool no_conversions);
3704 longlong val_int();
3705 double val_real() { return (double)val_int(); }
3706 void set(longlong packed, enum_mysql_timestamp_type ts_type);
3707 bool get_date(MYSQL_TIME *to, ulonglong fuzzydate)
3708 {
3709 *to= ltime;
3710 return false;
3711 }
3712};
3713
3714
3715/* decimal (fixed point) constant */
3716class Item_decimal :public Item_num
3717{
3718protected:
3719 my_decimal decimal_value;
3720public:
3721 Item_decimal(THD *thd, const char *str_arg, size_t length,
3722 CHARSET_INFO *charset);
3723 Item_decimal(THD *thd, const char *str, const my_decimal *val_arg,
3724 uint decimal_par, uint length);
3725 Item_decimal(THD *thd, my_decimal *value_par);
3726 Item_decimal(THD *thd, longlong val, bool unsig);
3727 Item_decimal(THD *thd, double val, int precision, int scale);
3728 Item_decimal(THD *thd, const uchar *bin, int precision, int scale);
3729
3730 enum Type type() const { return DECIMAL_ITEM; }
3731 const Type_handler *type_handler() const { return &type_handler_newdecimal; }
3732 longlong val_int();
3733 double val_real();
3734 String *val_str(String*);
3735 my_decimal *val_decimal(my_decimal *val) { return &decimal_value; }
3736 int save_in_field(Field *field, bool no_conversions);
3737 bool basic_const_item() const { return 1; }
3738 Item *clone_item(THD *thd);
3739 virtual void print(String *str, enum_query_type query_type);
3740 Item *neg(THD *thd);
3741 uint decimal_precision() const { return decimal_value.precision(); }
3742 bool eq(const Item *, bool binary_cmp) const;
3743 void set_decimal_value(my_decimal *value_par);
3744 Item *get_copy(THD *thd)
3745 { return get_item_copy<Item_decimal>(thd, this); }
3746};
3747
3748
3749class Item_float :public Item_num
3750{
3751 const char *presentation;
3752public:
3753 double value;
3754 Item_float(THD *thd, const char *str_arg, size_t length);
3755 Item_float(THD *thd, const char *str, double val_arg, uint decimal_par,
3756 uint length): Item_num(thd), value(val_arg)
3757 {
3758 presentation= name.str= str;
3759 name.length= safe_strlen(str);
3760 decimals=(uint8) decimal_par;
3761 max_length= length;
3762 fixed= 1;
3763 }
3764 Item_float(THD *thd, double value_par, uint decimal_par):
3765 Item_num(thd), presentation(0), value(value_par)
3766 {
3767 decimals= (uint8) decimal_par;
3768 fixed= 1;
3769 }
3770 int save_in_field(Field *field, bool no_conversions);
3771 enum Type type() const { return REAL_ITEM; }
3772 const Type_handler *type_handler() const { return &type_handler_double; }
3773 double val_real() { DBUG_ASSERT(fixed == 1); return value; }
3774 longlong val_int()
3775 {
3776 DBUG_ASSERT(fixed == 1);
3777 if (value <= (double) LONGLONG_MIN)
3778 {
3779 return LONGLONG_MIN;
3780 }
3781 else if (value >= (double) (ulonglong) LONGLONG_MAX)
3782 {
3783 return LONGLONG_MAX;
3784 }
3785 return (longlong) rint(value);
3786 }
3787 String *val_str(String*);
3788 my_decimal *val_decimal(my_decimal *);
3789 bool basic_const_item() const { return 1; }
3790 Item *clone_item(THD *thd);
3791 Item *neg(THD *thd);
3792 virtual void print(String *str, enum_query_type query_type);
3793 bool eq(const Item *item, bool binary_cmp) const
3794 { return real_eq(value, item); }
3795 Item *get_copy(THD *thd)
3796 { return get_item_copy<Item_float>(thd, this); }
3797};
3798
3799
3800class Item_static_float_func :public Item_float
3801{
3802 const char *func_name;
3803public:
3804 Item_static_float_func(THD *thd, const char *str, double val_arg,
3805 uint decimal_par, uint length):
3806 Item_float(thd, NullS, val_arg, decimal_par, length), func_name(str)
3807 {}
3808
3809 virtual inline void print(String *str, enum_query_type query_type)
3810 {
3811 str->append(func_name);
3812 }
3813
3814 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
3815 {
3816 return const_charset_converter(thd, tocs, true, func_name);
3817 }
3818};
3819
3820
3821class Item_string :public Item_basic_constant
3822{
3823protected:
3824 void fix_from_value(Derivation dv, const Metadata metadata)
3825 {
3826 fix_charset_and_length(str_value.charset(), dv, metadata);
3827 // it is constant => can be used without fix_fields (and frequently used)
3828 fixed= 1;
3829 }
3830 void fix_and_set_name_from_value(THD *thd, Derivation dv,
3831 const Metadata metadata)
3832 {
3833 fix_from_value(dv, metadata);
3834 set_name(thd, str_value.ptr(), str_value.length(), str_value.charset());
3835 }
3836protected:
3837 /* Just create an item and do not fill string representation */
3838 Item_string(THD *thd, CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE):
3839 Item_basic_constant(thd)
3840 {
3841 collation.set(cs, dv);
3842 max_length= 0;
3843 set_name(thd, NULL, 0, system_charset_info);
3844 decimals= NOT_FIXED_DEC;
3845 fixed= 1;
3846 }
3847public:
3848 Item_string(THD *thd, CHARSET_INFO *csi, const char *str_arg, uint length_arg):
3849 Item_basic_constant(thd)
3850 {
3851 collation.set(csi, DERIVATION_COERCIBLE);
3852 set_name(thd, NULL, 0, system_charset_info);
3853 decimals= NOT_FIXED_DEC;
3854 fixed= 1;
3855 str_value.copy(str_arg, length_arg, csi);
3856 max_length= str_value.numchars() * csi->mbmaxlen;
3857 }
3858 // Constructors with the item name set from its value
3859 Item_string(THD *thd, const char *str, uint length, CHARSET_INFO *cs,
3860 Derivation dv, uint repertoire): Item_basic_constant(thd)
3861 {
3862 str_value.set_or_copy_aligned(str, length, cs);
3863 fix_and_set_name_from_value(thd, dv, Metadata(&str_value, repertoire));
3864 }
3865 Item_string(THD *thd, const char *str, size_t length,
3866 CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE):
3867 Item_basic_constant(thd)
3868 {
3869 str_value.set_or_copy_aligned(str, length, cs);
3870 fix_and_set_name_from_value(thd, dv, Metadata(&str_value));
3871 }
3872 Item_string(THD *thd, const String *str, CHARSET_INFO *tocs, uint *conv_errors,
3873 Derivation dv, uint repertoire): Item_basic_constant(thd)
3874 {
3875 if (str_value.copy(str, tocs, conv_errors))
3876 str_value.set("", 0, tocs); // EOM ?
3877 str_value.mark_as_const();
3878 fix_and_set_name_from_value(thd, dv, Metadata(&str_value, repertoire));
3879 }
3880 // Constructors with an externally provided item name
3881 Item_string(THD *thd, const char *name_par, const char *str, size_t length,
3882 CHARSET_INFO *cs, Derivation dv= DERIVATION_COERCIBLE):
3883 Item_basic_constant(thd)
3884 {
3885 str_value.set_or_copy_aligned(str, length, cs);
3886 fix_from_value(dv, Metadata(&str_value));
3887 set_name(thd, name_par,safe_strlen(name_par), system_charset_info);
3888 }
3889 Item_string(THD *thd, const char *name_par, const char *str, size_t length,
3890 CHARSET_INFO *cs, Derivation dv, uint repertoire):
3891 Item_basic_constant(thd)
3892 {
3893 str_value.set_or_copy_aligned(str, length, cs);
3894 fix_from_value(dv, Metadata(&str_value, repertoire));
3895 set_name(thd, name_par, safe_strlen(name_par), system_charset_info);
3896 }
3897 void print_value(String *to) const
3898 {
3899 str_value.print(to);
3900 }
3901 enum Type type() const { return STRING_ITEM; }
3902 double val_real();
3903 longlong val_int();
3904 String *val_str(String*)
3905 {
3906 DBUG_ASSERT(fixed == 1);
3907 return (String*) &str_value;
3908 }
3909 my_decimal *val_decimal(my_decimal *);
3910 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
3911 {
3912 return get_date_from_string(ltime, fuzzydate);
3913 }
3914 int save_in_field(Field *field, bool no_conversions);
3915 const Type_handler *type_handler() const { return &type_handler_varchar; }
3916 bool basic_const_item() const { return 1; }
3917 bool eq(const Item *item, bool binary_cmp) const
3918 {
3919 return str_eq(&str_value, item, binary_cmp);
3920 }
3921 Item *clone_item(THD *thd);
3922 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
3923 {
3924 return const_charset_converter(thd, tocs, true);
3925 }
3926 inline void append(const char *str, uint length)
3927 {
3928 str_value.append(str, length);
3929 max_length= str_value.numchars() * collation.collation->mbmaxlen;
3930 }
3931 virtual void print(String *str, enum_query_type query_type);
3932 bool check_partition_func_processor(void *int_arg) {return FALSE;}
3933
3934 /**
3935 Return TRUE if character-set-introducer was explicitly specified in the
3936 original query for this item (text literal).
3937
3938 This operation is to be called from Item_string::print(). The idea is
3939 that when a query is generated (re-constructed) from the Item-tree,
3940 character-set-introducers should appear only for those literals, where
3941 they were explicitly specified by the user. Otherwise, that may lead to
3942 loss collation information (character set introducers implies default
3943 collation for the literal).
3944
3945 Basically, that makes sense only for views and hopefully will be gone
3946 one day when we start using original query as a view definition.
3947
3948 @return This operation returns the value of m_cs_specified attribute.
3949 @retval TRUE if character set introducer was explicitly specified in
3950 the original query.
3951 @retval FALSE otherwise.
3952 */
3953 virtual bool is_cs_specified() const
3954 {
3955 return false;
3956 }
3957
3958 String *check_well_formed_result(bool send_error)
3959 { return Item::check_well_formed_result(&str_value, send_error); }
3960
3961 enum_field_types odbc_temporal_literal_type(const LEX_CSTRING *type_str) const
3962 {
3963 /*
3964 If string is a reasonably short pure ASCII string literal,
3965 try to parse known ODBC style date, time or timestamp literals,
3966 e.g:
3967 SELECT {d'2001-01-01'};
3968 SELECT {t'10:20:30'};
3969 SELECT {ts'2001-01-01 10:20:30'};
3970 */
3971 if (collation.repertoire == MY_REPERTOIRE_ASCII &&
3972 str_value.length() < MAX_DATE_STRING_REP_LENGTH * 4)
3973 {
3974 if (type_str->length == 1)
3975 {
3976 if (type_str->str[0] == 'd') /* {d'2001-01-01'} */
3977 return MYSQL_TYPE_DATE;
3978 else if (type_str->str[0] == 't') /* {t'10:20:30'} */
3979 return MYSQL_TYPE_TIME;
3980 }
3981 else if (type_str->length == 2) /* {ts'2001-01-01 10:20:30'} */
3982 {
3983 if (type_str->str[0] == 't' && type_str->str[1] == 's')
3984 return MYSQL_TYPE_DATETIME;
3985 }
3986 }
3987 return MYSQL_TYPE_STRING; // Not a temporal literal
3988 }
3989 Item_basic_constant *make_string_literal_concat(THD *thd,
3990 const LEX_CSTRING *);
3991 Item *make_odbc_literal(THD *thd, const LEX_CSTRING *typestr);
3992
3993 Item *get_copy(THD *thd)
3994 { return get_item_copy<Item_string>(thd, this); }
3995
3996};
3997
3998
3999class Item_string_with_introducer :public Item_string
4000{
4001public:
4002 Item_string_with_introducer(THD *thd, const char *str, uint length,
4003 CHARSET_INFO *cs):
4004 Item_string(thd, str, length, cs)
4005 { }
4006 Item_string_with_introducer(THD *thd, const char *name_arg,
4007 const char *str, uint length, CHARSET_INFO *tocs):
4008 Item_string(thd, name_arg, str, length, tocs)
4009 { }
4010 virtual bool is_cs_specified() const
4011 {
4012 return true;
4013 }
4014};
4015
4016
4017class Item_string_sys :public Item_string
4018{
4019public:
4020 Item_string_sys(THD *thd, const char *str, uint length):
4021 Item_string(thd, str, length, system_charset_info)
4022 { }
4023 Item_string_sys(THD *thd, const char *str):
4024 Item_string(thd, str, (uint) strlen(str), system_charset_info)
4025 { }
4026};
4027
4028
4029class Item_string_ascii :public Item_string
4030{
4031public:
4032 Item_string_ascii(THD *thd, const char *str, uint length):
4033 Item_string(thd, str, length, &my_charset_latin1,
4034 DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII)
4035 { }
4036 Item_string_ascii(THD *thd, const char *str):
4037 Item_string(thd, str, (uint) strlen(str), &my_charset_latin1,
4038 DERIVATION_COERCIBLE, MY_REPERTOIRE_ASCII)
4039 { }
4040};
4041
4042
4043class Item_static_string_func :public Item_string
4044{
4045 const char *func_name;
4046public:
4047 Item_static_string_func(THD *thd, const char *name_par, const char *str,
4048 uint length, CHARSET_INFO *cs,
4049 Derivation dv= DERIVATION_COERCIBLE):
4050 Item_string(thd, NullS, str, length, cs, dv), func_name(name_par)
4051 {}
4052 Item_static_string_func(THD *thd, const char *name_par,
4053 const String *str,
4054 CHARSET_INFO *tocs, uint *conv_errors,
4055 Derivation dv, uint repertoire):
4056 Item_string(thd, str, tocs, conv_errors, dv, repertoire),
4057 func_name(name_par)
4058 {}
4059 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
4060 {
4061 return const_charset_converter(thd, tocs, true, func_name);
4062 }
4063
4064 virtual inline void print(String *str, enum_query_type query_type)
4065 {
4066 str->append(func_name);
4067 }
4068
4069 bool check_partition_func_processor(void *int_arg) {return TRUE;}
4070
4071 bool check_vcol_func_processor(void *arg)
4072 { // VCOL_TIME_FUNC because the value is not constant, but does not
4073 // require fix_fields() to be re-run for every statement.
4074 return mark_unsupported_function(func_name, arg, VCOL_TIME_FUNC);
4075 }
4076};
4077
4078
4079/* for show tables */
4080class Item_partition_func_safe_string: public Item_string
4081{
4082public:
4083 Item_partition_func_safe_string(THD *thd, const char *name_arg, uint length,
4084 CHARSET_INFO *cs= NULL):
4085 Item_string(thd, name_arg, length, cs)
4086 {}
4087 bool check_vcol_func_processor(void *arg)
4088 {
4089 return mark_unsupported_function("safe_string", arg, VCOL_IMPOSSIBLE);
4090 }
4091};
4092
4093
4094class Item_return_date_time :public Item_partition_func_safe_string
4095{
4096 enum_field_types date_time_field_type;
4097public:
4098 Item_return_date_time(THD *thd, const char *name_arg, uint length_arg,
4099 enum_field_types field_type_arg, uint dec_arg= 0):
4100 Item_partition_func_safe_string(thd, name_arg, length_arg, &my_charset_bin),
4101 date_time_field_type(field_type_arg)
4102 { decimals= dec_arg; }
4103 const Type_handler *type_handler() const
4104 {
4105 return Type_handler::get_handler_by_field_type(date_time_field_type);
4106 }
4107};
4108
4109
4110class Item_blob :public Item_partition_func_safe_string
4111{
4112public:
4113 Item_blob(THD *thd, const char *name_arg, uint length):
4114 Item_partition_func_safe_string(thd, name_arg, (uint) safe_strlen(name_arg),
4115 &my_charset_bin)
4116 { max_length= length; }
4117 enum Type type() const { return TYPE_HOLDER; }
4118 const Type_handler *type_handler() const
4119 {
4120 return Type_handler::blob_type_handler(max_length);
4121 }
4122 const Type_handler *real_type_handler() const
4123 {
4124 // Should not be called, Item_blob is used for SHOW purposes only.
4125 DBUG_ASSERT(0);
4126 return &type_handler_varchar;
4127 }
4128 Field *create_field_for_schema(THD *thd, TABLE *table)
4129 { return tmp_table_field_from_field_type(table); }
4130};
4131
4132
4133/**
4134 Item_empty_string -- is a utility class to put an item into List<Item>
4135 which is then used in protocol.send_result_set_metadata() when sending SHOW output to
4136 the client.
4137*/
4138
4139class Item_empty_string :public Item_partition_func_safe_string
4140{
4141public:
4142 Item_empty_string(THD *thd, const char *header,uint length,
4143 CHARSET_INFO *cs= NULL):
4144 Item_partition_func_safe_string(thd, "", 0,
4145 cs ? cs : &my_charset_utf8_general_ci)
4146 {
4147 name.str= header;
4148 name.length= strlen(name.str);
4149 max_length= length * collation.collation->mbmaxlen;
4150 }
4151 void make_send_field(THD *thd, Send_field *field);
4152};
4153
4154
4155class Item_return_int :public Item_int
4156{
4157 enum_field_types int_field_type;
4158public:
4159 Item_return_int(THD *thd, const char *name_arg, uint length,
4160 enum_field_types field_type_arg, longlong value_arg= 0):
4161 Item_int(thd, name_arg, value_arg, length), int_field_type(field_type_arg)
4162 {
4163 unsigned_flag=1;
4164 }
4165 const Type_handler *type_handler() const
4166 {
4167 return Type_handler::get_handler_by_field_type(int_field_type);
4168 }
4169};
4170
4171
4172/**
4173 Item_hex_constant -- a common class for hex literals: X'HHHH' and 0xHHHH
4174*/
4175class Item_hex_constant: public Item_basic_constant
4176{
4177private:
4178 void hex_string_init(THD *thd, const char *str, size_t str_length);
4179public:
4180 Item_hex_constant(THD *thd): Item_basic_constant(thd)
4181 {
4182 hex_string_init(thd, "", 0);
4183 }
4184 Item_hex_constant(THD *thd, const char *str, size_t str_length):
4185 Item_basic_constant(thd)
4186 {
4187 hex_string_init(thd, str, str_length);
4188 }
4189 enum Type type() const { return VARBIN_ITEM; }
4190 const Type_handler *type_handler() const { return &type_handler_varchar; }
4191 virtual Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
4192 {
4193 return const_charset_converter(thd, tocs, true);
4194 }
4195 bool check_partition_func_processor(void *int_arg) {return FALSE;}
4196 bool basic_const_item() const { return 1; }
4197 bool eq(const Item *item, bool binary_cmp) const
4198 {
4199 return item->basic_const_item() && item->type() == type() &&
4200 item->cast_to_int_type_handler() == cast_to_int_type_handler() &&
4201 str_value.bin_eq(&((Item_hex_constant*)item)->str_value);
4202 }
4203 String *val_str(String*) { DBUG_ASSERT(fixed == 1); return &str_value; }
4204 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
4205 {
4206 return type_handler()->Item_get_date(this, ltime, fuzzydate);
4207 }
4208};
4209
4210
4211/**
4212 Item_hex_hybrid -- is a class implementing 0xHHHH literals, e.g.:
4213 SELECT 0x3132;
4214 They can behave as numbers and as strings depending on context.
4215*/
4216class Item_hex_hybrid: public Item_hex_constant
4217{
4218public:
4219 Item_hex_hybrid(THD *thd): Item_hex_constant(thd) {}
4220 Item_hex_hybrid(THD *thd, const char *str, size_t str_length):
4221 Item_hex_constant(thd, str, str_length) {}
4222 uint decimal_precision() const;
4223 double val_real()
4224 {
4225 DBUG_ASSERT(fixed == 1);
4226 return (double) (ulonglong) Item_hex_hybrid::val_int();
4227 }
4228 longlong val_int()
4229 {
4230 // following assert is redundant, because fixed=1 assigned in constructor
4231 DBUG_ASSERT(fixed == 1);
4232 return longlong_from_hex_hybrid(str_value.ptr(), str_value.length());
4233 }
4234 my_decimal *val_decimal(my_decimal *decimal_value)
4235 {
4236 // following assert is redundant, because fixed=1 assigned in constructor
4237 DBUG_ASSERT(fixed == 1);
4238 longlong value= Item_hex_hybrid::val_int();
4239 int2my_decimal(E_DEC_FATAL_ERROR, value, TRUE, decimal_value);
4240 return decimal_value;
4241 }
4242 int save_in_field(Field *field, bool no_conversions)
4243 {
4244 field->set_notnull();
4245 return field->store_hex_hybrid(str_value.ptr(), str_value.length());
4246 }
4247 const Type_handler *cast_to_int_type_handler() const
4248 {
4249 return &type_handler_longlong;
4250 }
4251 const Type_handler *type_handler_for_system_time() const
4252 {
4253 return &type_handler_longlong;
4254 }
4255 void print(String *str, enum_query_type query_type);
4256 Item *get_copy(THD *thd)
4257 { return get_item_copy<Item_hex_hybrid>(thd, this); }
4258};
4259
4260
4261/**
4262 Item_hex_string -- is a class implementing X'HHHH' literals, e.g.:
4263 SELECT X'3132';
4264 Unlike Item_hex_hybrid, X'HHHH' literals behave as strings in all contexts.
4265 X'HHHH' are also used in replication of string constants in case of
4266 "dangerous" charsets (sjis, cp932, big5, gbk) who can have backslash (0x5C)
4267 as the second byte of a multi-byte character, so using '\' escaping for
4268 these charsets is not desirable.
4269*/
4270class Item_hex_string: public Item_hex_constant
4271{
4272public:
4273 Item_hex_string(THD *thd): Item_hex_constant(thd) {}
4274 Item_hex_string(THD *thd, const char *str, size_t str_length):
4275 Item_hex_constant(thd, str, str_length) {}
4276 longlong val_int()
4277 {
4278 DBUG_ASSERT(fixed == 1);
4279 return longlong_from_string_with_check(&str_value);
4280 }
4281 double val_real()
4282 {
4283 DBUG_ASSERT(fixed == 1);
4284 return double_from_string_with_check(&str_value);
4285 }
4286 my_decimal *val_decimal(my_decimal *decimal_value)
4287 {
4288 return val_decimal_from_string(decimal_value);
4289 }
4290 int save_in_field(Field *field, bool no_conversions)
4291 {
4292 field->set_notnull();
4293 return field->store(str_value.ptr(), str_value.length(),
4294 collation.collation);
4295 }
4296 void print(String *str, enum_query_type query_type);
4297 Item *get_copy(THD *thd)
4298 { return get_item_copy<Item_hex_string>(thd, this); }
4299};
4300
4301
4302class Item_bin_string: public Item_hex_hybrid
4303{
4304public:
4305 Item_bin_string(THD *thd, const char *str, size_t str_length);
4306};
4307
4308
4309class Item_temporal_literal :public Item_basic_constant
4310{
4311protected:
4312 MYSQL_TIME cached_time;
4313public:
4314 /**
4315 Constructor for Item_date_literal.
4316 @param ltime DATE value.
4317 */
4318 Item_temporal_literal(THD *thd, const MYSQL_TIME *ltime)
4319 :Item_basic_constant(thd)
4320 {
4321 collation.set(&my_charset_numeric, DERIVATION_NUMERIC, MY_REPERTOIRE_ASCII);
4322 decimals= 0;
4323 cached_time= *ltime;
4324 }
4325 Item_temporal_literal(THD *thd, const MYSQL_TIME *ltime, uint dec_arg):
4326 Item_basic_constant(thd)
4327 {
4328 collation.set(&my_charset_numeric, DERIVATION_NUMERIC, MY_REPERTOIRE_ASCII);
4329 decimals= dec_arg;
4330 cached_time= *ltime;
4331 }
4332 bool basic_const_item() const { return true; }
4333 bool const_item() const { return true; }
4334 enum Type type() const { return DATE_ITEM; }
4335 bool eq(const Item *item, bool binary_cmp) const;
4336
4337 bool check_partition_func_processor(void *int_arg) {return FALSE;}
4338
4339 bool is_null()
4340 { return is_null_from_temporal(); }
4341 bool get_date_with_sql_mode(MYSQL_TIME *to);
4342 String *val_str(String *str)
4343 { return val_string_from_date(str); }
4344 longlong val_int()
4345 { return val_int_from_date(); }
4346 double val_real()
4347 { return val_real_from_date(); }
4348 my_decimal *val_decimal(my_decimal *decimal_value)
4349 { return val_decimal_from_date(decimal_value); }
4350 int save_in_field(Field *field, bool no_conversions)
4351 { return save_date_in_field(field, no_conversions); }
4352};
4353
4354
4355/**
4356 DATE'2010-01-01'
4357*/
4358class Item_date_literal: public Item_temporal_literal
4359{
4360public:
4361 Item_date_literal(THD *thd, const MYSQL_TIME *ltime)
4362 :Item_temporal_literal(thd, ltime)
4363 {
4364 max_length= MAX_DATE_WIDTH;
4365 fixed= 1;
4366 /*
4367 If date has zero month or day, it can return NULL in case of
4368 NO_ZERO_DATE or NO_ZERO_IN_DATE.
4369 We can't just check the current sql_mode here in constructor,
4370 because sql_mode can change in case of prepared statements
4371 between PREPARE and EXECUTE.
4372 */
4373 maybe_null= !ltime->month || !ltime->day;
4374 }
4375 const Type_handler *type_handler() const { return &type_handler_newdate; }
4376 void print(String *str, enum_query_type query_type);
4377 Item *clone_item(THD *thd);
4378 bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
4379 Item *get_copy(THD *thd)
4380 { return get_item_copy<Item_date_literal>(thd, this); }
4381};
4382
4383
4384/**
4385 TIME'10:10:10'
4386*/
4387class Item_time_literal: public Item_temporal_literal
4388{
4389public:
4390 Item_time_literal(THD *thd, const MYSQL_TIME *ltime, uint dec_arg):
4391 Item_temporal_literal(thd, ltime, dec_arg)
4392 {
4393 max_length= MIN_TIME_WIDTH + (decimals ? decimals + 1 : 0);
4394 fixed= 1;
4395 }
4396 const Type_handler *type_handler() const { return &type_handler_time2; }
4397 void print(String *str, enum_query_type query_type);
4398 Item *clone_item(THD *thd);
4399 bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
4400 Item *get_copy(THD *thd)
4401 { return get_item_copy<Item_time_literal>(thd, this); }
4402};
4403
4404
4405/**
4406 TIMESTAMP'2001-01-01 10:20:30'
4407*/
4408class Item_datetime_literal: public Item_temporal_literal
4409{
4410public:
4411 Item_datetime_literal(THD *thd, const MYSQL_TIME *ltime, uint dec_arg):
4412 Item_temporal_literal(thd, ltime, dec_arg)
4413 {
4414 max_length= MAX_DATETIME_WIDTH + (decimals ? decimals + 1 : 0);
4415 fixed= 1;
4416 // See the comment on maybe_null in Item_date_literal
4417 maybe_null= !ltime->month || !ltime->day;
4418 }
4419 const Type_handler *type_handler() const { return &type_handler_datetime2; }
4420 void print(String *str, enum_query_type query_type);
4421 Item *clone_item(THD *thd);
4422 bool get_date(MYSQL_TIME *res, ulonglong fuzzy_date);
4423 Item *get_copy(THD *thd)
4424 { return get_item_copy<Item_datetime_literal>(thd, this); }
4425};
4426
4427
4428/**
4429 An error-safe counterpart for Item_date_literal
4430*/
4431class Item_date_literal_for_invalid_dates: public Item_date_literal
4432{
4433 /**
4434 During equal field propagation we can replace non-temporal constants
4435 found in equalities to their native temporal equivalents:
4436 WHERE date_column='2001-01-01' ... ->
4437 WHERE date_column=DATE'2001-01-01' ...
4438
4439 This is done to make the eqial field propagation code handle mixtures of
4440 different temporal types in the same expressions easier (MDEV-8706), e.g.
4441 WHERE LENGTH(date_column)=10 AND date_column=TIME'00:00:00'
4442
4443 Item_date_literal_for_invalid_dates::get_date()
4444 (unlike the regular Item_date_literal::get_date())
4445 does not check the result for NO_ZERO_IN_DATE and NO_ZERO_DATE,
4446 always returns success (false), and does not produce error/warning messages.
4447
4448 We need these _for_invalid_dates classes to be able to rewrite:
4449 SELECT * FROM t1 WHERE date_column='0000-00-00' ...
4450 to:
4451 SELECT * FROM t1 WHERE date_column=DATE'0000-00-00' ...
4452
4453 to avoid returning NULL value instead of '0000-00-00' even
4454 in sql_mode=TRADITIONAL.
4455 */
4456public:
4457 Item_date_literal_for_invalid_dates(THD *thd, const MYSQL_TIME *ltime)
4458 :Item_date_literal(thd, ltime) { }
4459 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
4460 {
4461 *ltime= cached_time;
4462 return (null_value= false);
4463 }
4464};
4465
4466
4467/**
4468 An error-safe counterpart for Item_datetime_literal
4469 (see Item_date_literal_for_invalid_dates for comments)
4470*/
4471class Item_datetime_literal_for_invalid_dates: public Item_datetime_literal
4472{
4473public:
4474 Item_datetime_literal_for_invalid_dates(THD *thd,
4475 const MYSQL_TIME *ltime, uint dec_arg)
4476 :Item_datetime_literal(thd, ltime, dec_arg) { }
4477 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
4478 {
4479 *ltime= cached_time;
4480 return (null_value= false);
4481 }
4482};
4483
4484
4485class Used_tables_and_const_cache
4486{
4487public:
4488 /*
4489 In some cases used_tables_cache is not what used_tables() return
4490 so the method should be used where one need used tables bit map
4491 (even internally in Item_func_* code).
4492 */
4493 table_map used_tables_cache;
4494 bool const_item_cache;
4495
4496 Used_tables_and_const_cache()
4497 :used_tables_cache(0),
4498 const_item_cache(true)
4499 { }
4500 Used_tables_and_const_cache(const Used_tables_and_const_cache *other)
4501 :used_tables_cache(other->used_tables_cache),
4502 const_item_cache(other->const_item_cache)
4503 { }
4504 void used_tables_and_const_cache_init()
4505 {
4506 used_tables_cache= 0;
4507 const_item_cache= true;
4508 }
4509 void used_tables_and_const_cache_join(const Item *item)
4510 {
4511 used_tables_cache|= item->used_tables();
4512 const_item_cache&= item->const_item();
4513 }
4514 void used_tables_and_const_cache_update_and_join(Item *item)
4515 {
4516 item->update_used_tables();
4517 used_tables_and_const_cache_join(item);
4518 }
4519 /*
4520 Call update_used_tables() for all "argc" items in the array "argv"
4521 and join with the current cache.
4522 "this" must be initialized with a constructor or
4523 re-initialized with used_tables_and_const_cache_init().
4524 */
4525 void used_tables_and_const_cache_update_and_join(uint argc, Item **argv)
4526 {
4527 for (uint i=0 ; i < argc ; i++)
4528 used_tables_and_const_cache_update_and_join(argv[i]);
4529 }
4530 /*
4531 Call update_used_tables() for all items in the list
4532 and join with the current cache.
4533 "this" must be initialized with a constructor or
4534 re-initialized with used_tables_and_const_cache_init().
4535 */
4536 void used_tables_and_const_cache_update_and_join(List<Item> &list)
4537 {
4538 List_iterator_fast<Item> li(list);
4539 Item *item;
4540 while ((item=li++))
4541 used_tables_and_const_cache_update_and_join(item);
4542 }
4543};
4544
4545
4546/**
4547 An abstract class representing common features of
4548 regular functions and aggregate functions.
4549*/
4550class Item_func_or_sum: public Item_result_field,
4551 public Item_args,
4552 public Used_tables_and_const_cache,
4553 public With_subquery_cache
4554{
4555protected:
4556 bool agg_arg_charsets(DTCollation &c, Item **items, uint nitems,
4557 uint flags, int item_sep)
4558 {
4559 return Type_std_attributes::agg_arg_charsets(c, func_name(),
4560 items, nitems,
4561 flags, item_sep);
4562 }
4563 bool agg_arg_charsets_for_string_result(DTCollation &c,
4564 Item **items, uint nitems,
4565 int item_sep= 1)
4566 {
4567 return Type_std_attributes::
4568 agg_arg_charsets_for_string_result(c, func_name(),
4569 items, nitems, item_sep);
4570 }
4571 bool agg_arg_charsets_for_string_result_with_comparison(DTCollation &c,
4572 Item **items,
4573 uint nitems,
4574 int item_sep= 1)
4575 {
4576 return Type_std_attributes::
4577 agg_arg_charsets_for_string_result_with_comparison(c, func_name(),
4578 items, nitems,
4579 item_sep);
4580 }
4581
4582 /*
4583 Aggregate arguments for comparison, e.g: a=b, a LIKE b, a RLIKE b
4584 - don't convert to @@character_set_connection if all arguments are numbers
4585 - don't allow DERIVATION_NONE
4586 */
4587 bool agg_arg_charsets_for_comparison(DTCollation &c,
4588 Item **items, uint nitems,
4589 int item_sep= 1)
4590 {
4591 return Type_std_attributes::
4592 agg_arg_charsets_for_comparison(c, func_name(), items, nitems, item_sep);
4593 }
4594
4595public:
4596 // This method is used by Arg_comparator
4597 bool agg_arg_charsets_for_comparison(CHARSET_INFO **cs, Item **a, Item **b)
4598 {
4599 DTCollation tmp;
4600 if (tmp.set((*a)->collation, (*b)->collation, MY_COLL_CMP_CONV) ||
4601 tmp.derivation == DERIVATION_NONE)
4602 {
4603 my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
4604 (*a)->collation.collation->name,
4605 (*a)->collation.derivation_name(),
4606 (*b)->collation.collation->name,
4607 (*b)->collation.derivation_name(),
4608 func_name());
4609 return true;
4610 }
4611 if (agg_item_set_converter(tmp, func_name(),
4612 a, 1, MY_COLL_CMP_CONV, 1) ||
4613 agg_item_set_converter(tmp, func_name(),
4614 b, 1, MY_COLL_CMP_CONV, 1))
4615 return true;
4616 *cs= tmp.collation;
4617 return false;
4618 }
4619
4620public:
4621 Item_func_or_sum(THD *thd): Item_result_field(thd), Item_args() {}
4622 Item_func_or_sum(THD *thd, Item *a): Item_result_field(thd), Item_args(a) { }
4623 Item_func_or_sum(THD *thd, Item *a, Item *b):
4624 Item_result_field(thd), Item_args(a, b) { }
4625 Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c):
4626 Item_result_field(thd), Item_args(thd, a, b, c) { }
4627 Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d):
4628 Item_result_field(thd), Item_args(thd, a, b, c, d) { }
4629 Item_func_or_sum(THD *thd, Item *a, Item *b, Item *c, Item *d, Item *e):
4630 Item_result_field(thd), Item_args(thd, a, b, c, d, e) { }
4631 Item_func_or_sum(THD *thd, Item_func_or_sum *item):
4632 Item_result_field(thd, item), Item_args(thd, item),
4633 Used_tables_and_const_cache(item) { }
4634 Item_func_or_sum(THD *thd, List<Item> &list):
4635 Item_result_field(thd), Item_args(thd, list) { }
4636 bool with_subquery() const { DBUG_ASSERT(fixed); return m_with_subquery; }
4637 bool walk(Item_processor processor, bool walk_subquery, void *arg)
4638 {
4639 if (walk_args(processor, walk_subquery, arg))
4640 return true;
4641 return (this->*processor)(arg);
4642 }
4643 /*
4644 This method is used for debug purposes to print the name of an
4645 item to the debug log. The second use of this method is as
4646 a helper function of print() and error messages, where it is
4647 applicable. To suit both goals it should return a meaningful,
4648 distinguishable and sintactically correct string. This method
4649 should not be used for runtime type identification, use enum
4650 {Sum}Functype and Item_func::functype()/Item_sum::sum_func()
4651 instead.
4652 Added here, to the parent class of both Item_func and Item_sum.
4653
4654 NOTE: for Items inherited from Item_sum, func_name() return part of
4655 function name till first argument (including '(') to make difference in
4656 names for functions with 'distinct' clause and without 'distinct' and
4657 also to make printing of items inherited from Item_sum uniform.
4658 */
4659 virtual const char *func_name() const= 0;
4660 virtual void fix_length_and_dec()= 0;
4661 bool const_item() const { return const_item_cache; }
4662 table_map used_tables() const { return used_tables_cache; }
4663 Item* build_clone(THD *thd);
4664};
4665
4666class sp_head;
4667class sp_name;
4668struct st_sp_security_context;
4669
4670class Item_sp
4671{
4672public:
4673 Name_resolution_context *context;
4674 sp_name *m_name;
4675 sp_head *m_sp;
4676 TABLE *dummy_table;
4677 uchar result_buf[64];
4678 sp_rcontext *func_ctx;
4679 MEM_ROOT sp_mem_root;
4680 Query_arena *sp_query_arena;
4681
4682 /*
4683 The result field of the stored function.
4684 */
4685 Field *sp_result_field;
4686 Item_sp(THD *thd, Name_resolution_context *context_arg, sp_name *name_arg);
4687 Item_sp(THD *thd, Item_sp *item);
4688 const char *func_name(THD *thd) const;
4689 void cleanup();
4690 bool sp_check_access(THD *thd);
4691 bool execute(THD *thd, bool *null_value, Item **args, uint arg_count);
4692 bool execute_impl(THD *thd, Item **args, uint arg_count);
4693 bool init_result_field(THD *thd, uint max_length, uint maybe_null,
4694 bool *null_value, LEX_CSTRING *name);
4695};
4696
4697class Item_ref :public Item_ident
4698{
4699protected:
4700 void set_properties();
4701 bool set_properties_only; // the item doesn't need full fix_fields
4702public:
4703 enum Ref_Type { REF, DIRECT_REF, VIEW_REF, OUTER_REF, AGGREGATE_REF };
4704 Item **ref;
4705 bool reference_trough_name;
4706 Item_ref(THD *thd, Name_resolution_context *context_arg,
4707 const char *db_arg, const char *table_name_arg,
4708 const LEX_CSTRING *field_name_arg):
4709 Item_ident(thd, context_arg, db_arg, table_name_arg, field_name_arg),
4710 set_properties_only(0), ref(0), reference_trough_name(1) {}
4711 /*
4712 This constructor is used in two scenarios:
4713 A) *item = NULL
4714 No initialization is performed, fix_fields() call will be necessary.
4715
4716 B) *item points to an Item this Item_ref will refer to. This is
4717 used for GROUP BY. fix_fields() will not be called in this case,
4718 so we call set_properties to make this item "fixed". set_properties
4719 performs a subset of action Item_ref::fix_fields does, and this subset
4720 is enough for Item_ref's used in GROUP BY.
4721
4722 TODO we probably fix a superset of problems like in BUG#6658. Check this
4723 with Bar, and if we have a more broader set of problems like this.
4724 */
4725 Item_ref(THD *thd, Name_resolution_context *context_arg, Item **item,
4726 const char *table_name_arg, const LEX_CSTRING *field_name_arg,
4727 bool alias_name_used_arg= FALSE);
4728 Item_ref(THD *thd, TABLE_LIST *view_arg, Item **item,
4729 const LEX_CSTRING *field_name_arg, bool alias_name_used_arg= FALSE);
4730
4731 /* Constructor need to process subselect with temporary tables (see Item) */
4732 Item_ref(THD *thd, Item_ref *item)
4733 :Item_ident(thd, item), set_properties_only(0), ref(item->ref) {}
4734 enum Type type() const { return REF_ITEM; }
4735 enum Type real_type() const { return ref ? (*ref)->type() :
4736 REF_ITEM; }
4737 bool eq(const Item *item, bool binary_cmp) const
4738 {
4739 Item *it= ((Item *) item)->real_item();
4740 return ref && (*ref)->eq(it, binary_cmp);
4741 }
4742 void save_val(Field *to);
4743 void save_result(Field *to);
4744 double val_real();
4745 longlong val_int();
4746 my_decimal *val_decimal(my_decimal *);
4747 bool val_bool();
4748 String *val_str(String* tmp);
4749 bool is_null();
4750 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
4751 double val_result();
4752 longlong val_int_result();
4753 String *str_result(String* tmp);
4754 my_decimal *val_decimal_result(my_decimal *);
4755 bool val_bool_result();
4756 bool is_null_result();
4757 bool send(Protocol *prot, st_value *buffer);
4758 void make_send_field(THD *thd, Send_field *field);
4759 bool fix_fields(THD *, Item **);
4760 void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge);
4761 int save_in_field(Field *field, bool no_conversions);
4762 void save_org_in_field(Field *field, fast_field_copier optimizer_data);
4763 fast_field_copier setup_fast_field_copier(Field *field)
4764 { return (*ref)->setup_fast_field_copier(field); }
4765 const Type_handler *type_handler() const { return (*ref)->type_handler(); }
4766 const Type_handler *real_type_handler() const
4767 { return (*ref)->real_type_handler(); }
4768 Field *get_tmp_table_field()
4769 { return result_field ? result_field : (*ref)->get_tmp_table_field(); }
4770 Item *get_tmp_table_item(THD *thd);
4771 table_map used_tables() const;
4772 void update_used_tables();
4773 COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
4774 bool link_item_fields,
4775 COND_EQUAL **cond_equal_ref)
4776 {
4777 /*
4778 normilize_cond() replaced all conditions of type
4779 WHERE/HAVING field
4780 to:
4781 WHERE/HAVING field<>0
4782 By the time of a build_equal_items() call, all such conditions should
4783 already be replaced. No Item_ref referencing to Item_field are possible.
4784 */
4785 DBUG_ASSERT(real_type() != FIELD_ITEM);
4786 return Item_ident::build_equal_items(thd, inherited, link_item_fields,
4787 cond_equal_ref);
4788 }
4789 bool const_item() const
4790 {
4791 return (*ref)->const_item();
4792 }
4793 table_map not_null_tables() const
4794 {
4795 return depended_from ? 0 : (*ref)->not_null_tables();
4796 }
4797 void save_in_result_field(bool no_conversions)
4798 {
4799 (*ref)->save_in_field(result_field, no_conversions);
4800 }
4801 Item *real_item()
4802 {
4803 return ref ? (*ref)->real_item() : this;
4804 }
4805 TYPELIB *get_typelib() const
4806 {
4807 return ref ? (*ref)->get_typelib() : NULL;
4808 }
4809
4810 bool walk(Item_processor processor, bool walk_subquery, void *arg)
4811 {
4812 if (ref && *ref)
4813 return (*ref)->walk(processor, walk_subquery, arg) ||
4814 (this->*processor)(arg);
4815 else
4816 return FALSE;
4817 }
4818 Item* transform(THD *thd, Item_transformer, uchar *arg);
4819 Item* compile(THD *thd, Item_analyzer analyzer, uchar **arg_p,
4820 Item_transformer transformer, uchar *arg_t);
4821 bool enumerate_field_refs_processor(void *arg)
4822 { return (*ref)->enumerate_field_refs_processor(arg); }
4823 void no_rows_in_result()
4824 {
4825 (*ref)->no_rows_in_result();
4826 }
4827 void restore_to_before_no_rows_in_result()
4828 {
4829 (*ref)->restore_to_before_no_rows_in_result();
4830 }
4831 virtual void print(String *str, enum_query_type query_type);
4832 void cleanup();
4833 Item_field *field_for_view_update()
4834 { return (*ref)->field_for_view_update(); }
4835 Load_data_outvar *get_load_data_outvar()
4836 {
4837 return (*ref)->get_load_data_outvar();
4838 }
4839 virtual Ref_Type ref_type() { return REF; }
4840
4841 // Row emulation: forwarding of ROW-related calls to ref
4842 uint cols() const
4843 {
4844 return ref && result_type() == ROW_RESULT ? (*ref)->cols() : 1;
4845 }
4846 Item* element_index(uint i)
4847 {
4848 return ref && result_type() == ROW_RESULT ? (*ref)->element_index(i) : this;
4849 }
4850 Item** addr(uint i)
4851 {
4852 return ref && result_type() == ROW_RESULT ? (*ref)->addr(i) : 0;
4853 }
4854 bool check_cols(uint c)
4855 {
4856 return ref && result_type() == ROW_RESULT ? (*ref)->check_cols(c)
4857 : Item::check_cols(c);
4858 }
4859 bool null_inside()
4860 {
4861 return ref && result_type() == ROW_RESULT ? (*ref)->null_inside() : 0;
4862 }
4863 void bring_value()
4864 {
4865 if (ref && result_type() == ROW_RESULT)
4866 (*ref)->bring_value();
4867 }
4868 bool check_vcol_func_processor(void *arg)
4869 {
4870 return mark_unsupported_function("ref", arg, VCOL_IMPOSSIBLE);
4871 }
4872 bool basic_const_item() const { return ref && (*ref)->basic_const_item(); }
4873 bool is_outer_field() const
4874 {
4875 DBUG_ASSERT(fixed);
4876 DBUG_ASSERT(ref);
4877 return (*ref)->is_outer_field();
4878 }
4879
4880 Item* build_clone(THD *thd);
4881
4882 /**
4883 Checks if the item tree that ref points to contains a subquery.
4884 */
4885 virtual bool with_subquery() const
4886 {
4887 return (*ref)->with_subquery();
4888 }
4889 Item *get_copy(THD *thd)
4890 { return get_item_copy<Item_ref>(thd, this); }
4891 bool excl_dep_on_table(table_map tab_map)
4892 {
4893 table_map used= used_tables();
4894 if (used & OUTER_REF_TABLE_BIT)
4895 return false;
4896 return (used == tab_map) || (*ref)->excl_dep_on_table(tab_map);
4897 }
4898 bool excl_dep_on_grouping_fields(st_select_lex *sel)
4899 { return (*ref)->excl_dep_on_grouping_fields(sel); }
4900 bool cleanup_excluding_fields_processor(void *arg)
4901 {
4902 Item *item= real_item();
4903 if (item && item->type() == FIELD_ITEM &&
4904 ((Item_field *)item)->field)
4905 return 0;
4906 return cleanup_processor(arg);
4907 }
4908 bool cleanup_excluding_const_fields_processor(void *arg)
4909 {
4910 Item *item= real_item();
4911 if (item && item->type() == FIELD_ITEM &&
4912 ((Item_field *) item)->field && item->const_item())
4913 return 0;
4914 return cleanup_processor(arg);
4915 }
4916};
4917
4918
4919/*
4920 The same as Item_ref, but get value from val_* family of method to get
4921 value of item on which it referred instead of result* family.
4922*/
4923class Item_direct_ref :public Item_ref
4924{
4925public:
4926 Item_direct_ref(THD *thd, Name_resolution_context *context_arg, Item **item,
4927 const char *table_name_arg,
4928 const LEX_CSTRING *field_name_arg,
4929 bool alias_name_used_arg= FALSE):
4930 Item_ref(thd, context_arg, item, table_name_arg,
4931 field_name_arg, alias_name_used_arg)
4932 {}
4933 /* Constructor need to process subselect with temporary tables (see Item) */
4934 Item_direct_ref(THD *thd, Item_direct_ref *item) : Item_ref(thd, item) {}
4935 Item_direct_ref(THD *thd, TABLE_LIST *view_arg, Item **item,
4936 const LEX_CSTRING *field_name_arg,
4937 bool alias_name_used_arg= FALSE):
4938 Item_ref(thd, view_arg, item, field_name_arg,
4939 alias_name_used_arg)
4940 {}
4941
4942 bool fix_fields(THD *thd, Item **it)
4943 {
4944 if ((!(*ref)->fixed && (*ref)->fix_fields(thd, ref)) ||
4945 (*ref)->check_cols(1))
4946 return TRUE;
4947 return Item_ref::fix_fields(thd, it);
4948 }
4949 void save_val(Field *to);
4950 double val_real();
4951 longlong val_int();
4952 String *val_str(String* tmp);
4953 my_decimal *val_decimal(my_decimal *);
4954 bool val_bool();
4955 bool is_null();
4956 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
4957 virtual Ref_Type ref_type() { return DIRECT_REF; }
4958 Item *get_copy(THD *thd)
4959 { return get_item_copy<Item_direct_ref>(thd, this); }
4960};
4961
4962
4963/**
4964 This class is the same as Item_direct_ref but created to wrap Item_ident
4965 before fix_fields() call
4966*/
4967
4968class Item_direct_ref_to_ident :public Item_direct_ref
4969{
4970 Item_ident *ident;
4971public:
4972 Item_direct_ref_to_ident(THD *thd, Item_ident *item):
4973 Item_direct_ref(thd, item->context, (Item**)&item, item->table_name,
4974 &item->field_name, FALSE)
4975 {
4976 ident= item;
4977 ref= (Item**)&ident;
4978 }
4979
4980 bool fix_fields(THD *thd, Item **it)
4981 {
4982 DBUG_ASSERT(ident->type() == FIELD_ITEM || ident->type() == REF_ITEM);
4983 if ((!ident->fixed && ident->fix_fields(thd, ref)) ||
4984 ident->check_cols(1))
4985 return TRUE;
4986 set_properties();
4987 return FALSE;
4988 }
4989
4990 virtual void print(String *str, enum_query_type query_type)
4991 { ident->print(str, query_type); }
4992
4993};
4994
4995
4996class Item_cache;
4997class Expression_cache;
4998class Expression_cache_tracker;
4999
5000/**
5001 The objects of this class can store its values in an expression cache.
5002*/
5003
5004class Item_cache_wrapper :public Item_result_field,
5005 public With_subquery_cache
5006{
5007private:
5008 /* Pointer on the cached expression */
5009 Item *orig_item;
5010 Expression_cache *expr_cache;
5011 /*
5012 In order to put the expression into the expression cache and return
5013 value of val_*() method, we will need to get the expression value twice
5014 (probably in different types). In order to avoid making two
5015 (potentially costly) orig_item->val_*() calls, we store expression value
5016 in this Item_cache object.
5017 */
5018 Item_cache *expr_value;
5019
5020 List<Item> parameters;
5021
5022 Item *check_cache();
5023 void cache();
5024 void init_on_demand();
5025
5026public:
5027 Item_cache_wrapper(THD *thd, Item *item_arg);
5028 ~Item_cache_wrapper();
5029
5030 enum Type type() const { return EXPR_CACHE_ITEM; }
5031 enum Type real_type() const { return orig_item->type(); }
5032 bool with_subquery() const { DBUG_ASSERT(fixed); return m_with_subquery; }
5033
5034 bool set_cache(THD *thd);
5035 Expression_cache_tracker* init_tracker(MEM_ROOT *mem_root);
5036
5037 bool fix_fields(THD *thd, Item **it);
5038 void cleanup();
5039
5040 Item *get_orig_item() const { return orig_item; }
5041
5042 /* Methods of getting value which should be cached in the cache */
5043 void save_val(Field *to);
5044 double val_real();
5045 longlong val_int();
5046 String *val_str(String* tmp);
5047 my_decimal *val_decimal(my_decimal *);
5048 bool val_bool();
5049 bool is_null();
5050 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
5051 bool send(Protocol *protocol, st_value *buffer);
5052 void save_org_in_field(Field *field,
5053 fast_field_copier data __attribute__ ((__unused__)))
5054 {
5055 save_val(field);
5056 }
5057 void save_in_result_field(bool no_conversions)
5058 {
5059 save_val(result_field);
5060 }
5061 Item* get_tmp_table_item(THD *thd_arg);
5062
5063 /* Following methods make this item transparent as much as possible */
5064
5065 virtual void print(String *str, enum_query_type query_type);
5066 virtual const char *full_name() const { return orig_item->full_name(); }
5067 virtual void make_send_field(THD *thd, Send_field *field)
5068 { orig_item->make_send_field(thd, field); }
5069 bool eq(const Item *item, bool binary_cmp) const
5070 {
5071 Item *it= ((Item *) item)->real_item();
5072 return orig_item->eq(it, binary_cmp);
5073 }
5074 void fix_after_pullout(st_select_lex *new_parent, Item **refptr, bool merge)
5075 {
5076 orig_item->fix_after_pullout(new_parent, &orig_item, merge);
5077 }
5078 int save_in_field(Field *to, bool no_conversions);
5079 const Type_handler *type_handler() const { return orig_item->type_handler(); }
5080 table_map used_tables() const { return orig_item->used_tables(); }
5081 void update_used_tables()
5082 {
5083 orig_item->update_used_tables();
5084 }
5085 bool const_item() const { return orig_item->const_item(); }
5086 table_map not_null_tables() const { return orig_item->not_null_tables(); }
5087 bool walk(Item_processor processor, bool walk_subquery, void *arg)
5088 {
5089 return orig_item->walk(processor, walk_subquery, arg) ||
5090 (this->*processor)(arg);
5091 }
5092 bool enumerate_field_refs_processor(void *arg)
5093 { return orig_item->enumerate_field_refs_processor(arg); }
5094 Item_field *field_for_view_update()
5095 { return orig_item->field_for_view_update(); }
5096
5097 /* Row emulation: forwarding of ROW-related calls to orig_item */
5098 uint cols() const
5099 { return result_type() == ROW_RESULT ? orig_item->cols() : 1; }
5100 Item* element_index(uint i)
5101 { return result_type() == ROW_RESULT ? orig_item->element_index(i) : this; }
5102 Item** addr(uint i)
5103 { return result_type() == ROW_RESULT ? orig_item->addr(i) : 0; }
5104 bool check_cols(uint c)
5105 {
5106 return (result_type() == ROW_RESULT ?
5107 orig_item->check_cols(c) :
5108 Item::check_cols(c));
5109 }
5110 bool null_inside()
5111 { return result_type() == ROW_RESULT ? orig_item->null_inside() : 0; }
5112 void bring_value()
5113 {
5114 if (result_type() == ROW_RESULT)
5115 orig_item->bring_value();
5116 }
5117 bool is_expensive() { return orig_item->is_expensive(); }
5118 bool is_expensive_processor(void *arg)
5119 { return orig_item->is_expensive_processor(arg); }
5120 bool check_vcol_func_processor(void *arg)
5121 {
5122 return mark_unsupported_function("cache", arg, VCOL_IMPOSSIBLE);
5123 }
5124 Item *get_copy(THD *thd)
5125 { return get_item_copy<Item_cache_wrapper>(thd, this); }
5126 Item *build_clone(THD *thd) { return 0; }
5127};
5128
5129
5130/*
5131 Class for view fields, the same as Item_direct_ref, but call fix_fields
5132 of reference if it is not called yet
5133*/
5134class Item_direct_view_ref :public Item_direct_ref
5135{
5136 Item_equal *item_equal;
5137 TABLE_LIST *view;
5138 TABLE *null_ref_table;
5139
5140#define NO_NULL_TABLE (reinterpret_cast<TABLE *>(0x1))
5141
5142 void set_null_ref_table()
5143 {
5144 if (!view->is_inner_table_of_outer_join() ||
5145 !(null_ref_table= view->get_real_join_table()))
5146 null_ref_table= NO_NULL_TABLE;
5147 }
5148
5149 bool check_null_ref()
5150 {
5151 DBUG_ASSERT(null_ref_table);
5152 if (null_ref_table != NO_NULL_TABLE && null_ref_table->null_row)
5153 {
5154 null_value= 1;
5155 return TRUE;
5156 }
5157 return FALSE;
5158 }
5159
5160public:
5161 Item_direct_view_ref(THD *thd, Name_resolution_context *context_arg,
5162 Item **item,
5163 const char *table_name_arg,
5164 LEX_CSTRING *field_name_arg,
5165 TABLE_LIST *view_arg):
5166 Item_direct_ref(thd, context_arg, item, table_name_arg, field_name_arg),
5167 item_equal(0), view(view_arg),
5168 null_ref_table(NULL)
5169 {
5170 if (fixed)
5171 set_null_ref_table();
5172 }
5173
5174 bool fix_fields(THD *, Item **);
5175 bool eq(const Item *item, bool binary_cmp) const;
5176 Item *get_tmp_table_item(THD *thd)
5177 {
5178 if (const_item())
5179 return copy_or_same(thd);
5180 Item *item= Item_ref::get_tmp_table_item(thd);
5181 item->name= name;
5182 return item;
5183 }
5184 virtual Ref_Type ref_type() { return VIEW_REF; }
5185 Item_equal *get_item_equal() { return item_equal; }
5186 void set_item_equal(Item_equal *item_eq) { item_equal= item_eq; }
5187 Item_equal *find_item_equal(COND_EQUAL *cond_equal);
5188 Item* propagate_equal_fields(THD *, const Context &, COND_EQUAL *);
5189 Item *replace_equal_field(THD *thd, uchar *arg);
5190 table_map used_tables() const;
5191 void update_used_tables();
5192 table_map not_null_tables() const;
5193 bool const_item() const { return used_tables() == 0; }
5194 bool walk(Item_processor processor, bool walk_subquery, void *arg)
5195 {
5196 return (*ref)->walk(processor, walk_subquery, arg) ||
5197 (this->*processor)(arg);
5198 }
5199 bool view_used_tables_processor(void *arg)
5200 {
5201 TABLE_LIST *view_arg= (TABLE_LIST *) arg;
5202 if (view_arg == view)
5203 view_arg->view_used_tables|= (*ref)->used_tables();
5204 return 0;
5205 }
5206 bool excl_dep_on_table(table_map tab_map);
5207 bool excl_dep_on_grouping_fields(st_select_lex *sel);
5208 Item *derived_field_transformer_for_having(THD *thd, uchar *arg);
5209 Item *derived_field_transformer_for_where(THD *thd, uchar *arg);
5210 Item *derived_grouping_field_transformer_for_where(THD *thd,
5211 uchar *arg);
5212
5213 void save_val(Field *to)
5214 {
5215 if (check_null_ref())
5216 to->set_null();
5217 else
5218 Item_direct_ref::save_val(to);
5219 }
5220 double val_real()
5221 {
5222 if (check_null_ref())
5223 return 0;
5224 else
5225 return Item_direct_ref::val_real();
5226 }
5227 longlong val_int()
5228 {
5229 if (check_null_ref())
5230 return 0;
5231 else
5232 return Item_direct_ref::val_int();
5233 }
5234 String *val_str(String* tmp)
5235 {
5236 if (check_null_ref())
5237 return NULL;
5238 else
5239 return Item_direct_ref::val_str(tmp);
5240 }
5241 my_decimal *val_decimal(my_decimal *tmp)
5242 {
5243 if (check_null_ref())
5244 return NULL;
5245 else
5246 return Item_direct_ref::val_decimal(tmp);
5247 }
5248 bool val_bool()
5249 {
5250 if (check_null_ref())
5251 return 0;
5252 else
5253 return Item_direct_ref::val_bool();
5254 }
5255 bool is_null()
5256 {
5257 if (check_null_ref())
5258 return 1;
5259 else
5260 return Item_direct_ref::is_null();
5261 }
5262 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5263 {
5264 if (check_null_ref())
5265 {
5266 bzero((char*) ltime,sizeof(*ltime));
5267 return 1;
5268 }
5269 return Item_direct_ref::get_date(ltime, fuzzydate);
5270 }
5271 bool send(Protocol *protocol, st_value *buffer);
5272 void save_org_in_field(Field *field,
5273 fast_field_copier data __attribute__ ((__unused__)))
5274 {
5275 if (check_null_ref())
5276 field->set_null();
5277 else
5278 Item_direct_ref::save_val(field);
5279 }
5280 void save_in_result_field(bool no_conversions)
5281 {
5282 if (check_null_ref())
5283 result_field->set_null();
5284 else
5285 Item_direct_ref::save_in_result_field(no_conversions);
5286 }
5287
5288 void cleanup()
5289 {
5290 null_ref_table= NULL;
5291 item_equal= NULL;
5292 Item_direct_ref::cleanup();
5293 }
5294 Item *get_copy(THD *thd)
5295 { return get_item_copy<Item_direct_view_ref>(thd, this); }
5296};
5297
5298
5299/*
5300 Class for outer fields.
5301 An object of this class is created when the select where the outer field was
5302 resolved is a grouping one. After it has been fixed the ref field will point
5303 to either an Item_ref or an Item_direct_ref object which will be used to
5304 access the field.
5305 See also comments for the fix_inner_refs() and the
5306 Item_field::fix_outer_field() functions.
5307*/
5308
5309class Item_sum;
5310class Item_outer_ref :public Item_direct_ref
5311{
5312public:
5313 Item *outer_ref;
5314 /* The aggregate function under which this outer ref is used, if any. */
5315 Item_sum *in_sum_func;
5316 /*
5317 TRUE <=> that the outer_ref is already present in the select list
5318 of the outer select.
5319 */
5320 bool found_in_select_list;
5321 bool found_in_group_by;
5322 Item_outer_ref(THD *thd, Name_resolution_context *context_arg,
5323 Item_field *outer_field_arg):
5324 Item_direct_ref(thd, context_arg, 0, outer_field_arg->table_name,
5325 &outer_field_arg->field_name),
5326 outer_ref(outer_field_arg), in_sum_func(0),
5327 found_in_select_list(0), found_in_group_by(0)
5328 {
5329 ref= &outer_ref;
5330 set_properties();
5331 fixed= 0; /* reset flag set in set_properties() */
5332 }
5333 Item_outer_ref(THD *thd, Name_resolution_context *context_arg, Item **item,
5334 const char *table_name_arg, LEX_CSTRING *field_name_arg,
5335 bool alias_name_used_arg):
5336 Item_direct_ref(thd, context_arg, item, table_name_arg, field_name_arg,
5337 alias_name_used_arg),
5338 outer_ref(0), in_sum_func(0), found_in_select_list(1), found_in_group_by(0)
5339 {}
5340 void save_in_result_field(bool no_conversions)
5341 {
5342 outer_ref->save_org_in_field(result_field, NULL);
5343 }
5344 bool fix_fields(THD *, Item **);
5345 void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge);
5346 table_map used_tables() const
5347 {
5348 return (*ref)->const_item() ? 0 : OUTER_REF_TABLE_BIT;
5349 }
5350 table_map not_null_tables() const { return 0; }
5351 virtual Ref_Type ref_type() { return OUTER_REF; }
5352 bool check_inner_refs_processor(void * arg);
5353};
5354
5355
5356class Item_in_subselect;
5357
5358
5359/*
5360 An object of this class:
5361 - Converts val_XXX() calls to ref->val_XXX_result() calls, like Item_ref.
5362 - Sets owner->was_null=TRUE if it has returned a NULL value from any
5363 val_XXX() function. This allows to inject an Item_ref_null_helper
5364 object into subquery and then check if the subquery has produced a row
5365 with NULL value.
5366*/
5367
5368class Item_ref_null_helper: public Item_ref
5369{
5370protected:
5371 Item_in_subselect* owner;
5372public:
5373 Item_ref_null_helper(THD *thd, Name_resolution_context *context_arg,
5374 Item_in_subselect* master, Item **item,
5375 const char *table_name_arg,
5376 const LEX_CSTRING *field_name_arg):
5377 Item_ref(thd, context_arg, item, table_name_arg, field_name_arg),
5378 owner(master) {}
5379 void save_val(Field *to);
5380 double val_real();
5381 longlong val_int();
5382 String* val_str(String* s);
5383 my_decimal *val_decimal(my_decimal *);
5384 bool val_bool();
5385 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
5386 virtual void print(String *str, enum_query_type query_type);
5387 table_map used_tables() const;
5388 Item *get_copy(THD *thd)
5389 { return get_item_copy<Item_ref_null_helper>(thd, this); }
5390};
5391
5392/*
5393 The following class is used to optimize comparing of date and bigint columns
5394 We need to save the original item ('ref') to be able to call
5395 ref->save_in_field(). This is used to create index search keys.
5396
5397 An instance of Item_int_with_ref may have signed or unsigned integer value.
5398
5399*/
5400
5401class Item_int_with_ref :public Item_int
5402{
5403 Item *ref;
5404public:
5405 Item_int_with_ref(THD *thd, longlong i, Item *ref_arg, bool unsigned_arg):
5406 Item_int(thd, i), ref(ref_arg)
5407 {
5408 unsigned_flag= unsigned_arg;
5409 }
5410 int save_in_field(Field *field, bool no_conversions)
5411 {
5412 return ref->save_in_field(field, no_conversions);
5413 }
5414 Item *clone_item(THD *thd);
5415 virtual Item *real_item() { return ref; }
5416};
5417
5418#ifdef MYSQL_SERVER
5419#include "gstream.h"
5420#include "spatial.h"
5421#include "item_sum.h"
5422#include "item_func.h"
5423#include "item_row.h"
5424#include "item_cmpfunc.h"
5425#include "item_strfunc.h"
5426#include "item_geofunc.h"
5427#include "item_timefunc.h"
5428#include "item_subselect.h"
5429#include "item_xmlfunc.h"
5430#include "item_jsonfunc.h"
5431#include "item_create.h"
5432#include "item_vers.h"
5433#endif
5434
5435/**
5436 Base class to implement typed value caching Item classes
5437
5438 Item_copy_ classes are very similar to the corresponding Item_
5439 classes (e.g. Item_copy_int is similar to Item_int) but they add
5440 the following additional functionality to Item_ :
5441 1. Nullability
5442 2. Possibility to store the value not only on instantiation time,
5443 but also later.
5444 Item_copy_ classes are a functionality subset of Item_cache_
5445 classes, as e.g. they don't support comparisons with the original Item
5446 as Item_cache_ classes do.
5447 Item_copy_ classes are used in GROUP BY calculation.
5448 TODO: Item_copy should be made an abstract interface and Item_copy_
5449 classes should inherit both the respective Item_ class and the interface.
5450 Ideally we should drop Item_copy_ classes altogether and merge
5451 their functionality to Item_cache_ (and these should be made to inherit
5452 from Item_).
5453*/
5454
5455class Item_copy :public Item,
5456 public Type_handler_hybrid_field_type
5457{
5458protected:
5459
5460 /**
5461 Type_handler_hybrid_field_type is used to
5462 store the type of the resulting field that would be used to store the data
5463 in the cache. This is to avoid calls to the original item.
5464 */
5465
5466 /** The original item that is copied */
5467 Item *item;
5468
5469 /**
5470 Constructor of the Item_copy class
5471
5472 stores metadata information about the original class as well as a
5473 pointer to it.
5474 */
5475 Item_copy(THD *thd, Item *i): Item(thd)
5476 {
5477 item= i;
5478 null_value=maybe_null=item->maybe_null;
5479 Type_std_attributes::set(item);
5480 name= item->name;
5481 set_handler(item->type_handler());
5482 fixed= item->fixed;
5483 }
5484
5485public:
5486 /**
5487 Factory method to create the appropriate subclass dependent on the type of
5488 the original item.
5489
5490 @param item the original item.
5491 */
5492 static Item_copy *create(THD *thd, Item *item);
5493
5494 /**
5495 Update the cache with the value of the original item
5496
5497 This is the method that updates the cached value.
5498 It must be explicitly called by the user of this class to store the value
5499 of the orginal item in the cache.
5500 */
5501 virtual void copy() = 0;
5502
5503 Item *get_item() { return item; }
5504 /** All of the subclasses should have the same type tag */
5505 enum Type type() const { return COPY_STR_ITEM; }
5506
5507 const Type_handler *type_handler() const
5508 { return Type_handler_hybrid_field_type::type_handler(); }
5509
5510 void make_send_field(THD *thd, Send_field *field)
5511 { item->make_send_field(thd, field); }
5512 table_map used_tables() const { return (table_map) 1L; }
5513 bool const_item() const { return 0; }
5514 bool is_null() { return null_value; }
5515 bool check_vcol_func_processor(void *arg)
5516 {
5517 return mark_unsupported_function("copy", arg, VCOL_IMPOSSIBLE);
5518 }
5519
5520 /*
5521 Override the methods below as pure virtual to make sure all the
5522 sub-classes implement them.
5523 */
5524
5525 virtual String *val_str(String*) = 0;
5526 virtual my_decimal *val_decimal(my_decimal *) = 0;
5527 virtual double val_real() = 0;
5528 virtual longlong val_int() = 0;
5529 virtual int save_in_field(Field *field, bool no_conversions) = 0;
5530 bool walk(Item_processor processor, bool walk_subquery, void *args)
5531 {
5532 return (item->walk(processor, walk_subquery, args)) ||
5533 (this->*processor)(args);
5534 }
5535};
5536
5537/**
5538 Implementation of a string cache.
5539
5540 Uses Item::str_value for storage
5541*/
5542class Item_copy_string : public Item_copy
5543{
5544public:
5545 Item_copy_string(THD *thd, Item *item_arg): Item_copy(thd, item_arg) {}
5546
5547 String *val_str(String*);
5548 my_decimal *val_decimal(my_decimal *);
5549 double val_real();
5550 longlong val_int();
5551 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5552 { return get_date_from_string(ltime, fuzzydate); }
5553 void copy();
5554 int save_in_field(Field *field, bool no_conversions);
5555 Item *get_copy(THD *thd)
5556 { return get_item_copy<Item_copy_string>(thd, this); }
5557};
5558
5559
5560class Item_copy_int : public Item_copy
5561{
5562protected:
5563 longlong cached_value;
5564public:
5565 Item_copy_int(THD *thd, Item *i): Item_copy(thd, i) {}
5566 int save_in_field(Field *field, bool no_conversions);
5567
5568 virtual String *val_str(String*);
5569 virtual my_decimal *val_decimal(my_decimal *);
5570 virtual double val_real()
5571 {
5572 return null_value ? 0.0 : (double) cached_value;
5573 }
5574 virtual longlong val_int()
5575 {
5576 return null_value ? 0 : cached_value;
5577 }
5578 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5579 { return get_date_from_int(ltime, fuzzydate); }
5580 virtual void copy();
5581 Item *get_copy(THD *thd)
5582 { return get_item_copy<Item_copy_int>(thd, this); }
5583};
5584
5585
5586class Item_copy_uint : public Item_copy_int
5587{
5588public:
5589 Item_copy_uint(THD *thd, Item *item_arg): Item_copy_int(thd, item_arg)
5590 {
5591 unsigned_flag= 1;
5592 }
5593
5594 String *val_str(String*);
5595 double val_real()
5596 {
5597 return null_value ? 0.0 : (double) (ulonglong) cached_value;
5598 }
5599 Item *get_copy(THD *thd)
5600 { return get_item_copy<Item_copy_uint>(thd, this); }
5601};
5602
5603
5604class Item_copy_float : public Item_copy
5605{
5606protected:
5607 double cached_value;
5608public:
5609 Item_copy_float(THD *thd, Item *i): Item_copy(thd, i) {}
5610 int save_in_field(Field *field, bool no_conversions);
5611
5612 String *val_str(String*);
5613 my_decimal *val_decimal(my_decimal *);
5614 double val_real()
5615 {
5616 return null_value ? 0.0 : cached_value;
5617 }
5618 longlong val_int()
5619 {
5620 return (longlong) rint(val_real());
5621 }
5622 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5623 {
5624 return get_date_from_real(ltime, fuzzydate);
5625 }
5626 void copy()
5627 {
5628 cached_value= item->val_real();
5629 null_value= item->null_value;
5630 }
5631 Item *get_copy(THD *thd)
5632 { return get_item_copy<Item_copy_float>(thd, this); }
5633};
5634
5635
5636class Item_copy_decimal : public Item_copy
5637{
5638protected:
5639 my_decimal cached_value;
5640public:
5641 Item_copy_decimal(THD *thd, Item *i): Item_copy(thd, i) {}
5642 int save_in_field(Field *field, bool no_conversions);
5643
5644 String *val_str(String*);
5645 my_decimal *val_decimal(my_decimal *)
5646 {
5647 return null_value ? NULL: &cached_value;
5648 }
5649 double val_real();
5650 longlong val_int();
5651 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
5652 {
5653 return get_date_from_decimal(ltime, fuzzydate);
5654 }
5655 void copy();
5656 Item *get_copy(THD *thd)
5657 { return get_item_copy<Item_copy_decimal>(thd, this); }
5658};
5659
5660
5661/*
5662 Cached_item_XXX objects are not exactly caches. They do the following:
5663
5664 Each Cached_item_XXX object has
5665 - its source item
5666 - saved value of the source item
5667 - cmp() method that compares the saved value with the current value of the
5668 source item, and if they were not equal saves item's value into the saved
5669 value.
5670
5671 TODO: add here:
5672 - a way to save the new value w/o comparison
5673 - a way to do less/equal/greater comparison
5674*/
5675
5676class Cached_item :public Sql_alloc
5677{
5678public:
5679 bool null_value;
5680 Cached_item() :null_value(0) {}
5681 /*
5682 Compare the cached value with the source value. If not equal, copy
5683 the source value to the cache.
5684 @return
5685 true - Not equal
5686 false - Equal
5687 */
5688 virtual bool cmp(void)=0;
5689
5690 /* Compare the cached value with the source value, without copying */
5691 virtual int cmp_read_only()=0;
5692
5693 virtual ~Cached_item(); /*line -e1509 */
5694};
5695
5696class Cached_item_item : public Cached_item
5697{
5698protected:
5699 Item *item;
5700
5701 Cached_item_item(Item *arg) : item(arg) {}
5702public:
5703 void fetch_value_from(Item *new_item)
5704 {
5705 Item *save= item;
5706 item= new_item;
5707 cmp();
5708 item= save;
5709 }
5710};
5711
5712class Cached_item_str :public Cached_item_item
5713{
5714 uint32 value_max_length;
5715 String value,tmp_value;
5716public:
5717 Cached_item_str(THD *thd, Item *arg);
5718 bool cmp(void);
5719 int cmp_read_only();
5720 ~Cached_item_str(); // Deallocate String:s
5721};
5722
5723
5724class Cached_item_real :public Cached_item_item
5725{
5726 double value;
5727public:
5728 Cached_item_real(Item *item_par) :Cached_item_item(item_par),value(0.0) {}
5729 bool cmp(void);
5730 int cmp_read_only();
5731};
5732
5733class Cached_item_int :public Cached_item_item
5734{
5735 longlong value;
5736public:
5737 Cached_item_int(Item *item_par) :Cached_item_item(item_par),value(0) {}
5738 bool cmp(void);
5739 int cmp_read_only();
5740};
5741
5742
5743class Cached_item_decimal :public Cached_item_item
5744{
5745 my_decimal value;
5746public:
5747 Cached_item_decimal(Item *item_par);
5748 bool cmp(void);
5749 int cmp_read_only();
5750};
5751
5752class Cached_item_field :public Cached_item
5753{
5754 uchar *buff;
5755 Field *field;
5756 uint length;
5757
5758public:
5759 Cached_item_field(THD *thd, Field *arg_field): field(arg_field)
5760 {
5761 field= arg_field;
5762 /* TODO: take the memory allocation below out of the constructor. */
5763 buff= (uchar*) thd_calloc(thd, length= field->pack_length());
5764 }
5765 bool cmp(void);
5766 int cmp_read_only();
5767};
5768
5769class Item_default_value : public Item_field
5770{
5771 void calculate();
5772public:
5773 Item *arg;
5774 Item_default_value(THD *thd, Name_resolution_context *context_arg)
5775 :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
5776 &null_clex_str),
5777 arg(NULL) {}
5778 Item_default_value(THD *thd, Name_resolution_context *context_arg, Item *a)
5779 :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
5780 &null_clex_str),
5781 arg(a) {}
5782 Item_default_value(THD *thd, Name_resolution_context *context_arg, Field *a)
5783 :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
5784 &null_clex_str),
5785 arg(NULL) {}
5786 enum Type type() const { return DEFAULT_VALUE_ITEM; }
5787 bool eq(const Item *item, bool binary_cmp) const;
5788 bool fix_fields(THD *, Item **);
5789 void print(String *str, enum_query_type query_type);
5790 String *val_str(String *str);
5791 double val_real();
5792 longlong val_int();
5793 my_decimal *val_decimal(my_decimal *decimal_value);
5794 bool get_date(MYSQL_TIME *ltime,ulonglong fuzzydate);
5795 bool send(Protocol *protocol, st_value *buffer);
5796 int save_in_field(Field *field_arg, bool no_conversions);
5797 bool save_in_param(THD *thd, Item_param *param)
5798 {
5799 // It should not be possible to have "EXECUTE .. USING DEFAULT(a)"
5800 DBUG_ASSERT(arg == NULL);
5801 param->set_default();
5802 return false;
5803 }
5804 table_map used_tables() const;
5805 Field *get_tmp_table_field() { return 0; }
5806 Item *get_tmp_table_item(THD *thd) { return this; }
5807 Item_field *field_for_view_update() { return 0; }
5808 bool update_vcol_processor(void *arg) { return 0; }
5809 bool check_func_default_processor(void *arg) { return true; }
5810
5811 bool walk(Item_processor processor, bool walk_subquery, void *args)
5812 {
5813 return (arg && arg->walk(processor, walk_subquery, args)) ||
5814 (this->*processor)(args);
5815 }
5816
5817 Item *transform(THD *thd, Item_transformer transformer, uchar *args);
5818};
5819
5820/**
5821 This class is used as bulk parameter INGNORE representation.
5822
5823 It just do nothing when assigned to a field
5824
5825*/
5826
5827class Item_ignore_value : public Item_default_value
5828{
5829public:
5830 Item_ignore_value(THD *thd, Name_resolution_context *context_arg)
5831 :Item_default_value(thd, context_arg)
5832 {};
5833
5834 void print(String *str, enum_query_type query_type);
5835 int save_in_field(Field *field_arg, bool no_conversions);
5836 bool save_in_param(THD *thd, Item_param *param)
5837 {
5838 param->set_ignore();
5839 return false;
5840 }
5841
5842 String *val_str(String *str);
5843 double val_real();
5844 longlong val_int();
5845 my_decimal *val_decimal(my_decimal *decimal_value);
5846 bool get_date(MYSQL_TIME *ltime,ulonglong fuzzydate);
5847 bool send(Protocol *protocol, st_value *buffer);
5848};
5849
5850
5851/*
5852 Item_insert_value -- an implementation of VALUES() function.
5853 You can use the VALUES(col_name) function in the UPDATE clause
5854 to refer to column values from the INSERT portion of the INSERT
5855 ... UPDATE statement. In other words, VALUES(col_name) in the
5856 UPDATE clause refers to the value of col_name that would be
5857 inserted, had no duplicate-key conflict occurred.
5858 In all other places this function returns NULL.
5859*/
5860
5861class Item_insert_value : public Item_field
5862{
5863public:
5864 Item *arg;
5865 Item_insert_value(THD *thd, Name_resolution_context *context_arg, Item *a)
5866 :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
5867 &null_clex_str),
5868 arg(a) {}
5869 bool eq(const Item *item, bool binary_cmp) const;
5870 bool fix_fields(THD *, Item **);
5871 virtual void print(String *str, enum_query_type query_type);
5872 int save_in_field(Field *field_arg, bool no_conversions)
5873 {
5874 return Item_field::save_in_field(field_arg, no_conversions);
5875 }
5876 enum Type type() const { return INSERT_VALUE_ITEM; }
5877 /*
5878 We use RAND_TABLE_BIT to prevent Item_insert_value from
5879 being treated as a constant and precalculated before execution
5880 */
5881 table_map used_tables() const { return RAND_TABLE_BIT; }
5882
5883 Item_field *field_for_view_update() { return 0; }
5884
5885 bool walk(Item_processor processor, bool walk_subquery, void *args)
5886 {
5887 return arg->walk(processor, walk_subquery, args) ||
5888 (this->*processor)(args);
5889 }
5890 bool check_partition_func_processor(void *int_arg) {return TRUE;}
5891 bool update_vcol_processor(void *arg) { return 0; }
5892 bool check_vcol_func_processor(void *arg)
5893 {
5894 return mark_unsupported_function("value()", arg, VCOL_IMPOSSIBLE);
5895 }
5896};
5897
5898
5899class Table_triggers_list;
5900
5901/*
5902 Represents NEW/OLD version of field of row which is
5903 changed/read in trigger.
5904
5905 Note: For this item main part of actual binding to Field object happens
5906 not during fix_fields() call (like for Item_field) but right after
5907 parsing of trigger definition, when table is opened, with special
5908 setup_field() call. On fix_fields() stage we simply choose one of
5909 two Field instances representing either OLD or NEW version of this
5910 field.
5911*/
5912class Item_trigger_field : public Item_field,
5913 private Settable_routine_parameter
5914{
5915public:
5916 /* Is this item represents row from NEW or OLD row ? */
5917 enum row_version_type {OLD_ROW, NEW_ROW};
5918 row_version_type row_version;
5919 /* Next in list of all Item_trigger_field's in trigger */
5920 Item_trigger_field *next_trg_field;
5921 /* Index of the field in the TABLE::field array */
5922 uint field_idx;
5923 /* Pointer to Table_trigger_list object for table of this trigger */
5924 Table_triggers_list *triggers;
5925
5926 Item_trigger_field(THD *thd, Name_resolution_context *context_arg,
5927 row_version_type row_ver_arg,
5928 const LEX_CSTRING *field_name_arg,
5929 ulong priv, const bool ro)
5930 :Item_field(thd, context_arg,
5931 (const char *)NULL, (const char *)NULL, field_name_arg),
5932 row_version(row_ver_arg), field_idx((uint)-1), original_privilege(priv),
5933 want_privilege(priv), table_grants(NULL), read_only (ro)
5934 {}
5935 void setup_field(THD *thd, TABLE *table, GRANT_INFO *table_grant_info);
5936 enum Type type() const { return TRIGGER_FIELD_ITEM; }
5937 bool eq(const Item *item, bool binary_cmp) const;
5938 bool fix_fields(THD *, Item **);
5939 virtual void print(String *str, enum_query_type query_type);
5940 table_map used_tables() const { return (table_map)0L; }
5941 Field *get_tmp_table_field() { return 0; }
5942 Item *copy_or_same(THD *thd) { return this; }
5943 Item *get_tmp_table_item(THD *thd) { return copy_or_same(thd); }
5944 void cleanup();
5945
5946private:
5947 void set_required_privilege(bool rw);
5948 bool set_value(THD *thd, sp_rcontext *ctx, Item **it);
5949
5950public:
5951 Settable_routine_parameter *get_settable_routine_parameter()
5952 {
5953 return (read_only ? 0 : this);
5954 }
5955
5956 bool set_value(THD *thd, Item **it)
5957 {
5958 return set_value(thd, NULL, it);
5959 }
5960
5961private:
5962 /*
5963 'want_privilege' holds privileges required to perform operation on
5964 this trigger field (SELECT_ACL if we are going to read it and
5965 UPDATE_ACL if we are going to update it). It is initialized at
5966 parse time but can be updated later if this trigger field is used
5967 as OUT or INOUT parameter of stored routine (in this case
5968 set_required_privilege() is called to appropriately update
5969 want_privilege and cleanup() is responsible for restoring of
5970 original want_privilege once parameter's value is updated).
5971 */
5972 ulong original_privilege;
5973 ulong want_privilege;
5974 GRANT_INFO *table_grants;
5975 /*
5976 Trigger field is read-only unless it belongs to the NEW row in a
5977 BEFORE INSERT of BEFORE UPDATE trigger.
5978 */
5979 bool read_only;
5980public:
5981 bool check_vcol_func_processor(void *arg);
5982};
5983
5984
5985/**
5986 @todo
5987 Implement the is_null() method for this class. Currently calling is_null()
5988 on any Item_cache object resolves to Item::is_null(), which returns FALSE
5989 for any value.
5990*/
5991
5992class Item_cache: public Item_basic_constant,
5993 public Type_handler_hybrid_field_type
5994{
5995protected:
5996 Item *example;
5997 /**
5998 Field that this object will get value from. This is used by
5999 index-based subquery engines to detect and remove the equality injected
6000 by IN->EXISTS transformation.
6001 */
6002 Field *cached_field;
6003 /*
6004 TRUE <=> cache holds value of the last stored item (i.e actual value).
6005 store() stores item to be cached and sets this flag to FALSE.
6006 On the first call of val_xxx function if this flag is set to FALSE the
6007 cache_value() will be called to actually cache value of saved item.
6008 cache_value() will set this flag to TRUE.
6009 */
6010 bool value_cached;
6011public:
6012 Item_cache(THD *thd):
6013 Item_basic_constant(thd),
6014 Type_handler_hybrid_field_type(&type_handler_string),
6015 example(0), cached_field(0),
6016 value_cached(0)
6017 {
6018 fixed= 1;
6019 maybe_null= 1;
6020 null_value= 1;
6021 }
6022protected:
6023 Item_cache(THD *thd, const Type_handler *handler):
6024 Item_basic_constant(thd),
6025 Type_handler_hybrid_field_type(handler),
6026 example(0), cached_field(0),
6027 value_cached(0)
6028 {
6029 fixed= 1;
6030 maybe_null= 1;
6031 null_value= 1;
6032 }
6033
6034public:
6035 virtual bool allocate(THD *thd, uint i) { return 0; }
6036 virtual bool setup(THD *thd, Item *item)
6037 {
6038 example= item;
6039 Type_std_attributes::set(item);
6040 if (item->type() == FIELD_ITEM)
6041 cached_field= ((Item_field *)item)->field;
6042 return 0;
6043 };
6044 enum Type type() const { return CACHE_ITEM; }
6045
6046 const Type_handler *type_handler() const
6047 { return Type_handler_hybrid_field_type::type_handler(); }
6048
6049 virtual void keep_array() {}
6050 virtual void print(String *str, enum_query_type query_type);
6051 bool eq_def(const Field *field)
6052 {
6053 return cached_field ? cached_field->eq_def (field) : FALSE;
6054 }
6055 bool eq(const Item *item, bool binary_cmp) const
6056 {
6057 return this == item;
6058 }
6059 bool check_vcol_func_processor(void *arg)
6060 {
6061 if (example)
6062 {
6063 Item::vcol_func_processor_result *res= (Item::vcol_func_processor_result*)arg;
6064 example->check_vcol_func_processor(arg);
6065 /*
6066 Item_cache of a non-deterministic function requires re-fixing
6067 even if the function itself doesn't (e.g. CURRENT_TIMESTAMP)
6068 */
6069 if (res->errors & VCOL_NOT_STRICTLY_DETERMINISTIC)
6070 res->errors|= VCOL_SESSION_FUNC;
6071 return false;
6072 }
6073 return mark_unsupported_function("cache", arg, VCOL_IMPOSSIBLE);
6074 }
6075 void cleanup()
6076 {
6077 clear();
6078 Item_basic_constant::cleanup();
6079 }
6080 /**
6081 Check if saved item has a non-NULL value.
6082 Will cache value of saved item if not already done.
6083 @return TRUE if cached value is non-NULL.
6084 */
6085 bool has_value()
6086 {
6087 return (value_cached || cache_value()) && !null_value;
6088 }
6089
6090 virtual void store(Item *item);
6091 virtual Item *get_item() { return example; }
6092 virtual bool cache_value()= 0;
6093 bool basic_const_item() const
6094 { return example && example->basic_const_item(); }
6095 virtual void clear() { null_value= TRUE; value_cached= FALSE; }
6096 bool is_null() { return !has_value(); }
6097 virtual bool is_expensive()
6098 {
6099 if (value_cached)
6100 return false;
6101 return example->is_expensive();
6102 }
6103 bool is_expensive_processor(void *arg)
6104 {
6105 DBUG_ASSERT(example);
6106 if (value_cached)
6107 return false;
6108 return example->is_expensive_processor(arg);
6109 }
6110 virtual void set_null();
6111 bool walk(Item_processor processor, bool walk_subquery, void *arg)
6112 {
6113 if (example && example->walk(processor, walk_subquery, arg))
6114 return TRUE;
6115 return (this->*processor)(arg);
6116 }
6117 virtual Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs);
6118 void split_sum_func2_example(THD *thd, Ref_ptr_array ref_pointer_array,
6119 List<Item> &fields, uint flags)
6120 {
6121 example->split_sum_func2(thd, ref_pointer_array, fields, &example, flags);
6122 }
6123 Item *get_example() const { return example; }
6124
6125 virtual Item *convert_to_basic_const_item(THD *thd) { return 0; };
6126 Item *derived_field_transformer_for_having(THD *thd, uchar *arg)
6127 { return convert_to_basic_const_item(thd); }
6128 Item *derived_field_transformer_for_where(THD *thd, uchar *arg)
6129 { return convert_to_basic_const_item(thd); }
6130 Item *derived_grouping_field_transformer_for_where(THD *thd, uchar *arg)
6131 { return convert_to_basic_const_item(thd); }
6132};
6133
6134
6135class Item_cache_int: public Item_cache
6136{
6137protected:
6138 longlong value;
6139public:
6140 Item_cache_int(THD *thd): Item_cache(thd, &type_handler_longlong),
6141 value(0) {}
6142 Item_cache_int(THD *thd, const Type_handler *handler):
6143 Item_cache(thd, handler), value(0) {}
6144
6145 double val_real();
6146 longlong val_int();
6147 String* val_str(String *str);
6148 my_decimal *val_decimal(my_decimal *);
6149 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6150 { return get_date_from_int(ltime, fuzzydate); }
6151 bool cache_value();
6152 int save_in_field(Field *field, bool no_conversions);
6153 Item *convert_to_basic_const_item(THD *thd);
6154 Item *get_copy(THD *thd)
6155 { return get_item_copy<Item_cache_int>(thd, this); }
6156};
6157
6158
6159class Item_cache_year: public Item_cache_int
6160{
6161public:
6162 Item_cache_year(THD *thd): Item_cache_int(thd, &type_handler_year) { }
6163 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6164 { return get_date_from_year(ltime, fuzzydate); }
6165};
6166
6167
6168class Item_cache_temporal: public Item_cache_int
6169{
6170protected:
6171 Item_cache_temporal(THD *thd, const Type_handler *handler);
6172public:
6173 String* val_str(String *str);
6174 my_decimal *val_decimal(my_decimal *);
6175 longlong val_int();
6176 longlong val_datetime_packed();
6177 longlong val_time_packed();
6178 double val_real();
6179 bool cache_value();
6180 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
6181 int save_in_field(Field *field, bool no_conversions);
6182 void store_packed(longlong val_arg, Item *example);
6183 /*
6184 Having a clone_item method tells optimizer that this object
6185 is a constant and need not be optimized further.
6186 Important when storing packed datetime values.
6187 */
6188 Item *clone_item(THD *thd);
6189 Item *convert_to_basic_const_item(THD *thd);
6190 virtual Item *make_literal(THD *) =0;
6191};
6192
6193
6194class Item_cache_time: public Item_cache_temporal
6195{
6196public:
6197 Item_cache_time(THD *thd)
6198 :Item_cache_temporal(thd, &type_handler_time2) { }
6199 bool cache_value();
6200 Item *get_copy(THD *thd)
6201 { return get_item_copy<Item_cache_time>(thd, this); }
6202 Item *make_literal(THD *);
6203};
6204
6205
6206class Item_cache_datetime: public Item_cache_temporal
6207{
6208public:
6209 Item_cache_datetime(THD *thd)
6210 :Item_cache_temporal(thd, &type_handler_datetime2) { }
6211 Item *get_copy(THD *thd)
6212 { return get_item_copy<Item_cache_datetime>(thd, this); }
6213 Item *make_literal(THD *);
6214};
6215
6216
6217class Item_cache_date: public Item_cache_temporal
6218{
6219public:
6220 Item_cache_date(THD *thd)
6221 :Item_cache_temporal(thd, &type_handler_newdate) { }
6222 Item *get_copy(THD *thd)
6223 { return get_item_copy<Item_cache_date>(thd, this); }
6224 Item *make_literal(THD *);
6225};
6226
6227
6228class Item_cache_real: public Item_cache
6229{
6230 double value;
6231public:
6232 Item_cache_real(THD *thd): Item_cache(thd, &type_handler_double),
6233 value(0) {}
6234
6235 double val_real();
6236 longlong val_int();
6237 String* val_str(String *str);
6238 my_decimal *val_decimal(my_decimal *);
6239 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6240 { return get_date_from_real(ltime, fuzzydate); }
6241 bool cache_value();
6242 Item *convert_to_basic_const_item(THD *thd);
6243 Item *get_copy(THD *thd)
6244 { return get_item_copy<Item_cache_real>(thd, this); }
6245};
6246
6247
6248class Item_cache_decimal: public Item_cache
6249{
6250protected:
6251 my_decimal decimal_value;
6252public:
6253 Item_cache_decimal(THD *thd): Item_cache(thd, &type_handler_newdecimal) {}
6254
6255 double val_real();
6256 longlong val_int();
6257 String* val_str(String *str);
6258 my_decimal *val_decimal(my_decimal *);
6259 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6260 { return get_date_from_decimal(ltime, fuzzydate); }
6261 bool cache_value();
6262 Item *convert_to_basic_const_item(THD *thd);
6263 Item *get_copy(THD *thd)
6264 { return get_item_copy<Item_cache_decimal>(thd, this); }
6265};
6266
6267
6268class Item_cache_str: public Item_cache
6269{
6270 char buffer[STRING_BUFFER_USUAL_SIZE];
6271 String *value, value_buff;
6272 bool is_varbinary;
6273
6274public:
6275 Item_cache_str(THD *thd, const Item *item):
6276 Item_cache(thd, item->type_handler()), value(0),
6277 is_varbinary(item->type() == FIELD_ITEM &&
6278 Item_cache_str::field_type() == MYSQL_TYPE_VARCHAR &&
6279 !((const Item_field *) item)->field->has_charset())
6280 {
6281 collation.set(const_cast<DTCollation&>(item->collation));
6282 }
6283 double val_real();
6284 longlong val_int();
6285 String* val_str(String *);
6286 my_decimal *val_decimal(my_decimal *);
6287 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6288 { return get_date_from_string(ltime, fuzzydate); }
6289 CHARSET_INFO *charset() const { return value->charset(); };
6290 int save_in_field(Field *field, bool no_conversions);
6291 bool cache_value();
6292 Item *convert_to_basic_const_item(THD *thd);
6293 Item *get_copy(THD *thd)
6294 { return get_item_copy<Item_cache_str>(thd, this); }
6295};
6296
6297
6298class Item_cache_str_for_nullif: public Item_cache_str
6299{
6300public:
6301 Item_cache_str_for_nullif(THD *thd, const Item *item)
6302 :Item_cache_str(thd, item)
6303 { }
6304 Item *safe_charset_converter(THD *thd, CHARSET_INFO *tocs)
6305 {
6306 /**
6307 Item_cache_str::safe_charset_converter() returns a new Item_cache
6308 with Item_func_conv_charset installed on "example". The original
6309 Item_cache is not referenced (neither directly nor recursively)
6310 from the result of Item_cache_str::safe_charset_converter().
6311
6312 For NULLIF() purposes we need a different behavior:
6313 we need a new instance of Item_func_conv_charset,
6314 with the original Item_cache referenced in args[0]. See MDEV-9181.
6315 */
6316 return Item::safe_charset_converter(thd, tocs);
6317 }
6318 Item *get_copy(THD *thd)
6319 { return get_item_copy<Item_cache_str_for_nullif>(thd, this); }
6320};
6321
6322
6323class Item_cache_row: public Item_cache
6324{
6325 Item_cache **values;
6326 uint item_count;
6327 bool save_array;
6328public:
6329 Item_cache_row(THD *thd):
6330 Item_cache(thd), values(0), item_count(2),
6331 save_array(0) {}
6332
6333 /*
6334 'allocate' used only in row transformer, to preallocate space for row
6335 cache.
6336 */
6337 bool allocate(THD *thd, uint num);
6338 /*
6339 'setup' is needed only by row => it not called by simple row subselect
6340 (only by IN subselect (in subselect optimizer))
6341 */
6342 bool setup(THD *thd, Item *item);
6343 void store(Item *item);
6344 void illegal_method_call(const char *);
6345 void make_send_field(THD *thd, Send_field *)
6346 {
6347 illegal_method_call((const char*)"make_send_field");
6348 };
6349 double val_real()
6350 {
6351 illegal_method_call((const char*)"val");
6352 return 0;
6353 };
6354 longlong val_int()
6355 {
6356 illegal_method_call((const char*)"val_int");
6357 return 0;
6358 };
6359 String *val_str(String *)
6360 {
6361 illegal_method_call((const char*)"val_str");
6362 return 0;
6363 };
6364 my_decimal *val_decimal(my_decimal *val)
6365 {
6366 illegal_method_call((const char*)"val_decimal");
6367 return 0;
6368 };
6369 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate)
6370 {
6371 illegal_method_call((const char*)"val_decimal");
6372 return true;
6373 }
6374
6375 uint cols() const { return item_count; }
6376 Item *element_index(uint i) { return values[i]; }
6377 Item **addr(uint i) { return (Item **) (values + i); }
6378 bool check_cols(uint c);
6379 bool null_inside();
6380 void bring_value();
6381 void keep_array() { save_array= 1; }
6382 void cleanup()
6383 {
6384 DBUG_ENTER("Item_cache_row::cleanup");
6385 Item_cache::cleanup();
6386 if (save_array)
6387 bzero(values, item_count*sizeof(Item**));
6388 else
6389 values= 0;
6390 DBUG_VOID_RETURN;
6391 }
6392 bool cache_value();
6393 virtual void set_null();
6394 Item *get_copy(THD *thd)
6395 { return get_item_copy<Item_cache_row>(thd, this); }
6396};
6397
6398
6399/*
6400 Item_type_holder used to store type. name, length of Item for UNIONS &
6401 derived tables.
6402
6403 Item_type_holder do not need cleanup() because its time of live limited by
6404 single SP/PS execution.
6405*/
6406class Item_type_holder: public Item,
6407 public Type_handler_hybrid_field_type,
6408 public Type_geometry_attributes
6409{
6410protected:
6411 TYPELIB *enum_set_typelib;
6412public:
6413 Item_type_holder(THD *thd, Item *item)
6414 :Item(thd, item),
6415 Type_handler_hybrid_field_type(item->real_type_handler()),
6416 enum_set_typelib(0)
6417 {
6418 DBUG_ASSERT(item->fixed);
6419 maybe_null= item->maybe_null;
6420 }
6421 Item_type_holder(THD *thd,
6422 Item *item,
6423 const Type_handler *handler,
6424 const Type_all_attributes *attr,
6425 bool maybe_null_arg)
6426 :Item(thd),
6427 Type_handler_hybrid_field_type(handler),
6428 Type_geometry_attributes(handler, attr),
6429 enum_set_typelib(attr->get_typelib())
6430 {
6431 name= item->name;
6432 Type_std_attributes::set(*attr);
6433 maybe_null= maybe_null_arg;
6434 }
6435
6436 const Type_handler *type_handler() const
6437 {
6438 return Type_handler_hybrid_field_type::type_handler()->
6439 type_handler_for_item_field();
6440 }
6441 const Type_handler *real_type_handler() const
6442 {
6443 return Type_handler_hybrid_field_type::type_handler();
6444 }
6445
6446 enum Type type() const { return TYPE_HOLDER; }
6447 TYPELIB *get_typelib() const { return enum_set_typelib; }
6448 double val_real();
6449 longlong val_int();
6450 my_decimal *val_decimal(my_decimal *);
6451 String *val_str(String*);
6452 bool get_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
6453 Field *create_tmp_field(bool group, TABLE *table)
6454 {
6455 return Item_type_holder::real_type_handler()->
6456 make_and_init_table_field(&name, Record_addr(maybe_null),
6457 *this, table);
6458 }
6459 Field::geometry_type get_geometry_type() const
6460 {
6461 return Type_geometry_attributes::get_geometry_type();
6462 }
6463 void set_geometry_type(uint type)
6464 {
6465 Type_geometry_attributes::set_geometry_type(type);
6466 }
6467 Item* get_copy(THD *thd) { return 0; }
6468
6469};
6470
6471
6472class st_select_lex;
6473void mark_select_range_as_dependent(THD *thd,
6474 st_select_lex *last_select,
6475 st_select_lex *current_sel,
6476 Field *found_field, Item *found_item,
6477 Item_ident *resolved_item);
6478
6479extern Cached_item *new_Cached_item(THD *thd, Item *item,
6480 bool pass_through_ref);
6481extern Item_result item_cmp_type(Item_result a,Item_result b);
6482extern void resolve_const_item(THD *thd, Item **ref, Item *cmp_item);
6483extern int stored_field_cmp_to_item(THD *thd, Field *field, Item *item);
6484
6485extern const String my_null_string;
6486
6487/**
6488 Interface for Item iterator
6489*/
6490
6491class Item_iterator
6492{
6493public:
6494 /**
6495 Shall set this iterator to the position before the first item
6496
6497 @note
6498 This method also may perform some other initialization actions like
6499 allocation of certain resources.
6500 */
6501 virtual void open()= 0;
6502 /**
6503 Shall return the next Item (or NULL if there is no next item) and
6504 move pointer to position after it.
6505 */
6506 virtual Item *next()= 0;
6507 /**
6508 Shall force iterator to free resources (if it holds them)
6509
6510 @note
6511 One should not use the iterator without open() call after close()
6512 */
6513 virtual void close()= 0;
6514
6515 virtual ~Item_iterator() {}
6516};
6517
6518
6519/**
6520 Item iterator over List_iterator_fast for Item references
6521*/
6522
6523class Item_iterator_ref_list: public Item_iterator
6524{
6525 List_iterator<Item*> list;
6526public:
6527 Item_iterator_ref_list(List_iterator<Item*> &arg_list):
6528 list(arg_list) {}
6529 void open() { list.rewind(); }
6530 Item *next() { return *(list++); }
6531 void close() {}
6532};
6533
6534
6535/**
6536 Item iterator over List_iterator_fast for Items
6537*/
6538
6539class Item_iterator_list: public Item_iterator
6540{
6541 List_iterator<Item> list;
6542public:
6543 Item_iterator_list(List_iterator<Item> &arg_list):
6544 list(arg_list) {}
6545 void open() { list.rewind(); }
6546 Item *next() { return (list++); }
6547 void close() {}
6548};
6549
6550
6551/**
6552 Item iterator over Item interface for rows
6553*/
6554
6555class Item_iterator_row: public Item_iterator
6556{
6557 Item *base_item;
6558 uint current;
6559public:
6560 Item_iterator_row(Item *base) : base_item(base), current(0) {}
6561 void open() { current= 0; }
6562 Item *next()
6563 {
6564 if (current >= base_item->cols())
6565 return NULL;
6566 return base_item->element_index(current++);
6567 }
6568 void close() {}
6569};
6570
6571
6572/*
6573 It's used in ::fix_fields() methods of LIKE and JSON_SEARCH
6574 functions to handle the ESCAPE parameter.
6575 This parameter is quite non-standard so the specific function.
6576*/
6577bool fix_escape_item(THD *thd, Item *escape_item, String *tmp_str,
6578 bool escape_used_in_parsing, CHARSET_INFO *cmp_cs,
6579 int *escape);
6580
6581inline bool Virtual_column_info::is_equal(const Virtual_column_info* vcol) const
6582{
6583 return field_type == vcol->get_real_type()
6584 && stored_in_db == vcol->is_stored()
6585 && expr->eq(vcol->expr, true);
6586}
6587
6588inline void Virtual_column_info::print(String* str)
6589{
6590 expr->print_for_table_def(str);
6591}
6592
6593#endif /* SQL_ITEM_INCLUDED */
6594