Drizzled Public API Documentation

sql_lex.h
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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 namespace drizzled {
00037 
00038 namespace plugin { class Function; }
00039 namespace statement { class Statement; }
00040 
00041 class st_lex_symbol;
00042 class select_result_interceptor;
00043 
00044 /* YACC and LEX Definitions */
00045 
00046 /* These may not be declared yet */
00047 class Table_ident;
00048 class file_exchange;
00049 class Lex_Column;
00050 class Item_outer_ref;
00051 
00052 } /* namespace drizzled */
00053 
00054 /*
00055   The following hack is needed because mysql_yacc.cc does not define
00056   YYSTYPE before including this file
00057 */
00058 
00059 #ifdef DRIZZLE_SERVER
00060 /* set_var should change to set_var here ... */
00061 # include <drizzled/sys_var.h>
00062 # include <drizzled/item/func.h>
00063 # ifdef DRIZZLE_YACC
00064 #  define LEX_YYSTYPE void *
00065 # else
00066 #  if defined(DRIZZLE_LEX)
00067 #   include <drizzled/foreign_key.h>
00068 #   include <drizzled/lex_symbol.h>
00069 #   include <drizzled/comp_creator.h>
00070 #   include <drizzled/sql_yacc.h>
00071 #   define LEX_YYSTYPE YYSTYPE *
00072 #  else
00073 #   define LEX_YYSTYPE void *
00074 #  endif /* defined(DRIZZLE_LEX) */
00075 # endif /* DRIZZLE_YACC */
00076 #endif /* DRIZZLE_SERVER */
00077 
00078 // describe/explain types
00079 #define DESCRIBE_NORMAL   1
00080 #define DESCRIBE_EXTENDED 2
00081 
00082 #ifdef DRIZZLE_SERVER
00083 
00084 #define DERIVED_NONE  0
00085 #define DERIVED_SUBQUERY  1
00086 
00087 namespace drizzled
00088 {
00089 
00090 typedef List<Item> List_item;
00091 
00092 enum sub_select_type
00093 {
00094   UNSPECIFIED_TYPE,
00095   UNION_TYPE,
00096   INTERSECT_TYPE,
00097   EXCEPT_TYPE,
00098   GLOBAL_OPTIONS_TYPE,
00099   DERIVED_TABLE_TYPE,
00100   OLAP_TYPE
00101 };
00102 
00103 enum olap_type
00104 {
00105   UNSPECIFIED_OLAP_TYPE,
00106   CUBE_TYPE,
00107   ROLLUP_TYPE
00108 };
00109 
00110 /*
00111   The state of the lex parsing for selects
00112 
00113    master and slaves are pointers to select_lex.
00114    master is pointer to upper level node.
00115    slave is pointer to lower level node
00116    select_lex is a SELECT without union
00117    unit is container of either
00118      - One SELECT
00119      - UNION of selects
00120    select_lex and unit are both inherited form select_lex_node
00121    neighbors are two select_lex or units on the same level
00122 
00123    All select describing structures linked with following pointers:
00124    - list of neighbors (next/prev) (prev of first element point to slave
00125      pointer of upper structure)
00126      - For select this is a list of UNION's (or one element list)
00127      - For units this is a list of sub queries for the upper level select
00128 
00129    - pointer to master (master), which is
00130      If this is a unit
00131        - pointer to outer select_lex
00132      If this is a select_lex
00133        - pointer to outer unit structure for select
00134 
00135    - pointer to slave (slave), which is either:
00136      If this is a unit:
00137        - first SELECT that belong to this unit
00138      If this is a select_lex
00139        - first unit that belong to this SELECT (subquries or derived tables)
00140 
00141    - list of all select_lex (link_next/link_prev)
00142      This is to be used for things like derived tables creation, where we
00143      go through this list and create the derived tables.
00144 
00145    If unit contain several selects (UNION now, INTERSECT etc later)
00146    then it have special select_lex called fake_select_lex. It used for
00147    storing global parameters (like ORDER BY, LIMIT) and executing union.
00148    Subqueries used in global ORDER BY clause will be attached to this
00149    fake_select_lex, which will allow them correctly resolve fields of
00150    'upper' UNION and outer selects.
00151 
00152    For example for following query:
00153 
00154    select *
00155      from table1
00156      where table1.field IN (select * from table1_1_1 union
00157                             select * from table1_1_2)
00158      union
00159    select *
00160      from table2
00161      where table2.field=(select (select f1 from table2_1_1_1_1
00162                                    where table2_1_1_1_1.f2=table2_1_1.f3)
00163                            from table2_1_1
00164                            where table2_1_1.f1=table2.f2)
00165      union
00166    select * from table3;
00167 
00168    we will have following structure:
00169 
00170    select1: (select * from table1 ...)
00171    select2: (select * from table2 ...)
00172    select3: (select * from table3)
00173    select1.1.1: (select * from table1_1_1)
00174    ...
00175 
00176      main unit
00177      fake0
00178      select1 select2 select3
00179      |^^     |^
00180     s|||     ||master
00181     l|||     |+---------------------------------+
00182     a|||     +---------------------------------+|
00183     v|||master                         slave   ||
00184     e||+-------------------------+             ||
00185      V|            neighbor      |             V|
00186      unit1.1<+==================>unit1.2       unit2.1
00187      fake1.1
00188      select1.1.1 select 1.1.2    select1.2.1   select2.1.1
00189                                                |^
00190                                                ||
00191                                                V|
00192                                                unit2.1.1.1
00193                                                select2.1.1.1.1
00194 
00195 
00196    relation in main unit will be following:
00197    (bigger picture for:
00198       main unit
00199       fake0
00200       select1 select2 select3
00201    in the above picture)
00202 
00203          main unit
00204          |^^^^|fake_select_lex
00205          |||||+--------------------------------------------+
00206          ||||+--------------------------------------------+|
00207          |||+------------------------------+              ||
00208          ||+--------------+                |              ||
00209     slave||master         |                |              ||
00210          V|      neighbor |       neighbor |        master|V
00211          select1<========>select2<========>select3        fake0
00212 
00213     list of all select_lex will be following (as it will be constructed by
00214     parser):
00215 
00216     select1->select2->select3->select2.1.1->select 2.1.2->select2.1.1.1.1-+
00217                                                                           |
00218     +---------------------------------------------------------------------+
00219     |
00220     +->select1.1.1->select1.1.2
00221 
00222 */
00223 
00224 /*
00225     Base class for Select_Lex (Select_Lex) &
00226     Select_Lex_Unit (Select_Lex_Unit)
00227 */
00228 class LEX;
00229 class Select_Lex;
00230 class Select_Lex_Unit;
00231 class Select_Lex_Node {
00232 protected:
00233   Select_Lex_Node *next, **prev,   /* neighbor list */
00234     *master, *slave,                  /* vertical links */
00235     *link_next, **link_prev;          /* list of whole Select_Lex */
00236 public:
00237 
00238   uint64_t options;
00239 
00240   /*
00241     result of this query can't be cached, bit field, can be :
00242       UNCACHEABLE_DEPENDENT
00243       UNCACHEABLE_RAND
00244       UNCACHEABLE_SIDEEFFECT
00245       UNCACHEABLE_EXPLAIN
00246       UNCACHEABLE_PREPARE
00247   */
00248   std::bitset<8> uncacheable;
00249   sub_select_type linkage;
00250   bool no_table_names_allowed; /* used for global order by */
00251   bool no_error; /* suppress error message (convert it to warnings) */
00252 
00253   static void *operator new(size_t size)
00254   {
00255     return memory::sql_alloc(size);
00256   }
00257   static void *operator new(size_t size, memory::Root *mem_root)
00258   { return (void*) mem_root->alloc_root((uint32_t) size); }
00259   static void operator delete(void *, size_t)
00260   {  }
00261   static void operator delete(void *, memory::Root *)
00262   {}
00263   Select_Lex_Node(): linkage(UNSPECIFIED_TYPE) {}
00264   virtual ~Select_Lex_Node() {}
00265   inline Select_Lex_Node* get_master() { return master; }
00266   virtual void init_query();
00267   virtual void init_select();
00268   void include_down(Select_Lex_Node *upper);
00269   void include_neighbour(Select_Lex_Node *before);
00270   void include_standalone(Select_Lex_Node *sel, Select_Lex_Node **ref);
00271   void include_global(Select_Lex_Node **plink);
00272   void exclude();
00273 
00274   virtual Select_Lex_Unit* master_unit()= 0;
00275   virtual Select_Lex* outer_select()= 0;
00276   virtual Select_Lex* return_after_parsing()= 0;
00277 
00278   virtual bool set_braces(bool value);
00279   virtual bool inc_in_sum_expr();
00280   virtual uint32_t get_in_sum_expr();
00281   virtual TableList* get_table_list();
00282   virtual List<Item>* get_item_list();
00283   virtual TableList *add_table_to_list(Session *session, Table_ident *table,
00284                                        LEX_STRING *alias,
00285                                        const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00286                                        thr_lock_type flags= TL_UNLOCK,
00287                                        List<Index_hint> *hints= 0,
00288                                        LEX_STRING *option= 0);
00289   virtual void set_lock_for_tables(thr_lock_type)
00290   {}
00291 
00292   friend class Select_Lex_Unit;
00293   friend bool new_select(LEX *lex, bool move_down);
00294 private:
00295   void fast_exclude();
00296 };
00297 
00298 /*
00299    Select_Lex_Unit - unit of selects (UNION, INTERSECT, ...) group
00300    Select_Lexs
00301 */
00302 class Session;
00303 class select_result;
00304 class Join;
00305 class select_union;
00306 class Select_Lex_Unit: public Select_Lex_Node {
00307 protected:
00308   TableList result_table_list;
00309   select_union *union_result;
00310   Table *table; /* temporary table using for appending UNION results */
00311 
00312   select_result *result;
00313   uint64_t found_rows_for_union;
00314   bool saved_error;
00315 
00316 public:
00317   bool  prepared, // prepare phase already performed for UNION (unit)
00318     optimized, // optimize phase already performed for UNION (unit)
00319     executed, // already executed
00320     cleaned;
00321 
00322   // list of fields which points to temporary table for union
00323   List<Item> item_list;
00324   /*
00325     list of types of items inside union (used for union & derived tables)
00326 
00327     Item_type_holders from which this list consist may have pointers to Field,
00328     pointers is valid only after preparing SELECTS of this unit and before
00329     any SELECT of this unit execution
00330 
00331     TODO:
00332     Possibly this member should be protected, and its direct use replaced
00333     by get_unit_column_types(). Check the places where it is used.
00334   */
00335   List<Item> types;
00336   /*
00337     Pointer to 'last' select or pointer to unit where stored
00338     global parameters for union
00339   */
00340   Select_Lex *global_parameters;
00341   //node on wich we should return current_select pointer after parsing subquery
00342   Select_Lex *return_to;
00343   /* LIMIT clause runtime counters */
00344   ha_rows select_limit_cnt, offset_limit_cnt;
00345   /* not NULL if unit used in subselect, point to subselect item */
00346   Item_subselect *item;
00347   /* thread handler */
00348   Session *session;
00349   /*
00350     Select_Lex for hidden SELECT in onion which process global
00351     ORDER BY and LIMIT
00352   */
00353   Select_Lex *fake_select_lex;
00354 
00355   Select_Lex *union_distinct; /* pointer to the last UNION DISTINCT */
00356   bool describe; /* union exec() called for EXPLAIN */
00357 
00358   void init_query();
00359   Select_Lex_Unit* master_unit();
00360   Select_Lex* outer_select();
00361   Select_Lex* first_select()
00362   {
00363     return reinterpret_cast<Select_Lex*>(slave);
00364   }
00365   Select_Lex_Unit* next_unit()
00366   {
00367     return reinterpret_cast<Select_Lex_Unit*>(next);
00368   }
00369   Select_Lex* return_after_parsing() { return return_to; }
00370   void exclude_level();
00371   void exclude_tree();
00372 
00373   /* UNION methods */
00374   bool prepare(Session *session, select_result *result,
00375                uint64_t additional_options);
00376   bool exec();
00377   bool cleanup();
00378   inline void unclean() { cleaned= 0; }
00379   void reinit_exec_mechanism();
00380 
00381   void print(String *str);
00382 
00383   bool add_fake_select_lex(Session *session);
00384   void init_prepare_fake_select_lex(Session *session);
00385   bool change_result(select_result_interceptor *result,
00386                      select_result_interceptor *old_result);
00387   void set_limit(Select_Lex *values);
00388   void set_session(Session *session_arg) { session= session_arg; }
00389   inline bool is_union ();
00390 
00391   friend void lex_start(Session *session);
00392 
00393   List<Item> *get_unit_column_types();
00394 };
00395 
00396 /*
00397   Select_Lex - store information of parsed SELECT statment
00398 */
00399 class Select_Lex: public Select_Lex_Node
00400 {
00401 public:
00402 
00403   Select_Lex() :
00404     context(),
00405     db(0),
00406     where(0),
00407     having(0),
00408     cond_value(),
00409     having_value(),
00410     parent_lex(0),
00411     olap(UNSPECIFIED_OLAP_TYPE),
00412     table_list(),
00413     group_list(),
00414     item_list(),
00415     interval_list(),
00416     is_item_list_lookup(false),
00417     join(0),
00418     top_join_list(),
00419     join_list(0),
00420     embedding(0),
00421     sj_nests(),
00422     leaf_tables(0),
00423     type(optimizer::ST_PRIMARY),
00424     order_list(),
00425     gorder_list(0),
00426     select_limit(0),
00427     offset_limit(0),
00428     ref_pointer_array(0),
00429     select_n_having_items(0),
00430     cond_count(0),
00431     between_count(0),
00432     max_equal_elems(0),
00433     select_n_where_fields(0),
00434     parsing_place(NO_MATTER),
00435     with_sum_func(0),
00436     in_sum_expr(0),
00437     select_number(0),
00438     nest_level(0),
00439     inner_sum_func_list(0),
00440     with_wild(0),
00441     braces(0),
00442     having_fix_field(0),
00443     inner_refs_list(),
00444     n_sum_items(0),
00445     n_child_sum_items(0),
00446     explicit_limit(0),
00447     is_cross(false),
00448     subquery_in_having(0),
00449     is_correlated(0),
00450     exclude_from_table_unique_test(0),
00451     non_agg_fields(),
00452     cur_pos_in_select_list(0),
00453     prev_join_using(0),
00454     full_group_by_flag(),
00455     current_index_hint_type(INDEX_HINT_IGNORE),
00456     current_index_hint_clause(),
00457     index_hints(0)
00458   {
00459   }
00460 
00461   Name_resolution_context context;
00462   char *db;
00463   /* An Item representing the WHERE clause */
00464   Item *where;
00465   /* An Item representing the HAVING clause */
00466   Item *having;
00467   /* Saved values of the WHERE and HAVING clauses*/
00468   Item::cond_result cond_value;
00469   Item::cond_result having_value;
00470   /* point on lex in which it was created, used in view subquery detection */
00471   LEX *parent_lex;
00472   olap_type olap;
00473   /* FROM clause - points to the beginning of the TableList::next_local list. */
00474   SQL_LIST table_list;
00475   SQL_LIST group_list; /* GROUP BY clause. */
00476   List<Item> item_list;  /* list of fields & expressions */
00477   List<String> interval_list;
00478   bool is_item_list_lookup;
00479   Join *join; /* after Join::prepare it is pointer to corresponding JOIN */
00480   List<TableList> top_join_list; /* join list of the top level          */
00481   List<TableList> *join_list;    /* list for the currently parsed join  */
00482   TableList *embedding;          /* table embedding to the above list   */
00483   List<TableList> sj_nests;
00484   /*
00485     Beginning of the list of leaves in a FROM clause, where the leaves
00486     inlcude all base tables including view tables. The tables are connected
00487     by TableList::next_leaf, so leaf_tables points to the left-most leaf.
00488   */
00489   TableList *leaf_tables;
00490   drizzled::optimizer::select_type type; /* type of select for EXPLAIN */
00491 
00492   SQL_LIST order_list;                /* ORDER clause */
00493   SQL_LIST *gorder_list;
00494   Item *select_limit, *offset_limit;  /* LIMIT clause parameters */
00495   /* Arrays of pointers to top elements of all_fields list */
00496   Item **ref_pointer_array;
00497 
00498   /*
00499     number of items in select_list and HAVING clause used to get number
00500     bigger then can be number of entries that will be added to all item
00501     list during split_sum_func
00502   */
00503   uint32_t select_n_having_items;
00504   uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
00505   uint32_t between_count; /* number of between predicates in where/having/on      */
00506   uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
00507   /*
00508     Number of fields used in select list or where clause of current select
00509     and all inner subselects.
00510   */
00511   uint32_t select_n_where_fields;
00512   enum_parsing_place parsing_place; /* where we are parsing expression */
00513   bool with_sum_func;   /* sum function indicator */
00514 
00515   uint32_t in_sum_expr;
00516   uint32_t select_number; /* number of select (used for EXPLAIN) */
00517   int8_t nest_level;     /* nesting level of select */
00518   Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
00519   uint32_t with_wild; /* item list contain '*' */
00520   bool braces;    /* SELECT ... UNION (SELECT ... ) <- this braces */
00521   /* true when having fix field called in processing of this SELECT */
00522   bool having_fix_field;
00523   /* List of references to fields referenced from inner selects */
00524   List<Item_outer_ref> inner_refs_list;
00525   /* Number of Item_sum-derived objects in this SELECT */
00526   uint32_t n_sum_items;
00527   /* Number of Item_sum-derived objects in children and descendant SELECTs */
00528   uint32_t n_child_sum_items;
00529 
00530   /* explicit LIMIT clause was used */
00531   bool explicit_limit;
00532 
00533   /* explicit CROSS JOIN was used */
00534   bool is_cross;
00535 
00536   /*
00537     there are subquery in HAVING clause => we can't close tables before
00538     query processing end even if we use temporary table
00539   */
00540   bool subquery_in_having;
00541   /* true <=> this SELECT is correlated w.r.t. some ancestor select */
00542   bool is_correlated;
00543   /* exclude this select from check of unique_table() */
00544   bool exclude_from_table_unique_test;
00545   /* List of fields that aren't under an aggregate function */
00546   List<Item_field> non_agg_fields;
00547   /* index in the select list of the expression currently being fixed */
00548   int cur_pos_in_select_list;
00549 
00550   /*
00551     This is a copy of the original JOIN USING list that comes from
00552     the parser. The parser :
00553       1. Sets the natural_join of the second TableList in the join
00554          and the Select_Lex::prev_join_using.
00555       2. Makes a parent TableList and sets its is_natural_join/
00556        join_using_fields members.
00557       3. Uses the wrapper TableList as a table in the upper level.
00558     We cannot assign directly to join_using_fields in the parser because
00559     at stage (1.) the parent TableList is not constructed yet and
00560     the assignment will override the JOIN USING fields of the lower level
00561     joins on the right.
00562   */
00563   List<String> *prev_join_using;
00564   /*
00565     Bitmap used in the ONLY_FULL_GROUP_BY_MODE to prevent mixture of aggregate
00566     functions and non aggregated fields when GROUP BY list is absent.
00567     Bits:
00568       0 - non aggregated fields are used in this select,
00569           defined as NON_AGG_FIELD_USED.
00570       1 - aggregate functions are used in this select,
00571           defined as SUM_FUNC_USED.
00572   */
00573   std::bitset<2> full_group_by_flag;
00574 
00575   void init_query();
00576   void init_select();
00577   Select_Lex_Unit* master_unit();
00578   Select_Lex_Unit* first_inner_unit()
00579   {
00580     return (Select_Lex_Unit*) slave;
00581   }
00582   Select_Lex* outer_select();
00583   Select_Lex* next_select()
00584   {
00585     return (Select_Lex*) next;
00586   }
00587   Select_Lex* next_select_in_list()
00588   {
00589     return (Select_Lex*) link_next;
00590   }
00591   Select_Lex_Node** next_select_in_list_addr()
00592   {
00593     return &link_next;
00594   }
00595   Select_Lex* return_after_parsing()
00596   {
00597     return master_unit()->return_after_parsing();
00598   }
00599 
00600   void mark_as_dependent(Select_Lex *last);
00601 
00602   bool set_braces(bool value);
00603   bool inc_in_sum_expr();
00604   uint32_t get_in_sum_expr();
00605 
00606   bool add_item_to_list(Session *session, Item *item);
00607   bool add_group_to_list(Session *session, Item *item, bool asc);
00608   bool add_order_to_list(Session *session, Item *item, bool asc);
00609   TableList* add_table_to_list(Session *session,
00610                                Table_ident *table,
00611                                LEX_STRING *alias,
00612                                const std::bitset<NUM_OF_TABLE_OPTIONS>& table_options,
00613                                thr_lock_type flags= TL_UNLOCK,
00614                                List<Index_hint> *hints= 0,
00615                                LEX_STRING *option= 0);
00616   TableList* get_table_list();
00617   bool init_nested_join(Session *session);
00618   TableList *end_nested_join(Session *session);
00619   TableList *nest_last_join(Session *session);
00620   void add_joined_table(TableList *table);
00621   TableList *convert_right_join();
00622   List<Item>* get_item_list();
00623   void set_lock_for_tables(thr_lock_type lock_type);
00624   inline void init_order()
00625   {
00626     order_list.elements= 0;
00627     order_list.first= 0;
00628     order_list.next= (unsigned char**) &order_list.first;
00629   }
00630   /*
00631     This method created for reiniting LEX in admin_table() and can be
00632     used only if you are going remove all Select_Lex & units except belonger
00633     to LEX (LEX::unit & LEX::select, for other purposes there are
00634     Select_Lex_Unit::exclude_level & Select_Lex_Unit::exclude_tree
00635   */
00636   void cut_subtree()
00637   {
00638     slave= 0;
00639   }
00640   bool test_limit();
00641 
00642   friend void lex_start(Session *session);
00643   void make_empty_select()
00644   {
00645     init_query();
00646     init_select();
00647   }
00648   bool setup_ref_array(Session *session, uint32_t order_group_num);
00649   void print(Session *session, String *str);
00650   static void print_order(String *str, Order *order);
00651 
00652   void print_limit(Session *session, String *str);
00653   void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
00654   /*
00655     Destroy the used execution plan (JOIN) of this subtree (this
00656     Select_Lex and all nested Select_Lexes and Select_Lex_Units).
00657   */
00658   bool cleanup();
00659   /*
00660     Recursively cleanup the join of this select lex and of all nested
00661     select lexes.
00662   */
00663   void cleanup_all_joins(bool full);
00664 
00665   void set_index_hint_type(index_hint_type type, index_clause_map clause);
00666 
00667   /*
00668    Add a index hint to the tagged list of hints. The type and clause of the
00669    hint will be the current ones (set by set_index_hint())
00670   */
00671   bool add_index_hint (Session *session, char *str, uint32_t length);
00672 
00673   /* make a list to hold index hints */
00674   void alloc_index_hints (Session *session);
00675   /* read and clear the index hints */
00676   List<Index_hint>* pop_index_hints(void)
00677   {
00678     List<Index_hint> *hints= index_hints;
00679     index_hints= NULL;
00680     return hints;
00681   }
00682 
00683   void clear_index_hints(void) { index_hints= NULL; }
00684 
00685 private:
00686   /* current index hint kind. used in filling up index_hints */
00687   index_hint_type current_index_hint_type;
00688   index_clause_map current_index_hint_clause;
00689   /* a list of USE/FORCE/IGNORE INDEX */
00690   List<Index_hint> *index_hints;
00691 };
00692 
00693 inline bool Select_Lex_Unit::is_union ()
00694 {
00695   return first_select()->next_select() &&
00696     first_select()->next_select()->linkage == UNION_TYPE;
00697 }
00698 
00699 enum xa_option_words
00700 {
00701   XA_NONE
00702 , XA_JOIN
00703 , XA_RESUME
00704 , XA_ONE_PHASE
00705 , XA_SUSPEND
00706 , XA_FOR_MIGRATE
00707 };
00708 
00709 extern const LEX_STRING null_lex_str;
00710 
00711 /*
00712   Class representing list of all tables used by statement.
00713   It also contains information about stored functions used by statement
00714   since during its execution we may have to add all tables used by its
00715   stored functions/triggers to this list in order to pre-open and lock
00716   them.
00717 
00718   Also used by st_lex::reset_n_backup/restore_backup_query_tables_list()
00719   methods to save and restore this information.
00720 */
00721 class Query_tables_list
00722 {
00723 public:
00724   /* Global list of all tables used by this statement */
00725   TableList *query_tables;
00726   /* Pointer to next_global member of last element in the previous list. */
00727   TableList **query_tables_last;
00728   /*
00729     If non-0 then indicates that query requires prelocking and points to
00730     next_global member of last own element in query table list (i.e. last
00731     table which was not added to it as part of preparation to prelocking).
00732     0 - indicates that this query does not need prelocking.
00733   */
00734   TableList **query_tables_own_last;
00735 
00736   /*
00737     These constructor and destructor serve for creation/destruction
00738     of Query_tables_list instances which are used as backup storage.
00739   */
00740   Query_tables_list() {}
00741   virtual ~Query_tables_list() {}
00742 
00743   /* Initializes (or resets) Query_tables_list object for "real" use. */
00744   void reset_query_tables_list(bool init);
00745 
00746   /*
00747     Direct addition to the list of query tables.
00748     If you are using this function, you must ensure that the table
00749     object, in particular table->db member, is initialized.
00750   */
00751   void add_to_query_tables(TableList *table)
00752   {
00753     *(table->prev_global= query_tables_last)= table;
00754     query_tables_last= &table->next_global;
00755   }
00756   /* Return pointer to first not-own table in query-tables or 0 */
00757   TableList* first_not_own_table()
00758   {
00759     return ( query_tables_own_last ? *query_tables_own_last : 0);
00760   }
00761   void chop_off_not_own_tables()
00762   {
00763     if (query_tables_own_last)
00764     {
00765       *query_tables_own_last= 0;
00766       query_tables_last= query_tables_own_last;
00767       query_tables_own_last= 0;
00768     }
00769   }
00770 };
00771 
00775 enum enum_comment_state
00776 {
00780   NO_COMMENT,
00785   PRESERVE_COMMENT,
00792   DISCARD_COMMENT
00793 };
00794 
00795 } /* namespace drizzled */
00796 
00797 #include <drizzled/lex_input_stream.h>
00798 
00799 namespace drizzled
00800 {
00801 
00802 /* The state of the lex parsing. This is saved in the Session struct */
00803 class LEX : public Query_tables_list
00804 {
00805 public:
00806   Select_Lex_Unit unit;                         /* most upper unit */
00807   Select_Lex select_lex;                        /* first Select_Lex */
00808   /* current Select_Lex in parsing */
00809   Select_Lex *current_select;
00810   /* list of all Select_Lex */
00811   Select_Lex *all_selects_list;
00812 
00813   /* This is the "scale" for DECIMAL (S,P) notation */
00814   char *length;
00815   /* This is the decimal precision in DECIMAL(S,P) notation */
00816   char *dec;
00817 
00824   LEX_STRING name;
00825   /* The string literal used in a LIKE expression */
00826   String *wild;
00827   file_exchange *exchange;
00828   select_result *result;
00829 
00836   LEX_STRING ident;
00837 
00838   unsigned char* yacc_yyss, *yacc_yyvs;
00839   /* The owning Session of this LEX */
00840   Session *session;
00841   const CHARSET_INFO *charset;
00842   bool text_string_is_7bit;
00843   /* store original leaf_tables for INSERT SELECT and PS/SP */
00844   TableList *leaf_tables_insert;
00845 
00846   List<Key_part_spec> col_list;
00847   List<Key_part_spec> ref_list;
00848   List<String>        interval_list;
00849   List<Lex_Column>    columns;
00850   List<Item>        *insert_list,field_list,value_list,update_list;
00851   List<List_item>     many_values;
00852   SetVarVector  var_list;
00853   /*
00854     A stack of name resolution contexts for the query. This stack is used
00855     at parse time to set local name resolution contexts for various parts
00856     of a query. For example, in a JOIN ... ON (some_condition) clause the
00857     Items in 'some_condition' must be resolved only against the operands
00858     of the the join, and not against the whole clause. Similarly, Items in
00859     subqueries should be resolved against the subqueries (and outer queries).
00860     The stack is used in the following way: when the parser detects that
00861     all Items in some clause need a local context, it creates a new context
00862     and pushes it on the stack. All newly created Items always store the
00863     top-most context in the stack. Once the parser leaves the clause that
00864     required a local context, the parser pops the top-most context.
00865   */
00866   List<Name_resolution_context> context_stack;
00867 
00868   SQL_LIST auxiliary_table_list;
00869   SQL_LIST save_list;
00870   CreateField *last_field;
00871   Item_sum *in_sum_func;
00872   plugin::Function *udf;
00873   uint32_t type;
00874   /*
00875     This variable is used in post-parse stage to declare that sum-functions,
00876     or functions which have sense only if GROUP BY is present, are allowed.
00877     For example in a query
00878     SELECT ... FROM ...WHERE MIN(i) == 1 GROUP BY ... HAVING MIN(i) > 2
00879     MIN(i) in the WHERE clause is not allowed in the opposite to MIN(i)
00880     in the HAVING clause. Due to possible nesting of select construct
00881     the variable can contain 0 or 1 for each nest level.
00882   */
00883   nesting_map allow_sum_func;
00884   enum_sql_command sql_command;
00885   statement::Statement *statement;
00886   /*
00887     Usually `expr` rule of yacc is quite reused but some commands better
00888     not support subqueries which comes standard with this rule, like
00889     KILL, HA_READ, CREATE/ALTER EVENT etc. Set this to `false` to get
00890     syntax error back.
00891   */
00892   bool expr_allows_subselect;
00893 
00894   thr_lock_type lock_option;
00895   enum enum_duplicates duplicates;
00896   union {
00897     enum ha_rkey_function ha_rkey_mode;
00898     enum xa_option_words xa_opt;
00899   };
00900   sql_var_t option_type;
00901 
00902   int nest_level;
00903   uint8_t describe;
00904   /*
00905     A flag that indicates what kinds of derived tables are present in the
00906     query (0 if no derived tables, otherwise DERIVED_SUBQUERY).
00907   */
00908   uint8_t derived_tables;
00909 
00910   /* Was the IGNORE symbol found in statement */
00911   bool ignore;
00912 
00919   bool use_only_table_context;
00920 
00921   /* Was the ESCAPE keyword used? */
00922   bool escape_used;
00923   bool is_lex_started; /* If lex_start() did run. For debugging. */
00924 
00925   LEX();
00926 
00927   /* Note that init and de-init mostly happen in lex_start and lex_end
00928      and not here. This is because LEX isn't delete/new for each new
00929      statement in a session. It's re-used by doing lex_end, lex_start
00930      in sql_lex.cc
00931   */
00932   virtual ~LEX();
00933 
00934   TableList *unlink_first_table(bool *link_to_local);
00935   void link_first_table_back(TableList *first, bool link_to_local);
00936   void first_lists_tables_same();
00937 
00938   void cleanup_after_one_table_open();
00939 
00940   bool push_context(Name_resolution_context *context)
00941   {
00942     return context_stack.push_front(context);
00943   }
00944 
00945   void pop_context()
00946   {
00947     context_stack.pop();
00948   }
00949 
00950   bool copy_db_to(char **p_db, size_t *p_db_length) const;
00951 
00952   Name_resolution_context *current_context()
00953   {
00954     return &context_stack.front();
00955   }
00956 
00964   bool is_single_level_stmt()
00965   {
00966     /*
00967       This check exploits the fact that the last added to all_select_list is
00968       on its top. So select_lex (as the first added) will be at the tail
00969       of the list.
00970     */
00971     if (&select_lex == all_selects_list)
00972     {
00973       assert(!all_selects_list->next_select_in_list());
00974       return true;
00975     }
00976     return false;
00977   }
00978   bool is_cross; // CROSS keyword was used
00979   bool isCacheable()
00980   {
00981     return cacheable;
00982   }
00983   void setCacheable(bool val)
00984   {
00985     cacheable= val;
00986   }
00987 
00988   void reset()
00989   {
00990     sum_expr_used= false;
00991     _exists= false;
00992   }
00993 
00994   void setSumExprUsed()
00995   {
00996     sum_expr_used= true;
00997   }
00998 
00999   bool isSumExprUsed()
01000   {
01001     return sum_expr_used;
01002   }
01003 
01004   void start(Session *session);
01005   void end();
01006 
01007   message::Table *table()
01008   {
01009     if (not _create_table)
01010       _create_table= new message::Table;
01011 
01012     return _create_table;
01013   }
01014 
01015   message::Table::Field *field()
01016   {
01017     return _create_field;
01018   }
01019 
01020   void setField(message::Table::Field *arg)
01021   {
01022     _create_field= arg;
01023   }
01024 
01025   void setExists()
01026   {
01027     _exists= true;
01028   }
01029 
01030   bool exists() const
01031   {
01032     return _exists;
01033   }
01034 
01035 private:
01036   bool cacheable;
01037   bool sum_expr_used;
01038   message::Table *_create_table;
01039   message::Table::Field *_create_field;
01040   bool _exists;
01041 };
01042 
01043 extern void lex_start(Session *session);
01044 extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);
01045 extern bool is_lex_native_function(const LEX_STRING *name);
01046 
01047 bool check_for_sql_keyword(drizzled::st_lex_symbol const&);
01048 bool check_for_sql_keyword(drizzled::lex_string_t const&);
01049 
01054 } /* namespace drizzled */
01055 
01056 #endif /* DRIZZLE_SERVER */