~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.h

  • Committer: Monty Taylor
  • Date: 2009-01-30 21:02:37 UTC
  • mto: (779.7.3 devel)
  • mto: This revision was merged to the branch mainline in revision 823.
  • Revision ID: mordred@inaugust.com-20090130210237-3n6ld8a9jc084jko
Commented out a test in subselect_sj - I think it might be a regression. Jay?

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000-2006 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
15
19
 
16
20
#ifndef DRIZZLE_SERVER_SQL_LEX_H
17
21
#define DRIZZLE_SERVER_SQL_LEX_H
20
24
  @defgroup Semantic_Analysis Semantic Analysis
21
25
*/
22
26
 
 
27
#include <drizzled/sql_udf.h>
 
28
#include <drizzled/name_resolution_context.h>
 
29
#include <drizzled/item/subselect.h>
 
30
#include <drizzled/item/param.h>
 
31
#include <drizzled/item/outer_ref.h>
 
32
#include <drizzled/table_list.h>
 
33
#include <drizzled/function/math/real.h>
 
34
#include <drizzled/alter_drop.h>
 
35
#include <drizzled/alter_column.h>
 
36
#include <drizzled/key.h>
 
37
#include <drizzled/foreign_key.h>
 
38
#include <drizzled/item/param.h>
 
39
 
 
40
class select_result_interceptor;
 
41
class virtual_column_info;
 
42
 
23
43
/* YACC and LEX Definitions */
24
44
 
25
45
/* These may not be declared yet */
26
46
class Table_ident;
27
47
class sql_exchange;
28
48
class LEX_COLUMN;
 
49
class Item_outer_ref;
29
50
 
30
 
#ifdef DRIZZLE_SERVER
31
51
/*
32
52
  The following hack is needed because mysql_yacc.cc does not define
33
53
  YYSTYPE before including this file
34
54
*/
35
55
 
36
 
#include "set_var.h"
37
 
 
38
 
#ifdef DRIZZLE_YACC
39
 
#define LEX_YYSTYPE void *
40
 
#else
41
 
#if defined(DRIZZLE_LEX)
42
 
#include "lex_symbol.h"
43
 
#include "sql_yacc.h"
44
 
#define LEX_YYSTYPE YYSTYPE *
45
 
#else
46
 
#define LEX_YYSTYPE void *
47
 
#endif
48
 
#endif
49
 
#endif
50
 
 
51
 
/*
52
 
  When a command is added here, be sure it's also added in mysqld.cc
53
 
  in "struct show_var_st status_vars[]= {" ...
54
 
 
55
 
  If the command returns a result set or is not allowed in stored
56
 
  functions or triggers, please also make sure that
57
 
  sp_get_flags_for_command (sp_head.cc) returns proper flags for the
58
 
  added SQLCOM_.
59
 
*/
60
 
 
61
 
enum enum_sql_command {
62
 
  SQLCOM_SELECT, SQLCOM_CREATE_TABLE, SQLCOM_CREATE_INDEX, SQLCOM_ALTER_TABLE,
63
 
  SQLCOM_UPDATE, SQLCOM_INSERT, SQLCOM_INSERT_SELECT,
64
 
  SQLCOM_DELETE, SQLCOM_TRUNCATE, SQLCOM_DROP_TABLE, SQLCOM_DROP_INDEX,
65
 
  SQLCOM_SHOW_DATABASES, SQLCOM_SHOW_TABLES, SQLCOM_SHOW_FIELDS,
66
 
  SQLCOM_SHOW_KEYS, SQLCOM_SHOW_VARIABLES, SQLCOM_SHOW_STATUS,
67
 
  SQLCOM_SHOW_ENGINE_LOGS, SQLCOM_SHOW_ENGINE_STATUS, SQLCOM_SHOW_ENGINE_MUTEX,
68
 
  SQLCOM_SHOW_PROCESSLIST, SQLCOM_SHOW_MASTER_STAT, SQLCOM_SHOW_SLAVE_STAT,
69
 
  SQLCOM_SHOW_CREATE, SQLCOM_SHOW_CHARSETS,
70
 
  SQLCOM_SHOW_COLLATIONS, SQLCOM_SHOW_CREATE_DB, SQLCOM_SHOW_TABLE_STATUS,
71
 
  SQLCOM_LOAD,SQLCOM_SET_OPTION,SQLCOM_LOCK_TABLES,SQLCOM_UNLOCK_TABLES,
72
 
  SQLCOM_CHANGE_DB, SQLCOM_CREATE_DB, SQLCOM_DROP_DB, SQLCOM_ALTER_DB,
73
 
  SQLCOM_REPAIR, SQLCOM_REPLACE, SQLCOM_REPLACE_SELECT,
74
 
  SQLCOM_OPTIMIZE, SQLCOM_CHECK,
75
 
  SQLCOM_ASSIGN_TO_KEYCACHE,
76
 
  SQLCOM_FLUSH, SQLCOM_KILL, SQLCOM_ANALYZE,
77
 
  SQLCOM_ROLLBACK, SQLCOM_ROLLBACK_TO_SAVEPOINT,
78
 
  SQLCOM_COMMIT, SQLCOM_SAVEPOINT, SQLCOM_RELEASE_SAVEPOINT,
79
 
  SQLCOM_SLAVE_START, SQLCOM_SLAVE_STOP,
80
 
  SQLCOM_BEGIN, SQLCOM_CHANGE_MASTER,
81
 
  SQLCOM_RENAME_TABLE,  
82
 
  SQLCOM_RESET, SQLCOM_PURGE, SQLCOM_PURGE_BEFORE, SQLCOM_SHOW_BINLOGS,
83
 
  SQLCOM_SHOW_OPEN_TABLES,
84
 
  SQLCOM_SHOW_SLAVE_HOSTS, SQLCOM_DELETE_MULTI, SQLCOM_UPDATE_MULTI,
85
 
  SQLCOM_SHOW_BINLOG_EVENTS,
86
 
  SQLCOM_SHOW_WARNS, SQLCOM_EMPTY_QUERY, SQLCOM_SHOW_ERRORS,
87
 
  SQLCOM_CHECKSUM,
88
 
  SQLCOM_BINLOG_BASE64_EVENT,
89
 
  SQLCOM_SHOW_PLUGINS,
90
 
  /*
91
 
    When a command is added here, be sure it's also added in mysqld.cc
92
 
    in "struct show_var_st status_vars[]= {" ...
93
 
  */
94
 
  /* This should be the last !!! */
95
 
  SQLCOM_END
96
 
};
 
56
#ifdef DRIZZLE_SERVER
 
57
# include <drizzled/set_var.h>
 
58
# include <drizzled/item/func.h>
 
59
# ifdef DRIZZLE_YACC
 
60
#  define LEX_YYSTYPE void *
 
61
# else
 
62
#  if defined(DRIZZLE_LEX)
 
63
#   include <drizzled/lex_symbol.h>
 
64
#   include <drizzled/sql_yacc.h>
 
65
#   define LEX_YYSTYPE YYSTYPE *
 
66
#  else
 
67
#   define LEX_YYSTYPE void *
 
68
#  endif /* defined(DRIZZLE_LEX) */
 
69
# endif /* DRIZZLE_YACC */
 
70
#endif /* DRIZZLE_SERVER */
97
71
 
98
72
// describe/explain types
99
73
#define DESCRIBE_NORMAL         1
125
99
    Enum is used for making it possible to detect if the user
126
100
    changed variable or if it should be left at old value
127
101
   */
128
 
  enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE}
129
 
    ssl, ssl_verify_server_cert, heartbeat_opt;
130
 
  char *ssl_key, *ssl_cert, *ssl_ca, *ssl_capath, *ssl_cipher;
 
102
  enum {LEX_MI_UNCHANGED, LEX_MI_DISABLE, LEX_MI_ENABLE} heartbeat_opt;
131
103
  char *relay_log_name;
132
104
  uint32_t relay_log_pos;
133
105
} LEX_MASTER_INFO;
139
111
  EXCEPT_TYPE, GLOBAL_OPTIONS_TYPE, DERIVED_TABLE_TYPE, OLAP_TYPE
140
112
};
141
113
 
142
 
enum olap_type 
 
114
enum olap_type
143
115
{
144
116
  UNSPECIFIED_OLAP_TYPE, CUBE_TYPE, ROLLUP_TYPE
145
117
};
149
121
  NO_TABLESPACE_OP, DISCARD_TABLESPACE, IMPORT_TABLESPACE
150
122
};
151
123
 
152
 
/* 
 
124
/*
153
125
  String names used to print a statement with index hints.
154
126
  Keep in sync with index_hint_type.
155
127
*/
156
128
extern const char * index_hint_type_name[];
157
 
typedef uchar index_clause_map;
 
129
typedef unsigned char index_clause_map;
158
130
 
159
131
/*
160
132
  Bits in index_clause_map : one for each possible FOR clause in
175
147
  enum index_hint_type type;
176
148
  /* Where the hit applies to. A bitmask of INDEX_HINT_MASK_<place> values */
177
149
  index_clause_map clause;
178
 
  /* 
179
 
    The index name. Empty (str=NULL) name represents an empty list 
180
 
    USE INDEX () clause 
181
 
  */ 
 
150
  /*
 
151
    The index name. Empty (str=NULL) name represents an empty list
 
152
    USE INDEX () clause
 
153
  */
182
154
  LEX_STRING key_name;
183
155
 
184
156
  Index_hint (enum index_hint_type type_arg, index_clause_map clause_arg,
189
161
    key_name.length= length;
190
162
  }
191
163
 
192
 
  void print(THD *thd, String *str);
193
 
}; 
194
 
 
195
 
/* 
196
 
  The state of the lex parsing for selects 
197
 
   
 
164
  void print(Session *session, String *str);
 
165
};
 
166
 
 
167
/*
 
168
  The state of the lex parsing for selects
 
169
 
198
170
   master and slaves are pointers to select_lex.
199
171
   master is pointer to upper level node.
200
172
   slave is pointer to lower level node
306
278
 
307
279
*/
308
280
 
309
 
/* 
310
 
    Base class for st_select_lex (SELECT_LEX) & 
 
281
/*
 
282
    Base class for st_select_lex (SELECT_LEX) &
311
283
    st_select_lex_unit (SELECT_LEX_UNIT)
312
284
*/
313
 
struct st_lex;
 
285
class LEX;
314
286
class st_select_lex;
315
287
class st_select_lex_unit;
316
288
class st_select_lex_node {
341
313
  }
342
314
  static void *operator new(size_t size, MEM_ROOT *mem_root)
343
315
  { return (void*) alloc_root(mem_root, (uint32_t) size); }
344
 
  static void operator delete(void *ptr __attribute__((unused)),
345
 
                              size_t size __attribute__((unused)))
 
316
  static void operator delete(void *, size_t)
346
317
  { TRASH(ptr, size); }
347
 
  static void operator delete(void *ptr __attribute__((unused)),
348
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
318
  static void operator delete(void *, MEM_ROOT *)
349
319
  {}
350
320
  st_select_lex_node(): linkage(UNSPECIFIED_TYPE) {}
351
321
  virtual ~st_select_lex_node() {}
365
335
  virtual bool set_braces(bool value);
366
336
  virtual bool inc_in_sum_expr();
367
337
  virtual uint32_t get_in_sum_expr();
368
 
  virtual TABLE_LIST* get_table_list();
 
338
  virtual TableList* get_table_list();
369
339
  virtual List<Item>* get_item_list();
370
340
  virtual uint32_t get_table_join_options();
371
 
  virtual TABLE_LIST *add_table_to_list(THD *thd, Table_ident *table,
 
341
  virtual TableList *add_table_to_list(Session *session, Table_ident *table,
372
342
                                        LEX_STRING *alias,
373
343
                                        uint32_t table_options,
374
344
                                        thr_lock_type flags= TL_UNLOCK,
375
345
                                        List<Index_hint> *hints= 0,
376
346
                                        LEX_STRING *option= 0);
377
 
  virtual void set_lock_for_tables(thr_lock_type lock_type __attribute__((unused)))
 
347
  virtual void set_lock_for_tables(thr_lock_type)
378
348
  {}
379
349
 
380
350
  friend class st_select_lex_unit;
381
 
  friend bool mysql_new_select(struct st_lex *lex, bool move_down);
 
351
  friend bool mysql_new_select(LEX *lex, bool move_down);
382
352
private:
383
353
  void fast_exclude();
384
354
};
385
355
typedef class st_select_lex_node SELECT_LEX_NODE;
386
356
 
387
 
/* 
388
 
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group 
 
357
/*
 
358
   SELECT_LEX_UNIT - unit of selects (UNION, INTERSECT, ...) group
389
359
   SELECT_LEXs
390
360
*/
391
 
class THD;
 
361
class Session;
392
362
class select_result;
393
363
class JOIN;
394
364
class select_union;
395
365
class st_select_lex_unit: public st_select_lex_node {
396
366
protected:
397
 
  TABLE_LIST result_table_list;
 
367
  TableList result_table_list;
398
368
  select_union *union_result;
399
 
  TABLE *table; /* temporary table using for appending UNION results */
 
369
  Table *table; /* temporary table using for appending UNION results */
400
370
 
401
371
  select_result *result;
402
372
  uint64_t found_rows_for_union;
412
382
  List<Item> item_list;
413
383
  /*
414
384
    list of types of items inside union (used for union & derived tables)
415
 
    
 
385
 
416
386
    Item_type_holders from which this list consist may have pointers to Field,
417
387
    pointers is valid only after preparing SELECTS of this unit and before
418
388
    any SELECT of this unit execution
434
404
  /* not NULL if unit used in subselect, point to subselect item */
435
405
  Item_subselect *item;
436
406
  /* thread handler */
437
 
  THD *thd;
 
407
  Session *session;
438
408
  /*
439
409
    SELECT_LEX for hidden SELECT in onion which process global
440
410
    ORDER BY and LIMIT
449
419
  st_select_lex* outer_select();
450
420
  st_select_lex* first_select()
451
421
  {
452
 
    return my_reinterpret_cast(st_select_lex*)(slave);
 
422
    return reinterpret_cast<st_select_lex*>(slave);
453
423
  }
454
424
  st_select_lex_unit* next_unit()
455
425
  {
456
 
    return my_reinterpret_cast(st_select_lex_unit*)(next);
 
426
    return reinterpret_cast<st_select_lex_unit*>(next);
457
427
  }
458
428
  st_select_lex* return_after_parsing() { return return_to; }
459
429
  void exclude_level();
460
430
  void exclude_tree();
461
431
 
462
432
  /* UNION methods */
463
 
  bool prepare(THD *thd, select_result *result, uint32_t additional_options);
 
433
  bool prepare(Session *session, select_result *result, uint32_t additional_options);
464
434
  bool exec();
465
435
  bool cleanup();
466
436
  inline void unclean() { cleaned= 0; }
468
438
 
469
439
  void print(String *str, enum_query_type query_type);
470
440
 
471
 
  bool add_fake_select_lex(THD *thd);
472
 
  void init_prepare_fake_select_lex(THD *thd);
 
441
  bool add_fake_select_lex(Session *session);
 
442
  void init_prepare_fake_select_lex(Session *session);
473
443
  bool change_result(select_result_interceptor *result,
474
444
                     select_result_interceptor *old_result);
475
445
  void set_limit(st_select_lex *values);
476
 
  void set_thd(THD *thd_arg) { thd= thd_arg; }
477
 
  inline bool is_union (); 
 
446
  void set_session(Session *session_arg) { session= session_arg; }
 
447
  inline bool is_union ();
478
448
 
479
 
  friend void lex_start(THD *thd);
 
449
  friend void lex_start(Session *session);
480
450
  friend int subselect_union_engine::exec();
481
451
 
482
452
  List<Item> *get_unit_column_types();
493
463
  Name_resolution_context context;
494
464
  char *db;
495
465
  Item *where, *having;                         /* WHERE & HAVING clauses */
496
 
  Item *prep_where; /* saved WHERE clause for prepared statement processing */
497
 
  Item *prep_having;/* saved HAVING clause for prepared statement processing */
498
466
  /* Saved values of the WHERE and HAVING clauses*/
499
467
  Item::cond_result cond_value, having_value;
500
468
  /* point on lex in which it was created, used in view subquery detection */
501
 
  st_lex *parent_lex;
 
469
  LEX *parent_lex;
502
470
  enum olap_type olap;
503
 
  /* FROM clause - points to the beginning of the TABLE_LIST::next_local list. */
 
471
  /* FROM clause - points to the beginning of the TableList::next_local list. */
504
472
  SQL_LIST            table_list;
505
473
  SQL_LIST            group_list; /* GROUP BY clause. */
506
474
  List<Item>          item_list;  /* list of fields & expressions */
507
475
  List<String>        interval_list;
508
476
  bool                is_item_list_lookup;
509
 
  /* 
510
 
    Despite their names, the following are used in unions. This should 
 
477
  /*
 
478
    Despite their names, the following are used in unions. This should
511
479
    be rewritten. -Brian
512
480
  */
513
481
  List<Item_real_func> *ftfunc_list;
514
482
  List<Item_real_func> ftfunc_list_alloc;
515
483
  JOIN *join; /* after JOIN::prepare it is pointer to corresponding JOIN */
516
 
  List<TABLE_LIST> top_join_list; /* join list of the top level          */
517
 
  List<TABLE_LIST> *join_list;    /* list for the currently parsed join  */
518
 
  TABLE_LIST *embedding;          /* table embedding to the above list   */
519
 
  List<TABLE_LIST> sj_nests;
 
484
  List<TableList> top_join_list; /* join list of the top level          */
 
485
  List<TableList> *join_list;    /* list for the currently parsed join  */
 
486
  TableList *embedding;          /* table embedding to the above list   */
 
487
  List<TableList> sj_nests;
520
488
  /*
521
489
    Beginning of the list of leaves in a FROM clause, where the leaves
522
490
    inlcude all base tables including view tables. The tables are connected
523
 
    by TABLE_LIST::next_leaf, so leaf_tables points to the left-most leaf.
 
491
    by TableList::next_leaf, so leaf_tables points to the left-most leaf.
524
492
  */
525
 
  TABLE_LIST *leaf_tables;
 
493
  TableList *leaf_tables;
526
494
  const char *type;               /* type of select for EXPLAIN          */
527
495
 
528
496
  SQL_LIST order_list;                /* ORDER clause */
539
507
  uint32_t select_n_having_items;
540
508
  uint32_t cond_count;    /* number of arguments of and/or/xor in where/having/on */
541
509
  uint32_t between_count; /* number of between predicates in where/having/on      */
542
 
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */   
 
510
  uint32_t max_equal_elems; /* maximal number of elements in multiple equalities  */
543
511
  /*
544
512
    Number of fields used in select list or where clause of current select
545
513
    and all inner subselects.
547
515
  uint32_t select_n_where_fields;
548
516
  enum_parsing_place parsing_place; /* where we are parsing expression */
549
517
  bool with_sum_func;   /* sum function indicator */
550
 
  /* 
551
 
    PS or SP cond natural joins was alredy processed with permanent
552
 
    arena and all additional items which we need alredy stored in it
553
 
  */
554
 
  bool conds_processed_with_permanent_arena;
555
518
 
556
519
  uint32_t table_join_options;
557
520
  uint32_t in_sum_expr;
558
521
  uint32_t select_number; /* number of select (used for EXPLAIN) */
559
522
  int nest_level;     /* nesting level of select */
560
 
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */ 
 
523
  Item_sum *inner_sum_func_list; /* list of sum func in nested selects */
561
524
  uint32_t with_wild; /* item list contain '*' */
562
525
  bool  braces;         /* SELECT ... UNION (SELECT ... ) <- this braces */
563
526
  /* true when having fix field called in processing of this SELECT */
578
541
  bool subquery_in_having;
579
542
  /* true <=> this SELECT is correlated w.r.t. some ancestor select */
580
543
  bool is_correlated;
581
 
  /*
582
 
    This variable is required to ensure proper work of subqueries and
583
 
    stored procedures. Generally, one should use the states of
584
 
    Query_arena to determine if it's a statement prepare or first
585
 
    execution of a stored procedure. However, in case when there was an
586
 
    error during the first execution of a stored procedure, the SP body
587
 
    is not expelled from the SP cache. Therefore, a deeply nested
588
 
    subquery might be left unoptimized. So we need this per-subquery
589
 
    variable to inidicate the optimization/execution state of every
590
 
    subquery. Prepared statements work OK in that regard, as in
591
 
    case of an error during prepare the PS is not created.
592
 
  */
593
 
  bool first_execution;
594
 
  bool first_cond_optimization;
595
544
  /* exclude this select from check of unique_table() */
596
545
  bool exclude_from_table_unique_test;
597
546
  /* List of fields that aren't under an aggregate function */
600
549
  int cur_pos_in_select_list;
601
550
 
602
551
  List<udf_func>     udf_list;                  /* udf function calls stack */
603
 
  /* 
 
552
  /*
604
553
    This is a copy of the original JOIN USING list that comes from
605
554
    the parser. The parser :
606
 
      1. Sets the natural_join of the second TABLE_LIST in the join
 
555
      1. Sets the natural_join of the second TableList in the join
607
556
         and the st_select_lex::prev_join_using.
608
 
      2. Makes a parent TABLE_LIST and sets its is_natural_join/
 
557
      2. Makes a parent TableList and sets its is_natural_join/
609
558
       join_using_fields members.
610
 
      3. Uses the wrapper TABLE_LIST as a table in the upper level.
 
559
      3. Uses the wrapper TableList as a table in the upper level.
611
560
    We cannot assign directly to join_using_fields in the parser because
612
 
    at stage (1.) the parent TABLE_LIST is not constructed yet and
 
561
    at stage (1.) the parent TableList is not constructed yet and
613
562
    the assignment will override the JOIN USING fields of the lower level
614
563
    joins on the right.
615
564
  */
627
576
  void init_query();
628
577
  void init_select();
629
578
  st_select_lex_unit* master_unit();
630
 
  st_select_lex_unit* first_inner_unit() 
631
 
  { 
632
 
    return (st_select_lex_unit*) slave; 
 
579
  st_select_lex_unit* first_inner_unit()
 
580
  {
 
581
    return (st_select_lex_unit*) slave;
633
582
  }
634
583
  st_select_lex* outer_select();
635
584
  st_select_lex* next_select() { return (st_select_lex*) next; }
636
 
  st_select_lex* next_select_in_list() 
 
585
  st_select_lex* next_select_in_list()
637
586
  {
638
587
    return (st_select_lex*) link_next;
639
588
  }
652
601
  bool inc_in_sum_expr();
653
602
  uint32_t get_in_sum_expr();
654
603
 
655
 
  bool add_item_to_list(THD *thd, Item *item);
656
 
  bool add_group_to_list(THD *thd, Item *item, bool asc);
657
 
  bool add_order_to_list(THD *thd, Item *item, bool asc);
658
 
  TABLE_LIST* add_table_to_list(THD *thd, Table_ident *table,
 
604
  bool add_item_to_list(Session *session, Item *item);
 
605
  bool add_group_to_list(Session *session, Item *item, bool asc);
 
606
  bool add_order_to_list(Session *session, Item *item, bool asc);
 
607
  TableList* add_table_to_list(Session *session, Table_ident *table,
659
608
                                LEX_STRING *alias,
660
609
                                uint32_t table_options,
661
610
                                thr_lock_type flags= TL_UNLOCK,
662
611
                                List<Index_hint> *hints= 0,
663
612
                                LEX_STRING *option= 0);
664
 
  TABLE_LIST* get_table_list();
665
 
  bool init_nested_join(THD *thd);
666
 
  TABLE_LIST *end_nested_join(THD *thd);
667
 
  TABLE_LIST *nest_last_join(THD *thd);
668
 
  void add_joined_table(TABLE_LIST *table);
669
 
  TABLE_LIST *convert_right_join();
 
613
  TableList* get_table_list();
 
614
  bool init_nested_join(Session *session);
 
615
  TableList *end_nested_join(Session *session);
 
616
  TableList *nest_last_join(Session *session);
 
617
  void add_joined_table(TableList *table);
 
618
  TableList *convert_right_join();
670
619
  List<Item>* get_item_list();
671
620
  uint32_t get_table_join_options();
672
621
  void set_lock_for_tables(thr_lock_type lock_type);
674
623
  {
675
624
    order_list.elements= 0;
676
625
    order_list.first= 0;
677
 
    order_list.next= (uchar**) &order_list.first;
 
626
    order_list.next= (unsigned char**) &order_list.first;
678
627
  }
679
628
  /*
680
629
    This method created for reiniting LEX in mysql_admin_table() and can be
685
634
  void cut_subtree() { slave= 0; }
686
635
  bool test_limit();
687
636
 
688
 
  friend void lex_start(THD *thd);
 
637
  friend void lex_start(Session *session);
689
638
  st_select_lex() : n_sum_items(0), n_child_sum_items(0) {}
690
639
  void make_empty_select()
691
640
  {
692
641
    init_query();
693
642
    init_select();
694
643
  }
695
 
  bool setup_ref_array(THD *thd, uint32_t order_group_num);
696
 
  void print(THD *thd, String *str, enum_query_type query_type);
 
644
  bool setup_ref_array(Session *session, uint32_t order_group_num);
 
645
  void print(Session *session, String *str, enum_query_type query_type);
697
646
  static void print_order(String *str,
698
 
                          ORDER *order,
 
647
                          order_st *order,
699
648
                          enum_query_type query_type);
700
 
  void print_limit(THD *thd, String *str, enum_query_type query_type);
701
 
  void fix_prepare_information(THD *thd, Item **conds, Item **having_conds);
 
649
  void print_limit(Session *session, String *str, enum_query_type query_type);
 
650
  void fix_prepare_information(Session *session, Item **conds, Item **having_conds);
702
651
  /*
703
652
    Destroy the used execution plan (JOIN) of this subtree (this
704
653
    SELECT_LEX and all nested SELECT_LEXes and SELECT_LEX_UNITs).
712
661
 
713
662
  void set_index_hint_type(enum index_hint_type type, index_clause_map clause);
714
663
 
715
 
  /* 
 
664
  /*
716
665
   Add a index hint to the tagged list of hints. The type and clause of the
717
 
   hint will be the current ones (set by set_index_hint()) 
 
666
   hint will be the current ones (set by set_index_hint())
718
667
  */
719
 
  bool add_index_hint (THD *thd, char *str, uint32_t length);
 
668
  bool add_index_hint (Session *session, char *str, uint32_t length);
720
669
 
721
670
  /* make a list to hold index hints */
722
 
  void alloc_index_hints (THD *thd);
 
671
  void alloc_index_hints (Session *session);
723
672
  /* read and clear the index hints */
724
 
  List<Index_hint>* pop_index_hints(void) 
 
673
  List<Index_hint>* pop_index_hints(void)
725
674
  {
726
675
    List<Index_hint> *hints= index_hints;
727
676
    index_hints= NULL;
730
679
 
731
680
  void clear_index_hints(void) { index_hints= NULL; }
732
681
 
733
 
private:  
 
682
private:
734
683
  /* current index hint kind. used in filling up index_hints */
735
684
  enum index_hint_type current_index_hint_type;
736
685
  index_clause_map current_index_hint_clause;
740
689
typedef class st_select_lex SELECT_LEX;
741
690
 
742
691
inline bool st_select_lex_unit::is_union ()
743
 
744
 
  return first_select()->next_select() && 
 
692
{
 
693
  return first_select()->next_select() &&
745
694
    first_select()->next_select()->linkage == UNION_TYPE;
746
695
}
747
696
 
767
716
#define ALTER_FOREIGN_KEY         (1L << 31)
768
717
 
769
718
/**
770
 
  @brief Parsing data for CREATE or ALTER TABLE.
 
719
  @brief Parsing data for CREATE or ALTER Table.
771
720
 
772
721
  This structure contains a list of columns or indexes to be created,
773
722
  altered or dropped.
840
789
{
841
790
public:
842
791
  /* Global list of all tables used by this statement */
843
 
  TABLE_LIST *query_tables;
 
792
  TableList *query_tables;
844
793
  /* Pointer to next_global member of last element in the previous list. */
845
 
  TABLE_LIST **query_tables_last;
 
794
  TableList **query_tables_last;
846
795
  /*
847
796
    If non-0 then indicates that query requires prelocking and points to
848
797
    next_global member of last own element in query table list (i.e. last
849
798
    table which was not added to it as part of preparation to prelocking).
850
799
    0 - indicates that this query does not need prelocking.
851
800
  */
852
 
  TABLE_LIST **query_tables_own_last;
853
 
  /*
854
 
    Set of stored routines called by statement.
855
 
    (Note that we use lazy-initialization for this hash).
856
 
  */
857
 
  enum { START_SROUTINES_HASH_SIZE= 16 };
858
 
  HASH sroutines;
859
 
  /*
860
 
    List linking elements of 'sroutines' set. Allows you to add new elements
861
 
    to this set as you iterate through the list of existing elements.
862
 
    'sroutines_list_own_last' is pointer to ::next member of last element of
863
 
    this list which represents routine which is explicitly used by query.
864
 
    'sroutines_list_own_elements' number of explicitly used routines.
865
 
    We use these two members for restoring of 'sroutines_list' to the state
866
 
    in which it was right after query parsing.
867
 
  */
868
 
  SQL_LIST sroutines_list;
869
 
  uchar    **sroutines_list_own_last;
870
 
  uint32_t     sroutines_list_own_elements;
 
801
  TableList **query_tables_own_last;
871
802
 
872
803
  /*
873
804
    These constructor and destructor serve for creation/destruction
889
820
    If you are using this function, you must ensure that the table
890
821
    object, in particular table->db member, is initialized.
891
822
  */
892
 
  void add_to_query_tables(TABLE_LIST *table)
 
823
  void add_to_query_tables(TableList *table)
893
824
  {
894
825
    *(table->prev_global= query_tables_last)= table;
895
826
    query_tables_last= &table->next_global;
896
827
  }
897
828
  /* Return pointer to first not-own table in query-tables or 0 */
898
 
  TABLE_LIST* first_not_own_table()
 
829
  TableList* first_not_own_table()
899
830
  {
900
831
    return ( query_tables_own_last ? *query_tables_own_last : 0);
901
832
  }
908
839
      query_tables_own_last= 0;
909
840
    }
910
841
  }
911
 
 
912
 
  /**
913
 
     Has the parser/scanner detected that this statement is unsafe?
914
 
   */
915
 
  inline bool is_stmt_unsafe() const {
916
 
    return binlog_stmt_flags & (1U << BINLOG_STMT_FLAG_UNSAFE);
917
 
  }
918
 
 
919
 
  /**
920
 
     Flag the current (top-level) statement as unsafe.
921
 
 
922
 
     The flag will be reset after the statement has finished.
923
 
 
924
 
   */
925
 
  inline void set_stmt_unsafe() {
926
 
    binlog_stmt_flags|= (1U << BINLOG_STMT_FLAG_UNSAFE);
927
 
  }
928
 
 
929
 
  inline void clear_stmt_unsafe() {
930
 
    binlog_stmt_flags&= ~(1U << BINLOG_STMT_FLAG_UNSAFE);
931
 
  }
932
 
 
933
 
  /**
934
 
    true if the parsed tree contains references to stored procedures
935
 
    or functions, false otherwise
936
 
  */
937
 
  bool uses_stored_routines() const
938
 
  { return sroutines_list.elements != 0; }
939
 
 
940
 
private:
941
 
  enum enum_binlog_stmt_flag {
942
 
    BINLOG_STMT_FLAG_UNSAFE,
943
 
    BINLOG_STMT_FLAG_COUNT
944
 
  };
945
 
 
946
 
  /*
947
 
    Tells if the parsing stage detected properties of the statement,
948
 
    for example: that some items require row-based binlogging to give
949
 
    a reliable binlog/replication, or if we will use stored functions
950
 
    or triggers which themselves need require row-based binlogging.
951
 
  */
952
 
  uint32_t binlog_stmt_flags;
953
842
};
954
843
 
955
844
 
960
849
 
961
850
struct st_parsing_options
962
851
{
963
 
  bool allows_variable;
964
 
  bool allows_select_into;
965
852
  bool allows_select_procedure;
966
 
  bool allows_derived;
967
853
 
968
854
  st_parsing_options() { reset(); }
969
855
  void reset();
1011
897
class Lex_input_stream
1012
898
{
1013
899
public:
1014
 
  Lex_input_stream(THD *thd, const char* buff, unsigned int length);
 
900
  Lex_input_stream(Session *session, const char* buff, unsigned int length);
1015
901
  ~Lex_input_stream();
1016
902
 
1017
903
  /**
1239
1125
    return m_body_utf8_ptr - m_body_utf8;
1240
1126
  }
1241
1127
 
1242
 
  void body_utf8_start(THD *thd, const char *begin_ptr);
 
1128
  void body_utf8_start(Session *session, const char *begin_ptr);
1243
1129
  void body_utf8_append(const char *ptr);
1244
1130
  void body_utf8_append(const char *ptr, const char *end_ptr);
1245
 
  void body_utf8_append_literal(THD *thd,
 
1131
  void body_utf8_append_literal(Session *session,
1246
1132
                                const LEX_STRING *txt,
1247
1133
                                const CHARSET_INFO * const txt_cs,
1248
1134
                                const char *end_ptr);
1249
1135
 
1250
1136
  /** Current thread. */
1251
 
  THD *m_thd;
 
1137
  Session *m_session;
1252
1138
 
1253
1139
  /** Current line number. */
1254
1140
  uint32_t yylineno;
1338
1224
  const char *found_semicolon;
1339
1225
 
1340
1226
  /** Token character bitmaps, to detect 7bit strings. */
1341
 
  uchar tok_bitmap;
 
1227
  unsigned char tok_bitmap;
1342
1228
 
1343
1229
  /** SQL_MODE = IGNORE_SPACE. */
1344
1230
  bool ignore_space;
1350
1236
    Starting position of the TEXT_STRING or IDENT in the pre-processed
1351
1237
    buffer.
1352
1238
 
1353
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1239
    NOTE: this member must be used within DRIZZLElex() function only.
1354
1240
  */
1355
1241
  const char *m_cpp_text_start;
1356
1242
 
1358
1244
    Ending position of the TEXT_STRING or IDENT in the pre-processed
1359
1245
    buffer.
1360
1246
 
1361
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1247
    NOTE: this member must be used within DRIZZLElex() function only.
1362
1248
    */
1363
1249
  const char *m_cpp_text_end;
1364
1250
 
1365
1251
  /**
1366
1252
    Character set specified by the character-set-introducer.
1367
1253
 
1368
 
    NOTE: this member must be used within MYSQLlex() function only.
 
1254
    NOTE: this member must be used within DRIZZLElex() function only.
1369
1255
  */
1370
1256
  const CHARSET_INFO *m_underscore_cs;
1371
1257
};
1372
1258
 
1373
1259
 
1374
 
/* The state of the lex parsing. This is saved in the THD struct */
 
1260
/* The state of the lex parsing. This is saved in the Session struct */
1375
1261
 
1376
 
typedef struct st_lex : public Query_tables_list
 
1262
class LEX : public Query_tables_list
1377
1263
{
 
1264
public:
1378
1265
  SELECT_LEX_UNIT unit;                         /* most upper unit */
1379
1266
  SELECT_LEX select_lex;                        /* first SELECT_LEX */
1380
1267
  /* current SELECT_LEX in parsing */
1386
1273
  LEX_STRING name;
1387
1274
  char *help_arg;
1388
1275
  char* to_log;                                 /* For PURGE MASTER LOGS TO */
1389
 
  char* x509_subject,*x509_issuer,*ssl_cipher;
1390
1276
  String *wild;
1391
1277
  sql_exchange *exchange;
1392
1278
  select_result *result;
1393
1279
  Item *default_value, *on_update_value;
1394
1280
  LEX_STRING comment, ident;
1395
1281
  XID *xid;
1396
 
  uchar* yacc_yyss, *yacc_yyvs;
1397
 
  THD *thd;
1398
 
 
1399
 
  /* maintain a list of used plugins for this LEX */
1400
 
  DYNAMIC_ARRAY plugins;
1401
 
  plugin_ref plugins_static_buffer[INITIAL_LEX_PLUGIN_LIST_SIZE];
 
1282
  unsigned char* yacc_yyss, *yacc_yyvs;
 
1283
  Session *session;
 
1284
  virtual_column_info *vcol_info;
1402
1285
 
1403
1286
  const CHARSET_INFO *charset;
1404
1287
  bool text_string_is_7bit;
1405
1288
  /* store original leaf_tables for INSERT SELECT and PS/SP */
1406
 
  TABLE_LIST *leaf_tables_insert;
 
1289
  TableList *leaf_tables_insert;
1407
1290
 
1408
1291
  List<Key_part_spec> col_list;
1409
1292
  List<Key_part_spec> ref_list;
1437
1320
  HA_CREATE_INFO create_info;
1438
1321
  KEY_CREATE_INFO key_create_info;
1439
1322
  LEX_MASTER_INFO mi;                           // used by CHANGE MASTER
1440
 
  LEX_SERVER_OPTIONS server_options;
1441
1323
  uint32_t type;
1442
1324
  /*
1443
1325
    This variable is used in post-parse stage to declare that sum-functions,
1457
1339
    syntax error back.
1458
1340
  */
1459
1341
  bool expr_allows_subselect;
 
1342
  /*
 
1343
    A special command "PARSE_VCOL_EXPR" is defined for the parser
 
1344
    to translate an expression statement of a virtual column \
 
1345
    (stored in the *.frm file as a string) into an Item object.
 
1346
    The following flag is used to prevent other applications to use
 
1347
    this command.
 
1348
  */
 
1349
  bool parse_vcol_expr;
1460
1350
 
1461
1351
  thr_lock_type lock_option;
1462
1352
  enum enum_duplicates duplicates;
1465
1355
  union {
1466
1356
    enum ha_rkey_function ha_rkey_mode;
1467
1357
    enum xa_option_words xa_opt;
1468
 
    bool lock_transactional;            /* For LOCK TABLE ... IN ... MODE */
 
1358
    bool lock_transactional;            /* For LOCK Table ... IN ... MODE */
1469
1359
  };
1470
1360
  enum enum_var_type option_type;
1471
1361
 
1476
1366
  enum Foreign_key::fk_match_opt fk_match_option;
1477
1367
  enum Foreign_key::fk_option fk_update_opt;
1478
1368
  enum Foreign_key::fk_option fk_delete_opt;
1479
 
  uint32_t slave_thd_opt, start_transaction_opt;
 
1369
  uint32_t slave_session_opt, start_transaction_opt;
1480
1370
  int nest_level;
1481
1371
  /*
1482
1372
    In LEX representing update which were transformed to multi-update
1492
1382
  uint8_t derived_tables;
1493
1383
  bool drop_if_exists, drop_temporary, local_file, one_shot_set;
1494
1384
  bool autocommit;
1495
 
  bool verbose, no_write_to_binlog;
 
1385
  bool verbose;
1496
1386
 
1497
1387
  bool tx_chain, tx_release;
1498
1388
  bool subqueries, ignore;
1500
1390
  Alter_info alter_info;
1501
1391
 
1502
1392
  /*
1503
 
    field_list was created for view and should be removed before PS/SP
1504
 
    rexecuton
1505
 
  */
1506
 
  bool empty_field_list_on_rset;
1507
 
 
1508
 
  /*
1509
1393
    Pointers to part of LOAD DATA statement that should be rewritten
1510
1394
    during replication ("LOCAL 'filename' REPLACE INTO" part).
1511
1395
  */
1512
1396
  const char *fname_start;
1513
1397
  const char *fname_end;
1514
 
  
 
1398
 
1515
1399
  /**
1516
 
    During name resolution search only in the table list given by 
 
1400
    During name resolution search only in the table list given by
1517
1401
    Name_resolution_context::first_name_resolution_table and
1518
1402
    Name_resolution_context::last_name_resolution_table
1519
 
    (see Item_field::fix_fields()). 
 
1403
    (see Item_field::fix_fields()).
1520
1404
  */
1521
1405
  bool use_only_table_context;
1522
 
  
 
1406
 
1523
1407
  bool escape_used;
1524
1408
  bool is_lex_started; /* If lex_start() did run. For debugging. */
1525
1409
 
1526
 
  st_lex();
 
1410
  LEX();
1527
1411
 
1528
 
  virtual ~st_lex()
 
1412
  virtual ~LEX()
1529
1413
  {
1530
1414
    destroy_query_tables_list();
1531
 
    plugin_unlock_list(NULL, (plugin_ref *)plugins.buffer, plugins.elements);
1532
 
    delete_dynamic(&plugins);
1533
1415
  }
1534
1416
 
1535
 
  TABLE_LIST *unlink_first_table(bool *link_to_local);
1536
 
  void link_first_table_back(TABLE_LIST *first, bool link_to_local);
 
1417
  TableList *unlink_first_table(bool *link_to_local);
 
1418
  void link_first_table_back(TableList *first, bool link_to_local);
1537
1419
  void first_lists_tables_same();
1538
1420
 
1539
 
  bool can_be_merged();
1540
 
  bool can_use_merged();
1541
 
  bool can_not_use_merged();
1542
1421
  bool only_view_structure();
1543
1422
  bool need_correct_ident();
1544
1423
 
1561
1440
    return context_stack.head();
1562
1441
  }
1563
1442
  /*
1564
 
    Restore the LEX and THD in case of a parse error.
 
1443
    Restore the LEX and Session in case of a parse error.
1565
1444
  */
1566
 
  static void cleanup_lex_after_parse_error(THD *thd);
1567
 
 
1568
 
  void reset_n_backup_query_tables_list(Query_tables_list *backup);
1569
 
  void restore_backup_query_tables_list(Query_tables_list *backup);
1570
 
 
1571
 
  bool table_or_sp_used();
 
1445
  static void cleanup_lex_after_parse_error(Session *session);
1572
1446
 
1573
1447
  /**
1574
1448
    @brief check if the statement is a single-level join
1575
1449
    @return result of the check
1576
 
      @retval true  The statement doesn't contain subqueries, unions and 
 
1450
      @retval true  The statement doesn't contain subqueries, unions and
1577
1451
                    stored procedure calls.
1578
1452
      @retval false There are subqueries, UNIONs or stored procedure calls.
1579
1453
  */
1580
 
  bool is_single_level_stmt() 
1581
 
  { 
1582
 
    /* 
 
1454
  bool is_single_level_stmt()
 
1455
  {
 
1456
    /*
1583
1457
      This check exploits the fact that the last added to all_select_list is
1584
 
      on its top. So select_lex (as the first added) will be at the tail 
 
1458
      on its top. So select_lex (as the first added) will be at the tail
1585
1459
      of the list.
1586
 
    */ 
1587
 
    if (&select_lex == all_selects_list && !sroutines.records)
 
1460
    */
 
1461
    if (&select_lex == all_selects_list)
1588
1462
    {
1589
1463
      assert(!all_selects_list->next_select_in_list());
1590
1464
      return true;
1591
1465
    }
1592
1466
    return false;
1593
1467
  }
1594
 
} LEX;
 
1468
};
1595
1469
 
1596
 
struct st_lex_local: public st_lex
 
1470
struct st_lex_local: public LEX
1597
1471
{
1598
1472
  static void *operator new(size_t size) throw()
1599
1473
  {
1603
1477
  {
1604
1478
    return (void*) alloc_root(mem_root, (uint32_t) size);
1605
1479
  }
1606
 
  static void operator delete(void *ptr __attribute__((unused)),
1607
 
                              size_t size __attribute__((unused)))
 
1480
  static void operator delete(void *, size_t)
1608
1481
  { TRASH(ptr, size); }
1609
 
  static void operator delete(void *ptr __attribute__((unused)),
1610
 
                              MEM_ROOT *mem_root __attribute__((unused)))
 
1482
  static void operator delete(void *, MEM_ROOT *)
1611
1483
  { /* Never called */ }
1612
1484
};
1613
1485
 
1614
 
extern void lex_init(void);
1615
 
extern void lex_free(void);
1616
 
extern void lex_start(THD *thd);
 
1486
extern void lex_start(Session *session);
1617
1487
extern void lex_end(LEX *lex);
1618
1488
 
1619
1489
extern void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str);