/
item.h
6593 lines (5901 loc) · 210 KB
/
item.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef SQL_ITEM_INCLUDED
#define SQL_ITEM_INCLUDED
/* Copyright (c) 2000, 2017, Oracle and/or its affiliates.
Copyright (c) 2009, 2018, MariaDB Corporation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
#ifdef USE_PRAGMA_INTERFACE
#pragma interface /* gcc class implementation */
#endif
#include "sql_priv.h" /* STRING_BUFFER_USUAL_SIZE */
#include "unireg.h"
#include "sql_const.h" /* RAND_TABLE_BIT, MAX_FIELD_NAME */
#include "field.h" /* Derivation */
#include "sql_type.h"
#include "sql_time.h"
C_MODE_START
#include <ma_dyncol.h>
/*
A prototype for a C-compatible structure to store a value of any data type.
Currently it has to stay in /sql, as it depends on String and my_decimal.
We'll do the following changes:
1. add pure C "struct st_string" and "struct st_my_decimal"
2. change type of m_string to struct st_string and move inside the union
3. change type of m_decmal to struct st_my_decimal and move inside the union
4. move the definition to some file in /include
*/
struct st_value
{
enum enum_dynamic_column_type m_type;
union
{
longlong m_longlong;
double m_double;
MYSQL_TIME m_time;
} value;
String m_string;
my_decimal m_decimal;
};
C_MODE_END
class Value: public st_value
{
public:
bool is_null() const { return m_type == DYN_COL_NULL; }
bool is_longlong() const
{
return m_type == DYN_COL_UINT || m_type == DYN_COL_INT;
}
bool is_double() const { return m_type == DYN_COL_DOUBLE; }
bool is_temporal() const { return m_type == DYN_COL_DATETIME; }
bool is_string() const { return m_type == DYN_COL_STRING; }
bool is_decimal() const { return m_type == DYN_COL_DECIMAL; }
};
template<size_t buffer_size>
class ValueBuffer: public Value
{
char buffer[buffer_size];
void reset_buffer()
{
m_string.set(buffer, buffer_size, &my_charset_bin);
}
public:
ValueBuffer()
{
reset_buffer();
}
};
#ifdef DBUG_OFF
static inline const char *dbug_print_item(Item *item) { return NULL; }
#else
const char *dbug_print_item(Item *item);
#endif
class Virtual_tmp_table;
class sp_head;
class Protocol;
struct TABLE_LIST;
void item_init(void); /* Init item functions */
class Item_field;
class Item_param;
class user_var_entry;
class JOIN;
struct KEY_FIELD;
struct SARGABLE_PARAM;
class RANGE_OPT_PARAM;
class SEL_TREE;
enum precedence {
LOWEST_PRECEDENCE,
ASSIGN_PRECEDENCE, // :=
OR_PRECEDENCE, // OR, || (unless PIPES_AS_CONCAT)
XOR_PRECEDENCE, // XOR
AND_PRECEDENCE, // AND, &&
NOT_PRECEDENCE, // NOT (unless HIGH_NOT_PRECEDENCE)
BETWEEN_PRECEDENCE, // BETWEEN, CASE, WHEN, THEN, ELSE
CMP_PRECEDENCE, // =, <=>, >=, >, <=, <, <>, !=, IS, LIKE, REGEXP, IN
BITOR_PRECEDENCE, // |
BITAND_PRECEDENCE, // &
SHIFT_PRECEDENCE, // <<, >>
ADDINTERVAL_PRECEDENCE, // first argument in +INTERVAL
ADD_PRECEDENCE, // +, -
MUL_PRECEDENCE, // *, /, DIV, %, MOD
BITXOR_PRECEDENCE, // ^
PIPES_PRECEDENCE, // || (if PIPES_AS_CONCAT)
NEG_PRECEDENCE, // unary -, ~
BANG_PRECEDENCE, // !, NOT (if HIGH_NOT_PRECEDENCE)
COLLATE_PRECEDENCE, // BINARY, COLLATE
INTERVAL_PRECEDENCE, // INTERVAL
DEFAULT_PRECEDENCE,
HIGHEST_PRECEDENCE
};
bool mark_unsupported_function(const char *where, void *store, uint result);
/* convenience helper for mark_unsupported_function() above */
bool mark_unsupported_function(const char *w1, const char *w2,
void *store, uint result);
/* Bits for the split_sum_func() function */
#define SPLIT_SUM_SKIP_REGISTERED 1 /* Skip registered funcs */
#define SPLIT_SUM_SELECT 2 /* SELECT item; Split all parts */
#define NO_EXTRACTION_FL (1 << 6)
#define FULL_EXTRACTION_FL (1 << 7)
#define EXTRACTION_MASK (NO_EXTRACTION_FL | FULL_EXTRACTION_FL)
extern const char *item_empty_name;
void dummy_error_processor(THD *thd, void *data);
void view_error_processor(THD *thd, void *data);
/*
Instances of Name_resolution_context store the information necesary for
name resolution of Items and other context analysis of a query made in
fix_fields().
This structure is a part of SELECT_LEX, a pointer to this structure is
assigned when an item is created (which happens mostly during parsing
(sql_yacc.yy)), but the structure itself will be initialized after parsing
is complete
TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
separate SELECT_LEX which allow to remove tricks of changing this
structure before and after INSERT/CREATE and its SELECT to make correct
field name resolution.
*/
struct Name_resolution_context: Sql_alloc
{
/*
The name resolution context to search in when an Item cannot be
resolved in this context (the context of an outer select)
*/
Name_resolution_context *outer_context;
/*
List of tables used to resolve the items of this context. Usually these
are tables from the FROM clause of SELECT statement. The exceptions are
INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
subquery is not moved to a separate SELECT_LEX. For these types of
statements we have to change this member dynamically to ensure correct
name resolution of different parts of the statement.
*/
TABLE_LIST *table_list;
/*
In most cases the two table references below replace 'table_list' above
for the purpose of name resolution. The first and last name resolution
table references allow us to search only in a sub-tree of the nested
join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
and JOIN ... ON.
*/
TABLE_LIST *first_name_resolution_table;
/*
Last table to search in the list of leaf table references that begins
with first_name_resolution_table.
*/
TABLE_LIST *last_name_resolution_table;
/* Cache first_name_resolution_table in setup_natural_join_row_types */
TABLE_LIST *natural_join_first_table;
/*
SELECT_LEX item belong to, in case of merged VIEW it can differ from
SELECT_LEX where item was created, so we can't use table_list/field_list
from there
*/
st_select_lex *select_lex;
/*
Processor of errors caused during Item name resolving, now used only to
hide underlying tables in errors about views (i.e. it substitute some
errors for views)
*/
void (*error_processor)(THD *, void *);
void *error_processor_data;
/*
When TRUE items are resolved in this context both against the
SELECT list and this->table_list. If FALSE, items are resolved
only against this->table_list.
*/
bool resolve_in_select_list;
/*
Security context of this name resolution context. It's used for views
and is non-zero only if the view is defined with SQL SECURITY DEFINER.
*/
Security_context *security_ctx;
Name_resolution_context()
:outer_context(0), table_list(0), select_lex(0),
error_processor_data(0),
security_ctx(0)
{}
void init()
{
resolve_in_select_list= FALSE;
error_processor= &dummy_error_processor;
first_name_resolution_table= NULL;
last_name_resolution_table= NULL;
}
void resolve_in_table_list_only(TABLE_LIST *tables)
{
table_list= first_name_resolution_table= tables;
resolve_in_select_list= FALSE;
}
void process_error(THD *thd)
{
(*error_processor)(thd, error_processor_data);
}
st_select_lex *outer_select()
{
return (outer_context ?
outer_context->select_lex :
NULL);
}
};
/*
Store and restore the current state of a name resolution context.
*/
class Name_resolution_context_state
{
private:
TABLE_LIST *save_table_list;
TABLE_LIST *save_first_name_resolution_table;
TABLE_LIST *save_next_name_resolution_table;
bool save_resolve_in_select_list;
TABLE_LIST *save_next_local;
public:
Name_resolution_context_state() {} /* Remove gcc warning */
public:
/* Save the state of a name resolution context. */
void save_state(Name_resolution_context *context, TABLE_LIST *table_list)
{
save_table_list= context->table_list;
save_first_name_resolution_table= context->first_name_resolution_table;
save_resolve_in_select_list= context->resolve_in_select_list;
save_next_local= table_list->next_local;
save_next_name_resolution_table= table_list->next_name_resolution_table;
}
/* Restore a name resolution context from saved state. */
void restore_state(Name_resolution_context *context, TABLE_LIST *table_list)
{
table_list->next_local= save_next_local;
table_list->next_name_resolution_table= save_next_name_resolution_table;
context->table_list= save_table_list;
context->first_name_resolution_table= save_first_name_resolution_table;
context->resolve_in_select_list= save_resolve_in_select_list;
}
TABLE_LIST *get_first_name_resolution_table()
{
return save_first_name_resolution_table;
}
};
class Name_resolution_context_backup
{
Name_resolution_context &ctx;
TABLE_LIST &table_list;
table_map save_map;
Name_resolution_context_state ctx_state;
public:
Name_resolution_context_backup(Name_resolution_context &_ctx, TABLE_LIST &_table_list)
: ctx(_ctx), table_list(_table_list), save_map(_table_list.map)
{
ctx_state.save_state(&ctx, &table_list);
ctx.table_list= &table_list;
ctx.first_name_resolution_table= &table_list;
}
~Name_resolution_context_backup()
{
ctx_state.restore_state(&ctx, &table_list);
table_list.map= save_map;
}
};
/*
This enum is used to report information about monotonicity of function
represented by Item* tree.
Monotonicity is defined only for Item* trees that represent table
partitioning expressions (i.e. have no subselects/user vars/PS parameters
etc etc). An Item* tree is assumed to have the same monotonicity properties
as its correspoinding function F:
[signed] longlong F(field1, field2, ...) {
put values of field_i into table record buffer;
return item->val_int();
}
NOTE
At the moment function monotonicity is not well defined (and so may be
incorrect) for Item trees with parameters/return types that are different
from INT_RESULT, may be NULL, or are unsigned.
It will be possible to address this issue once the related partitioning bugs
(BUG#16002, BUG#15447, BUG#13436) are fixed.
The NOT_NULL enums are used in TO_DAYS, since TO_DAYS('2001-00-00') returns
NULL which puts those rows into the NULL partition, but
'2000-12-31' < '2001-00-00' < '2001-01-01'. So special handling is needed
for this (see Bug#20577).
*/
typedef enum monotonicity_info
{
NON_MONOTONIC, /* none of the below holds */
MONOTONIC_INCREASING, /* F() is unary and (x < y) => (F(x) <= F(y)) */
MONOTONIC_INCREASING_NOT_NULL, /* But only for valid/real x and y */
MONOTONIC_STRICT_INCREASING,/* F() is unary and (x < y) => (F(x) < F(y)) */
MONOTONIC_STRICT_INCREASING_NOT_NULL /* But only for valid/real x and y */
} enum_monotonicity_info;
/*************************************************************************/
class sp_rcontext;
/**
A helper class to collect different behavior of various kinds of SP variables:
- local SP variables and SP parameters
- PACKAGE BODY routine variables
- (there will be more kinds in the future)
*/
class Sp_rcontext_handler
{
public:
virtual ~Sp_rcontext_handler() {}
/**
A prefix used for SP variable names in queries:
- EXPLAIN EXTENDED
- SHOW PROCEDURE CODE
Local variables and SP parameters have empty prefixes.
Package body variables are marked with a special prefix.
This improves readability of the output of these queries,
especially when a local variable or a parameter has the same
name with a package body variable.
*/
virtual const LEX_CSTRING *get_name_prefix() const= 0;
/**
At execution time THD->spcont points to the run-time context (sp_rcontext)
of the currently executed routine.
Local variables store their data in the sp_rcontext pointed by thd->spcont.
Package body variables store data in separate sp_rcontext that belongs
to the package.
This method provides access to the proper sp_rcontext structure,
depending on the SP variable kind.
*/
virtual sp_rcontext *get_rcontext(sp_rcontext *ctx) const= 0;
};
class Sp_rcontext_handler_local: public Sp_rcontext_handler
{
public:
const LEX_CSTRING *get_name_prefix() const;
sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
};
class Sp_rcontext_handler_package_body: public Sp_rcontext_handler
{
public:
const LEX_CSTRING *get_name_prefix() const;
sp_rcontext *get_rcontext(sp_rcontext *ctx) const;
};
extern MYSQL_PLUGIN_IMPORT
Sp_rcontext_handler_local sp_rcontext_handler_local;
extern MYSQL_PLUGIN_IMPORT
Sp_rcontext_handler_package_body sp_rcontext_handler_package_body;
class Item_equal;
struct st_join_table* const NO_PARTICULAR_TAB= (struct st_join_table*)0x1;
typedef struct replace_equal_field_arg
{
Item_equal *item_equal;
struct st_join_table *context_tab;
} REPLACE_EQUAL_FIELD_ARG;
class Settable_routine_parameter
{
public:
/*
Set required privileges for accessing the parameter.
SYNOPSIS
set_required_privilege()
rw if 'rw' is true then we are going to read and set the
parameter, so SELECT and UPDATE privileges might be
required, otherwise we only reading it and SELECT
privilege might be required.
*/
Settable_routine_parameter() {}
virtual ~Settable_routine_parameter() {}
virtual void set_required_privilege(bool rw) {};
/*
Set parameter value.
SYNOPSIS
set_value()
thd thread handle
ctx context to which parameter belongs (if it is local
variable).
it item which represents new value
RETURN
FALSE if parameter value has been set,
TRUE if error has occurred.
*/
virtual bool set_value(THD *thd, sp_rcontext *ctx, Item **it)= 0;
virtual void set_out_param_info(Send_field *info) {}
virtual const Send_field *get_out_param_info() const
{ return NULL; }
virtual Item_param *get_item_param() { return 0; }
};
/*
A helper class to calculate offset and length of a query fragment
- outside of SP
- inside an SP
- inside a compound block
*/
class Query_fragment
{
uint m_pos;
uint m_length;
void set(size_t pos, size_t length)
{
DBUG_ASSERT(pos < UINT_MAX32);
DBUG_ASSERT(length < UINT_MAX32);
m_pos= (uint) pos;
m_length= (uint) length;
}
public:
Query_fragment(THD *thd, sp_head *sphead, const char *start, const char *end);
uint pos() const { return m_pos; }
uint length() const { return m_length; }
};
/**
This is used for items in the query that needs to be rewritten
before binlogging
At the moment this applies to Item_param and Item_splocal
*/
class Rewritable_query_parameter
{
public:
/*
Offset inside the query text.
Value of 0 means that this object doesn't have to be replaced
(for example SP variables in control statements)
*/
uint pos_in_query;
/*
Byte length of parameter name in the statement. This is not
Item::name.length because name.length contains byte length of UTF8-encoded
name, but the query string is in the client charset.
*/
uint len_in_query;
bool limit_clause_param;
Rewritable_query_parameter(uint pos_in_q= 0, uint len_in_q= 0)
: pos_in_query(pos_in_q), len_in_query(len_in_q),
limit_clause_param(false)
{ }
virtual ~Rewritable_query_parameter() { }
virtual bool append_for_log(THD *thd, String *str) = 0;
};
class Copy_query_with_rewrite
{
THD *thd;
const char *src;
size_t src_len, from;
String *dst;
bool copy_up_to(size_t bytes)
{
DBUG_ASSERT(bytes >= from);
return dst->append(src + from, uint32(bytes - from));
}
public:
Copy_query_with_rewrite(THD *t, const char *s, size_t l, String *d)
:thd(t), src(s), src_len(l), from(0), dst(d) { }
bool append(Rewritable_query_parameter *p)
{
if (copy_up_to(p->pos_in_query) || p->append_for_log(thd, dst))
return true;
from= p->pos_in_query + p->len_in_query;
return false;
}
bool finalize()
{ return copy_up_to(src_len); }
};
struct st_dyncall_create_def
{
Item *key, *value;
CHARSET_INFO *cs;
uint len, frac;
DYNAMIC_COLUMN_TYPE type;
};
typedef struct st_dyncall_create_def DYNCALL_CREATE_DEF;
typedef bool (Item::*Item_processor) (void *arg);
/*
Analyzer function
SYNOPSIS
argp in/out IN: Analysis parameter
OUT: Parameter to be passed to the transformer
RETURN
TRUE Invoke the transformer
FALSE Don't do it
*/
typedef bool (Item::*Item_analyzer) (uchar **argp);
typedef Item* (Item::*Item_transformer) (THD *thd, uchar *arg);
typedef void (*Cond_traverser) (const Item *item, void *arg);
struct st_cond_statistic;
struct find_selective_predicates_list_processor_data
{
TABLE *table;
List<st_cond_statistic> list;
};
class MY_LOCALE;
class Item_equal;
class COND_EQUAL;
class st_select_lex_unit;
class Item_func_not;
class Item_splocal;
/**
String_copier that sends Item specific warnings.
*/
class String_copier_for_item: public String_copier
{
THD *m_thd;
public:
bool copy_with_warn(CHARSET_INFO *dstcs, String *dst,
CHARSET_INFO *srccs, const char *src,
uint32 src_length, uint32 nchars);
String_copier_for_item(THD *thd): m_thd(thd) { }
};
class Item: public Value_source,
public Type_all_attributes
{
void operator=(Item &);
/**
The index in the JOIN::join_tab array of the JOIN_TAB this Item is attached
to. Items are attached (or 'pushed') to JOIN_TABs during optimization by the
make_cond_for_table procedure. During query execution, this item is
evaluated when the join loop reaches the corresponding JOIN_TAB.
If the value of join_tab_idx >= MAX_TABLES, this means that there is no
corresponding JOIN_TAB.
*/
uint join_tab_idx;
static void *operator new(size_t size);
public:
static void *operator new(size_t size, MEM_ROOT *mem_root) throw ()
{ return alloc_root(mem_root, size); }
static void operator delete(void *ptr,size_t size) { TRASH_FREE(ptr, size); }
static void operator delete(void *ptr, MEM_ROOT *mem_root) {}
enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM,
WINDOW_FUNC_ITEM, STRING_ITEM,
INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
PARAM_ITEM, TRIGGER_FIELD_ITEM, DECIMAL_ITEM,
XPATH_NODESET, XPATH_NODESET_CMP,
VIEW_FIXER_ITEM, EXPR_CACHE_ITEM,
DATE_ITEM};
enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
enum traverse_order { POSTFIX, PREFIX };
/* Cache of the result of is_expensive(). */
int8 is_expensive_cache;
/* Reuse size, only used by SP local variable assignment, otherwize 0 */
uint rsize;
protected:
/*
str_values's main purpose is to be used to cache the value in
save_in_field
*/
String str_value;
SEL_TREE *get_mm_tree_for_const(RANGE_OPT_PARAM *param);
/**
Create a field based on the exact data type handler.
*/
Field *create_table_field_from_handler(TABLE *table)
{
const Type_handler *h= type_handler();
return h->make_and_init_table_field(&name, Record_addr(maybe_null),
*this, table);
}
/**
Create a field based on field_type of argument.
This is used to create a field for
- IFNULL(x,something)
- time functions
- prepared statement placeholders
- SP variables with data type references: DECLARE a TYPE OF t1.a;
@retval NULL error
@retval !NULL on success
*/
Field *tmp_table_field_from_field_type(TABLE *table)
{
const Type_handler *h= type_handler()->type_handler_for_tmp_table(this);
return h->make_and_init_table_field(&name, Record_addr(maybe_null),
*this, table);
}
Field *create_tmp_field_int(TABLE *table, uint convert_int_length);
void push_note_converted_to_negative_complement(THD *thd);
void push_note_converted_to_positive_complement(THD *thd);
/* Helper methods, to get an Item value from another Item */
double val_real_from_item(Item *item)
{
DBUG_ASSERT(fixed == 1);
double value= item->val_real();
null_value= item->null_value;
return value;
}
longlong val_int_from_item(Item *item)
{
DBUG_ASSERT(fixed == 1);
longlong value= item->val_int();
null_value= item->null_value;
return value;
}
String *val_str_from_item(Item *item, String *str)
{
DBUG_ASSERT(fixed == 1);
String *res= item->val_str(str);
if (res)
res->set_charset(collation.collation);
if ((null_value= item->null_value))
res= NULL;
return res;
}
my_decimal *val_decimal_from_item(Item *item, my_decimal *decimal_value)
{
DBUG_ASSERT(fixed == 1);
my_decimal *value= item->val_decimal(decimal_value);
if ((null_value= item->null_value))
value= NULL;
return value;
}
bool get_date_from_item(Item *item, MYSQL_TIME *ltime, ulonglong fuzzydate)
{
bool rc= item->get_date(ltime, fuzzydate);
null_value= MY_TEST(rc || item->null_value);
return rc;
}
/*
This method is used if the item was not null but convertion to
TIME/DATE/DATETIME failed. We return a zero date if allowed,
otherwise - null.
*/
bool make_zero_date(MYSQL_TIME *ltime, ulonglong fuzzydate);
public:
/*
Cache val_str() into the own buffer, e.g. to evaluate constant
expressions with subqueries in the ORDER/GROUP clauses.
*/
String *val_str() { return val_str(&str_value); }
const MY_LOCALE *locale_from_val_str();
LEX_CSTRING name; /* Name of item */
/* Original item name (if it was renamed)*/
const char *orig_name;
/**
Intrusive list pointer for free list. If not null, points to the next
Item on some Query_arena's free list. For instance, stored procedures
have their own Query_arena's.
@see Query_arena::free_list
*/
Item *next;
int marker;
bool maybe_null; /* If item may be null */
bool in_rollup; /* If used in GROUP BY list
of a query with ROLLUP */
bool null_value; /* if item is null */
bool with_sum_func; /* True if item contains a sum func */
bool with_param; /* True if contains an SP parameter */
bool with_window_func; /* True if item contains a window func */
/**
True if any item except Item_sum contains a field. Set during parsing.
*/
bool with_field;
bool fixed; /* If item fixed with fix_fields */
bool is_autogenerated_name; /* indicate was name of this Item
autogenerated or set by user */
// alloc & destruct is done as start of select on THD::mem_root
Item(THD *thd);
/*
Constructor used by Item_field, Item_ref & aggregate (sum) functions.
Used for duplicating lists in processing queries with temporary
tables
Also it used for Item_cond_and/Item_cond_or for creating
top AND/OR structure of WHERE clause to protect it of
optimisation changes in prepared statements
*/
Item(THD *thd, Item *item);
virtual ~Item()
{
#ifdef EXTRA_DEBUG
name.str= 0;
name.length= 0;
#endif
} /*lint -e1509 */
void set_name(THD *thd, const char *str, size_t length, CHARSET_INFO *cs);
void set_name_no_truncate(THD *thd, const char *str, uint length,
CHARSET_INFO *cs);
void init_make_send_field(Send_field *tmp_field,enum enum_field_types type);
virtual void cleanup();
virtual void make_send_field(THD *thd, Send_field *field);
virtual bool fix_fields(THD *, Item **);
/*
Fix after some tables has been pulled out. Basically re-calculate all
attributes that are dependent on the tables.
*/
virtual void fix_after_pullout(st_select_lex *new_parent, Item **ref,
bool merge)
{};
/*
This method should be used in case where we are sure that we do not need
complete fix_fields() procedure.
Usually this method is used by the optimizer when it has to create a new
item out of other already fixed items. For example, if the optimizer has
to create a new Item_func for an inferred equality whose left and right
parts are already fixed items. In some cases the optimizer cannot use
directly fixed items as the arguments of the created functional item,
but rather uses intermediate type conversion items. Then the method is
supposed to be applied recursively.
*/
virtual inline void quick_fix_field() { fixed= 1; }
bool save_in_value(struct st_value *value)
{
return type_handler()->Item_save_in_value(this, value);
}
/* Function returns 1 on overflow and -1 on fatal errors */
int save_in_field_no_warnings(Field *field, bool no_conversions);
virtual int save_in_field(Field *field, bool no_conversions);
virtual bool save_in_param(THD *thd, Item_param *param);
virtual void save_org_in_field(Field *field,
fast_field_copier data
__attribute__ ((__unused__)))
{ (void) save_in_field(field, 1); }
virtual fast_field_copier setup_fast_field_copier(Field *field)
{ return NULL; }
virtual int save_safe_in_field(Field *field)
{ return save_in_field(field, 1); }
virtual bool send(Protocol *protocol, st_value *buffer)
{
return type_handler()->Item_send(this, protocol, buffer);
}
virtual bool eq(const Item *, bool binary_cmp) const;
enum_field_types field_type() const
{
return type_handler()->field_type();
}
virtual const Type_handler *type_handler() const= 0;
const Type_handler *type_handler_for_comparison() const
{
return type_handler()->type_handler_for_comparison();
}
virtual const Type_handler *real_type_handler() const
{
return type_handler();
}
virtual const Type_handler *cast_to_int_type_handler() const
{
return type_handler();
}
virtual const Type_handler *type_handler_for_system_time() const
{
return real_type_handler();
}
/* result_type() of an item specifies how the value should be returned */
Item_result result_type() const
{
return type_handler()->result_type();
}
/* ... while cmp_type() specifies how it should be compared */
Item_result cmp_type() const
{
return type_handler()->cmp_type();
}
const Type_handler *string_type_handler() const
{
return Type_handler::string_type_handler(max_length);
}
/*
Calculate the maximum length of an expression.
This method is used in data type aggregation for UNION, e.g.:
SELECT 'b' UNION SELECT COALESCE(double_10_3_field) FROM t1;
The result is usually equal to max_length, except for some numeric types.
In case of the INT, FLOAT, DOUBLE data types Item::max_length and
Item::decimals are ignored, so the returned value depends only on the
data type itself. E.g. for an expression of the DOUBLE(10,3) data type,
the result is always 53 (length 10 and precision 3 do not matter).
max_length is ignored for these numeric data types because the length limit
means only "expected maximum length", it is not a hard limit, so it does
not impose any data truncation. E.g. a column of the type INT(4) can
normally store big values up to 2147483647 without truncation. When we're
aggregating such column for UNION it's important to create a long enough
result column, not to lose any data.
For detailed behaviour of various data types see implementations of
the corresponding Type_handler_xxx::max_display_length().
Note, Item_field::max_display_length() overrides this to get
max_display_length() from the underlying field.
*/
virtual uint32 max_display_length() const
{
return type_handler()->max_display_length(this);
}
TYPELIB *get_typelib() const { return NULL; }
void set_maybe_null(bool maybe_null_arg) { maybe_null= maybe_null_arg; }
void set_typelib(TYPELIB *typelib)
{
// Non-field Items (e.g. hybrid functions) never have ENUM/SET types yet.
DBUG_ASSERT(0);
}
Item_cache* get_cache(THD *thd) const
{
return type_handler()->Item_get_cache(thd, this);
}
virtual enum Type type() const =0;
/*
real_type() is the type of base item. This is same as type() for
most items, except Item_ref() and Item_cache_wrapper() where it
shows the type for the underlaying item.
*/
virtual enum Type real_type() const { return type(); }
/*
Return information about function monotonicity. See comment for
enum_monotonicity_info for details. This function can only be called
after fix_fields() call.
*/
virtual enum_monotonicity_info get_monotonicity_info() const
{ return NON_MONOTONIC; }
/*
Convert "func_arg $CMP$ const" half-interval into "FUNC(func_arg) $CMP2$ const2"
SYNOPSIS
val_int_endpoint()
left_endp FALSE <=> The interval is "x < const" or "x <= const"
TRUE <=> The interval is "x > const" or "x >= const"
incl_endp IN FALSE <=> the comparison is '<' or '>'
TRUE <=> the comparison is '<=' or '>='
OUT The same but for the "F(x) $CMP$ F(const)" comparison
DESCRIPTION
This function is defined only for unary monotonic functions. The caller
supplies the source half-interval
x $CMP$ const
The value of const is supplied implicitly as the value this item's
argument, the form of $CMP$ comparison is specified through the
function's arguments. The calle returns the result interval
F(x) $CMP2$ F(const)
passing back F(const) as the return value, and the form of $CMP2$
through the out parameter. NULL values are assumed to be comparable and
be less than any non-NULL values.
RETURN
The output range bound, which equal to the value of val_int()
- If the value of the function is NULL then the bound is the
smallest possible value of LONGLONG_MIN
*/
virtual longlong val_int_endpoint(bool left_endp, bool *incl_endp)
{ DBUG_ASSERT(0); return 0; }
/* valXXX methods must return NULL or 0 or 0.0 if null_value is set. */
/*
Return double precision floating point representation of item.
SYNOPSIS
val_real()
RETURN
In case of NULL value return 0.0 and set null_value flag to TRUE.
If value is not null null_value flag will be reset to FALSE.
*/