/
item.h
5817 lines (5196 loc) · 189 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, 2015, Oracle and/or its affiliates.
Copyright (c) 2009, 2016, MariaDB
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"
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
const char *dbug_print_item(Item *item);
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
};
typedef Bounds_checked_array<Item*> Ref_ptr_array;
static inline uint32
char_to_byte_length_safe(uint32 char_length_arg, uint32 mbmaxlen_arg)
{
ulonglong tmp= ((ulonglong) char_length_arg) * mbmaxlen_arg;
return (tmp > UINT_MAX32) ? (uint32) UINT_MAX32 : (uint32) tmp;
}
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 */
/*
"Declared Type Collation"
A combination of collation and its derivation.
Flags for collation aggregation modes:
MY_COLL_ALLOW_SUPERSET_CONV - allow conversion to a superset
MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
(i.e. constant).
MY_COLL_ALLOW_CONV - allow any kind of conversion
(combination of the above two)
MY_COLL_ALLOW_NUMERIC_CONV - if all items were numbers, convert to
@@character_set_connection
MY_COLL_DISALLOW_NONE - don't allow return DERIVATION_NONE
(e.g. when aggregating for comparison)
MY_COLL_CMP_CONV - combination of MY_COLL_ALLOW_CONV
and MY_COLL_DISALLOW_NONE
*/
#define MY_COLL_ALLOW_SUPERSET_CONV 1
#define MY_COLL_ALLOW_COERCIBLE_CONV 2
#define MY_COLL_DISALLOW_NONE 4
#define MY_COLL_ALLOW_NUMERIC_CONV 8
#define MY_COLL_ALLOW_CONV (MY_COLL_ALLOW_SUPERSET_CONV | MY_COLL_ALLOW_COERCIBLE_CONV)
#define MY_COLL_CMP_CONV (MY_COLL_ALLOW_CONV | MY_COLL_DISALLOW_NONE)
#define NO_EXTRACTION_FL (1 << 6)
#define FULL_EXTRACTION_FL (1 << 7)
#define EXTRACTION_MASK (NO_EXTRACTION_FL | FULL_EXTRACTION_FL)
class DTCollation {
public:
CHARSET_INFO *collation;
enum Derivation derivation;
uint repertoire;
void set_repertoire_from_charset(CHARSET_INFO *cs)
{
repertoire= cs->state & MY_CS_PUREASCII ?
MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
}
DTCollation()
{
collation= &my_charset_bin;
derivation= DERIVATION_NONE;
repertoire= MY_REPERTOIRE_UNICODE30;
}
DTCollation(CHARSET_INFO *collation_arg, Derivation derivation_arg)
{
collation= collation_arg;
derivation= derivation_arg;
set_repertoire_from_charset(collation_arg);
}
DTCollation(CHARSET_INFO *collation_arg,
Derivation derivation_arg,
uint repertoire_arg)
:collation(collation_arg),
derivation(derivation_arg),
repertoire(repertoire_arg)
{ }
void set(const DTCollation &dt)
{
collation= dt.collation;
derivation= dt.derivation;
repertoire= dt.repertoire;
}
void set(CHARSET_INFO *collation_arg, Derivation derivation_arg)
{
collation= collation_arg;
derivation= derivation_arg;
set_repertoire_from_charset(collation_arg);
}
void set(CHARSET_INFO *collation_arg,
Derivation derivation_arg,
uint repertoire_arg)
{
collation= collation_arg;
derivation= derivation_arg;
repertoire= repertoire_arg;
}
void set_numeric()
{
collation= &my_charset_numeric;
derivation= DERIVATION_NUMERIC;
repertoire= MY_REPERTOIRE_NUMERIC;
}
void set(CHARSET_INFO *collation_arg)
{
collation= collation_arg;
set_repertoire_from_charset(collation_arg);
}
void set(Derivation derivation_arg)
{ derivation= derivation_arg; }
bool aggregate(const DTCollation &dt, uint flags= 0);
bool set(DTCollation &dt1, DTCollation &dt2, uint flags= 0)
{ set(dt1); return aggregate(dt2, flags); }
const char *derivation_name() const
{
switch(derivation)
{
case DERIVATION_NUMERIC: return "NUMERIC";
case DERIVATION_IGNORABLE: return "IGNORABLE";
case DERIVATION_COERCIBLE: return "COERCIBLE";
case DERIVATION_IMPLICIT: return "IMPLICIT";
case DERIVATION_SYSCONST: return "SYSCONST";
case DERIVATION_EXPLICIT: return "EXPLICIT";
case DERIVATION_NONE: return "NONE";
default: return "UNKNOWN";
}
}
int sortcmp(const String *s, const String *t) const
{
return collation->coll->strnncollsp(collation,
(uchar *) s->ptr(), s->length(),
(uchar *) t->ptr(), t->length());
}
};
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;
}
};
/*
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;
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; }
};
/**
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, 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 Item_equal;
class COND_EQUAL;
class st_select_lex_unit;
class Item_func_not;
class Item_splocal;
/**
String_copier that honors the current sql_mode (strict vs non strict)
and can send 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) { }
};
/**
A class to store type attributes for the standard data types.
Does not include attributes for the extended data types
such as ENUM, SET, GEOMETRY.
*/
class Type_std_attributes
{
public:
DTCollation collation;
uint decimals;
/*
The maximum value length in characters multiplied by collation->mbmaxlen.
Almost always it's the maximum value length in bytes.
*/
uint32 max_length;
bool unsigned_flag;
Type_std_attributes()
:collation(&my_charset_bin, DERIVATION_COERCIBLE),
decimals(0), max_length(0), unsigned_flag(false)
{ }
Type_std_attributes(const Type_std_attributes *other)
:collation(other->collation),
decimals(other->decimals),
max_length(other->max_length),
unsigned_flag(other->unsigned_flag)
{ }
void set(const Type_std_attributes *other)
{
*this= *other;
}
void set(const Field *field)
{
decimals= field->decimals();
max_length= field->field_length;
collation.set(field->charset());
unsigned_flag= MY_TEST(field->flags & UNSIGNED_FLAG);
}
};
class Item: public Value_source,
public Type_std_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(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);
virtual Field *make_string_field(TABLE *table);
Field *tmp_table_field_from_field_type(TABLE *table,
bool fixed_length,
bool set_blob_packlength);
Field *create_tmp_field(bool group, TABLE *table, uint convert_int_length);
void push_note_converted_to_negative_complement(THD *thd);
void push_note_converted_to_positive_complement(THD *thd);
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); }
char * name; /* Name from select */
/* Original item name (if it was renamed)*/
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;
/*
TODO: convert name and name_length fields into LEX_STRING to keep them in
sync (see bug #11829681/60295 etc). Then also remove some strlen(name)
calls.
*/
uint name_length; /* Length of name */
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_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 */
bool with_subselect; /* If this item is a subselect or some
of its arguments is or contains a
subselect */
// 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=0;
#endif
} /*lint -e1509 */
void set_name(THD *thd, const char *str, uint length, CHARSET_INFO *cs);
void set_name_no_truncate(THD *thd, const char *str, uint length,
CHARSET_INFO *cs);
void set_name_for_rollback(THD *thd, const char *str, uint length,
CHARSET_INFO *cs);
void rename(char *new_name);
void init_make_field(Send_field *tmp_field,enum enum_field_types type);
virtual void cleanup();
virtual void make_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) {};
/*
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 store(struct st_value *value, ulonglong fuzzydate)
{
switch (cmp_type()) {
case INT_RESULT:
{
value->m_type= unsigned_flag ? DYN_COL_UINT : DYN_COL_INT;
value->value.m_longlong= val_int();
break;
}
case REAL_RESULT:
{
value->m_type= DYN_COL_DOUBLE;
value->value.m_double= val_real();
break;
}
case DECIMAL_RESULT:
{
value->m_type= DYN_COL_DECIMAL;
my_decimal *dec= val_decimal(&value->m_decimal);
if (dec != &value->m_decimal && !null_value)
my_decimal2decimal(dec, &value->m_decimal);
break;
}
case STRING_RESULT:
{
value->m_type= DYN_COL_STRING;
String *str= val_str(&value->m_string);
if (str != &value->m_string && !null_value)
value->m_string.set(str->ptr(), str->length(), str->charset());
break;
}
case TIME_RESULT:
{
value->m_type= DYN_COL_DATETIME;
get_date(&value->value.m_time, fuzzydate);
break;
}
case ROW_RESULT:
DBUG_ASSERT(false);
null_value= true;
break;
}
if (null_value)
{
value->m_type= DYN_COL_NULL;
return true;
}
return false;
}
/* 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, String *str);
virtual bool eq(const Item *, bool binary_cmp) const;
virtual enum_field_types field_type() const= 0;
virtual const Type_handler *type_handler() const
{
return Type_handler::get_handler_by_field_type(field_type());
}
/* result_type() of an item specifies how the value should be returned */
virtual Item_result result_type() const
{
return type_handler()->result_type();
}
/* ... while cmp_type() specifies how it should be compared */
virtual Item_result cmp_type() const
{
return type_handler()->cmp_type();
}
virtual Item_result cast_to_int_type() const { return cmp_type(); }
enum_field_types string_field_type() const
{
return Type_handler::string_type_handler(max_length)->field_type();
}
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.
*/
virtual double val_real()=0;
/*
Return integer representation of item.
SYNOPSIS
val_int()
RETURN
In case of NULL value return 0 and set null_value flag to TRUE.
If value is not null null_value flag will be reset to FALSE.
*/
virtual longlong val_int()=0;
/**
Get a value for CAST(x AS SIGNED).
Too large positive unsigned integer values are converted
to negative complements.
Values of non-integer data types are adjusted to the SIGNED range.
*/
virtual longlong val_int_signed_typecast();
/**
Get a value for CAST(x AS UNSIGNED).
Negative signed integer values are converted
to positive complements.
Values of non-integer data types are adjusted to the UNSIGNED range.
*/
virtual longlong val_int_unsigned_typecast();
/*
This is just a shortcut to avoid the cast. You should still use
unsigned_flag to check the sign of the item.
*/
inline ulonglong val_uint() { return (ulonglong) val_int(); }
/*
Return string representation of this item object.
SYNOPSIS
val_str()
str an allocated buffer this or any nested Item object can use to
store return value of this method.
NOTE
Buffer passed via argument should only be used if the item itself
doesn't have an own String buffer. In case when the item maintains
it's own string buffer, it's preferable to return it instead to
minimize number of mallocs/memcpys.
The caller of this method can modify returned string, but only in case
when it was allocated on heap, (is_alloced() is true). This allows
the caller to efficiently use a buffer allocated by a child without
having to allocate a buffer of it's own. The buffer, given to
val_str() as argument, belongs to the caller and is later used by the
caller at it's own choosing.
A few implications from the above:
- unless you return a string object which only points to your buffer
but doesn't manages it you should be ready that it will be
modified.
- even for not allocated strings (is_alloced() == false) the caller
can change charset (see Item_func_{typecast/binary}. XXX: is this
a bug?
- still you should try to minimize data copying and return internal
object whenever possible.