17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef drizzled_item_h
21
#define drizzled_item_h
20
#ifndef DRIZZLED_ITEM_H
21
#define DRIZZLED_ITEM_H
23
23
#include <drizzled/dtcollation.h>
24
#include <mysys/drizzle_time.h>
25
#include <drizzled/my_decimal.h>
26
#include <drizzled/sql_bitmap.h>
27
#include <drizzled/sql_list.h>
28
#include <drizzled/sql_alloc.h>
33
class Name_resolution_context;
39
class Item_in_subselect;
27
43
void item_init(void); /* Init item functions */
31
46
void dummy_error_processor(Session *session, void *data);
33
47
void view_error_processor(Session *session, void *data);
36
Instances of Name_resolution_context store the information necesary for
37
name resolution of Items and other context analysis of a query made in
40
This structure is a part of SELECT_LEX, a pointer to this structure is
41
assigned when an item is created (which happens mostly during parsing
42
(sql_yacc.yy)), but the structure itself will be initialized after parsing
45
TODO: move subquery of INSERT ... SELECT and CREATE ... SELECT to
46
separate SELECT_LEX which allow to remove tricks of changing this
47
structure before and after INSERT/CREATE and its SELECT to make correct
48
field name resolution.
50
struct Name_resolution_context: Sql_alloc
53
The name resolution context to search in when an Item cannot be
54
resolved in this context (the context of an outer select)
56
Name_resolution_context *outer_context;
59
List of tables used to resolve the items of this context. Usually these
60
are tables from the FROM clause of SELECT statement. The exceptions are
61
INSERT ... SELECT and CREATE ... SELECT statements, where SELECT
62
subquery is not moved to a separate SELECT_LEX. For these types of
63
statements we have to change this member dynamically to ensure correct
64
name resolution of different parts of the statement.
66
TableList *table_list;
68
In most cases the two table references below replace 'table_list' above
69
for the purpose of name resolution. The first and last name resolution
70
table references allow us to search only in a sub-tree of the nested
71
join tree in a FROM clause. This is needed for NATURAL JOIN, JOIN ... USING
74
TableList *first_name_resolution_table;
76
Last table to search in the list of leaf table references that begins
77
with first_name_resolution_table.
79
TableList *last_name_resolution_table;
82
SELECT_LEX item belong to, in case of merged VIEW it can differ from
83
SELECT_LEX where item was created, so we can't use table_list/field_list
86
st_select_lex *select_lex;
89
Processor of errors caused during Item name resolving, now used only to
90
hide underlying tables in errors about views (i.e. it substitute some
93
void (*error_processor)(Session *, void *);
94
void *error_processor_data;
97
When true items are resolved in this context both against the
98
SELECT list and this->table_list. If false, items are resolved
99
only against this->table_list.
101
bool resolve_in_select_list;
104
Security context of this name resolution context. It's used for views
105
and is non-zero only if the view is defined with SQL SECURITY DEFINER.
107
Security_context *security_ctx;
109
Name_resolution_context()
110
:outer_context(0), table_list(0), select_lex(0),
111
error_processor_data(0),
117
resolve_in_select_list= false;
118
error_processor= &dummy_error_processor;
119
first_name_resolution_table= NULL;
120
last_name_resolution_table= NULL;
123
void resolve_in_table_list_only(TableList *tables)
125
table_list= first_name_resolution_table= tables;
126
resolve_in_select_list= false;
129
void process_error(Session *session)
131
(*error_processor)(session, error_processor_data);
137
Store and restore the current state of a name resolution context.
140
class Name_resolution_context_state
143
TableList *save_table_list;
144
TableList *save_first_name_resolution_table;
145
TableList *save_next_name_resolution_table;
146
bool save_resolve_in_select_list;
147
TableList *save_next_local;
150
Name_resolution_context_state() {} /* Remove gcc warning */
153
/* Save the state of a name resolution context. */
154
void save_state(Name_resolution_context *context, TableList *table_list)
156
save_table_list= context->table_list;
157
save_first_name_resolution_table= context->first_name_resolution_table;
158
save_resolve_in_select_list= context->resolve_in_select_list;
159
save_next_local= table_list->next_local;
160
save_next_name_resolution_table= table_list->next_name_resolution_table;
163
/* Restore a name resolution context from saved state. */
164
void restore_state(Name_resolution_context *context, TableList *table_list)
166
table_list->next_local= save_next_local;
167
table_list->next_name_resolution_table= save_next_name_resolution_table;
168
context->table_list= save_table_list;
169
context->first_name_resolution_table= save_first_name_resolution_table;
170
context->resolve_in_select_list= save_resolve_in_select_list;
173
TableList *get_first_name_resolution_table()
175
return save_first_name_resolution_table;
180
50
/*************************************************************************/
195
65
typedef bool (Item::*Item_processor) (unsigned char *arg);
68
class Item: public Sql_alloc
200
Item(const Item &); /* Prevent use of these */
70
/* Prevent use of these */
201
72
void operator=(Item &);
202
74
/* Cache of the result of is_expensive(). */
203
75
int8_t is_expensive_cache;
204
virtual bool is_expensive_processor(unsigned char *arg __attribute__((unused)))
76
virtual bool is_expensive_processor(unsigned char *arg);
208
static void *operator new(size_t size)
209
{ return sql_alloc(size); }
210
static void *operator new(size_t size, MEM_ROOT *mem_root)
211
{ return alloc_root(mem_root, size); }
212
static void operator delete(void *ptr __attribute__((unused)),
213
size_t size __attribute__((unused)))
214
{ TRASH(ptr, size); }
215
static void operator delete(void *ptr __attribute__((unused)),
216
MEM_ROOT *mem_root __attribute__((unused)))
219
80
enum Type {FIELD_ITEM= 0, FUNC_ITEM, SUM_FUNC_ITEM, STRING_ITEM,
220
INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
221
COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
222
PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
223
FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
81
INT_ITEM, REAL_ITEM, NULL_ITEM, VARBIN_ITEM,
82
COPY_STR_ITEM, FIELD_AVG_ITEM, DEFAULT_VALUE_ITEM,
83
PROC_ITEM,COND_ITEM, REF_ITEM, FIELD_STD_ITEM,
84
FIELD_VARIANCE_ITEM, INSERT_VALUE_ITEM,
224
85
SUBSELECT_ITEM, ROW_ITEM, CACHE_ITEM, TYPE_HOLDER,
225
86
PARAM_ITEM, DECIMAL_ITEM,
273
140
#ifdef EXTRA_DEBUG
277
void set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs);
144
void set_name(const char *str, uint32_t length,
145
const CHARSET_INFO * const cs);
278
146
void rename(char *new_name);
279
147
void init_make_field(Send_field *tmp_field,enum enum_field_types type);
280
148
virtual void cleanup();
281
149
virtual void make_field(Send_field *field);
282
150
Field *make_string_field(Table *table);
283
151
virtual bool fix_fields(Session *, Item **);
285
154
Fix after some tables has been pulled out. Basically re-calculate all
286
155
attributes that are dependent on the tables.
288
virtual void fix_after_pullout(st_select_lex *new_parent __attribute__((unused)),
289
Item **ref __attribute__((unused))) {};
157
virtual void fix_after_pullout(st_select_lex *new_parent, Item **ref);
292
160
should be used in case where we are sure that we do not need
293
complete fix_fields() procedure.
161
complete fix_fields() procedure. */
295
162
inline void quick_fix_field() { fixed= 1; }
296
/* Function returns 1 on overflow and -1 on fatal errors */
165
Save value in field, but don't give any warnings
168
This is used to temporary store and retrieve a value in a column,
169
for example in opt_range to adjust the key value to fit the column.
170
Return: Function returns 1 on overflow and -1 on fatal errors
297
172
int save_in_field_no_warnings(Field *field, bool no_conversions);
298
174
virtual int save_in_field(Field *field, bool no_conversions);
299
175
virtual void save_org_in_field(Field *field)
300
176
{ (void) save_in_field(field, 1); }
530
406
query and why they should be generated from the Item-tree, @see
531
407
mysql_register_view().
533
virtual inline void print(String *str,
534
enum_query_type query_type __attribute__((unused)))
536
str->append(full_name());
409
virtual void print(String *str, enum_query_type query_type);
539
411
void print_item_w_name(String *, enum_query_type query_type);
540
412
virtual void update_used_tables() {}
541
virtual void split_sum_func(Session *session __attribute__((unused)),
542
Item **ref_pointer_array __attribute__((unused)),
543
List<Item> &fields __attribute__((unused))) {}
413
virtual void split_sum_func(Session *session, Item **ref_pointer_array,
544
416
/* Called for items that really have to be split */
545
void split_sum_func2(Session *session, Item **ref_pointer_array, List<Item> &fields,
546
Item **ref, bool skip_registered);
417
void split_sum_func(Session *session, Item **ref_pointer_array,
419
Item **ref, bool skip_registered);
547
421
virtual bool get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
548
422
virtual bool get_time(DRIZZLE_TIME *ltime);
549
virtual bool get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
550
{ return get_date(ltime,fuzzydate); }
423
virtual bool get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate);
552
The method allows to determine nullness of a complex expression
553
without fully evaluating it, instead of calling val/result*() then
426
The method allows to determine nullness of a complex expression
427
without fully evaluating it, instead of calling val/result*() then
554
428
checking null_value. Used in Item_func_isnull/Item_func_isnotnull
555
429
and Item_sum_count/Item_sum_count_distinct.
556
430
Any new item which can be NULL must implement this method.
558
virtual bool is_null() { return 0; }
432
virtual bool is_null();
561
435
Make sure the null_value member has a correct value.
563
virtual void update_null_value () { (void) val_int(); }
437
virtual void update_null_value ();
566
440
Inform the item that there will be no distinction between its result
572
446
Item_cond_and and subquery-related item) enable special optimizations
573
447
when they are "top level".
575
virtual void top_level_item(void) {}
449
virtual void top_level_item(void);
577
451
set field of temporary table for Item which can be switched on temporary
578
452
table during query processing (grouping and so on)
580
virtual void set_result_field(Field *field __attribute__((unused))) {}
581
virtual bool is_result_field(void) { return 0; }
582
virtual bool is_bool_func(void) { return 0; }
583
virtual void save_in_result_field(bool no_conversions __attribute__((unused)))
454
virtual void set_result_field(Field *field);
455
virtual bool is_result_field(void);
456
virtual bool is_bool_func(void);
457
virtual void save_in_result_field(bool no_conversions);
586
460
set value of aggregate function in case of no rows for grouping were found
588
virtual void no_rows_in_result(void) {}
589
virtual Item *copy_or_same(Session *session __attribute__((unused)))
591
virtual Item *copy_andor_structure(Session *session __attribute__((unused)))
593
virtual Item *real_item(void) { return this; }
594
virtual Item *get_tmp_table_item(Session *session) { return copy_or_same(session); }
462
virtual void no_rows_in_result(void);
463
virtual Item *copy_or_same(Session *session);
465
virtual Item *copy_andor_structure(Session *session);
467
virtual Item *real_item(void);
468
virtual Item *get_tmp_table_item(Session *session);
596
470
static const CHARSET_INFO *default_charset();
597
virtual const CHARSET_INFO *compare_collation() { return NULL; }
471
virtual const CHARSET_INFO *compare_collation();
599
virtual bool walk(Item_processor processor __attribute__((unused)),
600
bool walk_subquery __attribute__((unused)),
603
return (this->*processor)(arg);
473
virtual bool walk(Item_processor processor,
606
477
virtual Item* transform(Item_transformer transformer, unsigned char *arg);
609
480
This function performs a generic "compilation" of the Item tree.
610
The process of compilation is assumed to go as follows:
481
The process of compilation is assumed to go as follows:
614
485
if (this->*some_analyzer(...))
616
487
compile children if any;
621
492
i.e. analysis is performed top-down while transformation is done
624
495
virtual Item* compile(Item_analyzer analyzer, unsigned char **arg_p,
625
Item_transformer transformer, unsigned char *arg_t)
627
if ((this->*analyzer) (arg_p))
628
return ((this->*transformer) (arg_t));
632
virtual void traverse_cond(Cond_traverser traverser __attribute__((unused)),
634
traverse_order order __attribute__((unused)))
636
(*traverser)(this, arg);
639
virtual bool remove_dependence_processor(unsigned char * arg __attribute__((unused)))
641
virtual bool remove_fixed(unsigned char * arg __attribute__((unused)))
646
virtual bool cleanup_processor(unsigned char *arg __attribute__((unused)));
647
virtual bool collect_item_field_processor(unsigned char * arg __attribute__((unused)))
649
virtual bool find_item_in_field_list_processor(unsigned char *arg __attribute__((unused)))
651
virtual bool change_context_processor(unsigned char *context __attribute__((unused)))
653
virtual bool reset_query_id_processor(unsigned char *query_id_arg __attribute__((unused)))
655
virtual bool register_field_in_read_map(unsigned char *arg __attribute__((unused)))
496
Item_transformer transformer, unsigned char *arg_t);
498
virtual void traverse_cond(Cond_traverser traverser,
500
traverse_order order);
502
virtual bool remove_dependence_processor(unsigned char * arg);
503
virtual bool remove_fixed(unsigned char * arg);
504
virtual bool cleanup_processor(unsigned char *arg);
505
virtual bool collect_item_field_processor(unsigned char * arg);
506
virtual bool find_item_in_field_list_processor(unsigned char *arg);
507
virtual bool change_context_processor(unsigned char *context);
508
virtual bool reset_query_id_processor(unsigned char *query_id_arg);
509
virtual bool register_field_in_read_map(unsigned char *arg);
658
512
The next function differs from the previous one that a bitmap to be updated
659
513
is passed as unsigned char *arg.
661
virtual bool register_field_in_bitmap(unsigned char *arg __attribute__((unused)))
663
virtual bool subst_argument_checker(unsigned char **arg)
515
virtual bool register_field_in_bitmap(unsigned char *arg);
516
virtual bool subst_argument_checker(unsigned char **arg);
671
519
Check if an expression/function is allowed for a virtual column
676
524
TRUE Function not accepted
677
525
FALSE Function accepted
679
virtual bool check_vcol_func_processor(unsigned char *arg __attribute__((unused)))
682
virtual Item *equal_fields_propagator(unsigned char * arg __attribute__((unused))) { return this; }
683
virtual bool set_no_const_sub(unsigned char *arg __attribute__((unused))) { return false; }
684
virtual Item *replace_equal_field(unsigned char * arg __attribute__((unused))) { return this; }
527
virtual bool check_vcol_func_processor(unsigned char *arg);
528
virtual Item *equal_fields_propagator(unsigned char * arg);
529
virtual bool set_no_const_sub(unsigned char *arg);
530
virtual Item *replace_equal_field(unsigned char * arg);
688
534
For SP local variable returns pointer to Item representing its
689
535
current value and pointer to current Item otherwise.
691
virtual Item *this_item(void) { return this; }
692
virtual const Item *this_item(void) const { return this; }
537
virtual Item *this_item(void);
538
virtual const Item *this_item(void) const;
695
541
For SP local variable returns address of pointer to Item representing its
696
542
current value and pointer passed via parameter otherwise.
698
virtual Item **this_item_addr(Session *session __attribute__((unused)), Item **addr_arg) { return addr_arg; }
544
virtual Item **this_item_addr(Session *session, Item **addr_arg);
701
virtual uint32_t cols() { return 1; }
702
virtual Item* element_index(uint32_t i __attribute__((unused))) { return this; }
703
virtual Item** addr(uint32_t i __attribute__((unused))) { return 0; }
547
virtual uint32_t cols();
548
virtual Item* element_index(uint32_t i);
549
virtual Item** addr(uint32_t i);
704
550
virtual bool check_cols(uint32_t c);
705
551
// It is not row => null inside is impossible
706
virtual bool null_inside() { return 0; }
552
virtual bool null_inside();
707
553
// used in row subselects to get value of elements
708
virtual void bring_value() {}
554
virtual void bring_value();
710
556
Field *tmp_table_field_from_field_type(Table *table, bool fixed_length);
711
virtual Item_field *filed_for_view_update() { return 0; }
557
virtual Item_field *filed_for_view_update();
713
virtual Item *neg_transformer(Session *session __attribute__((unused))) { return NULL; }
714
virtual Item *update_value_transformer(unsigned char *select_arg __attribute__((unused))) { return this; }
559
virtual Item *neg_transformer(Session *session);
560
virtual Item *update_value_transformer(unsigned char *select_arg);
715
561
virtual Item *safe_charset_converter(const CHARSET_INFO * const tocs);
723
565
result_as_int64_t() must return true for Items representing DATE/TIME
1930
1739
bool const_item() const { return 0; }
1931
1740
bool is_null() { return null_value; }
1935
class Cached_item :public Sql_alloc
1939
Cached_item() :null_value(0) {}
1940
virtual bool cmp(void)=0;
1941
virtual ~Cached_item(); /*line -e1509 */
1944
class Cached_item_str :public Cached_item
1947
String value,tmp_value;
1949
Cached_item_str(Session *session, Item *arg);
1951
~Cached_item_str(); // Deallocate String:s
1955
class Cached_item_real :public Cached_item
1960
Cached_item_real(Item *item_par) :item(item_par),value(0.0) {}
1964
class Cached_item_int :public Cached_item
1969
Cached_item_int(Item *item_par) :item(item_par),value(0) {}
1974
class Cached_item_decimal :public Cached_item
1979
Cached_item_decimal(Item *item_par);
1983
class Cached_item_field :public Cached_item
1985
unsigned char *buff;
1990
Cached_item_field(Field *arg_field) : field(arg_field)
1993
/* TODO: take the memory allocation below out of the constructor. */
1994
buff= (unsigned char*) sql_calloc(length=field->pack_length());
1999
1742
class Item_default_value : public Item_field
2201
1935
int save_in_field(Field *field, bool no_conversions);
2204
class Item_cache_row: public Item_cache
2206
Item_cache **values;
2207
uint32_t item_count;
2211
:Item_cache(), values(0), item_count(2), save_array(0) {}
2214
'allocate' used only in row transformer, to preallocate space for row
2217
bool allocate(uint32_t num);
2219
'setup' is needed only by row => it not called by simple row subselect
2220
(only by IN subselect (in subselect optimizer))
2222
bool setup(Item *item);
2223
void store(Item *item);
2224
void illegal_method_call(const char *);
2225
void make_field(Send_field *)
2227
illegal_method_call((const char*)"make_field");
2231
illegal_method_call((const char*)"val");
2236
illegal_method_call((const char*)"val_int");
2239
String *val_str(String *)
2241
illegal_method_call((const char*)"val_str");
2244
my_decimal *val_decimal(my_decimal *val __attribute__((unused)))
2246
illegal_method_call((const char*)"val_decimal");
2250
enum Item_result result_type() const { return ROW_RESULT; }
2252
uint32_t cols() { return item_count; }
2253
Item *element_index(uint32_t i) { return values[i]; }
2254
Item **addr(uint32_t i) { return (Item **) (values + i); }
2255
bool check_cols(uint32_t c);
2258
void keep_array() { save_array= 1; }
2261
Item_cache::cleanup();
2263
memset(values, 0, item_count*sizeof(Item**));
2272
Item_type_holder used to store type. name, length of Item for UNIONS &
2275
Item_type_holder do not need cleanup() because its time of live limited by
2276
single SP/PS execution.
2278
class Item_type_holder: public Item
2281
TYPELIB *enum_set_typelib;
2282
enum_field_types fld_type;
2284
void get_full_info(Item *item);
2286
/* It is used to count decimal precision in join_types */
2287
int prev_decimal_int_part;
2289
Item_type_holder(Session*, Item*);
2291
Item_result result_type() const;
2292
enum_field_types field_type() const { return fld_type; };
2293
enum Type type() const { return TYPE_HOLDER; }
2296
my_decimal *val_decimal(my_decimal *);
2297
String *val_str(String*);
2298
bool join_types(Session *session, Item *);
2299
Field *make_field_by_type(Table *table);
2300
static uint32_t display_length(Item *item);
2301
static enum_field_types get_real_type(Item *);
2305
class st_select_lex;
2306
1940
void mark_select_range_as_dependent(Session *session,
2307
1941
st_select_lex *last_select,
2308
1942
st_select_lex *current_sel,
2309
1943
Field *found_field, Item *found_item,
2310
1944
Item_ident *resolved_item);
2312
extern Cached_item *new_Cached_item(Session *session, Item *item,
2313
bool use_result_field);
2314
1946
extern void resolve_const_item(Session *session, Item **ref, Item *cmp_item);
2315
1947
extern bool field_is_equal_to_item(Field *field,Item *item);