~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/sql_lex.cc

  • Committer: Brian Aker
  • Date: 2008-07-07 14:25:25 UTC
  • mto: (77.1.25 codestyle)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: brian@tangent.org-20080707142525-xzy2nl3ie2ebwfln
LL() cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
18
18
 
19
 
#define DRIZZLE_LEX 1
20
 
#include <drizzled/server_includes.h>
 
19
#define MYSQL_LEX 1
 
20
#include "mysql_priv.h"
 
21
#include "item_create.h"
 
22
#include <m_ctype.h>
 
23
#include <hash.h>
21
24
 
22
25
static int lex_one_token(void *arg, void *yythd);
23
26
 
42
45
  used when comparing keywords
43
46
*/
44
47
 
45
 
static unsigned char to_upper_lex[]=
 
48
static uchar to_upper_lex[]=
46
49
{
47
50
    0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
48
51
   16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
74
77
  "FORCE INDEX"
75
78
};
76
79
 
77
 
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
 
80
inline int lex_casecmp(const char *s, const char *t, uint len)
78
81
{
79
82
  while (len-- != 0 &&
80
 
         to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
 
83
         to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
81
84
  return (int) len+1;
82
85
}
83
86
 
86
89
 
87
90
void lex_init(void)
88
91
{
89
 
  uint32_t i;
 
92
  uint i;
 
93
  DBUG_ENTER("lex_init");
90
94
  for (i=0 ; i < array_elements(symbols) ; i++)
91
 
    symbols[i].length=(unsigned char) strlen(symbols[i].name);
 
95
    symbols[i].length=(uchar) strlen(symbols[i].name);
92
96
  for (i=0 ; i < array_elements(sql_functions) ; i++)
93
 
    sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
 
97
    sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
94
98
 
95
 
  return;
 
99
  DBUG_VOID_RETURN;
96
100
}
97
101
 
98
102
 
99
103
void lex_free(void)
100
104
{                                       // Call this when daemon ends
101
 
  return;
 
105
  DBUG_ENTER("lex_free");
 
106
  DBUG_VOID_RETURN;
102
107
}
103
108
 
104
109
 
105
110
void
106
111
st_parsing_options::reset()
107
112
{
108
 
  allows_variable= true;
109
 
  allows_select_into= true;
110
 
  allows_select_procedure= true;
111
 
  allows_derived= true;
 
113
  allows_variable= TRUE;
 
114
  allows_select_into= TRUE;
 
115
  allows_select_procedure= TRUE;
 
116
  allows_derived= TRUE;
112
117
}
113
118
 
114
119
Lex_input_stream::Lex_input_stream(THD *thd,
127
132
  m_tok_start_prev(NULL),
128
133
  m_buf(buffer),
129
134
  m_buf_length(length),
130
 
  m_echo(true),
 
135
  m_echo(TRUE),
131
136
  m_cpp_tok_start(NULL),
132
137
  m_cpp_tok_start_prev(NULL),
133
138
  m_cpp_tok_end(NULL),
136
141
  next_state(MY_LEX_START),
137
142
  found_semicolon(NULL),
138
143
  ignore_space(1),
 
144
  stmt_prepare_mode(FALSE),
139
145
  in_comment(NO_COMMENT),
140
146
  m_underscore_cs(NULL)
141
147
{
160
166
 
161
167
void Lex_input_stream::body_utf8_start(THD *thd, const char *begin_ptr)
162
168
{
163
 
  assert(begin_ptr);
164
 
  assert(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
 
169
  DBUG_ASSERT(begin_ptr);
 
170
  DBUG_ASSERT(m_cpp_buf <= begin_ptr && begin_ptr <= m_cpp_buf + m_buf_length);
165
171
 
166
 
  uint32_t body_utf8_length=
 
172
  uint body_utf8_length=
167
173
    (m_buf_length / thd->variables.character_set_client->mbminlen) *
168
174
    my_charset_utf8_bin.mbmaxlen;
169
175
 
198
204
void Lex_input_stream::body_utf8_append(const char *ptr,
199
205
                                        const char *end_ptr)
200
206
{
201
 
  assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
202
 
  assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
 
207
  DBUG_ASSERT(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
 
208
  DBUG_ASSERT(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
203
209
 
204
210
  if (!m_body_utf8)
205
211
    return;
243
249
 
244
250
void Lex_input_stream::body_utf8_append_literal(THD *thd,
245
251
                                                const LEX_STRING *txt,
246
 
                                                const CHARSET_INFO * const txt_cs,
 
252
                                                CHARSET_INFO *txt_cs,
247
253
                                                const char *end_ptr)
248
254
{
249
255
  if (!m_cpp_utf8_processed_ptr)
283
289
void lex_start(THD *thd)
284
290
{
285
291
  LEX *lex= thd->lex;
 
292
  DBUG_ENTER("lex_start");
286
293
 
287
294
  lex->thd= lex->unit.thd= thd;
288
295
 
295
302
  lex->value_list.empty();
296
303
  lex->update_list.empty();
297
304
  lex->param_list.empty();
 
305
  lex->view_list.empty();
298
306
  lex->auxiliary_table_list.empty();
299
307
  lex->unit.next= lex->unit.master=
300
308
    lex->unit.link_next= lex->unit.return_to= 0;
309
317
  lex->select_lex.init_order();
310
318
  lex->select_lex.group_list.empty();
311
319
  lex->describe= 0;
312
 
  lex->subqueries= false;
 
320
  lex->subqueries= FALSE;
313
321
  lex->derived_tables= 0;
314
322
  lex->lock_option= TL_READ;
315
323
  lex->leaf_tables_insert= 0;
326
334
  lex->duplicates= DUP_ERROR;
327
335
  lex->ignore= 0;
328
336
  lex->proc_list.first= 0;
329
 
  lex->escape_used= false;
 
337
  lex->escape_used= FALSE;
330
338
  lex->query_tables= 0;
331
 
  lex->reset_query_tables_list(false);
332
 
  lex->expr_allows_subselect= true;
333
 
  lex->use_only_table_context= false;
334
 
  lex->parse_vcol_expr= false;
 
339
  lex->reset_query_tables_list(FALSE);
 
340
  lex->expr_allows_subselect= TRUE;
 
341
  lex->use_only_table_context= FALSE;
335
342
 
336
343
  lex->name.str= 0;
337
344
  lex->name.length= 0;
340
347
  lex->in_sum_func= NULL;
341
348
  /*
342
349
    ok, there must be a better solution for this, long-term
343
 
    I tried "memset" in the sql_yacc.yy code, but that for
 
350
    I tried "bzero" in the sql_yacc.yy code, but that for
344
351
    some reason made the values zero, even if they were set
345
352
  */
346
353
  lex->server_options.server_name= 0;
350
357
  lex->server_options.username= 0;
351
358
  lex->server_options.password= 0;
352
359
  lex->server_options.scheme= 0;
 
360
  lex->server_options.socket= 0;
353
361
  lex->server_options.owner= 0;
354
362
  lex->server_options.port= -1;
355
363
 
356
 
  lex->is_lex_started= true;
357
 
  return;
 
364
  lex->is_lex_started= TRUE;
 
365
  DBUG_VOID_RETURN;
358
366
}
359
367
 
360
368
void lex_end(LEX *lex)
361
369
{
 
370
  DBUG_ENTER("lex_end");
 
371
  DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
362
372
  if (lex->yacc_yyss)
363
373
  {
364
 
    free(lex->yacc_yyss);
365
 
    free(lex->yacc_yyvs);
 
374
    my_free(lex->yacc_yyss, MYF(0));
 
375
    my_free(lex->yacc_yyvs, MYF(0));
366
376
    lex->yacc_yyss= 0;
367
377
    lex->yacc_yyvs= 0;
368
378
  }
372
382
                     lex->plugins.elements);
373
383
  reset_dynamic(&lex->plugins);
374
384
 
375
 
  delete lex->result;
376
 
  lex->result= 0;
377
 
 
378
 
  return;
 
385
  DBUG_VOID_RETURN;
379
386
}
380
387
 
381
388
 
382
 
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
 
389
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
383
390
{
384
391
  const char *tok= lip->get_tok_start();
385
392
 
408
415
    1         name isn't a keyword
409
416
*/
410
417
 
411
 
bool is_keyword(const char *name, uint32_t len)
 
418
bool is_keyword(const char *name, uint len)
412
419
{
413
 
  assert(len != 0);
 
420
  DBUG_ASSERT(len != 0);
414
421
  return get_hash_symbol(name,len,0)!=0;
415
422
}
416
423
 
417
424
bool is_lex_native_function(const LEX_STRING *name)
418
425
{
419
 
  assert(name != NULL);
 
426
  DBUG_ASSERT(name != NULL);
420
427
  return (get_hash_symbol(name->str, name->length, 1) != 0);
421
428
}
422
429
 
423
430
/* make a copy of token before ptr and set yytoklen */
424
431
 
425
 
static LEX_STRING get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
 
432
static LEX_STRING get_token(Lex_input_stream *lip, uint skip, uint length)
426
433
{
427
434
  LEX_STRING tmp;
428
435
  lip->yyUnget();                       // ptr points now after last token char
443
450
*/
444
451
 
445
452
static LEX_STRING get_quoted_token(Lex_input_stream *lip,
446
 
                                   uint32_t skip,
447
 
                                   uint32_t length, char quote)
 
453
                                   uint skip,
 
454
                                   uint length, char quote)
448
455
{
449
456
  LEX_STRING tmp;
450
457
  const char *from, *end;
479
486
 
480
487
static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
481
488
{
482
 
  register unsigned char c,sep;
483
 
  uint32_t found_escape=0;
484
 
  const CHARSET_INFO * const cs= lip->m_thd->charset();
 
489
  register uchar c,sep;
 
490
  uint found_escape=0;
 
491
  CHARSET_INFO *cs= lip->m_thd->charset();
485
492
 
486
493
  lip->tok_bitmap= 0;
487
494
  sep= lip->yyGetLast();                        // String should end with this
527
534
      /* Extract the text from the token */
528
535
      str += pre_skip;
529
536
      end -= post_skip;
530
 
      assert(end >= str);
 
537
      DBUG_ASSERT(end >= str);
531
538
 
532
539
      if (!(start= (char*) lip->m_thd->alloc((uint) (end-str)+1)))
533
540
        return (char*) "";              // Sql_alloc has set error flag
557
564
              continue;
558
565
          }
559
566
#endif
560
 
          if (*str == '\\' && str+1 != end)
 
567
          if (!(lip->m_thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
568
              *str == '\\' && str+1 != end)
561
569
          {
562
570
            switch(*++str) {
563
571
            case 'n':
603
611
 
604
612
 
605
613
/*
606
 
** Calc type of integer; long integer, int64_t integer or real.
 
614
** Calc type of integer; long integer, longlong integer or real.
607
615
** Returns smallest type that match the string.
608
 
** When using uint64_t values the result is converted to a real
 
616
** When using unsigned long long values the result is converted to a real
609
617
** because else they will be unexpected sign changes because all calculation
610
 
** is done with int64_t or double.
 
618
** is done with longlong or double.
611
619
*/
612
620
 
613
621
static const char *long_str="2147483647";
614
 
static const uint32_t long_len=10;
 
622
static const uint long_len=10;
615
623
static const char *signed_long_str="-2147483648";
616
 
static const char *int64_t_str="9223372036854775807";
617
 
static const uint32_t int64_t_len=19;
618
 
static const char *signed_int64_t_str="-9223372036854775808";
619
 
static const uint32_t signed_int64_t_len=19;
620
 
static const char *unsigned_int64_t_str="18446744073709551615";
621
 
static const uint32_t unsigned_int64_t_len=20;
 
624
static const char *longlong_str="9223372036854775807";
 
625
static const uint longlong_len=19;
 
626
static const char *signed_longlong_str="-9223372036854775808";
 
627
static const uint signed_longlong_len=19;
 
628
static const char *unsigned_longlong_str="18446744073709551615";
 
629
static const uint unsigned_longlong_len=20;
622
630
 
623
 
static inline uint32_t int_token(const char *str,uint32_t length)
 
631
static inline uint int_token(const char *str,uint length)
624
632
{
625
633
  if (length < long_len)                        // quick normal case
626
634
    return NUM;
642
650
  if (length < long_len)
643
651
    return NUM;
644
652
 
645
 
  uint32_t smaller,bigger;
 
653
  uint smaller,bigger;
646
654
  const char *cmp;
647
655
  if (neg)
648
656
  {
652
660
      smaller=NUM;                              // If <= signed_long_str
653
661
      bigger=LONG_NUM;                          // If >= signed_long_str
654
662
    }
655
 
    else if (length < signed_int64_t_len)
 
663
    else if (length < signed_longlong_len)
656
664
      return LONG_NUM;
657
 
    else if (length > signed_int64_t_len)
 
665
    else if (length > signed_longlong_len)
658
666
      return DECIMAL_NUM;
659
667
    else
660
668
    {
661
 
      cmp=signed_int64_t_str+1;
662
 
      smaller=LONG_NUM;                         // If <= signed_int64_t_str
 
669
      cmp=signed_longlong_str+1;
 
670
      smaller=LONG_NUM;                         // If <= signed_longlong_str
663
671
      bigger=DECIMAL_NUM;
664
672
    }
665
673
  }
671
679
      smaller=NUM;
672
680
      bigger=LONG_NUM;
673
681
    }
674
 
    else if (length < int64_t_len)
 
682
    else if (length < longlong_len)
675
683
      return LONG_NUM;
676
 
    else if (length > int64_t_len)
 
684
    else if (length > longlong_len)
677
685
    {
678
 
      if (length > unsigned_int64_t_len)
 
686
      if (length > unsigned_longlong_len)
679
687
        return DECIMAL_NUM;
680
 
      cmp=unsigned_int64_t_str;
 
688
      cmp=unsigned_longlong_str;
681
689
      smaller=ULONGLONG_NUM;
682
690
      bigger=DECIMAL_NUM;
683
691
    }
684
692
    else
685
693
    {
686
 
      cmp=int64_t_str;
 
694
      cmp=longlong_str;
687
695
      smaller=LONG_NUM;
688
696
      bigger= ULONGLONG_NUM;
689
697
    }
690
698
  }
691
699
  while (*cmp && *cmp++ == *str++) ;
692
 
  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
 
700
  return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
693
701
}
694
702
 
695
703
 
726
734
  switch(token) {
727
735
  case WITH:
728
736
    /*
729
 
      Parsing 'WITH' 'ROLLUP' requires 2 look ups,
 
737
      Parsing 'WITH' 'ROLLUP' or 'WITH' 'CUBE' requires 2 look ups,
730
738
      which makes the grammar LALR(2).
731
739
      Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
732
740
      to transform the grammar into a LALR(1) grammar,
733
741
      which sql_yacc.yy can process.
734
742
    */
735
743
    token= lex_one_token(arg, yythd);
736
 
    if (token == ROLLUP_SYM)
737
 
    {
 
744
    switch(token) {
 
745
    case CUBE_SYM:
 
746
      return WITH_CUBE_SYM;
 
747
    case ROLLUP_SYM:
738
748
      return WITH_ROLLUP_SYM;
739
 
    }
740
 
    else
741
 
    {
 
749
    default:
742
750
      /*
743
751
        Save the token following 'WITH'
744
752
      */
766
774
  Lex_input_stream *lip= thd->m_lip;
767
775
  LEX *lex= thd->lex;
768
776
  YYSTYPE *yylval=(YYSTYPE*) arg;
769
 
  const CHARSET_INFO * const cs= thd->charset();
770
 
  unsigned char *state_map= cs->state_map;
771
 
  unsigned char *ident_map= cs->ident_map;
 
777
  CHARSET_INFO *cs= thd->charset();
 
778
  uchar *state_map= cs->state_map;
 
779
  uchar *ident_map= cs->ident_map;
772
780
 
773
781
  lip->yylval=yylval;                   // The global state
774
782
 
827
835
        */
828
836
        lip->restart_token();
829
837
      }
 
838
      else
 
839
      {
 
840
        /*
 
841
          Check for a placeholder: it should not precede a possible identifier
 
842
          because of binlogging: when a placeholder is replaced with
 
843
          its value in a query for the binlog, the query must stay
 
844
          grammatically correct.
 
845
        */
 
846
        if (c == '?' && lip->stmt_prepare_mode && !ident_map[(uint8_t)(lip->yyPeek())])
 
847
        return(PARAM_MARKER);
 
848
      }
830
849
 
831
850
      return((int) c);
832
851
 
 
852
    case MY_LEX_IDENT_OR_NCHAR:
 
853
      if (lip->yyPeek() != '\'')
 
854
      {
 
855
        state= MY_LEX_IDENT;
 
856
        break;
 
857
      }
 
858
      /* Found N'string' */
 
859
      lip->yySkip();                         // Skip '
 
860
      if (!(yylval->lex_str.str = get_text(lip, 2, 1)))
 
861
      {
 
862
        state= MY_LEX_CHAR;             // Read char by char
 
863
        break;
 
864
      }
 
865
      yylval->lex_str.length= lip->yytoklen;
 
866
      lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
 
867
      return(NCHAR_STRING);
 
868
 
833
869
    case MY_LEX_IDENT_OR_HEX:
834
870
      if (lip->yyPeek() == '\'')
835
871
      {                                 // Found x'hex-number'
912
948
 
913
949
      if (yylval->lex_str.str[0] == '_')
914
950
      {
915
 
        const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
916
 
                                                             MY_CS_PRIMARY, MYF(0));
 
951
        CHARSET_INFO *cs= get_charset_by_csname(yylval->lex_str.str + 1,
 
952
                                                MY_CS_PRIMARY, MYF(0));
917
953
        if (cs)
918
954
        {
919
955
          yylval->charset= cs;
1037
1073
 
1038
1074
    case MY_LEX_USER_VARIABLE_DELIMITER:        // Found quote char
1039
1075
    {
1040
 
      uint32_t double_quotes= 0;
 
1076
      uint double_quotes= 0;
1041
1077
      char quote_char= c;                       // Used char
1042
1078
      while ((c=lip->yyGet()))
1043
1079
      {
1214
1250
      {
1215
1251
        lip->in_comment= DISCARD_COMMENT;
1216
1252
        /* Accept '/' '*' '!', but do not keep this marker. */
1217
 
        lip->set_echo(false);
 
1253
        lip->set_echo(FALSE);
1218
1254
        lip->yySkip();
1219
1255
        lip->yySkip();
1220
1256
        lip->yySkip();
1247
1283
          /* Accept 'M' 'm' 'm' 'd' 'd' */
1248
1284
          lip->yySkipn(5);
1249
1285
 
1250
 
          if (version <= DRIZZLE_VERSION_ID)
 
1286
          if (version <= MYSQL_VERSION_ID)
1251
1287
          {
1252
1288
            /* Expand the content of the special comment as real code */
1253
 
            lip->set_echo(true);
 
1289
            lip->set_echo(TRUE);
1254
1290
            state=MY_LEX_START;
1255
1291
            break;
1256
1292
          }
1258
1294
        else
1259
1295
        {
1260
1296
          state=MY_LEX_START;
1261
 
          lip->set_echo(true);
 
1297
          lip->set_echo(TRUE);
1262
1298
          break;
1263
1299
        }
1264
1300
      }
1276
1312
        Note: There is no such thing as nesting comments,
1277
1313
        the first '*' '/' sequence seen will mark the end.
1278
1314
      */
1279
 
      comment_closed= false;
 
1315
      comment_closed= FALSE;
1280
1316
      while (! lip->eof())
1281
1317
      {
1282
1318
        c= lip->yyGet();
1285
1321
          if (lip->yyPeek() == '/')
1286
1322
          {
1287
1323
            lip->yySkip();
1288
 
            comment_closed= true;
 
1324
            comment_closed= TRUE;
1289
1325
            state = MY_LEX_START;
1290
1326
            break;
1291
1327
          }
1298
1334
        return (ABORT_SYM);
1299
1335
      state = MY_LEX_START;             // Try again
1300
1336
      lip->in_comment= NO_COMMENT;
1301
 
      lip->set_echo(true);
 
1337
      lip->set_echo(TRUE);
1302
1338
      break;
1303
1339
    case MY_LEX_END_LONG_COMMENT:
1304
1340
      if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1309
1345
        lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1310
1346
        lip->yySkipn(2);
1311
1347
        /* And start recording the tokens again */
1312
 
        lip->set_echo(true);
 
1348
        lip->set_echo(TRUE);
1313
1349
        lip->in_comment=NO_COMMENT;
1314
1350
        state=MY_LEX_START;
1315
1351
      }
1327
1363
    case MY_LEX_SEMICOLON:                      // optional line terminator
1328
1364
      if (lip->yyPeek())
1329
1365
      {
1330
 
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS))
 
1366
        if ((thd->client_capabilities & CLIENT_MULTI_STATEMENTS) && 
 
1367
            !lip->stmt_prepare_mode)
1331
1368
        {
1332
1369
          lip->found_semicolon= lip->get_ptr();
1333
1370
          thd->server_status|= SERVER_MORE_RESULTS_EXISTS;
1334
1371
          lip->next_state= MY_LEX_END;
1335
 
          lip->set_echo(true);
 
1372
          lip->set_echo(TRUE);
1336
1373
          return (END_OF_INPUT);
1337
1374
        }
1338
1375
        state= MY_LEX_CHAR;             // Return ';'
1344
1381
      if (lip->eof())
1345
1382
      {
1346
1383
        lip->yyUnget();                 // Reject the last '\0'
1347
 
        lip->set_echo(false);
 
1384
        lip->set_echo(FALSE);
1348
1385
        lip->yySkip();
1349
 
        lip->set_echo(true);
 
1386
        lip->set_echo(TRUE);
1350
1387
        /* Unbalanced comments with a missing '*' '/' are a syntax error */
1351
1388
        if (lip->in_comment != NO_COMMENT)
1352
1389
          return (ABORT_SYM);
1476
1513
}
1477
1514
 
1478
1515
 
1479
 
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
 
1516
void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str)
1480
1517
{
1481
1518
  /*
1482
1519
    TODO:
1545
1582
  embedding= leaf_tables= 0;
1546
1583
  item_list.empty();
1547
1584
  join= 0;
1548
 
  having= where= 0;
 
1585
  having= prep_having= where= prep_where= 0;
1549
1586
  olap= UNSPECIFIED_OLAP_TYPE;
1550
1587
  having_fix_field= 0;
1551
1588
  context.select_lex= this;
1568
1605
  select_n_having_items= 0;
1569
1606
  subquery_in_having= explicit_limit= 0;
1570
1607
  is_item_list_lookup= 0;
 
1608
  first_execution= 1;
 
1609
  first_cond_optimization= 1;
1571
1610
  parsing_place= NO_MATTER;
1572
 
  exclude_from_table_unique_test= false;
 
1611
  exclude_from_table_unique_test= no_wrap_view_item= FALSE;
1573
1612
  nest_level= 0;
1574
1613
  link_next= 0;
1575
1614
}
1592
1631
  linkage= UNSPECIFIED_TYPE;
1593
1632
  order_list.elements= 0;
1594
1633
  order_list.first= 0;
1595
 
  order_list.next= (unsigned char**) &order_list.first;
 
1634
  order_list.next= (uchar**) &order_list.first;
1596
1635
  /* Set limit and offset to default values */
1597
1636
  select_limit= 0;      /* denotes the default limit = HA_POS_ERROR */
1598
1637
  offset_limit= 0;      /* denotes the default offset = 0 */
1805
1844
          sl->uncacheable|= UNCACHEABLE_UNITED;
1806
1845
      }
1807
1846
    }
1808
 
    s->is_correlated= true;
 
1847
    s->is_correlated= TRUE;
1809
1848
    Item_subselect *subquery_predicate= s->master_unit()->item;
1810
1849
    if (subquery_predicate)
1811
 
      subquery_predicate->is_correlated= true;
 
1850
      subquery_predicate->is_correlated= TRUE;
1812
1851
  }
1813
1852
}
1814
1853
 
1815
 
bool st_select_lex_node::set_braces(bool value __attribute__((unused)))
1816
 
{ return 1; }
 
1854
bool st_select_lex_node::set_braces(bool value)      { return 1; }
1817
1855
bool st_select_lex_node::inc_in_sum_expr()           { return 1; }
1818
 
uint32_t st_select_lex_node::get_in_sum_expr()           { return 0; }
1819
 
TableList* st_select_lex_node::get_table_list()     { return 0; }
 
1856
uint st_select_lex_node::get_in_sum_expr()           { return 0; }
 
1857
TABLE_LIST* st_select_lex_node::get_table_list()     { return 0; }
1820
1858
List<Item>* st_select_lex_node::get_item_list()      { return 0; }
1821
 
TableList *st_select_lex_node::add_table_to_list (THD *thd __attribute__((unused)),
1822
 
                                                   Table_ident *table __attribute__((unused)),
1823
 
                                                  LEX_STRING *alias __attribute__((unused)),
1824
 
                                                  uint32_t table_join_options __attribute__((unused)),
1825
 
                                                  thr_lock_type flags __attribute__((unused)),
1826
 
                                                  List<Index_hint> *hints __attribute__((unused)),
1827
 
                                                  LEX_STRING *option __attribute__((unused)))
 
1859
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
 
1860
                                                  LEX_STRING *alias,
 
1861
                                                  ulong table_join_options,
 
1862
                                                  thr_lock_type flags,
 
1863
                                                  List<Index_hint> *hints,
 
1864
                                                  LEX_STRING *option)
1828
1865
{
1829
1866
  return 0;
1830
1867
}
1831
 
uint32_t st_select_lex_node::get_table_join_options()
 
1868
ulong st_select_lex_node::get_table_join_options()
1832
1869
{
1833
1870
  return 0;
1834
1871
}
1866
1903
}
1867
1904
 
1868
1905
 
1869
 
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
1870
 
                                     Item *item)
 
1906
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
1871
1907
{
1872
 
  return(item_list.push_back(item));
 
1908
  DBUG_ENTER("st_select_lex::add_item_to_list");
 
1909
  DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
 
1910
  DBUG_RETURN(item_list.push_back(item));
1873
1911
}
1874
1912
 
1875
1913
 
1905
1943
}
1906
1944
 
1907
1945
 
1908
 
uint32_t st_select_lex::get_in_sum_expr()
 
1946
uint st_select_lex::get_in_sum_expr()
1909
1947
{
1910
1948
  return in_sum_expr;
1911
1949
}
1912
1950
 
1913
1951
 
1914
 
TableList* st_select_lex::get_table_list()
 
1952
TABLE_LIST* st_select_lex::get_table_list()
1915
1953
{
1916
 
  return (TableList*) table_list.first;
 
1954
  return (TABLE_LIST*) table_list.first;
1917
1955
}
1918
1956
 
1919
1957
List<Item>* st_select_lex::get_item_list()
1921
1959
  return &item_list;
1922
1960
}
1923
1961
 
1924
 
uint32_t st_select_lex::get_table_join_options()
 
1962
ulong st_select_lex::get_table_join_options()
1925
1963
{
1926
1964
  return table_join_options;
1927
1965
}
1928
1966
 
1929
1967
 
1930
 
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
 
1968
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1931
1969
{
1932
1970
  if (ref_pointer_array)
1933
1971
    return 0;
1934
1972
 
 
1973
  /*
 
1974
    We have to create array in prepared statement memory if it is
 
1975
    prepared statement
 
1976
  */
 
1977
  Query_arena *arena= thd->stmt_arena;
1935
1978
  return (ref_pointer_array=
1936
 
          (Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
 
1979
          (Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1937
1980
                                                 item_list.elements +
1938
1981
                                                 select_n_having_items +
1939
1982
                                                 select_n_where_fields +
1952
1995
      if (union_all)
1953
1996
        str->append(STRING_WITH_LEN("all "));
1954
1997
      else if (union_distinct == sl)
1955
 
        union_all= true;
 
1998
        union_all= TRUE;
1956
1999
    }
1957
2000
    if (sl->braces)
1958
2001
      str->append('(');
1967
2010
      str->append(STRING_WITH_LEN(" order by "));
1968
2011
      fake_select_lex->print_order(
1969
2012
        str,
1970
 
        (order_st *) fake_select_lex->order_list.first,
 
2013
        (ORDER *) fake_select_lex->order_list.first,
1971
2014
        query_type);
1972
2015
    }
1973
2016
    fake_select_lex->print_limit(thd, str, query_type);
1976
2019
 
1977
2020
 
1978
2021
void st_select_lex::print_order(String *str,
1979
 
                                order_st *order,
 
2022
                                ORDER *order,
1980
2023
                                enum_query_type query_type)
1981
2024
{
1982
2025
  for (; order; order= order->next)
1984
2027
    if (order->counter_used)
1985
2028
    {
1986
2029
      char buffer[20];
1987
 
      uint32_t length= snprintf(buffer, 20, "%d", order->counter);
 
2030
      uint length= my_snprintf(buffer, 20, "%d", order->counter);
1988
2031
      str->append(buffer, length);
1989
2032
    }
1990
2033
    else
1997
2040
}
1998
2041
 
1999
2042
 
2000
 
void st_select_lex::print_limit(THD *thd __attribute__((unused)),
 
2043
void st_select_lex::print_limit(THD *thd,
2001
2044
                                String *str,
2002
2045
                                enum_query_type query_type)
2003
2046
{
2011
2054
        subs_type == Item_subselect::IN_SUBS ||
2012
2055
        subs_type == Item_subselect::ALL_SUBS)
2013
2056
    {
2014
 
      assert(!item->fixed ||
 
2057
      DBUG_ASSERT(!item->fixed ||
2015
2058
                  /*
2016
2059
                    If not using materialization both:
2017
2060
                    select_limit == 1, and there should be no offset_limit.
2019
2062
                  (((subs_type == Item_subselect::IN_SUBS) &&
2020
2063
                    ((Item_in_subselect*)item)->exec_method ==
2021
2064
                    Item_in_subselect::MATERIALIZATION) ?
2022
 
                   true :
2023
 
                   (select_limit->val_int() == 1L) &&
 
2065
                   TRUE :
 
2066
                   (select_limit->val_int() == 1LL) &&
2024
2067
                   offset_limit == 0));
2025
2068
      return;
2026
2069
    }
2049
2092
  to implement the clean up.
2050
2093
*/
2051
2094
 
2052
 
void st_lex::cleanup_lex_after_parse_error(THD *thd __attribute__((unused)))
 
2095
void st_lex::cleanup_lex_after_parse_error(THD *thd)
2053
2096
{
2054
2097
}
2055
2098
 
2058
2101
 
2059
2102
  SYNOPSIS
2060
2103
    reset_query_tables_list()
2061
 
      init  true  - we should perform full initialization of object with
 
2104
      init  TRUE  - we should perform full initialization of object with
2062
2105
                    allocating needed memory
2063
 
            false - object is already initialized so we should only reset
 
2106
            FALSE - object is already initialized so we should only reset
2064
2107
                    its state so it can be used for parsing/processing
2065
2108
                    of new statement
2066
2109
 
2075
2118
{
2076
2119
  if (!init && query_tables)
2077
2120
  {
2078
 
    TableList *table= query_tables;
 
2121
    TABLE_LIST *table= query_tables;
2079
2122
    for (;;)
2080
2123
    {
2081
2124
      if (query_tables_last == &table->next_global ||
2142
2185
                         plugins_static_buffer,
2143
2186
                         INITIAL_LEX_PLUGIN_LIST_SIZE, 
2144
2187
                         INITIAL_LEX_PLUGIN_LIST_SIZE);
2145
 
  reset_query_tables_list(true);
 
2188
  reset_query_tables_list(TRUE);
2146
2189
}
2147
2190
 
2148
2191
 
2160
2203
    several underlying tables.
2161
2204
 
2162
2205
  RETURN
2163
 
    false - only temporary table algorithm can be used
2164
 
    true  - merge algorithm can be used
 
2206
    FALSE - only temporary table algorithm can be used
 
2207
    TRUE  - merge algorithm can be used
2165
2208
*/
2166
2209
 
2167
2210
bool st_lex::can_be_merged()
2206
2249
  DESCRIPTION
2207
2250
    Only listed here commands can use merge algorithm in top level
2208
2251
    SELECT_LEX (for subqueries will be used merge algorithm if
2209
 
    st_lex::can_not_use_merged() is not true).
 
2252
    st_lex::can_not_use_merged() is not TRUE).
2210
2253
 
2211
2254
  RETURN
2212
 
    false - command can't use merged VIEWs
2213
 
    true  - VIEWs with MERGE algorithms can be used
 
2255
    FALSE - command can't use merged VIEWs
 
2256
    TRUE  - VIEWs with MERGE algorithms can be used
2214
2257
*/
2215
2258
 
2216
2259
bool st_lex::can_use_merged()
2228
2271
  case SQLCOM_REPLACE:
2229
2272
  case SQLCOM_REPLACE_SELECT:
2230
2273
  case SQLCOM_LOAD:
2231
 
    return true;
 
2274
    return TRUE;
2232
2275
  default:
2233
 
    return false;
 
2276
    return FALSE;
2234
2277
  }
2235
2278
}
2236
2279
 
2245
2288
    listed here (see also st_lex::can_use_merged()).
2246
2289
 
2247
2290
  RETURN
2248
 
    false - command can't use merged VIEWs
2249
 
    true  - VIEWs with MERGE algorithms can be used
 
2291
    FALSE - command can't use merged VIEWs
 
2292
    TRUE  - VIEWs with MERGE algorithms can be used
2250
2293
*/
2251
2294
 
2252
2295
bool st_lex::can_not_use_merged()
2259
2302
    see get_schema_tables_result function
2260
2303
  */
2261
2304
  case SQLCOM_SHOW_FIELDS:
2262
 
    return true;
 
2305
    return TRUE;
2263
2306
  default:
2264
 
    return false;
 
2307
    return FALSE;
2265
2308
  }
2266
2309
}
2267
2310
 
2272
2315
    only_view_structure()
2273
2316
 
2274
2317
  RETURN
2275
 
    true yes, we need only structure
2276
 
    false no, we need data
 
2318
    TRUE yes, we need only structure
 
2319
    FALSE no, we need data
2277
2320
*/
2278
2321
 
2279
2322
bool st_lex::only_view_structure()
2282
2325
  case SQLCOM_SHOW_CREATE:
2283
2326
  case SQLCOM_SHOW_TABLES:
2284
2327
  case SQLCOM_SHOW_FIELDS:
2285
 
    return true;
 
2328
    return TRUE;
2286
2329
  default:
2287
 
    return false;
 
2330
    return FALSE;
2288
2331
  }
2289
2332
}
2290
2333
 
2296
2339
    need_correct_ident()
2297
2340
 
2298
2341
  RETURN
2299
 
    true yes, we need only structure
2300
 
    false no, we need data
 
2342
    TRUE yes, we need only structure
 
2343
    FALSE no, we need data
2301
2344
*/
2302
2345
 
2303
2346
 
2307
2350
  {
2308
2351
  case SQLCOM_SHOW_CREATE:
2309
2352
  case SQLCOM_SHOW_TABLES:
2310
 
    return true;
 
2353
    return TRUE;
2311
2354
  default:
2312
 
    return false;
 
2355
    return FALSE;
2313
2356
  }
2314
2357
}
2315
2358
 
 
2359
/*
 
2360
  Get effective type of CHECK OPTION for given view
 
2361
 
 
2362
  SYNOPSIS
 
2363
    get_effective_with_check()
 
2364
    view    given view
 
2365
 
 
2366
  NOTE
 
2367
    It have not sense to set CHECK OPTION for SELECT satement or subqueries,
 
2368
    so we do not.
 
2369
 
 
2370
  RETURN
 
2371
    VIEW_CHECK_NONE      no need CHECK OPTION
 
2372
    VIEW_CHECK_LOCAL     CHECK OPTION LOCAL
 
2373
    VIEW_CHECK_CASCADED  CHECK OPTION CASCADED
 
2374
*/
 
2375
 
 
2376
uint8 st_lex::get_effective_with_check(TABLE_LIST *view)
 
2377
{
 
2378
  return 0;
 
2379
}
 
2380
 
2316
2381
 
2317
2382
/**
2318
2383
  This method should be called only during parsing.
2329
2394
 
2330
2395
  This method is needed to support this rule.
2331
2396
 
2332
 
  @return true in case of error (parsing should be aborted, false in
 
2397
  @return TRUE in case of error (parsing should be aborted, FALSE in
2333
2398
  case of success
2334
2399
*/
2335
2400
 
2350
2415
void st_select_lex_unit::set_limit(st_select_lex *sl)
2351
2416
{
2352
2417
  ha_rows select_limit_val;
2353
 
  uint64_t val;
 
2418
  ulonglong val;
2354
2419
 
2355
2420
  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2356
2421
  select_limit_val= (ha_rows)val;
2357
2422
#ifndef BIG_TABLES
2358
2423
  /* 
2359
 
    Check for overflow : ha_rows can be smaller then uint64_t if
 
2424
    Check for overflow : ha_rows can be smaller then ulonglong if
2360
2425
    BIG_TABLES is off.
2361
2426
    */
2362
 
  if (val != (uint64_t)select_limit_val)
 
2427
  if (val != (ulonglong)select_limit_val)
2363
2428
    select_limit_val= HA_POS_ERROR;
2364
2429
#endif
2365
2430
  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2366
 
                                                 0UL);
 
2431
                                                 0ULL);
2367
2432
  select_limit_cnt= select_limit_val + offset_limit_cnt;
2368
2433
  if (select_limit_cnt < select_limit_val)
2369
2434
    select_limit_cnt= HA_POS_ERROR;             // no limit
2388
2453
      In this case link_to_local is set.
2389
2454
 
2390
2455
*/
2391
 
TableList *st_lex::unlink_first_table(bool *link_to_local)
 
2456
TABLE_LIST *st_lex::unlink_first_table(bool *link_to_local)
2392
2457
{
2393
 
  TableList *first;
 
2458
  TABLE_LIST *first;
2394
2459
  if ((first= query_tables))
2395
2460
  {
2396
2461
    /*
2409
2474
    {
2410
2475
      select_lex.context.table_list= 
2411
2476
        select_lex.context.first_name_resolution_table= first->next_local;
2412
 
      select_lex.table_list.first= (unsigned char*) (first->next_local);
 
2477
      select_lex.table_list.first= (uchar*) (first->next_local);
2413
2478
      select_lex.table_list.elements--; //safety
2414
2479
      first->next_local= 0;
2415
2480
      /*
2441
2506
 
2442
2507
void st_lex::first_lists_tables_same()
2443
2508
{
2444
 
  TableList *first_table= (TableList*) select_lex.table_list.first;
 
2509
  TABLE_LIST *first_table= (TABLE_LIST*) select_lex.table_list.first;
2445
2510
  if (query_tables != first_table && first_table != 0)
2446
2511
  {
2447
 
    TableList *next;
 
2512
    TABLE_LIST *next;
2448
2513
    if (query_tables_last == &first_table->next_global)
2449
2514
      query_tables_last= first_table->prev_global;
2450
2515
 
2475
2540
    global list
2476
2541
*/
2477
2542
 
2478
 
void st_lex::link_first_table_back(TableList *first,
 
2543
void st_lex::link_first_table_back(TABLE_LIST *first,
2479
2544
                                   bool link_to_local)
2480
2545
{
2481
2546
  if (first)
2488
2553
 
2489
2554
    if (link_to_local)
2490
2555
    {
2491
 
      first->next_local= (TableList*) select_lex.table_list.first;
 
2556
      first->next_local= (TABLE_LIST*) select_lex.table_list.first;
2492
2557
      select_lex.context.table_list= first;
2493
 
      select_lex.table_list.first= (unsigned char*) first;
 
2558
      select_lex.table_list.first= (uchar*) first;
2494
2559
      select_lex.table_list.elements++; //safety
2495
2560
    }
2496
2561
  }
2507
2572
  NOTE
2508
2573
    This method is mostly responsible for cleaning up of selects lists and
2509
2574
    derived tables state. To rollback changes in Query_tables_list one has
2510
 
    to call Query_tables_list::reset_query_tables_list(false).
 
2575
    to call Query_tables_list::reset_query_tables_list(FALSE).
2511
2576
*/
2512
2577
 
2513
2578
void st_lex::cleanup_after_one_table_open()
2545
2610
      backup  Pointer to Query_tables_list instance to be used for backup
2546
2611
*/
2547
2612
 
2548
 
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2613
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup)
2549
2614
{
2550
2615
}
2551
2616
 
2558
2623
      backup  Pointer to Query_tables_list instance used for backup
2559
2624
*/
2560
2625
 
2561
 
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
 
2626
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup)
2562
2627
{
2563
2628
}
2564
2629
 
2570
2635
    st_lex:table_or_sp_used()
2571
2636
 
2572
2637
  RETURN
2573
 
    false  No routines and tables used
2574
 
    true   Either or both routines and tables are used.
 
2638
    FALSE  No routines and tables used
 
2639
    TRUE   Either or both routines and tables are used.
2575
2640
*/
2576
2641
 
2577
2642
bool st_lex::table_or_sp_used()
2578
2643
{
 
2644
  DBUG_ENTER("table_or_sp_used");
 
2645
 
2579
2646
  if (sroutines.records || query_tables)
2580
 
    return(true);
 
2647
    DBUG_RETURN(TRUE);
2581
2648
 
2582
 
  return(false);
 
2649
  DBUG_RETURN(FALSE);
2583
2650
}
2584
2651
 
2585
2652
 
2598
2665
 
2599
2666
*/
2600
2667
 
2601
 
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
 
2668
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
2602
2669
{
2603
2670
  for (; tbl; tbl= tbl->next_local)
2604
2671
  {
2613
2680
 
2614
2681
 
2615
2682
/*
 
2683
  Save WHERE/HAVING/ON clauses and replace them with disposable copies
 
2684
 
 
2685
  SYNOPSIS
 
2686
    st_select_lex::fix_prepare_information
 
2687
      thd          thread handler
 
2688
      conds        in/out pointer to WHERE condition to be met at execution
 
2689
      having_conds in/out pointer to HAVING condition to be met at execution
 
2690
  
 
2691
  DESCRIPTION
 
2692
    The passed WHERE and HAVING are to be saved for the future executions.
 
2693
    This function saves it, and returns a copy which can be thrashed during
 
2694
    this execution of the statement. By saving/thrashing here we mean only
 
2695
    AND/OR trees.
 
2696
    The function also calls fix_prepare_info_in_table_list that saves all
 
2697
    ON expressions.    
 
2698
*/
 
2699
 
 
2700
void st_select_lex::fix_prepare_information(THD *thd, Item **conds, 
 
2701
                                            Item **having_conds)
 
2702
{
 
2703
  if (thd->stmt_arena->is_conventional() == false && first_execution)
 
2704
  {
 
2705
    first_execution= 0;
 
2706
    if (*conds)
 
2707
    {
 
2708
      prep_where= *conds;
 
2709
      *conds= where= prep_where->copy_andor_structure(thd);
 
2710
    }
 
2711
    if (*having_conds)
 
2712
    {
 
2713
      prep_having= *having_conds;
 
2714
      *having_conds= having= prep_having->copy_andor_structure(thd);
 
2715
    }
 
2716
    fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
 
2717
  }
 
2718
}
 
2719
 
 
2720
 
 
2721
/*
2616
2722
  There are st_select_lex::add_table_to_list &
2617
2723
  st_select_lex::set_lock_for_tables are in sql_parse.cc
2618
2724
 
2675
2781
  RETURN VALUE
2676
2782
    0 on success, non-zero otherwise
2677
2783
*/
2678
 
bool st_select_lex::add_index_hint (THD *thd, char *str, uint32_t length)
 
2784
bool st_select_lex::add_index_hint (THD *thd, char *str, uint length)
2679
2785
{
2680
2786
  return index_hints->push_front (new (thd->mem_root) 
2681
2787
                                 Index_hint(current_index_hint_type,