23 #include <drizzled/sql_reserved_words.h>
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>
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>
42 #include <drizzled/message/alter_table.pb.h>
61 order->item_ptr= item;
62 order->item= &order->item_ptr;
66 order->counter_used= 0;
67 list.link_in_list((
unsigned char*) order, (
unsigned char**) &order->next);
70 Lex_input_stream::Lex_input_stream(Session& session,
str_ref buffer) :
75 lookahead_token(END_OF_INPUT),
76 lookahead_yylval(NULL),
80 m_end_of_query(buffer.end()),
81 m_tok_start_prev(NULL),
83 m_buf_length(buffer.size()),
85 m_cpp_tok_start(NULL),
86 m_cpp_tok_start_prev(NULL),
89 m_cpp_utf8_processed_ptr(NULL),
90 next_state(MY_LEX_START),
92 in_comment(NO_COMMENT)
94 m_cpp_buf= (
char*) session.mem.alloc(buffer.size() + 1);
118 void Lex_input_stream::body_utf8_append(
const char *ptr,
const char *end_ptr)
120 assert(m_cpp_buf <= ptr && ptr <= m_cpp_buf + m_buf_length);
121 assert(m_cpp_buf <= end_ptr && end_ptr <= m_cpp_buf + m_buf_length);
126 if (m_cpp_utf8_processed_ptr >= ptr)
129 int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
131 memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
132 m_body_utf8_ptr += bytes_to_copy;
135 m_cpp_utf8_processed_ptr= end_ptr;
145 void Lex_input_stream::body_utf8_append(
const char *ptr)
147 body_utf8_append(ptr, ptr);
161 void Lex_input_stream::body_utf8_append_literal(
str_ref txt,
const char *end_ptr)
163 if (!m_cpp_utf8_processed_ptr)
168 memcpy(m_body_utf8_ptr, txt.data(), txt.size());
169 m_body_utf8_ptr += txt.size();
172 m_cpp_utf8_processed_ptr= end_ptr;
185 void lex_start(Session *session)
187 LEX *lex= &session->lex();
189 lex->session= lex->unit.session= session;
191 lex->context_stack.clear();
192 lex->unit.init_query();
193 lex->unit.init_select();
195 lex->select_lex.parent_lex= lex;
196 lex->select_lex.init_query();
197 lex->value_list.clear();
198 lex->update_list.clear();
199 lex->auxiliary_table_list.clear();
200 lex->unit.next= lex->unit.master=
201 lex->unit.link_next= lex->unit.return_to= 0;
202 lex->unit.prev= lex->unit.link_prev= 0;
203 lex->unit.slave= lex->unit.global_parameters= lex->current_select=
204 lex->all_selects_list= &lex->select_lex;
205 lex->select_lex.master= &lex->unit;
206 lex->select_lex.prev= &lex->unit.slave;
207 lex->select_lex.link_next= lex->select_lex.slave= lex->select_lex.next= 0;
208 lex->select_lex.link_prev= (Select_Lex_Node**)&(lex->all_selects_list);
209 lex->select_lex.options= 0;
210 lex->select_lex.init_order();
211 lex->select_lex.group_list.clear();
213 lex->derived_tables= 0;
214 lex->lock_option= TL_READ;
215 lex->leaf_tables_insert= 0;
216 lex->var_list.clear();
217 lex->select_lex.select_number= 1;
219 lex->select_lex.in_sum_expr=0;
220 lex->select_lex.group_list.clear();
221 lex->select_lex.order_list.clear();
222 lex->sql_command= SQLCOM_END;
223 lex->duplicates= DUP_ERROR;
225 lex->escape_used=
false;
226 lex->query_tables= 0;
227 lex->reset_query_tables_list(
false);
228 lex->expr_allows_subselect=
true;
229 lex->use_only_table_context=
false;
231 lex->name.assign(NULL, 0);
233 lex->allow_sum_func= 0;
234 lex->in_sum_func= NULL;
237 lex->is_lex_started=
true;
238 lex->statement= NULL;
240 lex->is_cross=
false;
255 safe_delete(_create_table);
256 safe_delete(_alter_table);
264 safe_delete(statement);
267 static int find_keyword(Lex_input_stream *lip, uint32_t len,
bool function)
271 const char *tok= lip->get_tok_start();
273 for (;tok_pos<len && tok_pos<63;tok_pos++)
274 tok_upper[tok_pos]= system_charset_info->toupper(tok[tok_pos]);
275 tok_upper[tok_pos]=0;
277 const SYMBOL *symbol= lookup_symbol(tok_upper, len,
function);
280 lip->yylval->symbol.symbol=symbol;
281 lip->yylval->symbol.str= (
char*) tok;
282 lip->yylval->symbol.length=len;
291 static lex_string_t get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
294 lip->yytoklen= length;
296 tmp.assign(lip->m_session->mem.strdup(lip->get_tok_start() + skip, length), length);
297 lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
298 lip->m_cpp_text_end= lip->m_cpp_text_start + tmp.size();
308 static lex_string_t get_quoted_token(Lex_input_stream *lip,
310 uint32_t length,
char quote)
313 lip->yytoklen= length;
315 tmp.assign((
char*)lip->m_session->mem.alloc(length + 1), length);
316 const char* from= lip->get_tok_start() + skip;
317 char* to= (
char*)tmp.data();
318 const char* end= to+length;
320 lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
321 lip->m_cpp_text_end= lip->m_cpp_text_start + length;
325 if ((*to++= *from++) == quote)
328 lip->m_cpp_text_start++;
340 static char *get_text(Lex_input_stream *lip,
int pre_skip,
int post_skip)
342 bool found_escape=
false;
343 const charset_info_st*
const cs= lip->m_session->charset();
346 unsigned char sep= lip->yyGetLast();
347 while (not lip->eof())
349 unsigned char c= lip->yyGet();
354 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
357 lip->skip_binary(l-1);
371 if (c == lip->yyGet())
380 const char* str= lip->get_tok_start();
381 const char* end= lip->get_ptr();
387 char* start= (
char*) lip->m_session->mem.alloc((uint32_t) (end-str)+1);
389 lip->m_cpp_text_start= lip->get_cpp_tok_start() + pre_skip;
390 lip->m_cpp_text_end= lip->get_cpp_ptr() - post_skip;
394 lip->yytoklen= (uint32_t) (end-str);
395 memcpy(start, str, lip->yytoklen);
396 start[lip->yytoklen]= 0;
402 for (to= start; str != end; str++)
406 int l= my_ismbchar(cs, str, end);
415 if (*str ==
'\\' && (str + 1) != end)
445 else if (*str == sep)
451 lip->yytoklen= (uint32_t) (to - start);
468 static const char *long_str=
"2147483647";
469 static const uint32_t long_len= 10;
470 static const char *signed_long_str=
"-2147483648";
471 static const char *int64_t_str=
"9223372036854775807";
472 static const uint32_t int64_t_len= 19;
473 static const char *signed_int64_t_str=
"-9223372036854775808";
474 static const uint32_t signed_int64_t_len= 19;
475 static const char *unsigned_int64_t_str=
"18446744073709551615";
476 static const uint32_t unsigned_int64_t_len= 20;
478 static inline uint32_t int_token(
const char *str,uint32_t length)
480 if (length < long_len)
488 else if (*str ==
'-')
493 while (*str ==
'0' && length)
497 if (length < long_len)
500 uint32_t smaller,bigger;
504 if (length == long_len)
506 cmp= signed_long_str+1;
510 else if (length < signed_int64_t_len)
512 else if (length > signed_int64_t_len)
516 cmp=signed_int64_t_str+1;
523 if (length == long_len)
529 else if (length < int64_t_len)
531 else if (length > int64_t_len)
533 if (length > unsigned_int64_t_len)
535 cmp=unsigned_int64_t_str;
536 smaller=ULONGLONG_NUM;
543 bigger= ULONGLONG_NUM;
546 while (*cmp && *cmp++ == *str++) ;
547 return ((
unsigned char) str[-1] <= (
unsigned char) cmp[-1]) ? smaller : bigger;
576 token= drizzled::lex_one_token(yylval, session);
587 token= drizzled::lex_one_token(yylval, session);
588 if (token == ROLLUP_SYM)
590 return WITH_ROLLUP_SYM;
616 int tokval, result_state;
618 enum my_lex_states state;
619 Lex_input_stream *lip= session->
m_lip;
620 LEX *lex= &session->lex();
621 const charset_info_st *
const cs= session->charset();
622 unsigned char *state_map= cs->state_map;
623 unsigned char *ident_map= cs->ident_map;
628 state=lip->next_state;
629 lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
633 case MY_LEX_OPERATOR_OR_IDENT:
636 while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
645 lip->restart_token();
647 state= (
enum my_lex_states) state_map[c];
650 if (lip->yyGet() ==
'N')
652 yylval->lex_str.assign(
"\\N", 2);
657 if (c ==
'-' && lip->yyPeek() ==
'-' &&
658 (cs->isspace(lip->yyPeekn(1)) ||
659 cs->iscntrl(lip->yyPeekn(1))))
661 state=MY_LEX_COMMENT;
666 lip->next_state= MY_LEX_START;
679 lip->restart_token();
684 case MY_LEX_IDENT_OR_HEX:
685 if (lip->yyPeek() ==
'\'')
687 state= MY_LEX_HEX_NUMBER;
690 case MY_LEX_IDENT_OR_BIN:
691 if (lip->yyPeek() ==
'\'')
693 state= MY_LEX_BIN_NUMBER;
700 result_state= IDENT_QUOTED;
701 if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
703 int l = my_ismbchar(cs,
705 lip->get_end_of_query());
710 lip->skip_binary(l - 1);
712 while (ident_map[c=lip->yyGet()])
714 if (my_mbcharlen(cs, c) > 1)
716 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
719 lip->skip_binary(l-1);
725 for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
727 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
729 length= lip->yyLength();
730 start= lip->get_ptr();
731 if (lip->ignore_space)
737 for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
739 if (start == lip->get_ptr() && c ==
'.' && ident_map[(uint8_t)lip->yyPeek()])
740 lip->next_state=MY_LEX_IDENT_SEP;
744 if ((tokval = find_keyword(lip, length, c ==
'(')))
746 lip->next_state= MY_LEX_START;
751 yylval->lex_str= get_token(lip, 0, length);
753 lip->body_utf8_append(lip->m_cpp_text_start);
755 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
757 return(result_state);
759 case MY_LEX_IDENT_SEP:
760 yylval->lex_str.assign(lip->get_ptr(), 1);
762 lip->next_state= MY_LEX_IDENT_START;
763 if (!ident_map[(uint8_t)lip->yyPeek()])
764 lip->next_state= MY_LEX_START;
767 case MY_LEX_NUMBER_IDENT:
768 if (lip->yyGetLast() ==
'0')
773 while (cs->isxdigit((c = lip->yyGet()))) ;
774 if ((lip->yyLength() >= 3) && !ident_map[c])
777 yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
781 state= MY_LEX_IDENT_START;
786 while ((c= lip->yyGet()) ==
'0' || c ==
'1') {};
787 if ((lip->yyLength() >= 3) && !ident_map[c])
790 yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
794 state= MY_LEX_IDENT_START;
800 while (cs->isdigit((c = lip->yyGet()))) ;
803 state=MY_LEX_INT_OR_REAL;
806 if (c ==
'e' || c ==
'E')
809 if (cs->isdigit(lip->yyPeek()) ||
810 (c=(lip->yyGet())) ==
'+' || c ==
'-')
812 if (cs->isdigit(lip->yyPeek()))
815 while (cs->isdigit(lip->yyGet())) ;
816 yylval->lex_str= get_token(lip, 0, lip->yyLength());
823 case MY_LEX_IDENT_START:
827 result_state= IDENT_QUOTED;
828 while (ident_map[c=lip->yyGet()])
830 if (my_mbcharlen(cs, c) > 1)
832 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
835 lip->skip_binary(l-1);
841 for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
843 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
845 if (c ==
'.' && ident_map[(uint8_t)lip->yyPeek()])
846 lip->next_state=MY_LEX_IDENT_SEP;
848 yylval->lex_str= get_token(lip, 0, lip->yyLength());
850 lip->body_utf8_append(lip->m_cpp_text_start);
851 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
853 return(result_state);
855 case MY_LEX_USER_VARIABLE_DELIMITER:
857 uint32_t double_quotes= 0;
859 while ((c=lip->yyGet()))
862 if ((var_length= my_mbcharlen(cs, c)) == 1)
866 if (lip->yyPeek() != quote_char)
873 else if (var_length < 1)
875 lip->skip_binary(var_length-1);
877 yylval->lex_str= double_quotes
878 ? get_quoted_token(lip, 1, lip->yyLength() - double_quotes - 1, quote_char)
879 : get_token(lip, 1, lip->yyLength() - 1);
882 lip->next_state= MY_LEX_START;
883 lip->body_utf8_append(lip->m_cpp_text_start);
884 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
887 case MY_LEX_INT_OR_REAL:
890 yylval->lex_str=get_token(lip, 0, lip->yyLength());
891 return int_token(yylval->lex_str.data(), yylval->lex_str.size());
895 while (cs->isdigit(c = lip->yyGet())) ;
897 if (c ==
'e' || c ==
'E')
900 if (c ==
'-' || c ==
'+')
907 while (cs->isdigit(lip->yyGet())) ;
908 yylval->lex_str=get_token(lip, 0, lip->yyLength());
911 yylval->lex_str=get_token(lip, 0, lip->yyLength());
914 case MY_LEX_HEX_NUMBER:
916 while (cs->isxdigit((c= lip->yyGet()))) ;
920 length= lip->yyLength();
923 yylval->lex_str=get_token(lip,
928 case MY_LEX_BIN_NUMBER:
930 while ((c= lip->yyGet()) ==
'0' || c ==
'1') {};
934 length= lip->yyLength();
935 yylval->lex_str= get_token(lip,
941 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
942 state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
944 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
946 lip->next_state= MY_LEX_START;
952 case MY_LEX_LONG_CMP_OP:
953 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
954 state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
957 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP)
960 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
962 lip->next_state= MY_LEX_START;
969 if (c != lip->yyPeek())
975 tokval = find_keyword(lip,2,0);
976 lip->next_state= MY_LEX_START;
979 case MY_LEX_STRING_OR_DELIMITER:
982 state= MY_LEX_USER_VARIABLE_DELIMITER;
987 if (!(yylval->lex_str.str_ = get_text(lip, 1, 1)))
992 yylval->lex_str.assign(yylval->lex_str.data(), lip->yytoklen);
994 lip->body_utf8_append(lip->m_cpp_text_start);
995 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
997 lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1000 case MY_LEX_COMMENT:
1001 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1002 while ((c = lip->yyGet()) !=
'\n' && c) ;
1004 state = MY_LEX_START;
1007 case MY_LEX_LONG_COMMENT:
1008 if (lip->yyPeek() !=
'*')
1013 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1017 if (lip->yyPeekn(2) ==
'!')
1019 lip->in_comment= DISCARD_COMMENT;
1021 lip->set_echo(
false);
1031 const int MAX_VERSION_SIZE= 16;
1032 char version_str[MAX_VERSION_SIZE];
1037 version_str[pos]= lip->yyPeekn(pos);
1039 }
while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1040 version_str[pos]= 0;
1046 version=strtoll(version_str, NULL, 10);
1049 lip->yySkipn(pos-1);
1051 if (version <= DRIZZLE_VERSION_ID)
1054 lip->set_echo(
true);
1062 lip->set_echo(
true);
1068 lip->in_comment= PRESERVE_COMMENT;
1080 comment_closed=
false;
1081 while (! lip->eof())
1086 if (lip->yyPeek() ==
'/')
1089 comment_closed=
true;
1090 state = MY_LEX_START;
1098 if (! comment_closed)
1100 state = MY_LEX_START;
1101 lip->in_comment= NO_COMMENT;
1102 lip->set_echo(
true);
1105 case MY_LEX_END_LONG_COMMENT:
1106 if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() ==
'/')
1111 lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1114 lip->set_echo(
true);
1115 lip->in_comment=NO_COMMENT;
1122 case MY_LEX_SET_VAR:
1123 if (lip->yyPeek() !=
'=')
1131 case MY_LEX_SEMICOLON:
1137 lip->next_state=MY_LEX_END;
1138 return(END_OF_INPUT);
1144 lip->set_echo(
false);
1146 lip->set_echo(
true);
1148 if (lip->in_comment != NO_COMMENT)
1150 lip->next_state=MY_LEX_END;
1151 return(END_OF_INPUT);
1157 lip->next_state=MY_LEX_END;
1162 case MY_LEX_REAL_OR_POINT:
1163 if (cs->isdigit(lip->yyPeek()))
1167 state= MY_LEX_IDENT_SEP;
1172 case MY_LEX_USER_END:
1173 switch (state_map[(uint8_t)lip->yyPeek()]) {
1175 case MY_LEX_USER_VARIABLE_DELIMITER:
1176 case MY_LEX_STRING_OR_DELIMITER:
1178 case MY_LEX_USER_END:
1179 lip->next_state=MY_LEX_SYSTEM_VAR;
1182 lip->next_state=MY_LEX_HOSTNAME;
1185 yylval->lex_str.assign(lip->get_ptr(), 1);
1188 case MY_LEX_HOSTNAME:
1189 for (c=lip->yyGet() ;
1190 cs->isalnum(c) || c ==
'.' || c ==
'_' || c ==
'$';
1192 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1193 return(LEX_HOSTNAME);
1195 case MY_LEX_SYSTEM_VAR:
1196 yylval->lex_str.assign(lip->get_ptr(), 1);
1198 lip->next_state= (state_map[(uint8_t)lip->yyPeek()] ==
1199 MY_LEX_USER_VARIABLE_DELIMITER ?
1200 MY_LEX_OPERATOR_OR_IDENT :
1201 MY_LEX_IDENT_OR_KEYWORD);
1204 case MY_LEX_IDENT_OR_KEYWORD:
1211 for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1213 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1216 lip->next_state=MY_LEX_IDENT_SEP;
1218 length= lip->yyLength();
1222 if ((tokval= find_keyword(lip, length,0)))
1227 yylval->lex_str=get_token(lip, 0, length);
1229 lip->body_utf8_append(lip->m_cpp_text_start);
1230 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
1232 return result_state;
1240 void Select_Lex_Node::init_query()
1243 linkage= UNSPECIFIED_TYPE;
1244 no_error= no_table_names_allowed= 0;
1245 uncacheable.reset();
1248 void Select_Lex_Node::init_select()
1252 void Select_Lex_Unit::init_query()
1254 Select_Lex_Node::init_query();
1255 linkage= GLOBAL_OPTIONS_TYPE;
1256 global_parameters= first_select();
1257 select_limit_cnt= HA_POS_ERROR;
1258 offset_limit_cnt= 0;
1260 prepared= optimized= executed= 0;
1268 found_rows_for_union= 0;
1271 void Select_Lex::init_query()
1273 Select_Lex_Node::init_query();
1275 top_join_list.clear();
1276 join_list= &top_join_list;
1277 embedding= leaf_tables= 0;
1281 olap= UNSPECIFIED_OLAP_TYPE;
1282 having_fix_field= 0;
1283 context.select_lex=
this;
1294 parent_lex->push_context(&context);
1295 cond_count= between_count= with_wild= 0;
1297 ref_pointer_array= 0;
1298 select_n_where_fields= 0;
1299 select_n_having_items= 0;
1300 subquery_in_having= explicit_limit= 0;
1301 is_item_list_lookup= 0;
1302 parsing_place= NO_MATTER;
1303 exclude_from_table_unique_test=
false;
1308 void Select_Lex::init_select()
1314 in_sum_expr= with_wild= 0;
1317 interval_list.clear();
1318 inner_sum_func_list= 0;
1319 linkage= UNSPECIFIED_TYPE;
1320 order_list.elements= 0;
1321 order_list.first= 0;
1322 order_list.next= (
unsigned char**) &order_list.first;
1329 cur_pos_in_select_list= UNDEF_POS;
1330 non_agg_fields.clear();
1331 cond_value= having_value= Item::COND_UNDEF;
1332 inner_refs_list.clear();
1333 full_group_by_flag.reset();
1341 void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1343 if ((next= upper->slave))
1345 prev= &upper->slave;
1359 void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1360 Select_Lex_Node **ref)
1369 void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1371 if ((next= before->next))
1373 prev= &before->next;
1375 master= before->master;
1380 void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1382 if ((link_next= *plink))
1383 link_next->link_prev= &link_next;
1389 void Select_Lex_Node::fast_exclude()
1393 if ((*link_prev= link_next))
1394 link_next->link_prev= link_prev;
1397 for (; slave; slave= slave->next)
1398 slave->fast_exclude();
1406 void Select_Lex_Node::exclude()
1431 void Select_Lex_Unit::exclude_level()
1433 Select_Lex_Unit *units= 0, **units_last= &units;
1434 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1437 if (sl->link_prev && (*sl->link_prev= sl->link_next))
1438 sl->link_next->link_prev= sl->link_prev;
1441 Select_Lex_Unit **last= 0;
1442 for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1445 last= (Select_Lex_Unit**)&(u->next);
1449 (*units_last)= sl->first_inner_unit();
1457 (*units_last)= (Select_Lex_Unit*)next;
1459 next->prev= (Select_Lex_Node**)units_last;
1474 void Select_Lex_Unit::exclude_tree()
1476 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1479 if (sl->link_prev && (*sl->link_prev= sl->link_next))
1480 sl->link_next->link_prev= sl->link_prev;
1483 for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1501 void Select_Lex::mark_as_dependent(Select_Lex *last)
1507 for (Select_Lex *s=
this;
1509 s= s->outer_select())
1511 if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
1514 s->uncacheable.set(UNCACHEABLE_DEPENDENT);
1515 s->uncacheable.set(UNCACHEABLE_UNITED);
1516 Select_Lex_Unit *munit= s->master_unit();
1517 munit->uncacheable.set(UNCACHEABLE_UNITED);
1518 munit->uncacheable.set(UNCACHEABLE_DEPENDENT);
1519 for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1522 ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1523 sl->uncacheable.set(UNCACHEABLE_UNITED);
1526 s->is_correlated=
true;
1527 Item_subselect *subquery_predicate= s->master_unit()->item;
1528 if (subquery_predicate)
1529 subquery_predicate->is_correlated=
true;
1533 bool Select_Lex_Node::set_braces(
bool)
1536 bool Select_Lex_Node::inc_in_sum_expr()
1539 uint32_t Select_Lex_Node::get_in_sum_expr()
1542 TableList* Select_Lex_Node::get_table_list()
1545 List<Item>* Select_Lex_Node::get_item_list()
1548 TableList *Select_Lex_Node::add_table_to_list(Session *,
1551 const bitset<NUM_OF_TABLE_OPTIONS>&,
1563 bool Select_Lex::test_limit()
1565 if (select_limit != 0)
1567 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1568 "LIMIT & IN/ALL/ANY/SOME subquery");
1574 Select_Lex_Unit* Select_Lex_Unit::master_unit()
1579 Select_Lex* Select_Lex_Unit::outer_select()
1581 return (Select_Lex*) master;
1584 void Select_Lex::add_order_to_list(Session *session, Item *item,
bool asc)
1589 void Select_Lex::add_item_to_list(Session *, Item *item)
1591 item_list.push_back(item);
1594 void Select_Lex::add_group_to_list(Session *session, Item *item,
bool asc)
1599 Select_Lex_Unit* Select_Lex::master_unit()
1601 return (Select_Lex_Unit*) master;
1604 Select_Lex* Select_Lex::outer_select()
1606 return (Select_Lex*) master->get_master();
1609 bool Select_Lex::set_braces(
bool value)
1615 bool Select_Lex::inc_in_sum_expr()
1621 uint32_t Select_Lex::get_in_sum_expr()
1626 TableList* Select_Lex::get_table_list()
1628 return (TableList*) table_list.first;
1631 List<Item>* Select_Lex::get_item_list()
1636 void Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1638 if (not ref_pointer_array)
1639 ref_pointer_array=
new (session->mem) Item*[5 * (n_child_sum_items + item_list.size() + select_n_having_items + select_n_where_fields + order_group_num)];
1642 void Select_Lex_Unit::print(String *str)
1644 bool union_all= !union_distinct;
1645 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1647 if (sl != first_select())
1649 str->append(STRING_WITH_LEN(
" union "));
1651 str->append(STRING_WITH_LEN(
"all "));
1652 else if (union_distinct == sl)
1657 sl->print(session, str);
1661 if (fake_select_lex == global_parameters)
1663 if (fake_select_lex->order_list.elements)
1665 str->append(STRING_WITH_LEN(
" order by "));
1666 fake_select_lex->print_order(
1668 (Order *) fake_select_lex->order_list.first);
1670 fake_select_lex->print_limit(session, str);
1674 void Select_Lex::print_order(String *str,
1677 for (; order; order= order->next)
1679 if (order->counter_used)
1682 uint32_t length= snprintf(buffer, 20,
"%d", order->counter);
1683 str->append(buffer, length);
1686 (*order->item)->print(str);
1688 str->append(STRING_WITH_LEN(
" desc"));
1694 void Select_Lex::print_limit(Session *, String *str)
1696 Select_Lex_Unit *unit= master_unit();
1697 Item_subselect *item= unit->item;
1699 if (item && unit->global_parameters ==
this)
1701 Item_subselect::subs_type subs_type= item->substype();
1702 if (subs_type == Item_subselect::EXISTS_SUBS ||
1703 subs_type == Item_subselect::IN_SUBS ||
1704 subs_type == Item_subselect::ALL_SUBS)
1706 assert(!item->fixed ||
1711 (((subs_type == Item_subselect::IN_SUBS) &&
1712 ((Item_in_subselect*)item)->exec_method ==
1713 Item_in_subselect::MATERIALIZATION) ?
1715 (select_limit->val_int() == 1L) &&
1716 offset_limit == 0));
1722 str->append(STRING_WITH_LEN(
" limit "));
1725 offset_limit->print(str);
1728 select_limit->print(str);
1734 delete _create_table;
1735 delete _alter_table;
1755 void Query_tables_list::reset_query_tables_list(
bool init)
1757 if (!init && query_tables)
1759 TableList *table= query_tables;
1762 if (query_tables_last == &table->next_global ||
1763 !(table= table->next_global))
1768 query_tables_last= &query_tables;
1769 query_tables_own_last= 0;
1792 sql_command(SQLCOM_END),
1794 option_type(OPT_DEFAULT),
1797 sum_expr_used(false),
1798 _create_table(NULL),
1800 _create_field(NULL),
1803 reset_query_tables_list(
true);
1813 void Select_Lex_Unit::set_limit(Select_Lex *sl)
1815 ha_rows select_limit_val;
1818 val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
1819 select_limit_val= (ha_rows)val;
1824 if (val != (uint64_t)select_limit_val)
1825 select_limit_val= HA_POS_ERROR;
1826 offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
1828 select_limit_cnt= select_limit_val + offset_limit_cnt;
1829 if (select_limit_cnt < select_limit_val)
1830 select_limit_cnt= HA_POS_ERROR;
1851 TableList *LEX::unlink_first_table(
bool *link_to_local)
1854 if ((first= query_tables))
1859 if ((query_tables= query_tables->next_global))
1860 query_tables->prev_global= &query_tables;
1862 query_tables_last= &query_tables;
1863 first->next_global= 0;
1868 if ((*link_to_local= test(select_lex.table_list.first)))
1870 select_lex.context.table_list=
1871 select_lex.context.first_name_resolution_table= first->next_local;
1872 select_lex.table_list.first= (
unsigned char*) (first->next_local);
1873 select_lex.table_list.elements--;
1874 first->next_local= 0;
1879 first_lists_tables_same();
1900 void LEX::first_lists_tables_same()
1902 TableList *first_table= (TableList*) select_lex.table_list.first;
1903 if (query_tables != first_table && first_table != 0)
1906 if (query_tables_last == &first_table->next_global)
1907 query_tables_last= first_table->prev_global;
1909 if ((next= *first_table->prev_global= first_table->next_global))
1910 next->prev_global= first_table->prev_global;
1912 first_table->next_global= query_tables;
1918 query_tables->prev_global= &first_table->next_global;
1919 first_table->prev_global= &query_tables;
1920 query_tables= first_table;
1934 void LEX::link_first_table_back(TableList *first,
bool link_to_local)
1938 if ((first->next_global= query_tables))
1939 query_tables->prev_global= &first->next_global;
1941 query_tables_last= &first->next_global;
1942 query_tables= first;
1946 first->next_local= (TableList*) select_lex.table_list.first;
1947 select_lex.context.table_list= first;
1948 select_lex.table_list.first= (
unsigned char*) first;
1949 select_lex.table_list.elements++;
1965 void LEX::cleanup_after_one_table_open()
1975 if (all_selects_list != &select_lex)
1979 for (Select_Lex_Unit *un= select_lex.first_inner_unit();
1981 un= un->next_unit())
1984 all_selects_list= &select_lex;
1986 select_lex.cut_subtree();
2017 void Select_Lex::set_index_hint_type(index_hint_type type_arg, index_clause_map clause)
2019 current_index_hint_type= type_arg;
2020 current_index_hint_clause= clause;
2030 void Select_Lex::alloc_index_hints (Session *session)
2032 index_hints=
new (session->mem_root) List<Index_hint>();
2048 void Select_Lex::add_index_hint(Session *session,
const char *str)
2050 index_hints->push_front(
new (session->mem_root) Index_hint(current_index_hint_type, current_index_hint_clause, str));
2053 message::AlterTable *LEX::alter_table()
2055 if (not _alter_table)
2056 _alter_table=
new message::AlterTable;
2058 return _alter_table;