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