~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
19
#define DRIZZLE_LEX 1
20
 
#include "drizzled/server_includes.h"
21
 
#include "drizzled/item/num.h"
22
 
#include "drizzled/error.h"
23
 
#include "drizzled/session.h"
24
 
#include "drizzled/sql_base.h"
25
 
#include "drizzled/lookup_symbol.h"
26
 
#include "drizzled/index_hint.h"
27
 
 
28
 
#include <ctype.h>
29
 
 
30
 
using namespace std;
31
 
 
32
 
static int lex_one_token(void *arg, void *yysession);
33
 
 
 
20
#include <drizzled/server_includes.h>
 
21
 
 
22
static int lex_one_token(void *arg, void *yythd);
34
23
 
35
24
/*
36
25
  We are using pointer to this variable for distinguishing between assignment
44
33
*/
45
34
const LEX_STRING null_lex_str= {NULL, 0};
46
35
 
 
36
/* Longest standard keyword name */
 
37
 
 
38
#define TOCK_NAME_LENGTH 24
 
39
 
 
40
/*
 
41
  The following data is based on the latin1 character set, and is only
 
42
  used when comparing keywords
 
43
*/
 
44
 
 
45
static unsigned char to_upper_lex[]=
 
46
{
 
47
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
 
48
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
 
49
   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 
50
   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
 
51
   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
52
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
 
53
   96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
 
54
   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
 
55
  128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
 
56
  144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
 
57
  160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
 
58
  176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
 
59
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
60
  208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
 
61
  192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
 
62
  208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
 
63
};
 
64
 
 
65
/* 
 
66
  Names of the index hints (for error messages). Keep in sync with 
 
67
  index_hint_type 
 
68
*/
 
69
 
 
70
const char * index_hint_type_name[] =
 
71
{
 
72
  "IGNORE INDEX", 
 
73
  "USE INDEX", 
 
74
  "FORCE INDEX"
 
75
};
 
76
 
 
77
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
 
78
{
 
79
  while (len-- != 0 &&
 
80
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
 
81
  return (int) len+1;
 
82
}
 
83
 
 
84
#include <lex_hash.h>
 
85
 
 
86
 
 
87
void lex_init(void)
 
88
{
 
89
  uint32_t i;
 
90
  for (i=0 ; i < array_elements(symbols) ; i++)
 
91
    symbols[i].length=(unsigned char) strlen(symbols[i].name);
 
92
  for (i=0 ; i < array_elements(sql_functions) ; i++)
 
93
    sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
 
94
 
 
95
  return;
 
96
}
 
97
 
 
98
 
 
99
void lex_free(void)
 
100
{                                       // Call this when daemon ends
 
101
  return;
 
102
}
 
103
 
 
104
 
47
105
void
48
106
st_parsing_options::reset()
49
107
{
 
108
  allows_variable= true;
 
109
  allows_select_into= true;
50
110
  allows_select_procedure= true;
 
111
  allows_derived= true;
51
112
}
52
113
 
53
 
Lex_input_stream::Lex_input_stream(Session *session,
 
114
Lex_input_stream::Lex_input_stream(THD *thd,
54
115
                                   const char* buffer,
55
116
                                   unsigned int length)
56
 
: m_session(session),
 
117
: m_thd(thd),
57
118
  yylineno(1),
58
119
  yytoklen(0),
59
120
  yylval(NULL),
78
139
  in_comment(NO_COMMENT),
79
140
  m_underscore_cs(NULL)
80
141
{
81
 
  m_cpp_buf= (char*) session->alloc(length + 1);
 
142
  m_cpp_buf= (char*) thd->alloc(length + 1);
82
143
  m_cpp_ptr= m_cpp_buf;
83
144
}
84
145
 
92
153
     statement;
93
154
  2) Determine the beginning of the body.
94
155
 
95
 
  @param session        Thread context.
 
156
  @param thd        Thread context.
96
157
  @param begin_ptr  Pointer to the start of the body in the pre-processed
97
158
                    buffer.
98
159
*/
99
160
 
100
 
void Lex_input_stream::body_utf8_start(Session *session, const char *begin_ptr)
 
161
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
101
162
{
102
163
  assert(begin_ptr);
103
164
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
104
165
 
105
166
  uint32_t body_utf8_length=
106
 
    (m_buf_length / default_charset_info->mbminlen) *
 
167
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
107
168
    my_charset_utf8_bin.mbmaxlen;
108
169
 
109
 
  m_body_utf8= (char *) session->alloc(body_utf8_length + 1);
 
170
  m_body_utf8= (char *) thd->alloc(body_utf8_length + 1);
110
171
  m_body_utf8_ptr= m_body_utf8;
111
172
  *m_body_utf8_ptr= 0;
112
173
 
172
233
  The operation converts the specified text literal to the utf8 and appends
173
234
  the result to the utf8-body.
174
235
 
175
 
  @param session      Thread context.
 
236
  @param thd      Thread context.
176
237
  @param txt      Text literal.
177
238
  @param txt_cs   Character set of the text literal.
178
239
  @param end_ptr  Pointer in the pre-processed buffer, to which
180
241
                  operation.
181
242
*/
182
243
 
183
 
void Lex_input_stream::body_utf8_append_literal(Session *session,
 
244
void Lex_input_stream::body_utf8_append_literal(THD *thd,
184
245
                                                const LEX_STRING *txt,
185
246
                                                const CHARSET_INFO * const txt_cs,
186
247
                                                const char *end_ptr)
192
253
 
193
254
  if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
194
255
  {
195
 
    session->convert_string(&utf_txt,
 
256
    thd->convert_string(&utf_txt,
196
257
                        &my_charset_utf8_general_ci,
197
258
                        txt->str, txt->length,
198
259
                        txt_cs);
219
280
  (We already do too much here)
220
281
*/
221
282
 
222
 
void lex_start(Session *session)
 
283
void lex_start(THD *thd)
223
284
{
224
 
  LEX *lex= session->lex;
 
285
  LEX *lex= thd->lex;
225
286
 
226
 
  lex->session= lex->unit.session= session;
 
287
  lex->thd= lex->unit.thd= thd;
227
288
 
228
289
  lex->context_stack.empty();
229
290
  lex->unit.init_query();
243
304
  lex->select_lex.master= &lex->unit;
244
305
  lex->select_lex.prev= &lex->unit.slave;
245
306
  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
246
 
  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
 
307
  lex->select_lex.link_prev= (st_select_lex_node**)&(lex->all_selects_list);
247
308
  lex->select_lex.options= 0;
248
309
  lex->select_lex.init_order();
249
310
  lex->select_lex.group_list.empty();
253
314
  lex->lock_option= TL_READ;
254
315
  lex->leaf_tables_insert= 0;
255
316
  lex->parsing_options.reset();
 
317
  lex->empty_field_list_on_rset= 0;
256
318
  lex->select_lex.select_number= 1;
257
319
  lex->length=0;
258
320
  lex->select_lex.in_sum_expr=0;
263
325
  lex->sql_command= SQLCOM_END;
264
326
  lex->duplicates= DUP_ERROR;
265
327
  lex->ignore= 0;
 
328
  lex->proc_list.first= 0;
266
329
  lex->escape_used= false;
267
330
  lex->query_tables= 0;
268
331
  lex->reset_query_tables_list(false);
269
332
  lex->expr_allows_subselect= true;
270
333
  lex->use_only_table_context= false;
271
 
  lex->parse_vcol_expr= false;
272
334
 
273
335
  lex->name.str= 0;
274
336
  lex->name.length= 0;
275
337
  lex->nest_level=0 ;
276
338
  lex->allow_sum_func= 0;
277
339
  lex->in_sum_func= NULL;
 
340
  /*
 
341
    ok, there must be a better solution for this, long-term
 
342
    I tried "memset" in the sql_yacc.yy code, but that for
 
343
    some reason made the values zero, even if they were set
 
344
  */
 
345
  lex->server_options.server_name= 0;
 
346
  lex->server_options.server_name_length= 0;
 
347
  lex->server_options.host= 0;
 
348
  lex->server_options.db= 0;
 
349
  lex->server_options.username= 0;
 
350
  lex->server_options.password= 0;
 
351
  lex->server_options.scheme= 0;
 
352
  lex->server_options.owner= 0;
 
353
  lex->server_options.port= -1;
278
354
 
279
355
  lex->is_lex_started= true;
 
356
  return;
280
357
}
281
358
 
282
359
void lex_end(LEX *lex)
289
366
    lex->yacc_yyvs= 0;
290
367
  }
291
368
 
 
369
  /* release used plugins */
 
370
  plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer, 
 
371
                     lex->plugins.elements);
 
372
  reset_dynamic(&lex->plugins);
 
373
 
292
374
  delete lex->result;
293
375
  lex->result= 0;
 
376
 
 
377
  return;
294
378
}
295
379
 
296
380
 
297
381
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
298
382
{
299
 
  /* Plenty of memory for the largest lex symbol we have */
300
 
  char tok_upper[64];
301
383
  const char *tok= lip->get_tok_start();
302
 
  uint32_t tok_pos= 0;
303
 
  for (;tok_pos<len && tok_pos<63;tok_pos++)
304
 
    tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
305
 
  tok_upper[tok_pos]=0;
306
384
 
307
 
  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
 
385
  SYMBOL *symbol= get_hash_symbol(tok, len, function);
308
386
  if (symbol)
309
387
  {
310
388
    lip->yylval->symbol.symbol=symbol;
313
391
 
314
392
    return symbol->tok;
315
393
  }
316
 
 
317
394
  return 0;
318
395
}
319
396
 
 
397
/*
 
398
  Check if name is a keyword
 
399
 
 
400
  SYNOPSIS
 
401
    is_keyword()
 
402
    name      checked name (must not be empty)
 
403
    len       length of checked name
 
404
 
 
405
  RETURN VALUES
 
406
    0         name is a keyword
 
407
    1         name isn't a keyword
 
408
*/
 
409
 
 
410
bool is_keyword(const char *name, uint32_t len)
 
411
{
 
412
  assert(len != 0);
 
413
  return get_hash_symbol(name,len,0)!=0;
 
414
}
320
415
 
321
416
bool is_lex_native_function(const LEX_STRING *name)
322
417
{
323
418
  assert(name != NULL);
324
 
  return (lookup_symbol(name->str, name->length, 1) != 0);
 
419
  return (get_hash_symbol(name->str, name->length, 1) != 0);
325
420
}
326
421
 
327
422
/* make a copy of token before ptr and set yytoklen */
331
426
  LEX_STRING tmp;
332
427
  lip->yyUnget();                       // ptr points now after last token char
333
428
  tmp.length=lip->yytoklen=length;
334
 
  tmp.str= lip->m_session->strmake(lip->get_tok_start() + skip, tmp.length);
 
429
  tmp.str= lip->m_thd->strmake(lip->get_tok_start() + skip, tmp.length);
335
430
 
336
431
  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
337
432
  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.length;
339
434
  return tmp;
340
435
}
341
436
 
342
 
/*
343
 
 todo:
344
 
   There are no dangerous charsets in mysql for function
345
 
   get_quoted_token yet. But it should be fixed in the
 
437
/* 
 
438
 todo: 
 
439
   There are no dangerous charsets in mysql for function 
 
440
   get_quoted_token yet. But it should be fixed in the 
346
441
   future to operate multichar strings (like ucs2)
347
442
*/
348
443
 
355
450
  char *to;
356
451
  lip->yyUnget();                       // ptr points now after last token char
357
452
  tmp.length= lip->yytoklen=length;
358
 
  tmp.str=(char*) lip->m_session->alloc(tmp.length+1);
 
453
  tmp.str=(char*) lip->m_thd->alloc(tmp.length+1);
359
454
  from= lip->get_tok_start() + skip;
360
455
  to= tmp.str;
361
456
  end= to+length;
385
480
{
386
481
  register unsigned char c,sep;
387
482
  uint32_t found_escape=0;
388
 
  const CHARSET_INFO * const cs= lip->m_session->charset();
 
483
  const CHARSET_INFO * const cs= lip->m_thd->charset();
389
484
 
390
485
  lip->tok_bitmap= 0;
391
486
  sep= lip->yyGetLast();                        // String should end with this
433
528
      end -= post_skip;
434
529
      assert(end >= str);
435
530
 
436
 
      if (!(start= (char*) lip->m_session->alloc((uint32_t) (end-str)+1)))
 
531
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
437
532
        return (char*) "";              // Sql_alloc has set error flag
438
533
 
439
534
      lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
441
536
 
442
537
      if (!found_escape)
443
538
      {
444
 
        lip->yytoklen=(uint32_t) (end-str);
 
539
        lip->yytoklen=(uint) (end-str);
445
540
        memcpy(start,str,lip->yytoklen);
446
541
        start[lip->yytoklen]=0;
447
542
      }
497
592
            *to++ = *str;
498
593
        }
499
594
        *to=0;
500
 
        lip->yytoklen=(uint32_t) (to-start);
 
595
        lip->yytoklen=(uint) (to-start);
501
596
      }
502
597
      return start;
503
598
    }
509
604
/*
510
605
** Calc type of integer; long integer, int64_t integer or real.
511
606
** Returns smallest type that match the string.
512
 
** When using uint64_t values the result is converted to a real
 
607
** When using unsigned long long values the result is converted to a real
513
608
** because else they will be unexpected sign changes because all calculation
514
609
** is done with int64_t or double.
515
610
*/
598
693
 
599
694
 
600
695
/*
601
 
  DRIZZLElex remember the following states from the following DRIZZLElex()
 
696
  MYSQLlex remember the following states from the following MYSQLlex()
602
697
 
603
698
  - MY_LEX_EOQ                  Found end of query
604
699
  - MY_LEX_OPERATOR_OR_IDENT    Last state was an ident, text or number
605
700
                                (which can't be followed by a signed number)
606
701
*/
607
702
 
608
 
int DRIZZLElex(void *arg, void *yysession)
 
703
int MYSQLlex(void *arg, void *yythd)
609
704
{
610
 
  Session *session= (Session *)yysession;
611
 
  Lex_input_stream *lip= session->m_lip;
 
705
  THD *thd= (THD *)yythd;
 
706
  Lex_input_stream *lip= thd->m_lip;
612
707
  YYSTYPE *yylval=(YYSTYPE*) arg;
613
708
  int token;
614
709
 
625
720
    return token;
626
721
  }
627
722
 
628
 
  token= lex_one_token(arg, yysession);
 
723
  token= lex_one_token(arg, yythd);
629
724
 
630
725
  switch(token) {
631
726
  case WITH:
636
731
      to transform the grammar into a LALR(1) grammar,
637
732
      which sql_yacc.yy can process.
638
733
    */
639
 
    token= lex_one_token(arg, yysession);
 
734
    token= lex_one_token(arg, yythd);
640
735
    if (token == ROLLUP_SYM)
641
736
    {
642
737
      return WITH_ROLLUP_SYM;
651
746
      lip->lookahead_token= token;
652
747
      return WITH;
653
748
    }
 
749
    break;
654
750
  default:
655
751
    break;
656
752
  }
658
754
  return token;
659
755
}
660
756
 
661
 
int lex_one_token(void *arg, void *yysession)
 
757
int lex_one_token(void *arg, void *yythd)
662
758
{
663
759
  register unsigned char c= 0; /* Just set to shutup GCC */
664
760
  bool comment_closed;
665
761
  int   tokval, result_state;
666
762
  unsigned int length;
667
763
  enum my_lex_states state;
668
 
  Session *session= (Session *)yysession;
669
 
  Lex_input_stream *lip= session->m_lip;
670
 
  LEX *lex= session->lex;
 
764
  THD *thd= (THD *)yythd;
 
765
  Lex_input_stream *lip= thd->m_lip;
 
766
  LEX *lex= thd->lex;
671
767
  YYSTYPE *yylval=(YYSTYPE*) arg;
672
 
  const CHARSET_INFO * const cs= session->charset();
 
768
  const CHARSET_INFO * const cs= thd->charset();
673
769
  unsigned char *state_map= cs->state_map;
674
770
  unsigned char *ident_map= cs->ident_map;
675
771
 
806
902
      }
807
903
      yylval->lex_str=get_token(lip, 0, length);
808
904
 
 
905
      /*
 
906
         Note: "SELECT _bla AS 'alias'"
 
907
         _bla should be considered as a IDENT if charset haven't been found.
 
908
         So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
 
909
         producing an error.
 
910
      */
 
911
 
 
912
      if (yylval->lex_str.str[0] == '_')
 
913
      {
 
914
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
915
                                                             MY_CS_PRIMARY, MYF(0));
 
916
        if (cs)
 
917
        {
 
918
          yylval->charset= cs;
 
919
          lip->m_underscore_cs= cs;
 
920
 
 
921
          lip->body_utf8_append(lip->m_cpp_text_start,
 
922
                                lip->get_cpp_tok_start() + length);
 
923
          return(UNDERSCORE_CHARSET);
 
924
        }
 
925
      }
 
926
 
809
927
      lip->body_utf8_append(lip->m_cpp_text_start);
810
928
 
811
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
929
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
812
930
                                    lip->m_cpp_text_end);
813
931
 
814
932
      return(result_state);                     // IDENT or IDENT_QUOTED
911
1029
 
912
1030
      lip->body_utf8_append(lip->m_cpp_text_start);
913
1031
 
914
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1032
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
915
1033
                                    lip->m_cpp_text_end);
916
1034
 
917
1035
      return(result_state);
952
1070
 
953
1071
      lip->body_utf8_append(lip->m_cpp_text_start);
954
1072
 
955
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1073
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
956
1074
                                    lip->m_cpp_text_end);
957
1075
 
958
1076
      return(IDENT_QUOTED);
1066
1184
 
1067
1185
      lip->body_utf8_append(lip->m_cpp_text_start);
1068
1186
 
1069
 
      lip->body_utf8_append_literal(session, &yylval->lex_str,
 
1187
      lip->body_utf8_append_literal(thd, &yylval->lex_str,
1070
1188
        lip->m_underscore_cs ? lip->m_underscore_cs : cs,
1071
1189
        lip->m_cpp_text_end);
1072
1190
 
1102
1220
 
1103
1221
        /*
1104
1222
          The special comment format is very strict:
1105
 
          '/' '*' '!', followed by digits ended by a non-digit.
1106
 
          There must be at least 5 digits for it to count
 
1223
          '/' '*' '!', followed by exactly
 
1224
          1 digit (major), 2 digits (minor), then 2 digits (dot).
 
1225
          32302 -> 3.23.02
 
1226
          50032 -> 5.0.32
 
1227
          50114 -> 5.1.14
1107
1228
        */
1108
 
        const int MAX_VERSION_SIZE= 16;
1109
 
        char version_str[MAX_VERSION_SIZE];
1110
 
 
1111
 
        int pos= 0;
1112
 
        do
1113
 
        {
1114
 
          version_str[pos]= lip->yyPeekn(pos);
1115
 
          pos++;
1116
 
        } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1117
 
        version_str[pos]= 0;
1118
 
 
1119
 
        /* To keep some semblance of compatibility, we impose a 5 digit floor */
1120
 
        if (pos > 4)
1121
 
        {
1122
 
          uint64_t version;
1123
 
          version=strtoll(version_str, NULL, 10);
 
1229
        char version_str[6];
 
1230
        version_str[0]= lip->yyPeekn(0);
 
1231
        version_str[1]= lip->yyPeekn(1);
 
1232
        version_str[2]= lip->yyPeekn(2);
 
1233
        version_str[3]= lip->yyPeekn(3);
 
1234
        version_str[4]= lip->yyPeekn(4);
 
1235
        version_str[5]= 0;
 
1236
        if (  my_isdigit(cs, version_str[0])
 
1237
           && my_isdigit(cs, version_str[1])
 
1238
           && my_isdigit(cs, version_str[2])
 
1239
           && my_isdigit(cs, version_str[3])
 
1240
           && my_isdigit(cs, version_str[4])
 
1241
           )
 
1242
        {
 
1243
          ulong version;
 
1244
          version=strtol(version_str, NULL, 10);
1124
1245
 
1125
1246
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1126
 
          lip->yySkipn(pos-1);
 
1247
          lip->yySkipn(5);
1127
1248
 
1128
1249
          if (version <= DRIZZLE_VERSION_ID)
1129
1250
          {
1205
1326
    case MY_LEX_SEMICOLON:                      // optional line terminator
1206
1327
      if (lip->yyPeek())
1207
1328
      {
1208
 
        if ((session->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1329
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
1209
1330
        {
1210
1331
          lip->found_semicolon= lip->get_ptr();
1211
 
          session->server_status|= SERVER_MORE_RESULTS_EXISTS;
 
1332
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
1212
1333
          lip->next_state= MY_LEX_END;
1213
1334
          lip->set_echo(true);
1214
1335
          return (END_OF_INPUT);
1236
1357
    case MY_LEX_END:
1237
1358
      lip->next_state=MY_LEX_END;
1238
1359
      return(0);                        // We found end of input last time
1239
 
 
 
1360
      
1240
1361
      /* Actually real shouldn't start with . but allow them anyhow */
1241
1362
    case MY_LEX_REAL_OR_POINT:
1242
1363
      if (my_isdigit(cs,lip->yyPeek()))
1303
1424
 
1304
1425
      lip->body_utf8_append(lip->m_cpp_text_start);
1305
1426
 
1306
 
      lip->body_utf8_append_literal(session, &yylval->lex_str, cs,
 
1427
      lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1307
1428
                                    lip->m_cpp_text_end);
1308
1429
 
1309
1430
      return(result_state);
1321
1442
  statements and stored procedures and is compensated by always
1322
1443
  supplying a copy of Alter_info to these functions.
1323
1444
 
1324
 
  @return You need to use check the error in Session for out
 
1445
  @return You need to use check the error in THD for out
1325
1446
  of memory condition after calling this function.
1326
1447
*/
1327
1448
 
1380
1501
 
1381
1502
 
1382
1503
/*
1383
 
  Select_Lex structures initialisations
 
1504
  st_select_lex structures initialisations
1384
1505
*/
1385
1506
 
1386
 
void Select_Lex_Node::init_query()
 
1507
void st_select_lex_node::init_query()
1387
1508
{
1388
1509
  options= 0;
1389
1510
  linkage= UNSPECIFIED_TYPE;
1391
1512
  uncacheable= 0;
1392
1513
}
1393
1514
 
1394
 
void Select_Lex_Node::init_select()
 
1515
void st_select_lex_node::init_select()
1395
1516
{
1396
1517
}
1397
1518
 
1398
 
void Select_Lex_Unit::init_query()
 
1519
void st_select_lex_unit::init_query()
1399
1520
{
1400
 
  Select_Lex_Node::init_query();
 
1521
  st_select_lex_node::init_query();
1401
1522
  linkage= GLOBAL_OPTIONS_TYPE;
1402
1523
  global_parameters= first_select();
1403
1524
  select_limit_cnt= HA_POS_ERROR;
1414
1535
  found_rows_for_union= 0;
1415
1536
}
1416
1537
 
1417
 
void Select_Lex::init_query()
 
1538
void st_select_lex::init_query()
1418
1539
{
1419
 
  Select_Lex_Node::init_query();
 
1540
  st_select_lex_node::init_query();
1420
1541
  table_list.empty();
1421
1542
  top_join_list.empty();
1422
1543
  join_list= &top_join_list;
1440
1561
  parent_lex->push_context(&context);
1441
1562
  cond_count= between_count= with_wild= 0;
1442
1563
  max_equal_elems= 0;
 
1564
  conds_processed_with_permanent_arena= 0;
1443
1565
  ref_pointer_array= 0;
1444
1566
  select_n_where_fields= 0;
1445
1567
  select_n_having_items= 0;
1451
1573
  link_next= 0;
1452
1574
}
1453
1575
 
1454
 
void Select_Lex::init_select()
 
1576
void st_select_lex::init_select()
1455
1577
{
1456
 
  Select_Lex_Node::init_select();
 
1578
  st_select_lex_node::init_select();
1457
1579
  sj_nests.empty();
1458
1580
  group_list.empty();
1459
1581
  type= db= 0;
1483
1605
}
1484
1606
 
1485
1607
/*
1486
 
  Select_Lex structures linking
 
1608
  st_select_lex structures linking
1487
1609
*/
1488
1610
 
1489
1611
/* include on level down */
1490
 
void Select_Lex_Node::include_down(Select_Lex_Node *upper)
 
1612
void st_select_lex_node::include_down(st_select_lex_node *upper)
1491
1613
{
1492
1614
  if ((next= upper->slave))
1493
1615
    next->prev= &next;
1501
1623
  include on level down (but do not link)
1502
1624
 
1503
1625
  SYNOPSYS
1504
 
    Select_Lex_Node::include_standalone()
 
1626
    st_select_lex_node::include_standalone()
1505
1627
    upper - reference on node underr which this node should be included
1506
1628
    ref - references on reference on this node
1507
1629
*/
1508
 
void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1509
 
                                            Select_Lex_Node **ref)
 
1630
void st_select_lex_node::include_standalone(st_select_lex_node *upper,
 
1631
                                            st_select_lex_node **ref)
1510
1632
{
1511
1633
  next= 0;
1512
1634
  prev= ref;
1515
1637
}
1516
1638
 
1517
1639
/* include neighbour (on same level) */
1518
 
void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
 
1640
void st_select_lex_node::include_neighbour(st_select_lex_node *before)
1519
1641
{
1520
1642
  if ((next= before->next))
1521
1643
    next->prev= &next;
1525
1647
  slave= 0;
1526
1648
}
1527
1649
 
1528
 
/* including in global Select_Lex list */
1529
 
void Select_Lex_Node::include_global(Select_Lex_Node **plink)
 
1650
/* including in global SELECT_LEX list */
 
1651
void st_select_lex_node::include_global(st_select_lex_node **plink)
1530
1652
{
1531
1653
  if ((link_next= *plink))
1532
1654
    link_next->link_prev= &link_next;
1535
1657
}
1536
1658
 
1537
1659
//excluding from global list (internal function)
1538
 
void Select_Lex_Node::fast_exclude()
 
1660
void st_select_lex_node::fast_exclude()
1539
1661
{
1540
1662
  if (link_prev)
1541
1663
  {
1545
1667
  // Remove slave structure
1546
1668
  for (; slave; slave= slave->next)
1547
1669
    slave->fast_exclude();
1548
 
 
 
1670
  
1549
1671
}
1550
1672
 
1551
1673
/*
1552
1674
  excluding select_lex structure (except first (first select can't be
1553
1675
  deleted, because it is most upper select))
1554
1676
*/
1555
 
void Select_Lex_Node::exclude()
 
1677
void st_select_lex_node::exclude()
1556
1678
{
1557
1679
  //exclude from global list
1558
1680
  fast_exclude();
1559
1681
  //exclude from other structures
1560
1682
  if ((*prev= next))
1561
1683
    next->prev= prev;
1562
 
  /*
1563
 
     We do not need following statements, because prev pointer of first
 
1684
  /* 
 
1685
     We do not need following statements, because prev pointer of first 
1564
1686
     list element point to master->slave
1565
1687
     if (master->slave == this)
1566
1688
       master->slave= next;
1572
1694
  Exclude level of current unit from tree of SELECTs
1573
1695
 
1574
1696
  SYNOPSYS
1575
 
    Select_Lex_Unit::exclude_level()
 
1697
    st_select_lex_unit::exclude_level()
1576
1698
 
1577
1699
  NOTE: units which belong to current will be brought up on level of
1578
 
  currernt unit
 
1700
  currernt unit 
1579
1701
*/
1580
 
void Select_Lex_Unit::exclude_level()
 
1702
void st_select_lex_unit::exclude_level()
1581
1703
{
1582
 
  Select_Lex_Unit *units= 0, **units_last= &units;
1583
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1704
  SELECT_LEX_UNIT *units= 0, **units_last= &units;
 
1705
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1584
1706
  {
1585
1707
    // unlink current level from global SELECTs list
1586
1708
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1587
1709
      sl->link_next->link_prev= sl->link_prev;
1588
1710
 
1589
1711
    // bring up underlay levels
1590
 
    Select_Lex_Unit **last= 0;
1591
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1712
    SELECT_LEX_UNIT **last= 0;
 
1713
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1592
1714
    {
1593
1715
      u->master= master;
1594
 
      last= (Select_Lex_Unit**)&(u->next);
 
1716
      last= (SELECT_LEX_UNIT**)&(u->next);
1595
1717
    }
1596
1718
    if (last)
1597
1719
    {
1603
1725
  {
1604
1726
    // include brought up levels in place of current
1605
1727
    (*prev)= units;
1606
 
    (*units_last)= (Select_Lex_Unit*)next;
 
1728
    (*units_last)= (SELECT_LEX_UNIT*)next;
1607
1729
    if (next)
1608
 
      next->prev= (Select_Lex_Node**)units_last;
 
1730
      next->prev= (SELECT_LEX_NODE**)units_last;
1609
1731
    units->prev= prev;
1610
1732
  }
1611
1733
  else
1622
1744
  Exclude subtree of current unit from tree of SELECTs
1623
1745
 
1624
1746
  SYNOPSYS
1625
 
    Select_Lex_Unit::exclude_tree()
 
1747
    st_select_lex_unit::exclude_tree()
1626
1748
*/
1627
 
void Select_Lex_Unit::exclude_tree()
 
1749
void st_select_lex_unit::exclude_tree()
1628
1750
{
1629
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1751
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1630
1752
  {
1631
1753
    // unlink current level from global SELECTs list
1632
1754
    if (sl->link_prev && (*sl->link_prev= sl->link_next))
1633
1755
      sl->link_next->link_prev= sl->link_prev;
1634
1756
 
1635
1757
    // unlink underlay levels
1636
 
    for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
 
1758
    for (SELECT_LEX_UNIT *u= sl->first_inner_unit(); u; u= u->next_unit())
1637
1759
    {
1638
1760
      u->exclude_level();
1639
1761
    }
1646
1768
 
1647
1769
 
1648
1770
/*
1649
 
  Select_Lex_Node::mark_as_dependent mark all Select_Lex struct from
 
1771
  st_select_lex_node::mark_as_dependent mark all st_select_lex struct from 
1650
1772
  this to 'last' as dependent
1651
1773
 
1652
1774
  SYNOPSIS
1653
 
    last - pointer to last Select_Lex struct, before wich all
1654
 
           Select_Lex have to be marked as dependent
 
1775
    last - pointer to last st_select_lex struct, before wich all 
 
1776
           st_select_lex have to be marked as dependent
1655
1777
 
1656
1778
  NOTE
1657
 
    'last' should be reachable from this Select_Lex_Node
 
1779
    'last' should be reachable from this st_select_lex_node
1658
1780
*/
1659
1781
 
1660
 
void Select_Lex::mark_as_dependent(Select_Lex *last)
 
1782
void st_select_lex::mark_as_dependent(st_select_lex *last)
1661
1783
{
1662
1784
  /*
1663
1785
    Mark all selects from resolved to 1 before select where was
1664
1786
    found table as depended (of select where was found table)
1665
1787
  */
1666
 
  for (Select_Lex *s= this;
 
1788
  for (SELECT_LEX *s= this;
1667
1789
       s && s != last;
1668
1790
       s= s->outer_select())
1669
1791
  {
1672
1794
      // Select is dependent of outer select
1673
1795
      s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1674
1796
                       UNCACHEABLE_DEPENDENT;
1675
 
      Select_Lex_Unit *munit= s->master_unit();
 
1797
      SELECT_LEX_UNIT *munit= s->master_unit();
1676
1798
      munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1677
1799
                       UNCACHEABLE_DEPENDENT;
1678
 
      for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
 
1800
      for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1679
1801
      {
1680
1802
        if (sl != s &&
1681
1803
            !(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1689
1811
  }
1690
1812
}
1691
1813
 
1692
 
bool Select_Lex_Node::set_braces(bool)
 
1814
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
1693
1815
{ return 1; }
1694
 
bool Select_Lex_Node::inc_in_sum_expr()           { return 1; }
1695
 
uint32_t Select_Lex_Node::get_in_sum_expr()           { return 0; }
1696
 
TableList* Select_Lex_Node::get_table_list()     { return 0; }
1697
 
List<Item>* Select_Lex_Node::get_item_list()      { return 0; }
1698
 
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
1699
 
                                                  thr_lock_type, List<Index_hint> *, LEX_STRING *)
 
1816
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
 
1817
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1818
TableList* st_select_lex_node::get_table_list()     { return 0; }
 
1819
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
 
1820
TableList *st_select_lex_node::add_table_to_list (THD *thd __attribute__((unused)),
 
1821
                                                   Table_ident *table __attribute__((unused)),
 
1822
                                                  LEX_STRING *alias __attribute__((unused)),
 
1823
                                                  uint32_t table_join_options __attribute__((unused)),
 
1824
                                                  thr_lock_type flags __attribute__((unused)),
 
1825
                                                  List<Index_hint> *hints __attribute__((unused)),
 
1826
                                                  LEX_STRING *option __attribute__((unused)))
1700
1827
{
1701
1828
  return 0;
1702
1829
}
1703
 
uint32_t Select_Lex_Node::get_table_join_options()
 
1830
uint32_t st_select_lex_node::get_table_join_options()
1704
1831
{
1705
1832
  return 0;
1706
1833
}
1708
1835
/*
1709
1836
  prohibit using LIMIT clause
1710
1837
*/
1711
 
bool Select_Lex::test_limit()
 
1838
bool st_select_lex::test_limit()
1712
1839
{
1713
1840
  if (select_limit != 0)
1714
1841
  {
1720
1847
}
1721
1848
 
1722
1849
 
1723
 
Select_Lex_Unit* Select_Lex_Unit::master_unit()
 
1850
st_select_lex_unit* st_select_lex_unit::master_unit()
1724
1851
{
1725
1852
    return this;
1726
1853
}
1727
1854
 
1728
1855
 
1729
 
Select_Lex* Select_Lex_Unit::outer_select()
1730
 
{
1731
 
  return (Select_Lex*) master;
1732
 
}
1733
 
 
1734
 
 
1735
 
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1736
 
{
1737
 
  return add_to_list(session, order_list, item, asc);
1738
 
}
1739
 
 
1740
 
 
1741
 
bool Select_Lex::add_item_to_list(Session *, Item *item)
 
1856
st_select_lex* st_select_lex_unit::outer_select()
 
1857
{
 
1858
  return (st_select_lex*) master;
 
1859
}
 
1860
 
 
1861
 
 
1862
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
 
1863
{
 
1864
  return add_to_list(thd, order_list, item, asc);
 
1865
}
 
1866
 
 
1867
 
 
1868
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
 
1869
                                     Item *item)
1742
1870
{
1743
1871
  return(item_list.push_back(item));
1744
1872
}
1745
1873
 
1746
1874
 
1747
 
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1748
 
{
1749
 
  return add_to_list(session, group_list, item, asc);
1750
 
}
1751
 
 
1752
 
 
1753
 
Select_Lex_Unit* Select_Lex::master_unit()
1754
 
{
1755
 
  return (Select_Lex_Unit*) master;
1756
 
}
1757
 
 
1758
 
 
1759
 
Select_Lex* Select_Lex::outer_select()
1760
 
{
1761
 
  return (Select_Lex*) master->get_master();
1762
 
}
1763
 
 
1764
 
 
1765
 
bool Select_Lex::set_braces(bool value)
 
1875
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
 
1876
{
 
1877
  return add_to_list(thd, group_list, item, asc);
 
1878
}
 
1879
 
 
1880
 
 
1881
st_select_lex_unit* st_select_lex::master_unit()
 
1882
{
 
1883
  return (st_select_lex_unit*) master;
 
1884
}
 
1885
 
 
1886
 
 
1887
st_select_lex* st_select_lex::outer_select()
 
1888
{
 
1889
  return (st_select_lex*) master->get_master();
 
1890
}
 
1891
 
 
1892
 
 
1893
bool st_select_lex::set_braces(bool value)
1766
1894
{
1767
1895
  braces= value;
1768
 
  return 0;
 
1896
  return 0; 
1769
1897
}
1770
1898
 
1771
1899
 
1772
 
bool Select_Lex::inc_in_sum_expr()
 
1900
bool st_select_lex::inc_in_sum_expr()
1773
1901
{
1774
1902
  in_sum_expr++;
1775
1903
  return 0;
1776
1904
}
1777
1905
 
1778
1906
 
1779
 
uint32_t Select_Lex::get_in_sum_expr()
 
1907
uint32_t st_select_lex::get_in_sum_expr()
1780
1908
{
1781
1909
  return in_sum_expr;
1782
1910
}
1783
1911
 
1784
1912
 
1785
 
TableList* Select_Lex::get_table_list()
 
1913
TableList* st_select_lex::get_table_list()
1786
1914
{
1787
1915
  return (TableList*) table_list.first;
1788
1916
}
1789
1917
 
1790
 
List<Item>* Select_Lex::get_item_list()
 
1918
List<Item>* st_select_lex::get_item_list()
1791
1919
{
1792
1920
  return &item_list;
1793
1921
}
1794
1922
 
1795
 
uint32_t Select_Lex::get_table_join_options()
 
1923
uint32_t st_select_lex::get_table_join_options()
1796
1924
{
1797
1925
  return table_join_options;
1798
1926
}
1799
1927
 
1800
1928
 
1801
 
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
 
1929
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
1802
1930
{
1803
1931
  if (ref_pointer_array)
1804
1932
    return 0;
1805
1933
 
1806
1934
  return (ref_pointer_array=
1807
 
          (Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
 
1935
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
1808
1936
                                                 item_list.elements +
1809
1937
                                                 select_n_having_items +
1810
1938
                                                 select_n_where_fields +
1812
1940
}
1813
1941
 
1814
1942
 
1815
 
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
 
1943
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1816
1944
{
1817
1945
  bool union_all= !union_distinct;
1818
 
  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
 
1946
  for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1819
1947
  {
1820
1948
    if (sl != first_select())
1821
1949
    {
1827
1955
    }
1828
1956
    if (sl->braces)
1829
1957
      str->append('(');
1830
 
    sl->print(session, str, query_type);
 
1958
    sl->print(thd, str, query_type);
1831
1959
    if (sl->braces)
1832
1960
      str->append(')');
1833
1961
  }
1841
1969
        (order_st *) fake_select_lex->order_list.first,
1842
1970
        query_type);
1843
1971
    }
1844
 
    fake_select_lex->print_limit(session, str, query_type);
 
1972
    fake_select_lex->print_limit(thd, str, query_type);
1845
1973
  }
1846
1974
}
1847
1975
 
1848
1976
 
1849
 
void Select_Lex::print_order(String *str,
 
1977
void st_select_lex::print_order(String *str,
1850
1978
                                order_st *order,
1851
1979
                                enum_query_type query_type)
1852
1980
{
1866
1994
      str->append(',');
1867
1995
  }
1868
1996
}
1869
 
 
1870
 
 
1871
 
void Select_Lex::print_limit(Session *, String *str,
 
1997
 
 
1998
 
 
1999
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
 
2000
                                String *str,
1872
2001
                                enum_query_type query_type)
1873
2002
{
1874
 
  Select_Lex_Unit *unit= master_unit();
 
2003
  SELECT_LEX_UNIT *unit= master_unit();
1875
2004
  Item_subselect *item= unit->item;
1876
2005
 
1877
2006
  if (item && unit->global_parameters == this)
1908
2037
}
1909
2038
 
1910
2039
/**
1911
 
  @brief Restore the LEX and Session in case of a parse error.
 
2040
  @brief Restore the LEX and THD in case of a parse error.
1912
2041
 
1913
2042
  This is a clean up call that is invoked by the Bison generated
1914
 
  parser before returning an error from DRIZZLEparse. If your
 
2043
  parser before returning an error from MYSQLparse. If your
1915
2044
  semantic actions manipulate with the global thread state (which
1916
2045
  is a very bad practice and should not normally be employed) and
1917
2046
  need a clean-up in case of error, and you can not use %destructor
1919
2048
  to implement the clean up.
1920
2049
*/
1921
2050
 
1922
 
void LEX::cleanup_lex_after_parse_error(Session *)
 
2051
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
1923
2052
{
1924
2053
}
1925
2054
 
1956
2085
  query_tables= 0;
1957
2086
  query_tables_last= &query_tables;
1958
2087
  query_tables_own_last= 0;
 
2088
  if (init)
 
2089
  {
 
2090
    /*
 
2091
      We delay real initialization of hash (and therefore related
 
2092
      memory allocation) until first insertion into this hash.
 
2093
    */
 
2094
    hash_clear(&sroutines);
 
2095
  }
 
2096
  else if (sroutines.records)
 
2097
  {
 
2098
    /* Non-zero sroutines.records means that hash was initialized. */
 
2099
    my_hash_reset(&sroutines);
 
2100
  }
 
2101
  sroutines_list.empty();
 
2102
  sroutines_list_own_last= sroutines_list.next;
 
2103
  sroutines_list_own_elements= 0;
 
2104
  binlog_stmt_flags= 0;
1959
2105
}
1960
2106
 
1961
2107
 
1968
2114
 
1969
2115
void Query_tables_list::destroy_query_tables_list()
1970
2116
{
 
2117
  hash_free(&sroutines);
1971
2118
}
1972
2119
 
1973
2120
 
1975
2122
  Initialize LEX object.
1976
2123
 
1977
2124
  SYNOPSIS
1978
 
    LEX::LEX()
 
2125
    st_lex::st_lex()
1979
2126
 
1980
2127
  NOTE
1981
2128
    LEX object initialized with this constructor can be used as part of
1982
 
    Session object for which one can safely call open_tables(), lock_tables()
 
2129
    THD object for which one can safely call open_tables(), lock_tables()
1983
2130
    and close_thread_tables() functions. But it is not yet ready for
1984
2131
    statement parsing. On should use lex_start() function to prepare LEX
1985
2132
    for this.
1986
2133
*/
1987
2134
 
1988
 
LEX::LEX()
 
2135
st_lex::st_lex()
1989
2136
  :result(0), yacc_yyss(0), yacc_yyvs(0),
1990
2137
   sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1991
2138
{
1992
2139
 
 
2140
  my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
 
2141
                         plugins_static_buffer,
 
2142
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
 
2143
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
1993
2144
  reset_query_tables_list(true);
1994
2145
}
1995
2146
 
 
2147
 
 
2148
/*
 
2149
  Check whether the merging algorithm can be used on this VIEW
 
2150
 
 
2151
  SYNOPSIS
 
2152
    st_lex::can_be_merged()
 
2153
 
 
2154
  DESCRIPTION
 
2155
    We can apply merge algorithm if it is single SELECT view  with
 
2156
    subqueries only in WHERE clause (we do not count SELECTs of underlying
 
2157
    views, and second level subqueries) and we have not grpouping, ordering,
 
2158
    HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
 
2159
    several underlying tables.
 
2160
 
 
2161
  RETURN
 
2162
    false - only temporary table algorithm can be used
 
2163
    true  - merge algorithm can be used
 
2164
*/
 
2165
 
 
2166
bool st_lex::can_be_merged()
 
2167
{
 
2168
  // TODO: do not forget implement case when select_lex.table_list.elements==0
 
2169
 
 
2170
  /* find non VIEW subqueries/unions */
 
2171
  bool selects_allow_merge= select_lex.next_select() == 0;
 
2172
  if (selects_allow_merge)
 
2173
  {
 
2174
    for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
 
2175
         tmp_unit;
 
2176
         tmp_unit= tmp_unit->next_unit())
 
2177
    {
 
2178
      if (tmp_unit->first_select()->parent_lex == this &&
 
2179
          (tmp_unit->item == 0 ||
 
2180
           (tmp_unit->item->place() != IN_WHERE &&
 
2181
            tmp_unit->item->place() != IN_ON)))
 
2182
      {
 
2183
        selects_allow_merge= 0;
 
2184
        break;
 
2185
      }
 
2186
    }
 
2187
  }
 
2188
 
 
2189
  return (selects_allow_merge &&
 
2190
          select_lex.group_list.elements == 0 &&
 
2191
          select_lex.having == 0 &&
 
2192
          select_lex.with_sum_func == 0 &&
 
2193
          select_lex.table_list.elements >= 1 &&
 
2194
          !(select_lex.options & SELECT_DISTINCT) &&
 
2195
          select_lex.select_limit == 0);
 
2196
}
 
2197
 
 
2198
 
 
2199
/*
 
2200
  check if command can use VIEW with MERGE algorithm (for top VIEWs)
 
2201
 
 
2202
  SYNOPSIS
 
2203
    st_lex::can_use_merged()
 
2204
 
 
2205
  DESCRIPTION
 
2206
    Only listed here commands can use merge algorithm in top level
 
2207
    SELECT_LEX (for subqueries will be used merge algorithm if
 
2208
    st_lex::can_not_use_merged() is not true).
 
2209
 
 
2210
  RETURN
 
2211
    false - command can't use merged VIEWs
 
2212
    true  - VIEWs with MERGE algorithms can be used
 
2213
*/
 
2214
 
 
2215
bool st_lex::can_use_merged()
 
2216
{
 
2217
  switch (sql_command)
 
2218
  {
 
2219
  case SQLCOM_SELECT:
 
2220
  case SQLCOM_CREATE_TABLE:
 
2221
  case SQLCOM_UPDATE:
 
2222
  case SQLCOM_UPDATE_MULTI:
 
2223
  case SQLCOM_DELETE:
 
2224
  case SQLCOM_DELETE_MULTI:
 
2225
  case SQLCOM_INSERT:
 
2226
  case SQLCOM_INSERT_SELECT:
 
2227
  case SQLCOM_REPLACE:
 
2228
  case SQLCOM_REPLACE_SELECT:
 
2229
  case SQLCOM_LOAD:
 
2230
    return true;
 
2231
  default:
 
2232
    return false;
 
2233
  }
 
2234
}
 
2235
 
 
2236
/*
 
2237
  Check if command can't use merged views in any part of command
 
2238
 
 
2239
  SYNOPSIS
 
2240
    st_lex::can_not_use_merged()
 
2241
 
 
2242
  DESCRIPTION
 
2243
    Temporary table algorithm will be used on all SELECT levels for queries
 
2244
    listed here (see also st_lex::can_use_merged()).
 
2245
 
 
2246
  RETURN
 
2247
    false - command can't use merged VIEWs
 
2248
    true  - VIEWs with MERGE algorithms can be used
 
2249
*/
 
2250
 
 
2251
bool st_lex::can_not_use_merged()
 
2252
{
 
2253
  switch (sql_command)
 
2254
  {
 
2255
  /*
 
2256
    SQLCOM_SHOW_FIELDS is necessary to make 
 
2257
    information schema tables working correctly with views.
 
2258
    see get_schema_tables_result function
 
2259
  */
 
2260
  case SQLCOM_SHOW_FIELDS:
 
2261
    return true;
 
2262
  default:
 
2263
    return false;
 
2264
  }
 
2265
}
 
2266
 
1996
2267
/*
1997
2268
  Detect that we need only table structure of derived table/view
1998
2269
 
2004
2275
    false no, we need data
2005
2276
*/
2006
2277
 
2007
 
bool LEX::only_view_structure()
 
2278
bool st_lex::only_view_structure()
2008
2279
{
2009
2280
  switch (sql_command) {
2010
2281
  case SQLCOM_SHOW_CREATE:
2029
2300
*/
2030
2301
 
2031
2302
 
2032
 
bool LEX::need_correct_ident()
 
2303
bool st_lex::need_correct_ident()
2033
2304
{
2034
2305
  switch(sql_command)
2035
2306
  {
2048
2319
  and will initialize the destination with the default
2049
2320
  database of the stored routine, rather than the default
2050
2321
  database of the connection it is parsed in.
2051
 
  E.g. if one has no current database selected, or current database
 
2322
  E.g. if one has no current database selected, or current database 
2052
2323
  set to 'bar' and then issues:
2053
2324
 
2054
2325
  CREATE PROCEDURE foo.p1() BEGIN SELECT * FROM t1 END//
2062
2333
*/
2063
2334
 
2064
2335
bool
2065
 
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
 
2336
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
2066
2337
{
2067
 
  return session->copy_db_to(p_db, p_db_length);
 
2338
  return thd->copy_db_to(p_db, p_db_length);
2068
2339
}
2069
2340
 
2070
2341
/*
2071
2342
  initialize limit counters
2072
2343
 
2073
2344
  SYNOPSIS
2074
 
    Select_Lex_Unit::set_limit()
2075
 
    values      - Select_Lex with initial values for counters
 
2345
    st_select_lex_unit::set_limit()
 
2346
    values      - SELECT_LEX with initial values for counters
2076
2347
*/
2077
2348
 
2078
 
void Select_Lex_Unit::set_limit(Select_Lex *sl)
 
2349
void st_select_lex_unit::set_limit(st_select_lex *sl)
2079
2350
{
2080
2351
  ha_rows select_limit_val;
2081
2352
  uint64_t val;
2082
2353
 
2083
2354
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2084
2355
  select_limit_val= (ha_rows)val;
2085
 
  /*
 
2356
#ifndef BIG_TABLES
 
2357
  /* 
2086
2358
    Check for overflow : ha_rows can be smaller then uint64_t if
2087
2359
    BIG_TABLES is off.
2088
2360
    */
2089
2361
  if (val != (uint64_t)select_limit_val)
2090
2362
    select_limit_val= HA_POS_ERROR;
 
2363
#endif
2091
2364
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2092
2365
                                                 0UL);
2093
2366
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2114
2387
      In this case link_to_local is set.
2115
2388
 
2116
2389
*/
2117
 
TableList *LEX::unlink_first_table(bool *link_to_local)
 
2390
TableList *st_lex::unlink_first_table(bool *link_to_local)
2118
2391
{
2119
2392
  TableList *first;
2120
2393
  if ((first= query_tables))
2133
2406
    */
2134
2407
    if ((*link_to_local= test(select_lex.table_list.first)))
2135
2408
    {
2136
 
      select_lex.context.table_list=
 
2409
      select_lex.context.table_list= 
2137
2410
        select_lex.context.first_name_resolution_table= first->next_local;
2138
2411
      select_lex.table_list.first= (unsigned char*) (first->next_local);
2139
2412
      select_lex.table_list.elements--; //safety
2154
2427
  table list
2155
2428
 
2156
2429
  SYNOPSYS
2157
 
     LEX::first_lists_tables_same()
 
2430
     st_lex::first_lists_tables_same()
2158
2431
 
2159
2432
  NOTES
2160
2433
    In many cases (for example, usual INSERT/DELETE/...) the first table of
2161
 
    main Select_Lex have special meaning => check that it is the first table
 
2434
    main SELECT_LEX have special meaning => check that it is the first table
2162
2435
    in global list and re-link to be first in the global list if it is
2163
2436
    necessary.  We need such re-linking only for queries with sub-queries in
2164
2437
    the select list, as only in this case tables of sub-queries will go to
2165
2438
    the global list first.
2166
2439
*/
2167
2440
 
2168
 
void LEX::first_lists_tables_same()
 
2441
void st_lex::first_lists_tables_same()
2169
2442
{
2170
2443
  TableList *first_table= (TableList*) select_lex.table_list.first;
2171
2444
  if (query_tables != first_table && first_table != 0)
2201
2474
    global list
2202
2475
*/
2203
2476
 
2204
 
void LEX::link_first_table_back(TableList *first,
 
2477
void st_lex::link_first_table_back(TableList *first,
2205
2478
                                   bool link_to_local)
2206
2479
{
2207
2480
  if (first)
2228
2501
  cleanup lex for case when we open table by table for processing
2229
2502
 
2230
2503
  SYNOPSIS
2231
 
    LEX::cleanup_after_one_table_open()
 
2504
    st_lex::cleanup_after_one_table_open()
2232
2505
 
2233
2506
  NOTE
2234
2507
    This method is mostly responsible for cleaning up of selects lists and
2236
2509
    to call Query_tables_list::reset_query_tables_list(false).
2237
2510
*/
2238
2511
 
2239
 
void LEX::cleanup_after_one_table_open()
 
2512
void st_lex::cleanup_after_one_table_open()
2240
2513
{
2241
2514
  /*
2242
 
    session->lex->derived_tables & additional units may be set if we open
2243
 
    a view. It is necessary to clear session->lex->derived_tables flag
 
2515
    thd->lex->derived_tables & additional units may be set if we open
 
2516
    a view. It is necessary to clear thd->lex->derived_tables flag
2244
2517
    to prevent processing of derived tables during next open_and_lock_tables
2245
2518
    if next table is a real table and cleanup & remove underlying units
2246
 
    NOTE: all units will be connected to session->lex->select_lex, because we
 
2519
    NOTE: all units will be connected to thd->lex->select_lex, because we
2247
2520
    have not UNION on most upper level.
2248
2521
    */
2249
2522
  if (all_selects_list != &select_lex)
2250
2523
  {
2251
2524
    derived_tables= 0;
2252
2525
    /* cleunup underlying units (units of VIEW) */
2253
 
    for (Select_Lex_Unit *un= select_lex.first_inner_unit();
 
2526
    for (SELECT_LEX_UNIT *un= select_lex.first_inner_unit();
2254
2527
         un;
2255
2528
         un= un->next_unit())
2256
2529
      un->cleanup();
2263
2536
 
2264
2537
 
2265
2538
/*
 
2539
  Save current state of Query_tables_list for this LEX, and prepare it
 
2540
  for processing of new statemnt.
 
2541
 
 
2542
  SYNOPSIS
 
2543
    reset_n_backup_query_tables_list()
 
2544
      backup  Pointer to Query_tables_list instance to be used for backup
 
2545
*/
 
2546
 
 
2547
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2548
{
 
2549
}
 
2550
 
 
2551
 
 
2552
/*
 
2553
  Restore state of Query_tables_list for this LEX from backup.
 
2554
 
 
2555
  SYNOPSIS
 
2556
    restore_backup_query_tables_list()
 
2557
      backup  Pointer to Query_tables_list instance used for backup
 
2558
*/
 
2559
 
 
2560
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2561
{
 
2562
}
 
2563
 
 
2564
 
 
2565
/*
 
2566
  Checks for usage of routines and/or tables in a parsed statement
 
2567
 
 
2568
  SYNOPSIS
 
2569
    st_lex:table_or_sp_used()
 
2570
 
 
2571
  RETURN
 
2572
    false  No routines and tables used
 
2573
    true   Either or both routines and tables are used.
 
2574
*/
 
2575
 
 
2576
bool st_lex::table_or_sp_used()
 
2577
{
 
2578
  if (sroutines.records || query_tables)
 
2579
    return(true);
 
2580
 
 
2581
  return(false);
 
2582
}
 
2583
 
 
2584
 
 
2585
/*
2266
2586
  Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2267
2587
 
2268
2588
  SYNOPSIS
2269
2589
    fix_prepare_info_in_table_list()
2270
 
      session  Thread handle
 
2590
      thd  Thread handle
2271
2591
      tbl  List of tables to process
2272
2592
 
2273
2593
  DESCRIPTION
2277
2597
 
2278
2598
*/
2279
2599
 
2280
 
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
 
2600
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
2281
2601
{
2282
2602
  for (; tbl; tbl= tbl->next_local)
2283
2603
  {
2284
2604
    if (tbl->on_expr)
2285
2605
    {
2286
2606
      tbl->prep_on_expr= tbl->on_expr;
2287
 
      tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
 
2607
      tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
2288
2608
    }
2289
 
    fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
 
2609
    fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
2290
2610
  }
2291
2611
}
2292
2612
 
2293
2613
 
2294
2614
/*
2295
 
  There are Select_Lex::add_table_to_list &
2296
 
  Select_Lex::set_lock_for_tables are in sql_parse.cc
2297
 
 
2298
 
  Select_Lex::print is in sql_select.cc
2299
 
 
2300
 
  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2301
 
  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2302
 
  Select_Lex_Unit::change_result
 
2615
  There are st_select_lex::add_table_to_list &
 
2616
  st_select_lex::set_lock_for_tables are in sql_parse.cc
 
2617
 
 
2618
  st_select_lex::print is in sql_select.cc
 
2619
 
 
2620
  st_select_lex_unit::prepare, st_select_lex_unit::exec,
 
2621
  st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
 
2622
  st_select_lex_unit::change_result
2303
2623
  are in sql_union.cc
2304
2624
*/
2305
2625
 
2313
2633
 
2314
2634
  DESCRIPTION
2315
2635
    Used in filling up the tagged hints list.
2316
 
    This list is filled by first setting the kind of the hint as a
 
2636
    This list is filled by first setting the kind of the hint as a 
2317
2637
    context variable and then adding hints of the current kind.
2318
2638
    Then the context variable index_hint_type can be reset to the
2319
2639
    next hint type.
2320
2640
*/
2321
 
void Select_Lex::set_index_hint_type(enum index_hint_type type_arg,
 
2641
void st_select_lex::set_index_hint_type(enum index_hint_type type_arg,
2322
2642
                                        index_clause_map clause)
2323
 
{
 
2643
2324
2644
  current_index_hint_type= type_arg;
2325
2645
  current_index_hint_clause= clause;
2326
2646
}
2331
2651
 
2332
2652
  SYNOPSIS
2333
2653
    alloc_index_hints()
2334
 
      session         current thread.
 
2654
      thd         current thread.
2335
2655
*/
2336
2656
 
2337
 
void Select_Lex::alloc_index_hints (Session *session)
2338
 
{
2339
 
  index_hints= new (session->mem_root) List<Index_hint>();
 
2657
void st_select_lex::alloc_index_hints (THD *thd)
 
2658
 
2659
  index_hints= new (thd->mem_root) List<Index_hint>(); 
2340
2660
}
2341
2661
 
2342
2662
 
2343
2663
 
2344
2664
/*
2345
 
  adds an element to the array storing index usage hints
 
2665
  adds an element to the array storing index usage hints 
2346
2666
  (ADD/FORCE/IGNORE INDEX).
2347
2667
 
2348
2668
  SYNOPSIS
2349
2669
    add_index_hint()
2350
 
      session         current thread.
 
2670
      thd         current thread.
2351
2671
      str         name of the index.
2352
2672
      length      number of characters in str.
2353
2673
 
2354
2674
  RETURN VALUE
2355
2675
    0 on success, non-zero otherwise
2356
2676
*/
2357
 
bool Select_Lex::add_index_hint (Session *session, char *str, uint32_t length)
 
2677
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
2358
2678
{
2359
 
  return index_hints->push_front (new (session->mem_root)
 
2679
  return index_hints->push_front (new (thd->mem_root) 
2360
2680
                                 Index_hint(current_index_hint_type,
2361
2681
                                            current_index_hint_clause,
2362
2682
                                            str, length));