00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #pragma once
00021
00025 #include <drizzled/message/table.pb.h>
00026 #include <drizzled/name_resolution_context.h>
00027 #include <drizzled/table_list.h>
00028 #include <drizzled/function/math/real.h>
00029 #include <drizzled/key_part_spec.h>
00030 #include <drizzled/index_hint.h>
00031 #include <drizzled/optimizer/explain_plan.h>
00032
00033 #include <bitset>
00034 #include <string>
00035
00036
00037
00038
00039
00040
00041 #ifdef DRIZZLE_SERVER
00042
00043 # include <drizzled/sys_var.h>
00044 # include <drizzled/item/func.h>
00045 # ifdef DRIZZLE_YACC
00046 # define LEX_YYSTYPE void *
00047 # else
00048 # if defined(DRIZZLE_LEX)
00049 # include <drizzled/foreign_key.h>
00050 # include <drizzled/lex_symbol.h>
00051 # include <drizzled/comp_creator.h>
00052 # include <drizzled/sql_yacc.hh>
00053 # define LEX_YYSTYPE YYSTYPE *
00054 # else
00055 # define LEX_YYSTYPE void *
00056 # endif
00057 # endif
00058 #endif
00059
00060
00061 #define DESCRIBE_NORMAL 1
00062 #define DESCRIBE_EXTENDED 2
00063
00064 #ifdef DRIZZLE_SERVER
00065
00066 #define DERIVED_NONE 0
00067 #define DERIVED_SUBQUERY 1
00068
00069 namespace drizzled
00070 {
00071
00072 typedef List<Item> List_item;
00073
00074 enum sub_select_type
00075 {
00076 UNSPECIFIED_TYPE,
00077 UNION_TYPE,
00078 INTERSECT_TYPE,
00079 EXCEPT_TYPE,
00080 GLOBAL_OPTIONS_TYPE,
00081 DERIVED_TABLE_TYPE,
00082 OLAP_TYPE
00083 };
00084
00085 enum olap_type
00086 {
00087 UNSPECIFIED_OLAP_TYPE,
00088 CUBE_TYPE,
00089 ROLLUP_TYPE
00090 };
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 class Select_Lex_Node {
00211 protected:
00212 Select_Lex_Node *next, **prev,
00213 *master, *slave,
00214 *link_next, **link_prev;
00215 public:
00216
00217 uint64_t options;
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 std::bitset<8> uncacheable;
00228 sub_select_type linkage;
00229 bool no_table_names_allowed;
00230 bool no_error;
00231
00232 static void *operator new(size_t size)
00233 {
00234 return memory::sql_alloc(size);
00235 }
00236 static void *operator new(size_t size, memory::Root *mem_root)
00237 { return mem_root->alloc(size); }
00238 static void operator delete(void*, size_t)
00239 { }
00240 static void operator delete(void*, memory::Root*)
00241 {}
00242 Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
00243 virtual ~Select_Lex_Node() {}
00244 inline Select_Lex_Node* get_master() { return master; }
00245 virtual void init_query();
00246 virtual void init_select();
00247 void include_down(Select_Lex_Node *upper);
00248 void include_neighbour(Select_Lex_Node *before);
00249 void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
00250 void include_global(Select_Lex_Node **plink);
00251 void exclude();
00252
00253 virtual Select_Lex_Unit* master_unit()= 0;
00254 virtual Select_Lex* outer_select()= 0;
00255 virtual Select_Lex* return_after_parsing()= 0;
00256
00257 virtual bool set_braces(bool value);
00258 virtual bool inc_in_sum_expr();
00259 virtual uint32_t get_in_sum_expr();
00260 virtual TableList* get_table_list();
00261 virtual List<Item>* get_item_list();
00262 virtual TableList *add_table_to_list(Session *session, Table_ident *table,
00263 lex_string_t *alias,
00264 const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00265 thr_lock_type flags= TL_UNLOCK,
00266 List<Index_hint> *hints= 0,
00267 lex_string_t *option= 0);
00268 virtual void set_lock_for_tables(thr_lock_type)
00269 {}
00270
00271 friend class Select_Lex_Unit;
00272 friend bool new_select(LEX *lex, bool move_down);
00273 private:
00274 void fast_exclude();
00275 };
00276
00277
00278
00279
00280
00281 class Select_Lex_Unit: public Select_Lex_Node {
00282 protected:
00283 TableList result_table_list;
00284 select_union *union_result;
00285 Table *table;
00286
00287 select_result *result;
00288 uint64_t found_rows_for_union;
00289 bool saved_error;
00290
00291 public:
00292 bool prepared,
00293 optimized,
00294 executed,
00295 cleaned;
00296
00297
00298 List<Item> item_list;
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 List<Item> types;
00311
00312
00313
00314
00315 Select_Lex *global_parameters;
00316
00317 Select_Lex *return_to;
00318
00319 ha_rows select_limit_cnt, offset_limit_cnt;
00320
00321 Item_subselect *item;
00322
00323 Session *session;
00324
00325
00326
00327
00328 Select_Lex *fake_select_lex;
00329
00330 Select_Lex *union_distinct;
00331 bool describe;
00332
00333 void init_query();
00334 Select_Lex_Unit* master_unit();
00335 Select_Lex* outer_select();
00336 Select_Lex* first_select()
00337 {
00338 return reinterpret_cast<Select_Lex*>(slave);
00339 }
00340 Select_Lex_Unit* next_unit()
00341 {
00342 return reinterpret_cast<Select_Lex_Unit*>(next);
00343 }
00344 Select_Lex* return_after_parsing() { return return_to; }
00345 void exclude_level();
00346 void exclude_tree();
00347
00348
00349 bool prepare(Session *session, select_result *result,
00350 uint64_t additional_options);
00351 bool exec();
00352 bool cleanup();
00353 inline void unclean() { cleaned= 0; }
00354 void reinit_exec_mechanism();
00355
00356 void print(String *str);
00357
00358 bool add_fake_select_lex(Session *session);
00359 void init_prepare_fake_select_lex(Session *session);
00360 bool change_result(select_result_interceptor *result,
00361 select_result_interceptor *old_result);
00362 void set_limit(Select_Lex *values);
00363 void set_session(Session *session_arg) { session= session_arg; }
00364 inline bool is_union();
00365
00366 friend void lex_start(Session*);
00367
00368 List<Item> *get_unit_column_types();
00369 };
00370
00371
00372
00373
00374 class Select_Lex : public Select_Lex_Node
00375 {
00376 public:
00377
00378 Select_Lex() :
00379 context(),
00380 db(0),
00381 where(0),
00382 having(0),
00383 cond_value(),
00384 having_value(),
00385 parent_lex(0),
00386 olap(UNSPECIFIED_OLAP_TYPE),
00387 table_list(),
00388 group_list(),
00389 item_list(),
00390 interval_list(),
00391 is_item_list_lookup(false),
00392 join(0),
00393 top_join_list(),
00394 join_list(0),
00395 embedding(0),
00396 sj_nests(),
00397 leaf_tables(0),
00398 type(optimizer::ST_PRIMARY),
00399 order_list(),
00400 gorder_list(0),
00401 select_limit(0),
00402 offset_limit(0),
00403 ref_pointer_array(0),
00404 select_n_having_items(0),
00405 cond_count(0),
00406 between_count(0),
00407 max_equal_elems(0),
00408 select_n_where_fields(0),
00409 parsing_place(NO_MATTER),
00410 with_sum_func(0),
00411 in_sum_expr(0),
00412 select_number(0),
00413 nest_level(0),
00414 inner_sum_func_list(0),
00415 with_wild(0),
00416 braces(0),
00417 having_fix_field(0),
00418 inner_refs_list(),
00419 n_sum_items(0),
00420 n_child_sum_items(0),
00421 explicit_limit(0),
00422 is_cross(false),
00423 subquery_in_having(0),
00424 is_correlated(0),
00425 exclude_from_table_unique_test(0),
00426 non_agg_fields(),
00427 cur_pos_in_select_list(0),
00428 prev_join_using(0),
00429 full_group_by_flag(),
00430 current_index_hint_type(INDEX_HINT_IGNORE),
00431 current_index_hint_clause(),
00432 index_hints(0)
00433 {
00434 }
00435
00436 Name_resolution_context context;
00437 const char *db;
00438
00439 Item *where;
00440
00441 Item *having;
00442
00443 Item::cond_result cond_value;
00444 Item::cond_result having_value;
00445
00446 LEX *parent_lex;
00447 olap_type olap;
00448
00449 SQL_LIST table_list;
00450 SQL_LIST group_list;
00451 List<Item> item_list;
00452 List<String> interval_list;
00453 bool is_item_list_lookup;
00454 Join *join;
00455 List<TableList> top_join_list;
00456 List<TableList> *join_list;
00457 TableList *embedding;
00458 List<TableList> sj_nests;
00459
00460
00461
00462
00463
00464 TableList *leaf_tables;
00465 drizzled::optimizer::select_type type;
00466
00467 SQL_LIST order_list;
00468 SQL_LIST *gorder_list;
00469 Item *select_limit, *offset_limit;
00470
00471 Item **ref_pointer_array;
00472
00473
00474
00475
00476
00477
00478 uint32_t select_n_having_items;
00479 uint32_t cond_count;
00480 uint32_t between_count;
00481 uint32_t max_equal_elems;
00482
00483
00484
00485
00486 uint32_t select_n_where_fields;
00487 enum_parsing_place parsing_place;
00488 bool with_sum_func;
00489
00490 uint32_t in_sum_expr;
00491 uint32_t select_number;
00492 int8_t nest_level;
00493 Item_sum *inner_sum_func_list;
00494 uint32_t with_wild;
00495 bool braces;
00496
00497 bool having_fix_field;
00498
00499 List<Item_outer_ref> inner_refs_list;
00500
00501 uint32_t n_sum_items;
00502
00503 uint32_t n_child_sum_items;
00504
00505
00506 bool explicit_limit;
00507
00508
00509 bool is_cross;
00510
00511
00512
00513
00514
00515 bool subquery_in_having;
00516
00517 bool is_correlated;
00518
00519 bool exclude_from_table_unique_test;
00520
00521 List<Item_field> non_agg_fields;
00522
00523 int cur_pos_in_select_list;
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538 List<String> *prev_join_using;
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 std::bitset<2> full_group_by_flag;
00549
00550 void init_query();
00551 void init_select();
00552 Select_Lex_Unit* master_unit();
00553 Select_Lex_Unit* first_inner_unit()
00554 {
00555 return (Select_Lex_Unit*) slave;
00556 }
00557 Select_Lex* outer_select();
00558 Select_Lex* next_select()
00559 {
00560 return (Select_Lex*) next;
00561 }
00562 Select_Lex* next_select_in_list()
00563 {
00564 return (Select_Lex*) link_next;
00565 }
00566 Select_Lex_Node** next_select_in_list_addr()
00567 {
00568 return &link_next;
00569 }
00570 Select_Lex* return_after_parsing()
00571 {
00572 return master_unit()->return_after_parsing();
00573 }
00574
00575 void mark_as_dependent(Select_Lex *last);
00576
00577 bool set_braces(bool value);
00578 bool inc_in_sum_expr();
00579 uint32_t get_in_sum_expr();
00580
00581 void add_item_to_list(Session *session, Item *item);
00582 void add_group_to_list(Session *session, Item *item, bool asc);
00583 void add_order_to_list(Session *session, Item *item, bool asc);
00584 TableList* add_table_to_list(Session *session,
00585 Table_ident *table,
00586 lex_string_t *alias,
00587 const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00588 thr_lock_type flags= TL_UNLOCK,
00589 List<Index_hint> *hints= 0,
00590 lex_string_t *option= 0);
00591 TableList* get_table_list();
00592 void init_nested_join(Session&);
00593 TableList *end_nested_join();
00594 TableList *nest_last_join(Session*);
00595 void add_joined_table(TableList *table);
00596 TableList *convert_right_join();
00597 List<Item>* get_item_list();
00598 void set_lock_for_tables(thr_lock_type lock_type);
00599 inline void init_order()
00600 {
00601 order_list.elements= 0;
00602 order_list.first= 0;
00603 order_list.next= (unsigned char**) &order_list.first;
00604 }
00605
00606
00607
00608
00609
00610
00611 void cut_subtree()
00612 {
00613 slave= 0;
00614 }
00615 bool test_limit();
00616
00617 friend void lex_start(Session*);
00618 void make_empty_select()
00619 {
00620 init_query();
00621 init_select();
00622 }
00623 void setup_ref_array(Session *session, uint32_t order_group_num);
00624 void print(Session *session, String *str);
00625 static void print_order(String *str, Order *order);
00626
00627 void print_limit(Session *session, String *str);
00628 void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
00629
00630
00631
00632
00633 bool cleanup();
00634
00635
00636
00637
00638 void cleanup_all_joins(bool full);
00639
00640 void set_index_hint_type(index_hint_type type, index_clause_map clause);
00641
00642
00643
00644
00645
00646 void add_index_hint(Session*, const char*);
00647
00648
00649 void alloc_index_hints (Session *session);
00650
00651 List<Index_hint>* pop_index_hints()
00652 {
00653 List<Index_hint> *hints= index_hints;
00654 index_hints= NULL;
00655 return hints;
00656 }
00657
00658 void clear_index_hints() { index_hints= NULL; }
00659
00660 private:
00661
00662 index_hint_type current_index_hint_type;
00663 index_clause_map current_index_hint_clause;
00664
00665 List<Index_hint> *index_hints;
00666 };
00667
00668 inline bool Select_Lex_Unit::is_union()
00669 {
00670 return first_select()->next_select() && first_select()->next_select()->linkage == UNION_TYPE;
00671 }
00672
00673 enum xa_option_words
00674 {
00675 XA_NONE,
00676 XA_JOIN,
00677 XA_RESUME,
00678 XA_ONE_PHASE,
00679 XA_SUSPEND,
00680 XA_FOR_MIGRATE
00681 };
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693 class Query_tables_list
00694 {
00695 public:
00696
00697 TableList *query_tables;
00698
00699 TableList **query_tables_last;
00700
00701
00702
00703
00704
00705
00706 TableList **query_tables_own_last;
00707
00708
00709 void reset_query_tables_list(bool init);
00710
00711
00712
00713
00714
00715
00716 void add_to_query_tables(TableList *table)
00717 {
00718 *(table->prev_global= query_tables_last)= table;
00719 query_tables_last= &table->next_global;
00720 }
00721
00722 TableList* first_not_own_table()
00723 {
00724 return ( query_tables_own_last ? *query_tables_own_last : 0);
00725 }
00726 void chop_off_not_own_tables()
00727 {
00728 if (query_tables_own_last)
00729 {
00730 *query_tables_own_last= 0;
00731 query_tables_last= query_tables_own_last;
00732 query_tables_own_last= 0;
00733 }
00734 }
00735 };
00736
00740 enum enum_comment_state
00741 {
00745 NO_COMMENT,
00750 PRESERVE_COMMENT,
00757 DISCARD_COMMENT
00758 };
00759
00760 }
00761
00762 namespace drizzled {
00763
00764
00765 class LEX : public Query_tables_list
00766 {
00767 public:
00768 Select_Lex_Unit unit;
00769 Select_Lex select_lex;
00770
00771 Select_Lex *current_select;
00772
00773 Select_Lex *all_selects_list;
00774
00775
00776 const char *length;
00777
00778 const char *dec;
00779
00786 lex_string_t name;
00787
00788 String *wild;
00789 file_exchange *exchange;
00790 select_result *result;
00791
00798 lex_string_t ident;
00799
00800 unsigned char* yacc_yyss, *yacc_yyvs;
00801
00802 Session *session;
00803 const charset_info_st *charset;
00804 bool text_string_is_7bit;
00805
00806 TableList *leaf_tables_insert;
00807
00808 List<Key_part_spec> col_list;
00809 List<Key_part_spec> ref_list;
00810 List<String> interval_list;
00811 List<Lex_Column> columns;
00812 List<Item> *insert_list,field_list,value_list,update_list;
00813 List<List_item> many_values;
00814 SetVarVector var_list;
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828 List<Name_resolution_context> context_stack;
00829
00830 SQL_LIST auxiliary_table_list;
00831 SQL_LIST save_list;
00832 CreateField *last_field;
00833 Item_sum *in_sum_func;
00834 plugin::Function *udf;
00835 uint32_t type;
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845 nesting_map allow_sum_func;
00846 enum_sql_command sql_command;
00847 statement::Statement *statement;
00848
00849
00850
00851
00852
00853
00854 bool expr_allows_subselect;
00855
00856 thr_lock_type lock_option;
00857 enum enum_duplicates duplicates;
00858 union {
00859 enum ha_rkey_function ha_rkey_mode;
00860 enum xa_option_words xa_opt;
00861 };
00862 sql_var_t option_type;
00863
00864 int nest_level;
00865 uint8_t describe;
00866
00867
00868
00869
00870 uint8_t derived_tables;
00871
00872
00873 bool ignore;
00874
00881 bool use_only_table_context;
00882
00883
00884 bool escape_used;
00885 bool is_lex_started;
00886
00887 LEX();
00888
00889
00890
00891
00892
00893
00894 virtual ~LEX();
00895
00896 TableList *unlink_first_table(bool *link_to_local);
00897 void link_first_table_back(TableList *first, bool link_to_local);
00898 void first_lists_tables_same();
00899
00900 void cleanup_after_one_table_open();
00901
00902 void push_context(Name_resolution_context *context)
00903 {
00904 context_stack.push_front(context);
00905 }
00906
00907 void pop_context()
00908 {
00909 context_stack.pop();
00910 }
00911
00912 Name_resolution_context *current_context()
00913 {
00914 return &context_stack.front();
00915 }
00916
00924 bool is_single_level_stmt()
00925 {
00926
00927
00928
00929
00930
00931 if (&select_lex == all_selects_list)
00932 {
00933 assert(!all_selects_list->next_select_in_list());
00934 return true;
00935 }
00936 return false;
00937 }
00938 bool is_cross;
00939 bool isCacheable()
00940 {
00941 return cacheable;
00942 }
00943 void setCacheable(bool val)
00944 {
00945 cacheable= val;
00946 }
00947
00948 void reset()
00949 {
00950 sum_expr_used= false;
00951 _exists= false;
00952 }
00953
00954 void setSumExprUsed()
00955 {
00956 sum_expr_used= true;
00957 }
00958
00959 bool isSumExprUsed()
00960 {
00961 return sum_expr_used;
00962 }
00963
00964 void start(Session *session);
00965 void end();
00966
00967 message::Table *table()
00968 {
00969 if (not _create_table)
00970 _create_table= new message::Table;
00971
00972 return _create_table;
00973 }
00974
00975 message::AlterTable *alter_table();
00976
00977 message::Table::Field *field()
00978 {
00979 return _create_field;
00980 }
00981
00982 void setField(message::Table::Field *arg)
00983 {
00984 _create_field= arg;
00985 }
00986
00987 void setExists()
00988 {
00989 _exists= true;
00990 }
00991
00992 bool exists() const
00993 {
00994 return _exists;
00995 }
00996
00997 private:
00998 bool cacheable;
00999 bool sum_expr_used;
01000 message::Table *_create_table;
01001 message::AlterTable *_alter_table;
01002 message::Table::Field *_create_field;
01003 bool _exists;
01004 };
01005
01006 extern void lex_start(Session *session);
01007
01012 }
01013
01014 #endif