Drizzled Public API Documentation

sql_lex.cc
1 /* Copyright (C) 2000-2006 MySQL AB
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 
17 /* A lexical scanner on a temporary buffer with a yacc interface */
18 
19 #include <config.h>
20 
21 #define DRIZZLE_LEX 1
22 
23 #include <drizzled/sql_reserved_words.h>
24 
25 #include <drizzled/configmake.h>
26 #include <drizzled/item/num.h>
27 #include <drizzled/error.h>
28 #include <drizzled/session.h>
29 #include <drizzled/sql_base.h>
30 #include <drizzled/lookup_symbol.h>
31 #include <drizzled/index_hint.h>
32 #include <drizzled/select_result.h>
33 #include <drizzled/item/subselect.h>
34 #include <drizzled/statement.h>
35 #include <drizzled/sql_lex.h>
36 #include <drizzled/plugin.h>
37 #include <drizzled/lex_input_stream.h>
38 
39 #include <cstdio>
40 #include <ctype.h>
41 
42 #include <drizzled/message/alter_table.pb.h>
43 
44 union ParserType;
45 
46 using namespace std;
47 
48 /* Stay outside of the namespace because otherwise bison goes nuts */
49 int base_sql_lex(ParserType *arg, drizzled::Session *yysession);
50 
51 namespace drizzled {
52 
53 static int lex_one_token(ParserType *arg, drizzled::Session *yysession);
54 
58 static void add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
59 {
60  Order* order = new (session->mem) Order;
61  order->item_ptr= item;
62  order->item= &order->item_ptr;
63  order->asc = asc;
64  order->free_me=0;
65  order->used=0;
66  order->counter_used= 0;
67  list.link_in_list((unsigned char*) order, (unsigned char**) &order->next);
68 }
69 
70 Lex_input_stream::Lex_input_stream(Session& session, str_ref buffer) :
71  m_session(&session),
72  yylineno(1),
73  yytoklen(0),
74  yylval(NULL),
75  lookahead_token(END_OF_INPUT),
76  lookahead_yylval(NULL),
77  m_ptr(buffer.data()),
78  m_tok_start(NULL),
79  m_tok_end(NULL),
80  m_end_of_query(buffer.end()),
81  m_tok_start_prev(NULL),
82  m_buf(buffer.data()),
83  m_buf_length(buffer.size()),
84  m_echo(true),
85  m_cpp_tok_start(NULL),
86  m_cpp_tok_start_prev(NULL),
87  m_cpp_tok_end(NULL),
88  m_body_utf8(NULL),
89  m_cpp_utf8_processed_ptr(NULL),
90  next_state(MY_LEX_START),
91  ignore_space(1),
92  in_comment(NO_COMMENT)
93 {
94  m_cpp_buf= (char*) session.mem.alloc(buffer.size() + 1);
95  m_cpp_ptr= m_cpp_buf;
96 }
97 
118 void Lex_input_stream::body_utf8_append(const char *ptr, const char *end_ptr)
119 {
120  assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
121  assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
122 
123  if (!m_body_utf8)
124  return;
125 
126  if (m_cpp_utf8_processed_ptr >= ptr)
127  return;
128 
129  int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
130 
131  memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
132  m_body_utf8_ptr += bytes_to_copy;
133  *m_body_utf8_ptr= 0;
134 
135  m_cpp_utf8_processed_ptr= end_ptr;
136 }
137 
145 void Lex_input_stream::body_utf8_append(const char *ptr)
146 {
147  body_utf8_append(ptr, ptr);
148 }
149 
161 void Lex_input_stream::body_utf8_append_literal(str_ref txt, const char *end_ptr)
162 {
163  if (!m_cpp_utf8_processed_ptr)
164  return;
165 
166  /* NOTE: utf_txt.length is in bytes, not in symbols. */
167 
168  memcpy(m_body_utf8_ptr, txt.data(), txt.size());
169  m_body_utf8_ptr += txt.size();
170  *m_body_utf8_ptr= 0;
171 
172  m_cpp_utf8_processed_ptr= end_ptr;
173 }
174 
175 /*
176  This is called before every query that is to be parsed.
177  Because of this, it's critical to not do too much things here.
178  (We already do too much here)
179 */
180 void LEX::start(Session *arg)
181 {
182  lex_start(arg);
183 }
184 
185 void lex_start(Session *session)
186 {
187  LEX *lex= &session->lex();
188 
189  lex->session= lex->unit.session= session;
190 
191  lex->context_stack.clear();
192  lex->unit.init_query();
193  lex->unit.init_select();
194  /* 'parent_lex' is used in init_query() so it must be before it. */
195  lex->select_lex.parent_lex= lex;
196  lex->select_lex.init_query();
197  lex->value_list.clear();
198  lex->update_list.clear();
199  lex->auxiliary_table_list.clear();
200  lex->unit.next= lex->unit.master=
201  lex->unit.link_next= lex->unit.return_to= 0;
202  lex->unit.prev= lex->unit.link_prev= 0;
203  lex->unit.slave= lex->unit.global_parameters= lex->current_select=
204  lex->all_selects_list= &lex->select_lex;
205  lex->select_lex.master= &lex->unit;
206  lex->select_lex.prev= &lex->unit.slave;
207  lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
208  lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
209  lex->select_lex.options= 0;
210  lex->select_lex.init_order();
211  lex->select_lex.group_list.clear();
212  lex->describe= 0;
213  lex->derived_tables= 0;
214  lex->lock_option= TL_READ;
215  lex->leaf_tables_insert= 0;
216  lex->var_list.clear();
217  lex->select_lex.select_number= 1;
218  lex->length=0;
219  lex->select_lex.in_sum_expr=0;
220  lex->select_lex.group_list.clear();
221  lex->select_lex.order_list.clear();
222  lex->sql_command= SQLCOM_END;
223  lex->duplicates= DUP_ERROR;
224  lex->ignore= 0;
225  lex->escape_used= false;
226  lex->query_tables= 0;
227  lex->reset_query_tables_list(false);
228  lex->expr_allows_subselect= true;
229  lex->use_only_table_context= false;
230 
231  lex->name.assign(NULL, 0);
232  lex->nest_level=0 ;
233  lex->allow_sum_func= 0;
234  lex->in_sum_func= NULL;
235  lex->type= 0;
236 
237  lex->is_lex_started= true;
238  lex->statement= NULL;
239 
240  lex->is_cross= false;
241  lex->reset();
242 }
243 
244 void LEX::end()
245 {
246  if (yacc_yyss)
247  {
248  free(yacc_yyss);
249  free(yacc_yyvs);
250  yacc_yyss= 0;
251  yacc_yyvs= 0;
252  }
253 
254  safe_delete(result);
255  safe_delete(_create_table);
256  safe_delete(_alter_table);
257  _create_table= NULL;
258  _alter_table= NULL;
259  _create_field= NULL;
260 
261  result= 0;
262  setCacheable(true);
263 
264  safe_delete(statement);
265 }
266 
267 static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
268 {
269  /* Plenty of memory for the largest lex symbol we have */
270  char tok_upper[64];
271  const char *tok= lip->get_tok_start();
272  uint32_t tok_pos= 0;
273  for (;tok_pos<len && tok_pos<63;tok_pos++)
274  tok_upper[tok_pos]= system_charset_info->toupper(tok[tok_pos]);
275  tok_upper[tok_pos]=0;
276 
277  const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
278  if (symbol)
279  {
280  lip->yylval->symbol.symbol=symbol;
281  lip->yylval->symbol.str= (char*) tok;
282  lip->yylval->symbol.length=len;
283 
284  return symbol->tok;
285  }
286 
287  return 0;
288 }
289 
290 /* make a copy of token before ptr and set yytoklen */
291 static lex_string_t get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
292 {
293  lip->yyUnget(); // ptr points now after last token char
294  lip->yytoklen= length;
295  lex_string_t tmp;
296  tmp.assign(lip->m_session->mem.strdup(lip->get_tok_start() + skip, length), length);
297  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
298  lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.size();
299  return tmp;
300 }
301 
302 /*
303  todo:
304  There are no dangerous charsets in mysql for function
305  get_quoted_token yet. But it should be fixed in the
306  future to operate multichar strings (like ucs2)
307 */
308 static lex_string_t get_quoted_token(Lex_input_stream *lip,
309  uint32_t skip,
310  uint32_t length, char quote)
311 {
312  lip->yyUnget(); // ptr points now after last token char
313  lip->yytoklen= length;
314  lex_string_t tmp;
315  tmp.assign((char*)lip->m_session->mem.alloc(length + 1), length);
316  const char* from= lip->get_tok_start() + skip;
317  char* to= (char*)tmp.data();
318  const char* end= to+length;
319 
320  lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
321  lip->m_cpp_text_end= lip->m_cpp_text_start + length;
322 
323  for ( ; to != end; )
324  {
325  if ((*to++= *from++) == quote)
326  {
327  from++; // Skip double quotes
328  lip->m_cpp_text_start++;
329  }
330  }
331  *to= 0; // End null for safety
332  return tmp;
333 }
334 
335 
336 /*
337  Return an unescaped text literal without quotes
338  Fix sometimes to do only one scan of the string
339 */
340 static char *get_text(Lex_input_stream *lip, int pre_skip, int post_skip)
341 {
342  bool found_escape= false;
343  const charset_info_st* const cs= lip->m_session->charset();
344 
345  lip->tok_bitmap= 0;
346  unsigned char sep= lip->yyGetLast(); // String should end with this
347  while (not lip->eof())
348  {
349  unsigned char c= lip->yyGet();
350  lip->tok_bitmap|= c;
351  {
352  if (use_mb(cs))
353  {
354  int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
355  if (l != 0)
356  {
357  lip->skip_binary(l-1);
358  continue;
359  }
360  }
361  }
362  if (c == '\\')
363  { // Escaped character
364  found_escape= true;
365  if (lip->eof())
366  return 0;
367  lip->yySkip();
368  }
369  else if (c == sep)
370  {
371  if (c == lip->yyGet()) // Check if two separators in a row
372  {
373  found_escape= true; // duplicate. Remember for delete
374  continue;
375  }
376  else
377  lip->yyUnget();
378 
379  /* Found end. Unescape and return string */
380  const char* str= lip->get_tok_start();
381  const char* end= lip->get_ptr();
382  /* Extract the text from the token */
383  str+= pre_skip;
384  end-= post_skip;
385  assert(end >= str);
386 
387  char* start= (char*) lip->m_session->mem.alloc((uint32_t) (end-str)+1);
388 
389  lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
390  lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
391 
392  if (! found_escape)
393  {
394  lip->yytoklen= (uint32_t) (end-str);
395  memcpy(start, str, lip->yytoklen);
396  start[lip->yytoklen]= 0;
397  }
398  else
399  {
400  char *to;
401 
402  for (to= start; str != end; str++)
403  {
404  if (use_mb(cs))
405  {
406  int l= my_ismbchar(cs, str, end);
407  if (l != 0)
408  {
409  while (l--)
410  *to++= *str++;
411  str--;
412  continue;
413  }
414  }
415  if (*str == '\\' && (str + 1) != end)
416  {
417  switch (*++str) {
418  case 'n':
419  *to++= '\n';
420  break;
421  case 't':
422  *to++= '\t';
423  break;
424  case 'r':
425  *to++= '\r';
426  break;
427  case 'b':
428  *to++= '\b';
429  break;
430  case '0':
431  *to++= 0; // Ascii null
432  break;
433  case 'Z': // ^Z must be escaped on Win32
434  *to++= '\032';
435  break;
436  case '_':
437  case '%':
438  *to++= '\\'; // remember prefix for wildcard
439  /* Fall through */
440  default:
441  *to++= *str;
442  break;
443  }
444  }
445  else if (*str == sep)
446  *to++= *str++; // Two ' or "
447  else
448  *to++ = *str;
449  }
450  *to= 0;
451  lip->yytoklen= (uint32_t) (to - start);
452  }
453  return start;
454  }
455  }
456  return 0; // unexpected end of query
457 }
458 
459 
460 /*
461 ** Calc type of integer; long integer, int64_t integer or real.
462 ** Returns smallest type that match the string.
463 ** When using uint64_t values the result is converted to a real
464 ** because else they will be unexpected sign changes because all calculation
465 ** is done with int64_t or double.
466 */
467 
468 static const char *long_str= "2147483647";
469 static const uint32_t long_len= 10;
470 static const char *signed_long_str= "-2147483648";
471 static const char *int64_t_str= "9223372036854775807";
472 static const uint32_t int64_t_len= 19;
473 static const char *signed_int64_t_str= "-9223372036854775808";
474 static const uint32_t signed_int64_t_len= 19;
475 static const char *unsigned_int64_t_str= "18446744073709551615";
476 static const uint32_t unsigned_int64_t_len= 20;
477 
478 static inline uint32_t int_token(const char *str,uint32_t length)
479 {
480  if (length < long_len) // quick normal case
481  return NUM;
482  bool neg=0;
483 
484  if (*str == '+') // Remove sign and pre-zeros
485  {
486  str++; length--;
487  }
488  else if (*str == '-')
489  {
490  str++; length--;
491  neg=1;
492  }
493  while (*str == '0' && length)
494  {
495  str++; length --;
496  }
497  if (length < long_len)
498  return NUM;
499 
500  uint32_t smaller,bigger;
501  const char *cmp;
502  if (neg)
503  {
504  if (length == long_len)
505  {
506  cmp= signed_long_str+1;
507  smaller=NUM; // If <= signed_long_str
508  bigger=LONG_NUM; // If >= signed_long_str
509  }
510  else if (length < signed_int64_t_len)
511  return LONG_NUM;
512  else if (length > signed_int64_t_len)
513  return DECIMAL_NUM;
514  else
515  {
516  cmp=signed_int64_t_str+1;
517  smaller=LONG_NUM; // If <= signed_int64_t_str
518  bigger=DECIMAL_NUM;
519  }
520  }
521  else
522  {
523  if (length == long_len)
524  {
525  cmp= long_str;
526  smaller=NUM;
527  bigger=LONG_NUM;
528  }
529  else if (length < int64_t_len)
530  return LONG_NUM;
531  else if (length > int64_t_len)
532  {
533  if (length > unsigned_int64_t_len)
534  return DECIMAL_NUM;
535  cmp=unsigned_int64_t_str;
536  smaller=ULONGLONG_NUM;
537  bigger=DECIMAL_NUM;
538  }
539  else
540  {
541  cmp=int64_t_str;
542  smaller=LONG_NUM;
543  bigger= ULONGLONG_NUM;
544  }
545  }
546  while (*cmp && *cmp++ == *str++) ;
547  return ((unsigned char) str[-1] <= (unsigned char) cmp[-1]) ? smaller : bigger;
548 }
549 
550 } /* namespace drizzled */
551 /*
552  base_sql_lex remember the following states from the following sql_baselex()
553 
554  - MY_LEX_EOQ Found end of query
555  - MY_LEX_OPERATOR_OR_IDENT Last state was an ident, text or number
556  (which can't be followed by a signed number)
557 */
558 int base_sql_lex(union ParserType *yylval, drizzled::Session *session)
559 {
560  drizzled::Lex_input_stream *lip= session->m_lip;
561  int token;
562 
563  if (lip->lookahead_token != END_OF_INPUT)
564  {
565  /*
566  The next token was already parsed in advance,
567  return it.
568  */
569  token= lip->lookahead_token;
570  lip->lookahead_token= END_OF_INPUT;
571  *yylval= *(lip->lookahead_yylval);
572  lip->lookahead_yylval= NULL;
573  return token;
574  }
575 
576  token= drizzled::lex_one_token(yylval, session);
577 
578  switch(token) {
579  case WITH:
580  /*
581  Parsing 'WITH' 'ROLLUP' requires 2 look ups,
582  which makes the grammar LALR(2).
583  Replace by a single 'WITH_ROLLUP' or 'WITH_CUBE' token,
584  to transform the grammar into a LALR(1) grammar,
585  which sql_yacc.yy can process.
586  */
587  token= drizzled::lex_one_token(yylval, session);
588  if (token == ROLLUP_SYM)
589  {
590  return WITH_ROLLUP_SYM;
591  }
592  else
593  {
594  /*
595  Save the token following 'WITH'
596  */
597  lip->lookahead_yylval= lip->yylval;
598  lip->yylval= NULL;
599  lip->lookahead_token= token;
600  return WITH;
601  }
602  default:
603  break;
604  }
605 
606  return token;
607 }
608 
609 namespace drizzled
610 {
611 
612 int lex_one_token(ParserType *yylval, drizzled::Session *session)
613 {
614  unsigned char c= 0; /* Just set to shutup GCC */
615  bool comment_closed;
616  int tokval, result_state;
617  unsigned int length;
618  enum my_lex_states state;
619  Lex_input_stream *lip= session->m_lip;
620  LEX *lex= &session->lex();
621  const charset_info_st * const cs= session->charset();
622  unsigned char *state_map= cs->state_map;
623  unsigned char *ident_map= cs->ident_map;
624 
625  lip->yylval=yylval; // The global state
626 
627  lip->start_token();
628  state=lip->next_state;
629  lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
630  for (;;)
631  {
632  switch (state) {
633  case MY_LEX_OPERATOR_OR_IDENT: // Next is operator or keyword
634  case MY_LEX_START: // Start of token
635  // Skip starting whitespace
636  while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
637  {
638  if (c == '\n')
639  lip->yylineno++;
640 
641  lip->yySkip();
642  }
643 
644  /* Start of real token */
645  lip->restart_token();
646  c= lip->yyGet();
647  state= (enum my_lex_states) state_map[c];
648  break;
649  case MY_LEX_ESCAPE:
650  if (lip->yyGet() == 'N')
651  { // Allow \N as shortcut for NULL
652  yylval->lex_str.assign("\\N", 2);
653  return NULL_SYM;
654  }
655  case MY_LEX_CHAR: // Unknown or single char token
656  case MY_LEX_SKIP: // This should not happen
657  if (c == '-' && lip->yyPeek() == '-' &&
658  (cs->isspace(lip->yyPeekn(1)) ||
659  cs->iscntrl(lip->yyPeekn(1))))
660  {
661  state=MY_LEX_COMMENT;
662  break;
663  }
664 
665  if (c != ')')
666  lip->next_state= MY_LEX_START; // Allow signed numbers
667 
668  if (c == ',')
669  {
670  /*
671  Warning:
672  This is a work around, to make the "remember_name" rule in
673  sql/sql_yacc.yy work properly.
674  The problem is that, when parsing "select expr1, expr2",
675  the code generated by bison executes the *pre* action
676  remember_name (see select_item) *before* actually parsing the
677  first token of expr2.
678  */
679  lip->restart_token();
680  }
681 
682  return((int) c);
683 
684  case MY_LEX_IDENT_OR_HEX:
685  if (lip->yyPeek() == '\'')
686  { // Found x'hex-number'
687  state= MY_LEX_HEX_NUMBER;
688  break;
689  }
690  case MY_LEX_IDENT_OR_BIN:
691  if (lip->yyPeek() == '\'')
692  { // Found b'bin-number'
693  state= MY_LEX_BIN_NUMBER;
694  break;
695  }
696  case MY_LEX_IDENT:
697  const char *start;
698  if (use_mb(cs))
699  {
700  result_state= IDENT_QUOTED;
701  if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
702  {
703  int l = my_ismbchar(cs,
704  lip->get_ptr() -1,
705  lip->get_end_of_query());
706  if (l == 0) {
707  state = MY_LEX_CHAR;
708  continue;
709  }
710  lip->skip_binary(l - 1);
711  }
712  while (ident_map[c=lip->yyGet()])
713  {
714  if (my_mbcharlen(cs, c) > 1)
715  {
716  int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
717  if (l == 0)
718  break;
719  lip->skip_binary(l-1);
720  }
721  }
722  }
723  else
724  {
725  for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
726  /* If there were non-ASCII characters, mark that we must convert */
727  result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
728  }
729  length= lip->yyLength();
730  start= lip->get_ptr();
731  if (lip->ignore_space)
732  {
733  /*
734  If we find a space then this can't be an identifier. We notice this
735  below by checking start != lex->ptr.
736  */
737  for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
738  }
739  if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
740  lip->next_state=MY_LEX_IDENT_SEP;
741  else
742  { // '(' must follow directly if function
743  lip->yyUnget();
744  if ((tokval = find_keyword(lip, length, c == '(')))
745  {
746  lip->next_state= MY_LEX_START; // Allow signed numbers
747  return(tokval); // Was keyword
748  }
749  lip->yySkip(); // next state does a unget
750  }
751  yylval->lex_str= get_token(lip, 0, length);
752 
753  lip->body_utf8_append(lip->m_cpp_text_start);
754 
755  lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
756 
757  return(result_state); // IDENT or IDENT_QUOTED
758 
759  case MY_LEX_IDENT_SEP: // Found ident and now '.'
760  yylval->lex_str.assign(lip->get_ptr(), 1);
761  c= lip->yyGet(); // should be '.'
762  lip->next_state= MY_LEX_IDENT_START;// Next is an ident (not a keyword)
763  if (!ident_map[(uint8_t)lip->yyPeek()]) // Probably ` or "
764  lip->next_state= MY_LEX_START;
765  return((int) c);
766 
767  case MY_LEX_NUMBER_IDENT: // number or ident which num-start
768  if (lip->yyGetLast() == '0')
769  {
770  c= lip->yyGet();
771  if (c == 'x')
772  {
773  while (cs->isxdigit((c = lip->yyGet()))) ;
774  if ((lip->yyLength() >= 3) && !ident_map[c])
775  {
776  /* skip '0x' */
777  yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
778  return (HEX_NUM);
779  }
780  lip->yyUnget();
781  state= MY_LEX_IDENT_START;
782  break;
783  }
784  else if (c == 'b')
785  {
786  while ((c= lip->yyGet()) == '0' || c == '1') {};
787  if ((lip->yyLength() >= 3) && !ident_map[c])
788  {
789  /* Skip '0b' */
790  yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
791  return (BIN_NUM);
792  }
793  lip->yyUnget();
794  state= MY_LEX_IDENT_START;
795  break;
796  }
797  lip->yyUnget();
798  }
799 
800  while (cs->isdigit((c = lip->yyGet()))) ;
801  if (!ident_map[c])
802  { // Can't be identifier
803  state=MY_LEX_INT_OR_REAL;
804  break;
805  }
806  if (c == 'e' || c == 'E')
807  {
808  // The following test is written this way to allow numbers of type 1e1
809  if (cs->isdigit(lip->yyPeek()) ||
810  (c=(lip->yyGet())) == '+' || c == '-')
811  { // Allow 1E+10
812  if (cs->isdigit(lip->yyPeek())) // Number must have digit after sign
813  {
814  lip->yySkip();
815  while (cs->isdigit(lip->yyGet())) ;
816  yylval->lex_str= get_token(lip, 0, lip->yyLength());
817  return(FLOAT_NUM);
818  }
819  }
820  lip->yyUnget();
821  }
822  // fall through
823  case MY_LEX_IDENT_START: // We come here after '.'
824  result_state= IDENT;
825  if (use_mb(cs))
826  {
827  result_state= IDENT_QUOTED;
828  while (ident_map[c=lip->yyGet()])
829  {
830  if (my_mbcharlen(cs, c) > 1)
831  {
832  int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
833  if (l == 0)
834  break;
835  lip->skip_binary(l-1);
836  }
837  }
838  }
839  else
840  {
841  for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
842  /* If there were non-ASCII characters, mark that we must convert */
843  result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
844  }
845  if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
846  lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
847 
848  yylval->lex_str= get_token(lip, 0, lip->yyLength());
849 
850  lip->body_utf8_append(lip->m_cpp_text_start);
851  lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
852 
853  return(result_state);
854 
855  case MY_LEX_USER_VARIABLE_DELIMITER: // Found quote char
856  {
857  uint32_t double_quotes= 0;
858  char quote_char= c; // Used char
859  while ((c=lip->yyGet()))
860  {
861  int var_length;
862  if ((var_length= my_mbcharlen(cs, c)) == 1)
863  {
864  if (c == quote_char)
865  {
866  if (lip->yyPeek() != quote_char)
867  break;
868  c=lip->yyGet();
869  double_quotes++;
870  continue;
871  }
872  }
873  else if (var_length < 1)
874  break; // Error
875  lip->skip_binary(var_length-1);
876  }
877  yylval->lex_str= double_quotes
878  ? get_quoted_token(lip, 1, lip->yyLength() - double_quotes - 1, quote_char)
879  : get_token(lip, 1, lip->yyLength() - 1);
880  if (c == quote_char)
881  lip->yySkip(); // Skip end `
882  lip->next_state= MY_LEX_START;
883  lip->body_utf8_append(lip->m_cpp_text_start);
884  lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
885  return IDENT_QUOTED;
886  }
887  case MY_LEX_INT_OR_REAL: // Complete int or incomplete real
888  if (c != '.')
889  { // Found complete integer number.
890  yylval->lex_str=get_token(lip, 0, lip->yyLength());
891  return int_token(yylval->lex_str.data(), yylval->lex_str.size());
892  }
893  // fall through
894  case MY_LEX_REAL: // Incomplete real number
895  while (cs->isdigit(c = lip->yyGet())) ;
896 
897  if (c == 'e' || c == 'E')
898  {
899  c = lip->yyGet();
900  if (c == '-' || c == '+')
901  c = lip->yyGet(); // Skip sign
902  if (!cs->isdigit(c))
903  { // No digit after sign
904  state= MY_LEX_CHAR;
905  break;
906  }
907  while (cs->isdigit(lip->yyGet())) ;
908  yylval->lex_str=get_token(lip, 0, lip->yyLength());
909  return(FLOAT_NUM);
910  }
911  yylval->lex_str=get_token(lip, 0, lip->yyLength());
912  return(DECIMAL_NUM);
913 
914  case MY_LEX_HEX_NUMBER: // Found x'hexstring'
915  lip->yySkip(); // Accept opening '
916  while (cs->isxdigit((c= lip->yyGet()))) ;
917  if (c != '\'')
918  return(ABORT_SYM); // Illegal hex constant
919  lip->yySkip(); // Accept closing '
920  length= lip->yyLength(); // Length of hexnum+3
921  if (length % 2 == 0)
922  return ABORT_SYM; // odd number of hex digits
923  yylval->lex_str=get_token(lip,
924  2, // skip x'
925  length-3); // don't count x' and last '
926  return (HEX_NUM);
927 
928  case MY_LEX_BIN_NUMBER: // Found b'bin-string'
929  lip->yySkip(); // Accept opening '
930  while ((c= lip->yyGet()) == '0' || c == '1') {};
931  if (c != '\'')
932  return(ABORT_SYM); // Illegal hex constant
933  lip->yySkip(); // Accept closing '
934  length= lip->yyLength(); // Length of bin-num + 3
935  yylval->lex_str= get_token(lip,
936  2, // skip b'
937  length-3); // don't count b' and last '
938  return (BIN_NUM);
939 
940  case MY_LEX_CMP_OP: // Incomplete comparison operator
941  if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
942  state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
943  lip->yySkip();
944  if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
945  {
946  lip->next_state= MY_LEX_START; // Allow signed numbers
947  return(tokval);
948  }
949  state = MY_LEX_CHAR; // Something fishy found
950  break;
951 
952  case MY_LEX_LONG_CMP_OP: // Incomplete comparison operator
953  if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
954  state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
955  {
956  lip->yySkip();
957  if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP)
958  lip->yySkip();
959  }
960  if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
961  {
962  lip->next_state= MY_LEX_START; // Found long op
963  return(tokval);
964  }
965  state = MY_LEX_CHAR; // Something fishy found
966  break;
967 
968  case MY_LEX_BOOL:
969  if (c != lip->yyPeek())
970  {
971  state=MY_LEX_CHAR;
972  break;
973  }
974  lip->yySkip();
975  tokval = find_keyword(lip,2,0); // Is a bool operator
976  lip->next_state= MY_LEX_START; // Allow signed numbers
977  return(tokval);
978 
979  case MY_LEX_STRING_OR_DELIMITER:
980  if (0)
981  {
982  state= MY_LEX_USER_VARIABLE_DELIMITER;
983  break;
984  }
985  /* " used for strings */
986  case MY_LEX_STRING: // Incomplete text string
987  if (!(yylval->lex_str.str_ = get_text(lip, 1, 1)))
988  {
989  state= MY_LEX_CHAR; // Read char by char
990  break;
991  }
992  yylval->lex_str.assign(yylval->lex_str.data(), lip->yytoklen);
993 
994  lip->body_utf8_append(lip->m_cpp_text_start);
995  lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
996 
997  lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
998  return(TEXT_STRING);
999 
1000  case MY_LEX_COMMENT: // Comment
1001  lex->select_lex.options|= OPTION_FOUND_COMMENT;
1002  while ((c = lip->yyGet()) != '\n' && c) ;
1003  lip->yyUnget(); // Safety against eof
1004  state = MY_LEX_START; // Try again
1005  break;
1006 
1007  case MY_LEX_LONG_COMMENT: /* Long C comment? */
1008  if (lip->yyPeek() != '*')
1009  {
1010  state=MY_LEX_CHAR; // Probable division
1011  break;
1012  }
1013  lex->select_lex.options|= OPTION_FOUND_COMMENT;
1014  /* Reject '/' '*', since we might need to turn off the echo */
1015  lip->yyUnget();
1016 
1017  if (lip->yyPeekn(2) == '!')
1018  {
1019  lip->in_comment= DISCARD_COMMENT;
1020  /* Accept '/' '*' '!', but do not keep this marker. */
1021  lip->set_echo(false);
1022  lip->yySkip();
1023  lip->yySkip();
1024  lip->yySkip();
1025 
1026  /*
1027  The special comment format is very strict:
1028  '/' '*' '!', followed by digits ended by a non-digit.
1029  There must be at least 5 digits for it to count
1030  */
1031  const int MAX_VERSION_SIZE= 16;
1032  char version_str[MAX_VERSION_SIZE];
1033 
1034  int pos= 0;
1035  do
1036  {
1037  version_str[pos]= lip->yyPeekn(pos);
1038  pos++;
1039  } while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1040  version_str[pos]= 0;
1041 
1042  /* To keep some semblance of compatibility, we impose a 5 digit floor */
1043  if (pos > 4)
1044  {
1045  uint64_t version;
1046  version=strtoll(version_str, NULL, 10);
1047 
1048  /* Accept 'M' 'm' 'm' 'd' 'd' */
1049  lip->yySkipn(pos-1);
1050 
1051  if (version <= DRIZZLE_VERSION_ID)
1052  {
1053  /* Expand the content of the special comment as real code */
1054  lip->set_echo(true);
1055  state=MY_LEX_START;
1056  break;
1057  }
1058  }
1059  else
1060  {
1061  state=MY_LEX_START;
1062  lip->set_echo(true);
1063  break;
1064  }
1065  }
1066  else
1067  {
1068  lip->in_comment= PRESERVE_COMMENT;
1069  lip->yySkip(); // Accept /
1070  lip->yySkip(); // Accept *
1071  }
1072  /*
1073  Discard:
1074  - regular '/' '*' comments,
1075  - special comments '/' '*' '!' for a future version,
1076  by scanning until we find a closing '*' '/' marker.
1077  Note: There is no such thing as nesting comments,
1078  the first '*' '/' sequence seen will mark the end.
1079  */
1080  comment_closed= false;
1081  while (! lip->eof())
1082  {
1083  c= lip->yyGet();
1084  if (c == '*')
1085  {
1086  if (lip->yyPeek() == '/')
1087  {
1088  lip->yySkip();
1089  comment_closed= true;
1090  state = MY_LEX_START;
1091  break;
1092  }
1093  }
1094  else if (c == '\n')
1095  lip->yylineno++;
1096  }
1097  /* Unbalanced comments with a missing '*' '/' are a syntax error */
1098  if (! comment_closed)
1099  return (ABORT_SYM);
1100  state = MY_LEX_START; // Try again
1101  lip->in_comment= NO_COMMENT;
1102  lip->set_echo(true);
1103  break;
1104 
1105  case MY_LEX_END_LONG_COMMENT:
1106  if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() == '/')
1107  {
1108  /* Reject '*' '/' */
1109  lip->yyUnget();
1110  /* Accept '*' '/', with the proper echo */
1111  lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1112  lip->yySkipn(2);
1113  /* And start recording the tokens again */
1114  lip->set_echo(true);
1115  lip->in_comment=NO_COMMENT;
1116  state=MY_LEX_START;
1117  }
1118  else
1119  state=MY_LEX_CHAR; // Return '*'
1120  break;
1121 
1122  case MY_LEX_SET_VAR: // Check if ':='
1123  if (lip->yyPeek() != '=')
1124  {
1125  state=MY_LEX_CHAR; // Return ':'
1126  break;
1127  }
1128  lip->yySkip();
1129  return (SET_VAR);
1130 
1131  case MY_LEX_SEMICOLON: // optional line terminator
1132  if (lip->yyPeek())
1133  {
1134  state= MY_LEX_CHAR; // Return ';'
1135  break;
1136  }
1137  lip->next_state=MY_LEX_END; // Mark for next loop
1138  return(END_OF_INPUT);
1139 
1140  case MY_LEX_EOL:
1141  if (lip->eof())
1142  {
1143  lip->yyUnget(); // Reject the last '\0'
1144  lip->set_echo(false);
1145  lip->yySkip();
1146  lip->set_echo(true);
1147  /* Unbalanced comments with a missing '*' '/' are a syntax error */
1148  if (lip->in_comment != NO_COMMENT)
1149  return (ABORT_SYM);
1150  lip->next_state=MY_LEX_END; // Mark for next loop
1151  return(END_OF_INPUT);
1152  }
1153  state=MY_LEX_CHAR;
1154  break;
1155 
1156  case MY_LEX_END:
1157  lip->next_state=MY_LEX_END;
1158  return false; // We found end of input last time
1159 
1160  /* Actually real shouldn't start with . but allow them anyhow */
1161 
1162  case MY_LEX_REAL_OR_POINT:
1163  if (cs->isdigit(lip->yyPeek()))
1164  state= MY_LEX_REAL; // Real
1165  else
1166  {
1167  state= MY_LEX_IDENT_SEP; // return '.'
1168  lip->yyUnget(); // Put back '.'
1169  }
1170  break;
1171 
1172  case MY_LEX_USER_END: // end '@' of user@hostname
1173  switch (state_map[(uint8_t)lip->yyPeek()]) {
1174  case MY_LEX_STRING:
1175  case MY_LEX_USER_VARIABLE_DELIMITER:
1176  case MY_LEX_STRING_OR_DELIMITER:
1177  break;
1178  case MY_LEX_USER_END:
1179  lip->next_state=MY_LEX_SYSTEM_VAR;
1180  break;
1181  default:
1182  lip->next_state=MY_LEX_HOSTNAME;
1183  break;
1184  }
1185  yylval->lex_str.assign(lip->get_ptr(), 1);
1186  return '@';
1187 
1188  case MY_LEX_HOSTNAME: // end '@' of user@hostname
1189  for (c=lip->yyGet() ;
1190  cs->isalnum(c) || c == '.' || c == '_' || c == '$';
1191  c= lip->yyGet()) ;
1192  yylval->lex_str=get_token(lip, 0, lip->yyLength());
1193  return(LEX_HOSTNAME);
1194 
1195  case MY_LEX_SYSTEM_VAR:
1196  yylval->lex_str.assign(lip->get_ptr(), 1);
1197  lip->yySkip(); // Skip '@'
1198  lip->next_state= (state_map[(uint8_t)lip->yyPeek()] ==
1199  MY_LEX_USER_VARIABLE_DELIMITER ?
1200  MY_LEX_OPERATOR_OR_IDENT :
1201  MY_LEX_IDENT_OR_KEYWORD);
1202  return((int) '@');
1203 
1204  case MY_LEX_IDENT_OR_KEYWORD:
1205  /*
1206  We come here when we have found two '@' in a row.
1207  We should now be able to handle:
1208  [(global | local | session) .]variable_name
1209  */
1210 
1211  for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1212  /* If there were non-ASCII characters, mark that we must convert */
1213  result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1214 
1215  if (c == '.')
1216  lip->next_state=MY_LEX_IDENT_SEP;
1217 
1218  length= lip->yyLength();
1219  if (length == 0)
1220  return(ABORT_SYM); // Names must be nonempty.
1221 
1222  if ((tokval= find_keyword(lip, length,0)))
1223  {
1224  lip->yyUnget(); // Put back 'c'
1225  return(tokval); // Was keyword
1226  }
1227  yylval->lex_str=get_token(lip, 0, length);
1228 
1229  lip->body_utf8_append(lip->m_cpp_text_start);
1230  lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
1231 
1232  return result_state;
1233  }
1234  }
1235 }
1236 
1237 /*
1238  Select_Lex structures initialisations
1239 */
1240 void Select_Lex_Node::init_query()
1241 {
1242  options= 0;
1243  linkage= UNSPECIFIED_TYPE;
1244  no_error= no_table_names_allowed= 0;
1245  uncacheable.reset();
1246 }
1247 
1248 void Select_Lex_Node::init_select()
1249 {
1250 }
1251 
1252 void Select_Lex_Unit::init_query()
1253 {
1254  Select_Lex_Node::init_query();
1255  linkage= GLOBAL_OPTIONS_TYPE;
1256  global_parameters= first_select();
1257  select_limit_cnt= HA_POS_ERROR;
1258  offset_limit_cnt= 0;
1259  union_distinct= 0;
1260  prepared= optimized= executed= 0;
1261  item= 0;
1262  union_result= 0;
1263  table= 0;
1264  fake_select_lex= 0;
1265  cleaned= 0;
1266  item_list.clear();
1267  describe= 0;
1268  found_rows_for_union= 0;
1269 }
1270 
1271 void Select_Lex::init_query()
1272 {
1273  Select_Lex_Node::init_query();
1274  table_list.clear();
1275  top_join_list.clear();
1276  join_list= &top_join_list;
1277  embedding= leaf_tables= 0;
1278  item_list.clear();
1279  join= 0;
1280  having= where= 0;
1281  olap= UNSPECIFIED_OLAP_TYPE;
1282  having_fix_field= 0;
1283  context.select_lex= this;
1284  context.init();
1285  /*
1286  Add the name resolution context of the current (sub)query to the
1287  stack of contexts for the whole query.
1288  TODO:
1289  push_context may return an error if there is no memory for a new
1290  element in the stack, however this method has no return value,
1291  thus push_context should be moved to a place where query
1292  initialization is checked for failure.
1293  */
1294  parent_lex->push_context(&context);
1295  cond_count= between_count= with_wild= 0;
1296  max_equal_elems= 0;
1297  ref_pointer_array= 0;
1298  select_n_where_fields= 0;
1299  select_n_having_items= 0;
1300  subquery_in_having= explicit_limit= 0;
1301  is_item_list_lookup= 0;
1302  parsing_place= NO_MATTER;
1303  exclude_from_table_unique_test= false;
1304  nest_level= 0;
1305  link_next= 0;
1306 }
1307 
1308 void Select_Lex::init_select()
1309 {
1310  sj_nests.clear();
1311  group_list.clear();
1312  db= 0;
1313  having= 0;
1314  in_sum_expr= with_wild= 0;
1315  options= 0;
1316  braces= 0;
1317  interval_list.clear();
1318  inner_sum_func_list= 0;
1319  linkage= UNSPECIFIED_TYPE;
1320  order_list.elements= 0;
1321  order_list.first= 0;
1322  order_list.next= (unsigned char**) &order_list.first;
1323  /* Set limit and offset to default values */
1324  select_limit= 0; /* denotes the default limit = HA_POS_ERROR */
1325  offset_limit= 0; /* denotes the default offset = 0 */
1326  with_sum_func= 0;
1327  is_cross= false;
1328  is_correlated= 0;
1329  cur_pos_in_select_list= UNDEF_POS;
1330  non_agg_fields.clear();
1331  cond_value= having_value= Item::COND_UNDEF;
1332  inner_refs_list.clear();
1333  full_group_by_flag.reset();
1334 }
1335 
1336 /*
1337  Select_Lex structures linking
1338 */
1339 
1340 /* include on level down */
1341 void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1342 {
1343  if ((next= upper->slave))
1344  next->prev= &next;
1345  prev= &upper->slave;
1346  upper->slave= this;
1347  master= upper;
1348  slave= 0;
1349 }
1350 
1351 /*
1352  include on level down (but do not link)
1353 
1354  SYNOPSYS
1355  Select_Lex_Node::include_standalone()
1356  upper - reference on node underr which this node should be included
1357  ref - references on reference on this node
1358 */
1359 void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1360  Select_Lex_Node **ref)
1361 {
1362  next= 0;
1363  prev= ref;
1364  master= upper;
1365  slave= 0;
1366 }
1367 
1368 /* include neighbour (on same level) */
1369 void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1370 {
1371  if ((next= before->next))
1372  next->prev= &next;
1373  prev= &before->next;
1374  before->next= this;
1375  master= before->master;
1376  slave= 0;
1377 }
1378 
1379 /* including in global Select_Lex list */
1380 void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1381 {
1382  if ((link_next= *plink))
1383  link_next->link_prev= &link_next;
1384  link_prev= plink;
1385  *plink= this;
1386 }
1387 
1388 //excluding from global list (internal function)
1389 void Select_Lex_Node::fast_exclude()
1390 {
1391  if (link_prev)
1392  {
1393  if ((*link_prev= link_next))
1394  link_next->link_prev= link_prev;
1395  }
1396  // Remove slave structure
1397  for (; slave; slave= slave->next)
1398  slave->fast_exclude();
1399 
1400 }
1401 
1402 /*
1403  excluding select_lex structure (except first (first select can't be
1404  deleted, because it is most upper select))
1405 */
1406 void Select_Lex_Node::exclude()
1407 {
1408  //exclude from global list
1409  fast_exclude();
1410  //exclude from other structures
1411  if ((*prev= next))
1412  next->prev= prev;
1413  /*
1414  We do not need following statements, because prev pointer of first
1415  list element point to master->slave
1416  if (master->slave == this)
1417  master->slave= next;
1418  */
1419 }
1420 
1421 
1422 /*
1423  Exclude level of current unit from tree of SELECTs
1424 
1425  SYNOPSYS
1426  Select_Lex_Unit::exclude_level()
1427 
1428  NOTE: units which belong to current will be brought up on level of
1429  currernt unit
1430 */
1431 void Select_Lex_Unit::exclude_level()
1432 {
1433  Select_Lex_Unit *units= 0, **units_last= &units;
1434  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1435  {
1436  // unlink current level from global SELECTs list
1437  if (sl->link_prev && (*sl->link_prev= sl->link_next))
1438  sl->link_next->link_prev= sl->link_prev;
1439 
1440  // bring up underlay levels
1441  Select_Lex_Unit **last= 0;
1442  for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1443  {
1444  u->master= master;
1445  last= (Select_Lex_Unit**)&(u->next);
1446  }
1447  if (last)
1448  {
1449  (*units_last)= sl->first_inner_unit();
1450  units_last= last;
1451  }
1452  }
1453  if (units)
1454  {
1455  // include brought up levels in place of current
1456  (*prev)= units;
1457  (*units_last)= (Select_Lex_Unit*)next;
1458  if (next)
1459  next->prev= (Select_Lex_Node**)units_last;
1460  units->prev= prev;
1461  }
1462  else
1463  {
1464  // exclude currect unit from list of nodes
1465  (*prev)= next;
1466  if (next)
1467  next->prev= prev;
1468  }
1469 }
1470 
1471 /*
1472  Exclude subtree of current unit from tree of SELECTs
1473 */
1474 void Select_Lex_Unit::exclude_tree()
1475 {
1476  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1477  {
1478  // unlink current level from global SELECTs list
1479  if (sl->link_prev && (*sl->link_prev= sl->link_next))
1480  sl->link_next->link_prev= sl->link_prev;
1481 
1482  // unlink underlay levels
1483  for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1484  {
1485  u->exclude_level();
1486  }
1487  }
1488  // exclude currect unit from list of nodes
1489  (*prev)= next;
1490  if (next)
1491  next->prev= prev;
1492 }
1493 
1501 void Select_Lex::mark_as_dependent(Select_Lex *last)
1502 {
1503  /*
1504  Mark all selects from resolved to 1 before select where was
1505  found table as depended (of select where was found table)
1506  */
1507  for (Select_Lex *s= this;
1508  s && s != last;
1509  s= s->outer_select())
1510  {
1511  if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
1512  {
1513  // Select is dependent of outer select
1514  s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1515  s->uncacheable.set(UNCACHEABLE_UNITED);
1516  Select_Lex_Unit *munit= s->master_unit();
1517  munit->uncacheable.set(UNCACHEABLE_UNITED);
1518  munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1519  for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1520  {
1521  if (sl != s &&
1522  ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1523  sl->uncacheable.set(UNCACHEABLE_UNITED);
1524  }
1525  }
1526  s->is_correlated= true;
1527  Item_subselect *subquery_predicate= s->master_unit()->item;
1528  if (subquery_predicate)
1529  subquery_predicate->is_correlated= true;
1530  }
1531 }
1532 
1533 bool Select_Lex_Node::set_braces(bool)
1534 { return true; }
1535 
1536 bool Select_Lex_Node::inc_in_sum_expr()
1537 { return true; }
1538 
1539 uint32_t Select_Lex_Node::get_in_sum_expr()
1540 { return 0; }
1541 
1542 TableList* Select_Lex_Node::get_table_list()
1543 { return NULL; }
1544 
1545 List<Item>* Select_Lex_Node::get_item_list()
1546 { return NULL; }
1547 
1548 TableList *Select_Lex_Node::add_table_to_list(Session *,
1549  Table_ident *,
1550  lex_string_t *,
1551  const bitset<NUM_OF_TABLE_OPTIONS>&,
1552  thr_lock_type,
1553  List<Index_hint> *,
1554  lex_string_t *)
1555 {
1556  return 0;
1557 }
1558 
1559 
1560 /*
1561  prohibit using LIMIT clause
1562 */
1563 bool Select_Lex::test_limit()
1564 {
1565  if (select_limit != 0)
1566  {
1567  my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1568  "LIMIT & IN/ALL/ANY/SOME subquery");
1569  return true;
1570  }
1571  return false;
1572 }
1573 
1574 Select_Lex_Unit* Select_Lex_Unit::master_unit()
1575 {
1576  return this;
1577 }
1578 
1579 Select_Lex* Select_Lex_Unit::outer_select()
1580 {
1581  return (Select_Lex*) master;
1582 }
1583 
1584 void Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1585 {
1586  add_to_list(session, order_list, item, asc);
1587 }
1588 
1589 void Select_Lex::add_item_to_list(Session *, Item *item)
1590 {
1591  item_list.push_back(item);
1592 }
1593 
1594 void Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1595 {
1596  add_to_list(session, group_list, item, asc);
1597 }
1598 
1599 Select_Lex_Unit* Select_Lex::master_unit()
1600 {
1601  return (Select_Lex_Unit*) master;
1602 }
1603 
1604 Select_Lex* Select_Lex::outer_select()
1605 {
1606  return (Select_Lex*) master->get_master();
1607 }
1608 
1609 bool Select_Lex::set_braces(bool value)
1610 {
1611  braces= value;
1612  return false;
1613 }
1614 
1615 bool Select_Lex::inc_in_sum_expr()
1616 {
1617  in_sum_expr++;
1618  return false;
1619 }
1620 
1621 uint32_t Select_Lex::get_in_sum_expr()
1622 {
1623  return in_sum_expr;
1624 }
1625 
1626 TableList* Select_Lex::get_table_list()
1627 {
1628  return (TableList*) table_list.first;
1629 }
1630 
1631 List<Item>* Select_Lex::get_item_list()
1632 {
1633  return &item_list;
1634 }
1635 
1636 void Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1637 {
1638  if (not ref_pointer_array)
1639  ref_pointer_array= new (session->mem) Item*[5 * (n_child_sum_items + item_list.size() + select_n_having_items + select_n_where_fields + order_group_num)];
1640 }
1641 
1642 void Select_Lex_Unit::print(String *str)
1643 {
1644  bool union_all= !union_distinct;
1645  for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1646  {
1647  if (sl != first_select())
1648  {
1649  str->append(STRING_WITH_LEN(" union "));
1650  if (union_all)
1651  str->append(STRING_WITH_LEN("all "));
1652  else if (union_distinct == sl)
1653  union_all= true;
1654  }
1655  if (sl->braces)
1656  str->append('(');
1657  sl->print(session, str);
1658  if (sl->braces)
1659  str->append(')');
1660  }
1661  if (fake_select_lex == global_parameters)
1662  {
1663  if (fake_select_lex->order_list.elements)
1664  {
1665  str->append(STRING_WITH_LEN(" order by "));
1666  fake_select_lex->print_order(
1667  str,
1668  (Order *) fake_select_lex->order_list.first);
1669  }
1670  fake_select_lex->print_limit(session, str);
1671  }
1672 }
1673 
1674 void Select_Lex::print_order(String *str,
1675  Order *order)
1676 {
1677  for (; order; order= order->next)
1678  {
1679  if (order->counter_used)
1680  {
1681  char buffer[20];
1682  uint32_t length= snprintf(buffer, 20, "%d", order->counter);
1683  str->append(buffer, length);
1684  }
1685  else
1686  (*order->item)->print(str);
1687  if (!order->asc)
1688  str->append(STRING_WITH_LEN(" desc"));
1689  if (order->next)
1690  str->append(',');
1691  }
1692 }
1693 
1694 void Select_Lex::print_limit(Session *, String *str)
1695 {
1696  Select_Lex_Unit *unit= master_unit();
1697  Item_subselect *item= unit->item;
1698 
1699  if (item && unit->global_parameters == this)
1700  {
1701  Item_subselect::subs_type subs_type= item->substype();
1702  if (subs_type == Item_subselect::EXISTS_SUBS ||
1703  subs_type == Item_subselect::IN_SUBS ||
1704  subs_type == Item_subselect::ALL_SUBS)
1705  {
1706  assert(!item->fixed ||
1707  /*
1708  If not using materialization both:
1709  select_limit == 1, and there should be no offset_limit.
1710  */
1711  (((subs_type == Item_subselect::IN_SUBS) &&
1712  ((Item_in_subselect*)item)->exec_method ==
1713  Item_in_subselect::MATERIALIZATION) ?
1714  true :
1715  (select_limit->val_int() == 1L) &&
1716  offset_limit == 0));
1717  return;
1718  }
1719  }
1720  if (explicit_limit)
1721  {
1722  str->append(STRING_WITH_LEN(" limit "));
1723  if (offset_limit)
1724  {
1725  offset_limit->print(str);
1726  str->append(',');
1727  }
1728  select_limit->print(str);
1729  }
1730 }
1731 
1732 LEX::~LEX()
1733 {
1734  delete _create_table;
1735  delete _alter_table;
1736 }
1737 
1738 /*
1739  Initialize (or reset) Query_tables_list object.
1740 
1741  SYNOPSIS
1742  reset_query_tables_list()
1743  init true - we should perform full initialization of object with
1744  allocating needed memory
1745  false - object is already initialized so we should only reset
1746  its state so it can be used for parsing/processing
1747  of new statement
1748 
1749  DESCRIPTION
1750  This method initializes Query_tables_list so it can be used as part
1751  of LEX object for parsing/processing of statement. One can also use
1752  this method to reset state of already initialized Query_tables_list
1753  so it can be used for processing of new statement.
1754 */
1755 void Query_tables_list::reset_query_tables_list(bool init)
1756 {
1757  if (!init && query_tables)
1758  {
1759  TableList *table= query_tables;
1760  for (;;)
1761  {
1762  if (query_tables_last == &table->next_global ||
1763  !(table= table->next_global))
1764  break;
1765  }
1766  }
1767  query_tables= 0;
1768  query_tables_last= &query_tables;
1769  query_tables_own_last= 0;
1770 }
1771 
1772 /*
1773  Initialize LEX object.
1774 
1775  SYNOPSIS
1776  LEX::LEX()
1777 
1778  NOTE
1779  LEX object initialized with this constructor can be used as part of
1780  Session object for which one can safely call open_tables(), lock_tables()
1781  and close_thread_tables() functions. But it is not yet ready for
1782  statement parsing. On should use lex_start() function to prepare LEX
1783  for this.
1784 */
1785 LEX::LEX() :
1786  result(0),
1787  yacc_yyss(0),
1788  yacc_yyvs(0),
1789  session(NULL),
1790  charset(NULL),
1791  var_list(),
1792  sql_command(SQLCOM_END),
1793  statement(NULL),
1794  option_type(OPT_DEFAULT),
1795  is_lex_started(0),
1796  cacheable(true),
1797  sum_expr_used(false),
1798  _create_table(NULL),
1799  _alter_table(NULL),
1800  _create_field(NULL),
1801  _exists(false)
1802 {
1803  reset_query_tables_list(true);
1804 }
1805 
1806 /*
1807  initialize limit counters
1808 
1809  SYNOPSIS
1810  Select_Lex_Unit::set_limit()
1811  values - Select_Lex with initial values for counters
1812 */
1813 void Select_Lex_Unit::set_limit(Select_Lex *sl)
1814 {
1815  ha_rows select_limit_val;
1816  uint64_t val;
1817 
1818  val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
1819  select_limit_val= (ha_rows)val;
1820  /*
1821  Check for overflow : ha_rows can be smaller then uint64_t if
1822  BIG_TABLES is off.
1823  */
1824  if (val != (uint64_t)select_limit_val)
1825  select_limit_val= HA_POS_ERROR;
1826  offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
1827  0UL);
1828  select_limit_cnt= select_limit_val + offset_limit_cnt;
1829  if (select_limit_cnt < select_limit_val)
1830  select_limit_cnt= HA_POS_ERROR; // no limit
1831 }
1832 
1833 /*
1834  Unlink the first table from the global table list and the first table from
1835  outer select (lex->select_lex) local list
1836 
1837  SYNOPSIS
1838  unlink_first_table()
1839  link_to_local Set to 1 if caller should link this table to local list
1840 
1841  NOTES
1842  We assume that first tables in both lists is the same table or the local
1843  list is empty.
1844 
1845  RETURN
1846  0 If 'query_tables' == 0
1847  unlinked table
1848  In this case link_to_local is set.
1849 
1850 */
1851 TableList *LEX::unlink_first_table(bool *link_to_local)
1852 {
1853  TableList *first;
1854  if ((first= query_tables))
1855  {
1856  /*
1857  Exclude from global table list
1858  */
1859  if ((query_tables= query_tables->next_global))
1860  query_tables->prev_global= &query_tables;
1861  else
1862  query_tables_last= &query_tables;
1863  first->next_global= 0;
1864 
1865  /*
1866  and from local list if it is not empty
1867  */
1868  if ((*link_to_local= test(select_lex.table_list.first)))
1869  {
1870  select_lex.context.table_list=
1871  select_lex.context.first_name_resolution_table= first->next_local;
1872  select_lex.table_list.first= (unsigned char*) (first->next_local);
1873  select_lex.table_list.elements--; //safety
1874  first->next_local= 0;
1875  /*
1876  Ensure that the global list has the same first table as the local
1877  list.
1878  */
1879  first_lists_tables_same();
1880  }
1881  }
1882  return first;
1883 }
1884 
1885 /*
1886  Bring first local table of first most outer select to first place in global
1887  table list
1888 
1889  SYNOPSYS
1890  LEX::first_lists_tables_same()
1891 
1892  NOTES
1893  In many cases (for example, usual INSERT/DELETE/...) the first table of
1894  main Select_Lex have special meaning => check that it is the first table
1895  in global list and re-link to be first in the global list if it is
1896  necessary. We need such re-linking only for queries with sub-queries in
1897  the select list, as only in this case tables of sub-queries will go to
1898  the global list first.
1899 */
1900 void LEX::first_lists_tables_same()
1901 {
1902  TableList *first_table= (TableList*) select_lex.table_list.first;
1903  if (query_tables != first_table && first_table != 0)
1904  {
1905  TableList *next;
1906  if (query_tables_last == &first_table->next_global)
1907  query_tables_last= first_table->prev_global;
1908 
1909  if ((next= *first_table->prev_global= first_table->next_global))
1910  next->prev_global= first_table->prev_global;
1911  /* include in new place */
1912  first_table->next_global= query_tables;
1913  /*
1914  We are sure that query_tables is not 0, because first_table was not
1915  first table in the global list => we can use
1916  query_tables->prev_global without check of query_tables
1917  */
1918  query_tables->prev_global= &first_table->next_global;
1919  first_table->prev_global= &query_tables;
1920  query_tables= first_table;
1921  }
1922 }
1923 
1924 /*
1925  Link table back that was unlinked with unlink_first_table()
1926 
1927  SYNOPSIS
1928  link_first_table_back()
1929  link_to_local do we need link this table to local
1930 
1931  RETURN
1932  global list
1933 */
1934 void LEX::link_first_table_back(TableList *first, bool link_to_local)
1935 {
1936  if (first)
1937  {
1938  if ((first->next_global= query_tables))
1939  query_tables->prev_global= &first->next_global;
1940  else
1941  query_tables_last= &first->next_global;
1942  query_tables= first;
1943 
1944  if (link_to_local)
1945  {
1946  first->next_local= (TableList*) select_lex.table_list.first;
1947  select_lex.context.table_list= first;
1948  select_lex.table_list.first= (unsigned char*) first;
1949  select_lex.table_list.elements++; //safety
1950  }
1951  }
1952 }
1953 
1954 /*
1955  cleanup lex for case when we open table by table for processing
1956 
1957  SYNOPSIS
1958  LEX::cleanup_after_one_table_open()
1959 
1960  NOTE
1961  This method is mostly responsible for cleaning up of selects lists and
1962  derived tables state. To rollback changes in Query_tables_list one has
1963  to call Query_tables_list::reset_query_tables_list(false).
1964 */
1965 void LEX::cleanup_after_one_table_open()
1966 {
1967  /*
1968  session->lex().derived_tables & additional units may be set if we open
1969  a view. It is necessary to clear session->lex().derived_tables flag
1970  to prevent processing of derived tables during next openTablesLock
1971  if next table is a real table and cleanup & remove underlying units
1972  NOTE: all units will be connected to session->lex().select_lex, because we
1973  have not UNION on most upper level.
1974  */
1975  if (all_selects_list != &select_lex)
1976  {
1977  derived_tables= 0;
1978  /* cleunup underlying units (units of VIEW) */
1979  for (Select_Lex_Unit *un= select_lex.first_inner_unit();
1980  un;
1981  un= un->next_unit())
1982  un->cleanup();
1983  /* reduce all selects list to default state */
1984  all_selects_list= &select_lex;
1985  /* remove underlying units (units of VIEW) subtree */
1986  select_lex.cut_subtree();
1987  }
1988 }
1989 
1990 /*
1991  There are Select_Lex::add_table_to_list &
1992  Select_Lex::set_lock_for_tables are in sql_parse.cc
1993 
1994  Select_Lex::print is in sql_select.cc
1995 
1996  Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
1997  Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
1998  Select_Lex_Unit::change_result
1999  are in sql_union.cc
2000 */
2001 
2002 /*
2003  Sets the kind of hints to be added by the calls to add_index_hint().
2004 
2005  SYNOPSIS
2006  set_index_hint_type()
2007  type_arg The kind of hints to be added from now on.
2008  clause The clause to use for hints to be added from now on.
2009 
2010  DESCRIPTION
2011  Used in filling up the tagged hints list.
2012  This list is filled by first setting the kind of the hint as a
2013  context variable and then adding hints of the current kind.
2014  Then the context variable index_hint_type can be reset to the
2015  next hint type.
2016 */
2017 void Select_Lex::set_index_hint_type(index_hint_type type_arg, index_clause_map clause)
2018 {
2019  current_index_hint_type= type_arg;
2020  current_index_hint_clause= clause;
2021 }
2022 
2023 /*
2024  Makes an array to store index usage hints (ADD/FORCE/IGNORE INDEX).
2025 
2026  SYNOPSIS
2027  alloc_index_hints()
2028  session current thread.
2029 */
2030 void Select_Lex::alloc_index_hints (Session *session)
2031 {
2032  index_hints= new (session->mem_root) List<Index_hint>();
2033 }
2034 
2035 /*
2036  adds an element to the array storing index usage hints
2037  (ADD/FORCE/IGNORE INDEX).
2038 
2039  SYNOPSIS
2040  add_index_hint()
2041  session current thread.
2042  str name of the index.
2043  length number of characters in str.
2044 
2045  RETURN VALUE
2046  0 on success, non-zero otherwise
2047 */
2048 void Select_Lex::add_index_hint(Session *session, const char *str)
2049 {
2050  index_hints->push_front(new (session->mem_root) Index_hint(current_index_hint_type, current_index_hint_clause, str));
2051 }
2052 
2053 message::AlterTable *LEX::alter_table()
2054 {
2055  if (not _alter_table)
2056  _alter_table= new message::AlterTable;
2057 
2058  return _alter_table;
2059 }
2060 
2061 } /* namespace drizzled */