forked from Meituan-Dianping/SQLAdvisor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.h
executable file
·2668 lines (2312 loc) · 85 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 ITEM_INCLUDED
#define ITEM_INCLUDED
/* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights
reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
#include "sql_priv.h" /* STRING_BUFFER_USUAL_SIZE */
#include "unireg.h"
#include "sql_const.h" /* RAND_TABLE_BIT, MAX_FIELD_NAME */
#include "unireg.h" // REQUIRED: for other includes
#include "thr_malloc.h" /* sql_calloc */
#include "sql_array.h"
#include "sql_string.h"
#include "mysqld.h"
#include "table.h"
#include "my_decimal.h" /* my_decimal */
#include "sql_error.h" /* Sql_condition */
#include "mysql_version.h" /* FRM_VER */
class Protocol;
struct TABLE_LIST;
void item_init(void); /* Init item functions */
class Item_field;
class user_var_entry;
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;
}
/**
Tests if field type is temporal, i.e. represents
DATE, TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field type, as returned by field->type().
@retval true If field type is temporal
@retval false If field type is not temporal
*/
inline bool is_temporal_type(enum_field_types type) {
switch (type) {
case MYSQL_TYPE_TIME:
case MYSQL_TYPE_DATETIME:
case MYSQL_TYPE_TIMESTAMP:
case MYSQL_TYPE_DATE:
case MYSQL_TYPE_NEWDATE:
return true;
default:
return false;
}
}
/**
Tests if field real type is temporal, i.e. represents
all existing implementations of
DATE, TIME, DATETIME or TIMESTAMP types in SQL.
@param type Field real type, as returned by field->real_type()
@retval true If field real type is temporal
@retval false If field real type is not temporal
*/
inline bool is_temporal_real_type(enum_field_types type)
{
switch (type)
{
case MYSQL_TYPE_TIME2:
case MYSQL_TYPE_TIMESTAMP2:
case MYSQL_TYPE_DATETIME2:
return true;
default:
return is_temporal_type(type);
}
}
/**
Tests if field real type can have "DEFAULT CURRENT_TIMESTAMP",
i.e. represents TIMESTAMP types in SQL.
@param type Field type, as returned by field->real_type().
@retval true If field real type can have "DEFAULT CURRENT_TIMESTAMP".
@retval false If field real type can not have "DEFAULT CURRENT_TIMESTAMP".
*/
inline bool real_type_with_now_as_default(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
}
/**
Tests if field real type can have "ON UPDATE CURRENT_TIMESTAMP",
i.e. represents TIMESTAMP types in SQL.
@param type Field type, as returned by field->real_type().
@retval true If field real type can have "ON UPDATE CURRENT_TIMESTAMP".
@retval false If field real type can not have "ON UPDATE CURRENT_TIMESTAMP".
*/
inline bool real_type_with_now_on_update(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2 ||
type == MYSQL_TYPE_DATETIME || type == MYSQL_TYPE_DATETIME2;
}
/**
Recognizer for concrete data type (called real_type for some reason),
returning true if it is one of the TIMESTAMP types.
*/
inline bool is_timestamp_type(enum_field_types type)
{
return type == MYSQL_TYPE_TIMESTAMP || type == MYSQL_TYPE_TIMESTAMP2;
}
inline uint get_enum_pack_length(int elements)
{
return elements < 256 ? 1 : 2;
}
inline uint get_set_pack_length(int elements)
{
uint len= (elements + 7) / 8;
return len > 4 ? 8 : len;
}
enum utype { NONE,DATE,SHIELD,NOEMPTY,CASEUP,PNR,BGNR,PGNR,YES,NO,REL,
CHECK,EMPTY,UNKNOWN_FIELD,CASEDN,NEXT_NUMBER,INTERVAL_FIELD,
BIT_FIELD, TIMESTAMP_OLD_FIELD, CAPITALIZE, BLOB_FIELD,
TIMESTAMP_DN_FIELD, TIMESTAMP_UN_FIELD, TIMESTAMP_DNUN_FIELD};
enum geometry_type
{
GEOM_GEOMETRY = 0, GEOM_POINT = 1, GEOM_LINESTRING = 2, GEOM_POLYGON = 3,
GEOM_MULTIPOINT = 4, GEOM_MULTILINESTRING = 5, GEOM_MULTIPOLYGON = 6,
GEOM_GEOMETRYCOLLECTION = 7
};
/*
Create field class for CREATE TABLE
*/
class Create_field :public Sql_alloc
{
public:
const char *field_name;
const char *change; // If done with alter table
const char *after; // Put column after this one
LEX_STRING comment; // Comment for field
/**
The declared default value, if any, otherwise NULL. Note that this member
is NULL if the default is a function. If the column definition has a
function declared as the default, the information is found in
Create_field::unireg_check.
@see Create_field::unireg_check
*/
Item *def;
enum enum_field_types sql_type;
/*
At various stages in execution this can be length of field in bytes or
max number of characters.
*/
ulong length;
/*
The value of `length' as set by parser: is the number of characters
for most of the types, or of bytes for BLOBs or numeric types.
*/
uint32 char_length;
uint decimals, flags, pack_length, key_length;
utype unireg_check;
TYPELIB *interval; // Which interval to use
TYPELIB *save_interval; // Temporary copy for the above
// Used only for UCS2 intervals
List<String> interval_list;
const CHARSET_INFO *charset;
geometry_type geom_type;
uint8 row,col,sc_length,interval_id; // For rea_create_table
uint offset,pack_flag;
Create_field() :after(NULL) {}
/* Used to make a clone of this object for ALTER/CREATE TABLE */
Create_field *clone(MEM_ROOT *mem_root) const
{ return new (mem_root) Create_field(*this); }
void create_length_to_internal_length(void);
bool init(THD *thd, const char *field_name, enum_field_types type,
const char *length, const char *decimals, uint type_modifier,
Item *default_value, Item *on_update_value, LEX_STRING *comment,
const char *change, List<String> *interval_list,
const CHARSET_INFO *cs, uint uint_geom_type);
};
/*
"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 my_charset_numeric my_charset_latin1
#define MY_REPERTOIRE_NUMERIC MY_REPERTOIRE_ASCII
enum Derivation
{
DERIVATION_IGNORABLE= 6,
DERIVATION_NUMERIC= 5,
DERIVATION_COERCIBLE= 4,
DERIVATION_SYSCONST= 3,
DERIVATION_IMPLICIT= 2,
DERIVATION_NONE= 1,
DERIVATION_EXPLICIT= 0
};
class DTCollation {
public:
const CHARSET_INFO *collation;
enum Derivation derivation;
uint repertoire;
void set_repertoire_from_charset(const 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(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
{
collation= collation_arg;
derivation= derivation_arg;
set_repertoire_from_charset(collation_arg);
}
void set(DTCollation &dt)
{
collation= dt.collation;
derivation= dt.derivation;
repertoire= dt.repertoire;
}
void set(const CHARSET_INFO *collation_arg, Derivation derivation_arg)
{
collation= collation_arg;
derivation= derivation_arg;
set_repertoire_from_charset(collation_arg);
}
void set(const 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(const CHARSET_INFO *collation_arg)
{
collation= collation_arg;
set_repertoire_from_charset(collation_arg);
}
void set(Derivation derivation_arg)
{ derivation= derivation_arg; }
void set_repertoire(uint repertoire_arg)
{ repertoire= repertoire_arg; }
bool aggregate(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";
}
}
};
/*************************************************************************/
/**
Storage for name strings.
Enpowers Simple_cstring with allocation routines from the sql_strmake family.
This class must stay as small as possible as we often
pass it into functions using call-by-value evaluation.
Don't add new members or virual methods into this class!
*/
class Name_string: public Simple_cstring
{
private:
void set_or_copy(const char *str, size_t length, bool is_null_terminated)
{
if (is_null_terminated)
set(str, length);
else
copy(str, length);
}
public:
Name_string(): Simple_cstring() {}
/*
Please do NOT add constructor Name_string(const char *str) !
It will involve hidden strlen() call, which can affect
performance negatively. Use Name_string(str, len) instead.
*/
Name_string(const char *str, size_t length):
Simple_cstring(str, length) {}
Name_string(const LEX_STRING str): Simple_cstring(str) {}
Name_string(const char *str, size_t length, bool is_null_terminated):
Simple_cstring()
{
set_or_copy(str, length, is_null_terminated);
}
Name_string(const LEX_STRING str, bool is_null_terminated):
Simple_cstring()
{
set_or_copy(str.str, str.length, is_null_terminated);
}
/**
Allocate space using sql_strmake() or sql_strmake_with_convert().
*/
void copy(const char *str, size_t length, const CHARSET_INFO *cs);
/**
Variants for copy(), for various argument combinations.
*/
void copy(const char *str, size_t length)
{
copy(str, length, system_charset_info);
}
void copy(const char *str)
{
copy(str, (str ? strlen(str) : 0), system_charset_info);
}
void copy(const LEX_STRING lex)
{
copy(lex.str, lex.length);
}
void copy(const LEX_STRING *lex)
{
copy(lex->str, lex->length);
}
void copy(const Name_string str)
{
copy(str.ptr(), str.length());
}
};
#define NAME_STRING(x) Name_string(C_STRING_WITH_LEN(x))
extern const Name_string null_name_string;
/**
Storage for Item names.
Adds "autogenerated" flag and warning functionality to Name_string.
*/
class Item_name_string: public Name_string
{
private:
bool m_is_autogenerated; /* indicates if name of this Item
was autogenerated or set by user */
public:
Item_name_string(): Name_string(), m_is_autogenerated(true)
{ }
Item_name_string(const Name_string name)
:Name_string(name), m_is_autogenerated(true)
{ }
/**
Set m_is_autogenerated flag to the given value.
*/
void set_autogenerated(bool is_autogenerated)
{
m_is_autogenerated= is_autogenerated;
}
/**
Return the auto-generated flag.
*/
bool is_autogenerated() const { return m_is_autogenerated; }
using Name_string::copy;
/**
Copy name together with autogenerated flag.
Produce a warning if name was cut.
*/
void copy(const char *str_arg, size_t length_arg, const CHARSET_INFO *cs_arg,
bool is_autogenerated_arg);
};
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;
/*
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 against
SELECT_LEX::item_list, SELECT_lex::group_list and
this->table_list. If FALSE, items are resolved only against
this->table_list.
@see st_select_lex::item_list, st_select_lex::group_list
*/
bool resolve_in_select_list;
Name_resolution_context()
:outer_context(0), table_list(0), select_lex(0),
error_processor_data(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);
}
};
/*
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 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) {};
};
typedef bool (Item::*Item_processor) (uchar *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) (uchar *arg);
typedef void (*Cond_traverser) (const Item *item, void *arg);
class Item
{
Item(const Item &); /* Prevent use of these */
void operator=(Item &);
/* Cache of the result of is_expensive(). */
int8 is_expensive_cache;
virtual bool is_expensive_processor(uchar *arg) { return 0; }
public:
static void *operator new(size_t size) throw ()
{ return sql_alloc(size); }
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, 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};
enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
enum traverse_order { POSTFIX, PREFIX };
/* Reuse size, only used by SP local variable assignment, otherwize 0 */
uint rsize;
/*
str_values's main purpose is to be used to cache the value in
save_in_field
*/
String str_value;
Item_name_string item_name; /* Name from select */
Item_name_string orig_name; /* Original item name (if it was renamed)*/
/**
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;
uint32 max_length; /* Maximum length, in bytes */
/**
This member has several successive meanings, depending on the phase we're
in:
- during field resolution: it contains the index, in the "all_fields"
list, of the expression to which this field belongs; or a special
constant UNDEF_POS; see st_select_lex::cur_pos_in_all_fields and
match_exprs_for_only_full_group_by().
- when attaching conditions to tables: it says whether some condition
needs to be attached or can be omitted (for example because it is already
implemented by 'ref' access)
- when pushing index conditions: it says whether a condition uses only
indexed columns
- when creating an internal temporary table: it says how to store BIT
fields
- when we change DISTINCT to GROUP BY: it is used for book-keeping of
fields.
*/
int marker;
uint8 decimals;
my_bool maybe_null; /* If item may be null */
my_bool null_value; /* if item is null */
my_bool unsigned_flag;
my_bool with_sum_func;
my_bool fixed; /* If item fixed with fix_fields */
DTCollation collation;
Item_result cmp_context; /* Comparison context */
/*
If this item was created in runtime memroot,it cannot be used for
substitution in subquery transformation process
*/
bool runtime_item;
protected:
my_bool with_subselect; /* If this item is a subselect or some
of its arguments is or contains a
subselect. Computed by fix_fields
and updated by update_used_tables. */
my_bool with_stored_program; /* If this item is a stored program
or some of its arguments is or
contains a stored program.
Computed by fix_fields and updated
by update_used_tables. */
/**
This variable is a cache of 'Needed tables are locked'. True if either
'No tables locks is needed' or 'Needed tables are locked'.
If tables are used, then it will be set to
current_thd->lex->is_query_tables_locked().
It is used when checking const_item()/can_be_evaluated_now().
*/
bool tables_locked_cache;
public:
// alloc & destruct is done as start of select using sql_alloc
Item();
/*
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
item_name.set(0);
#endif
} /*lint -e1509 */
void rename(char *new_name);
/*
should be used in case where we are sure that we do not need
complete fix_fields() procedure.
*/
inline void quick_fix_field() { fixed= 1; }
virtual Item_result result_type() const { return REAL_RESULT; }
/**
Result type when an item appear in a numeric context.
See Field::numeric_context_result_type() for more comments.
*/
virtual enum Item_result numeric_context_result_type() const
{
if (result_type() == STRING_RESULT)
return REAL_RESULT;
return result_type();
}
virtual Item_result cast_to_int_type() const { return result_type(); }
virtual enum_field_types string_field_type() const;
virtual enum_field_types field_type() const;
virtual enum Type type() const =0;
/*
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; }
/* 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;
/*
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.
RETURN
In case of NULL value return 0 (NULL pointer) and set null_value flag
to TRUE.
If value is not null null_value flag will be reset to FALSE.
*/
virtual String *val_str(String *str)=0;
/*
Returns string representation of this item in ASCII format.
SYNOPSIS
val_str_ascii()
str - similar to val_str();
NOTE
This method is introduced for performance optimization purposes.
1. val_str() result of some Items in string context
depends on @@character_set_results.
@@character_set_results can be set to a "real multibyte" character
set like UCS2, UTF16, UTF32. (We'll use only UTF32 in the examples
below for convenience.)
So the default string result of such functions
in these circumstances is real multi-byte character set, like UTF32.
For example, all numbers in string context
return result in @@character_set_results:
SELECT CONCAT(20010101); -> UTF32
We do sprintf() first (to get ASCII representation)
and then convert to UTF32;
So these kind "data sources" can use ASCII representation
internally, but return multi-byte data only because
@@character_set_results wants so.
Therefore, conversion from ASCII to UTF32 is applied internally.
2. Some other functions need in fact ASCII input.
For example,
inet_aton(), GeometryFromText(), Convert_TZ(), GET_FORMAT().
Similar, fields of certain type, like DATE, TIME,
when you insert string data into them, expect in fact ASCII input.
If they get non-ASCII input, for example UTF32, they
convert input from UTF32 to ASCII, and then use ASCII
representation to do further processing.
3. Now imagine we pass result of a data source of the first type
to a data destination of the second type.
What happens
a. data source converts data from ASCII to UTF32, because
@@character_set_results wants so and passes the result to
data destination.
b. data destination gets UTF32 string.
c. data destination converts UTF32 string to ASCII,
because it needs ASCII representation to be able to handle data
correctly.
As a result we get two steps of unnecessary conversion:
From ASCII to UTF32, then from UTF32 to ASCII.
A better way to handle these situations is to pass ASCII
representation directly from the source to the destination.
This is why val_str_ascii() introduced.
RETURN
Similar to val_str()
*/
virtual String *val_str_ascii(String *str);
/*
Return decimal representation of item with fixed point.
SYNOPSIS
val_decimal()
decimal_buffer buffer which can be used by Item for returning value
(but can be not)
NOTE
Returned value should not be changed if it is not the same which was
passed via argument.
RETURN
Return pointer on my_decimal (it can be other then passed via argument)
if value is not NULL (null_value flag will be reset to FALSE).
In case of NULL value it return 0 pointer and set null_value flag
to TRUE.
*/
virtual my_decimal *val_decimal(my_decimal *decimal_buffer)= 0;
/*
Return boolean value of item.
RETURN
FALSE value is false or NULL
TRUE value is true (not equal to 0)
*/
virtual bool val_bool();
virtual String *val_nodeset(String*) { return 0; }
protected:
/* Helper functions, see item_sum.cc */
String *val_string_from_real(String *str);
String *val_string_from_int(String *str);
String *val_string_from_decimal(String *str);
my_decimal *val_decimal_from_real(my_decimal *decimal_value);
my_decimal *val_decimal_from_int(my_decimal *decimal_value);
my_decimal *val_decimal_from_string(my_decimal *decimal_value);
longlong val_int_from_decimal();
double val_real_from_decimal();
public:
virtual const char *full_name() const