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