/
item.h
7949 lines (7193 loc) · 260 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, 2022, 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-1335 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"
#include "mem_root_array.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
*/
class st_value
{
public:
st_value() {}
st_value(char *buffer, size_t buffer_size) :
m_string(buffer, buffer_size, &my_charset_bin)
{}
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:
Value(char *buffer, size_t buffer_size) : st_value(buffer, buffer_size)
{}
Value()
{}
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];
public:
ValueBuffer(): Value(buffer, buffer_size)
{}
void reset_buffer()
{
m_string.set_buffer_if_not_allocated(buffer, buffer_size, &my_charset_bin);
}
};
#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_basic_value;
class Item_result_field;
class Item_field;
class Item_ref;
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)
CMP_PRECEDENCE, // =, <=>, >=, >, <=, <, <>, !=, IS
BETWEEN_PRECEDENCE, // BETWEEN
IN_PRECEDENCE, // IN, LIKE, REGEXP
BITOR_PRECEDENCE, // |
BITAND_PRECEDENCE, // &
SHIFT_PRECEDENCE, // <<, >>
INTERVAL_PRECEDENCE, // first argument in +INTERVAL
ADD_PRECEDENCE, // +, -
MUL_PRECEDENCE, // *, /, DIV, %, MOD
BITXOR_PRECEDENCE, // ^
PIPES_PRECEDENCE, // || (if PIPES_AS_CONCAT)
NEG_PRECEDENCE, // unary -, ~, !, NOT (if HIGH_NOT_PRECEDENCE)
COLLATE_PRECEDENCE, // BINARY, COLLATE
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 */
/*
Values for item->marker for cond items in the WHERE clause as used
by the optimizer.
Note that for Item_fields, the marker contains
'select->cur_pos_in_select_list
*/
/* Used to check GROUP BY list in the MODE_ONLY_FULL_GROUP_BY mode */
#define MARKER_UNDEF_POS -1
#define MARKER_UNUSED 0
#define MARKER_CHANGE_COND 1
#define MARKER_PROCESSED 2
#define MARKER_CHECK_ON_READ 3
#define MARKER_NULL_KEY 4
#define MARKER_FOUND_IN_ORDER 6
/* Used as bits in marker by Item::check_pushable_cond() */
#define MARKER_NO_EXTRACTION (1 << 6)
#define MARKER_FULL_EXTRACTION (1 << 7)
#define MARKER_DELETION (1 << 8)
#define MARKER_IMMUTABLE (1 << 9)
#define MARKER_SUBSTITUTION (1 << 10)
/* Used as bits in marker by window functions */
#define MARKER_SORTORDER_CHANGE (1 << 11)
#define MARKER_PARTITION_CHANGE (1 << 12)
#define MARKER_FRAME_CHANGE (1 << 13)
#define MARKER_EXTRACTION_MASK \
(MARKER_NO_EXTRACTION | MARKER_FULL_EXTRACTION | MARKER_DELETION | \
MARKER_IMMUTABLE)
extern const char *item_empty_name;
void dummy_error_processor(THD *thd, void *data);
void view_error_processor(THD *thd, void *data);
typedef List<TABLE_LIST>* ignored_tables_list_t;
bool ignored_list_includes_table(ignored_tables_list_t list, TABLE_LIST *tbl);
/*
Instances of Name_resolution_context store the information necessary 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= nullptr;
/*
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= nullptr;
/*
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= nullptr;
/*
Last table to search in the list of leaf table references that begins
with first_name_resolution_table.
*/
TABLE_LIST *last_name_resolution_table= nullptr;
/* Cache first_name_resolution_table in setup_natural_join_row_types */
TABLE_LIST *natural_join_first_table= nullptr;
/*
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= nullptr;
/*
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 *)= &dummy_error_processor;
void *error_processor_data= nullptr;
/*
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= false;
/*
Bitmap of tables that should be ignored when doing name resolution.
Normally it is {0}. Non-zero values are used by table functions.
*/
ignored_tables_list_t ignored_tables= nullptr;
/*
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= nullptr;
Name_resolution_context() = default;
/**
Name resolution context with resolution in only one table
*/
Name_resolution_context(TABLE_LIST *table) :
first_name_resolution_table(table), last_name_resolution_table(table)
{}
void init()
{
resolve_in_select_list= FALSE;
error_processor= &dummy_error_processor;
ignored_tables= nullptr;
first_name_resolution_table= nullptr;
last_name_resolution_table= nullptr;
}
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() = default; /* 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 corresponding 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() = default;
/**
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() = default;
virtual ~Settable_routine_parameter() = default;
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)
*/
my_ptrdiff_t 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() = default;
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);
typedef bool (Item::*Pushdown_checker) (uchar *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) { }
};
/**
A helper class describing what kind of Item created a temporary field.
- If m_field is set, then the temporary field was created from Field
(e.g. when the Item was Item_field, or Item_ref pointing to Item_field)
- If m_default_field is set, then there is a usable DEFAULT value.
(e.g. when the Item is Item_field)
- If m_item_result_field is set, then the temporary field was created
from certain sub-types of Item_result_field (e.g. Item_func)
See create_tmp_field() in sql_select.cc for details.
*/
class Tmp_field_src
{
Field *m_field;
Field *m_default_field;
Item_result_field *m_item_result_field;
public:
Tmp_field_src()
:m_field(0),
m_default_field(0),
m_item_result_field(0)
{ }
Field *field() const { return m_field; }
Field *default_field() const { return m_default_field; }
Item_result_field *item_result_field() const { return m_item_result_field; }
void set_field(Field *field) { m_field= field; }
void set_default_field(Field *field) { m_default_field= field; }
void set_item_result_field(Item_result_field *item)
{ m_item_result_field= item; }
};
/**
Parameters for create_tmp_field_ex().
See create_tmp_field() in sql_select.cc for details.
*/
class Tmp_field_param
{
bool m_group;
bool m_modify_item;
bool m_table_cant_handle_bit_fields;
bool m_make_copy_field;
public:
Tmp_field_param(bool group,
bool modify_item,
bool table_cant_handle_bit_fields,
bool make_copy_field)
:m_group(group),
m_modify_item(modify_item),
m_table_cant_handle_bit_fields(table_cant_handle_bit_fields),
m_make_copy_field(make_copy_field)
{ }
bool group() const { return m_group; }
bool modify_item() const { return m_modify_item; }
bool table_cant_handle_bit_fields() const
{ return m_table_cant_handle_bit_fields; }
bool make_copy_field() const { return m_make_copy_field; }
void set_modify_item(bool to) { m_modify_item= to; }
};
class Item_const
{
public:
virtual ~Item_const() = default;
virtual const Type_all_attributes *get_type_all_attributes_from_const() const= 0;
virtual bool const_is_null() const { return false; }
virtual const longlong *const_ptr_longlong() const { return NULL; }
virtual const double *const_ptr_double() const { return NULL; }
virtual const my_decimal *const_ptr_my_decimal() const { return NULL; }
virtual const MYSQL_TIME *const_ptr_mysql_time() const { return NULL; }
virtual const String *const_ptr_string() const { return NULL; }
};
/****************************************************************************/
#define STOP_PTR ((void *) 1)
/* Base flags (including IN) for an item */
typedef uint8 item_flags_t;
enum class item_base_t : item_flags_t
{
NONE= 0,
#define ITEM_FLAGS_MAYBE_NULL_SHIFT 0 // Must match MAYBE_NULL
MAYBE_NULL= (1<<0), // May be NULL.
IN_ROLLUP= (1<<1), // Appears in GROUP BY list
// of a query with ROLLUP.
FIXED= (1<<2), // Was fixed with fix_fields().
IS_EXPLICIT_NAME= (1<<3), // The name of this Item was set by the user
// (or was auto generated otherwise)
IS_IN_WITH_CYCLE= (1<<4), // This item is in CYCLE clause of WITH.
AT_TOP_LEVEL= (1<<5) // At top (AND) level of item tree
};
/* Flags that tells us what kind of items the item contains */
enum class item_with_t : item_flags_t
{
NONE= 0,
SP_VAR= (1<<0), // If Item contains a stored procedure variable
WINDOW_FUNC= (1<<1), // If item contains a window func
FIELD= (1<<2), // If any item except Item_sum contains a field.
SUM_FUNC= (1<<3), // If item contains a sum func
SUBQUERY= (1<<4), // If item containts a sub query
ROWNUM_FUNC= (1<<5)
};
/* Make operations in item_base_t and item_with_t work like 'int' */
static inline item_base_t operator&(const item_base_t a, const item_base_t b)
{
return (item_base_t) (((item_flags_t) a) & ((item_flags_t) b));
}
static inline item_base_t & operator&=(item_base_t &a, item_base_t b)
{
a= (item_base_t) (((item_flags_t) a) & (item_flags_t) b);
return a;
}
static inline item_base_t operator|(const item_base_t a, const item_base_t b)
{
return (item_base_t) (((item_flags_t) a) | ((item_flags_t) b));
}
static inline item_base_t & operator|=(item_base_t &a, item_base_t b)
{
a= (item_base_t) (((item_flags_t) a) | (item_flags_t) b);
return a;
}
static inline item_base_t operator~(const item_base_t a)
{
return (item_base_t) ~(item_flags_t) a;
}
static inline item_with_t operator&(const item_with_t a, const item_with_t b)
{
return (item_with_t) (((item_flags_t) a) & ((item_flags_t) b));
}
static inline item_with_t & operator&=(item_with_t &a, item_with_t b)
{
a= (item_with_t) (((item_flags_t) a) & (item_flags_t) b);
return a;
}
static inline item_with_t operator|(const item_with_t a, const item_with_t b)
{
return (item_with_t) (((item_flags_t) a) | ((item_flags_t) b));
}
static inline item_with_t & operator|=(item_with_t &a, item_with_t b)
{
a= (item_with_t) (((item_flags_t) a) | (item_flags_t) b);
return a;
}
static inline item_with_t operator~(const item_with_t a)
{
return (item_with_t) ~(item_flags_t) a;
}
class Item :public Value_source,
public Type_all_attributes
{
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,
/*
NOT NULL literal-alike constants, which do not change their
value during an SQL statement execution, but can optionally
change their value between statements:
- Item_literal - real NOT NULL constants
- Item_param - can change between statements
- Item_splocal - can change between statements
- Item_user_var_as_out_param - hack
Note, Item_user_var_as_out_param actually abuses the type code.
It should be moved out of the Item tree eventually.
*/
CONST_ITEM,
NULL_ITEM, // Item_null or Item_param bound to NULL
COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
CONTEXTUALLY_TYPED_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,
EXPR_CACHE_ITEM};
enum cond_result { COND_UNDEF,COND_OK,COND_TRUE,COND_FALSE };
enum traverse_order { POSTFIX, PREFIX };
protected:
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(MEM_ROOT *root, TABLE *table)
{
const Type_handler *h= type_handler();
return h->make_and_init_table_field(root, &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(MEM_ROOT *root, TABLE *table)
{
DBUG_ASSERT(fixed());
const Type_handler *h= type_handler()->type_handler_for_tmp_table(this);
return h->make_and_init_table_field(root, &name,
Record_addr(maybe_null()),
*this, table);
}
/**
Create a temporary field for a simple Item, which does not
need any special action after the field creation:
- is not an Item_field descendant (and not a reference to Item_field)
- is not an Item_result_field descendant
- does not need to copy any DEFAULT value to the result Field
- does not need to set Field::is_created_from_null_item for the result
See create_tmp_field_ex() for details on parameters and return values.
*/
Field *create_tmp_field_ex_simple(MEM_ROOT *root,
TABLE *table,
Tmp_field_src *src,
const Tmp_field_param *param)
{
DBUG_ASSERT(!param->make_copy_field());
DBUG_ASSERT(!is_result_field());
DBUG_ASSERT(type() != NULL_ITEM);
return tmp_table_field_from_field_type(root, table);
}
Field *create_tmp_field_int(MEM_ROOT *root, TABLE *table,
uint convert_int_length);
Field *tmp_table_field_from_field_type_maybe_null(MEM_ROOT *root,
TABLE *table,
Tmp_field_src *src,
const Tmp_field_param *param,
bool is_explicit_null);
void raise_error_not_evaluable();
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());
double value= item->val_real();
null_value= item->null_value;
return value;
}
longlong val_int_from_item(Item *item)
{
DBUG_ASSERT(fixed());
longlong value= item->val_int();
null_value= item->null_value;
return value;
}
String *val_str_from_item(Item *item, String *str)
{
DBUG_ASSERT(fixed());
String *res= item->val_str(str);
if (res)
res->set_charset(collation.collation);
if ((null_value= item->null_value))
res= NULL;
return res;
}
bool val_native_from_item(THD *thd, Item *item, Native *to)
{
DBUG_ASSERT(fixed());
null_value= item->val_native(thd, to);
DBUG_ASSERT(null_value == item->null_value);
return null_value;
}
bool val_native_from_field(Field *field, Native *to)
{
if ((null_value= field->is_null()))
return true;
return (null_value= field->val_native(to));
}
bool val_native_with_conversion_from_item(THD *thd, Item *item, Native *to,
const Type_handler *handler)
{
DBUG_ASSERT(fixed());
return (null_value= item->val_native_with_conversion(thd, to, handler));
}
my_decimal *val_decimal_from_item(Item *item, my_decimal *decimal_value)
{
DBUG_ASSERT(fixed());
my_decimal *value= item->val_decimal(decimal_value);
if ((null_value= item->null_value))
value= NULL;
return value;
}