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,
72 unsigned int length) :
77 lookahead_token(END_OF_INPUT),
78 lookahead_yylval(NULL),
82 m_end_of_query(buffer + length),
83 m_tok_start_prev(NULL),
87 m_cpp_tok_start(NULL),
88 m_cpp_tok_start_prev(NULL),
91 m_cpp_utf8_processed_ptr(NULL),
92 next_state(MY_LEX_START),
94 in_comment(NO_COMMENT)
96 m_cpp_buf= (
char*) session->mem.alloc(length + 1);
120 void Lex_input_stream::body_utf8_append(
const char *ptr,
const char *end_ptr)
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);
128 if (m_cpp_utf8_processed_ptr >= ptr)
131 int bytes_to_copy= ptr - m_cpp_utf8_processed_ptr;
133 memcpy(m_body_utf8_ptr, m_cpp_utf8_processed_ptr, bytes_to_copy);
134 m_body_utf8_ptr += bytes_to_copy;
137 m_cpp_utf8_processed_ptr= end_ptr;
147 void Lex_input_stream::body_utf8_append(
const char *ptr)
149 body_utf8_append(ptr, ptr);
163 void Lex_input_stream::body_utf8_append_literal(
str_ref txt,
const char *end_ptr)
165 if (!m_cpp_utf8_processed_ptr)
170 memcpy(m_body_utf8_ptr, txt.data(), txt.size());
171 m_body_utf8_ptr += txt.size();
174 m_cpp_utf8_processed_ptr= end_ptr;
187 void lex_start(Session *session)
189 LEX *lex= &session->lex();
191 lex->session= lex->unit.session= session;
193 lex->context_stack.clear();
194 lex->unit.init_query();
195 lex->unit.init_select();
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();
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;
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;
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;
233 lex->name.assign(NULL, 0);
235 lex->allow_sum_func= 0;
236 lex->in_sum_func= NULL;
239 lex->is_lex_started=
true;
240 lex->statement= NULL;
242 lex->is_cross=
false;
257 safe_delete(_create_table);
258 safe_delete(_alter_table);
266 safe_delete(statement);
269 static int find_keyword(Lex_input_stream *lip, uint32_t len,
bool function)
273 const char *tok= lip->get_tok_start();
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;
279 const SYMBOL *symbol= lookup_symbol(tok_upper, len,
function);
282 lip->yylval->symbol.symbol=symbol;
283 lip->yylval->symbol.str= (
char*) tok;
284 lip->yylval->symbol.length=len;
293 static lex_string_t get_token(Lex_input_stream *lip, uint32_t skip, uint32_t length)
296 lip->yytoklen= length;
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();
310 static lex_string_t get_quoted_token(Lex_input_stream *lip,
312 uint32_t length,
char quote)
315 lip->yytoklen= length;
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;
322 lip->m_cpp_text_start= lip->get_cpp_tok_start() + skip;
323 lip->m_cpp_text_end= lip->m_cpp_text_start + length;
327 if ((*to++= *from++) == quote)
330 lip->m_cpp_text_start++;
342 static char *get_text(Lex_input_stream *lip,
int pre_skip,
int post_skip)
344 bool found_escape=
false;
345 const charset_info_st*
const cs= lip->m_session->charset();
348 unsigned char sep= lip->yyGetLast();
349 while (not lip->eof())
351 unsigned char c= lip->yyGet();
356 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
359 lip->skip_binary(l-1);
373 if (c == lip->yyGet())
382 const char* str= lip->get_tok_start();
383 const char* end= lip->get_ptr();
389 char* start= (
char*) lip->m_session->mem.alloc((uint32_t) (end-str)+1);
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;
396 lip->yytoklen= (uint32_t) (end-str);
397 memcpy(start, str, lip->yytoklen);
398 start[lip->yytoklen]= 0;
404 for (to= start; str != end; str++)
408 int l= my_ismbchar(cs, str, end);
417 if (*str ==
'\\' && (str + 1) != end)
447 else if (*str == sep)
453 lip->yytoklen= (uint32_t) (to - start);
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;
480 static inline uint32_t int_token(
const char *str,uint32_t length)
482 if (length < long_len)
490 else if (*str ==
'-')
495 while (*str ==
'0' && length)
499 if (length < long_len)
502 uint32_t smaller,bigger;
506 if (length == long_len)
508 cmp= signed_long_str+1;
512 else if (length < signed_int64_t_len)
514 else if (length > signed_int64_t_len)
518 cmp=signed_int64_t_str+1;
525 if (length == long_len)
531 else if (length < int64_t_len)
533 else if (length > int64_t_len)
535 if (length > unsigned_int64_t_len)
537 cmp=unsigned_int64_t_str;
538 smaller=ULONGLONG_NUM;
545 bigger= ULONGLONG_NUM;
548 while (*cmp && *cmp++ == *str++) ;
549 return ((
unsigned char) str[-1] <= (
unsigned char) cmp[-1]) ? smaller : bigger;
578 token= drizzled::lex_one_token(yylval, session);
589 token= drizzled::lex_one_token(yylval, session);
590 if (token == ROLLUP_SYM)
592 return WITH_ROLLUP_SYM;
618 int tokval, result_state;
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;
630 state=lip->next_state;
631 lip->next_state=MY_LEX_OPERATOR_OR_IDENT;
635 case MY_LEX_OPERATOR_OR_IDENT:
638 while(state_map[c= lip->yyPeek()] == MY_LEX_SKIP)
647 lip->restart_token();
649 state= (
enum my_lex_states) state_map[c];
652 if (lip->yyGet() ==
'N')
654 yylval->lex_str.assign(
"\\N", 2);
659 if (c ==
'-' && lip->yyPeek() ==
'-' &&
660 (cs->isspace(lip->yyPeekn(1)) ||
661 cs->iscntrl(lip->yyPeekn(1))))
663 state=MY_LEX_COMMENT;
668 lip->next_state= MY_LEX_START;
681 lip->restart_token();
686 case MY_LEX_IDENT_OR_HEX:
687 if (lip->yyPeek() ==
'\'')
689 state= MY_LEX_HEX_NUMBER;
692 case MY_LEX_IDENT_OR_BIN:
693 if (lip->yyPeek() ==
'\'')
695 state= MY_LEX_BIN_NUMBER;
702 result_state= IDENT_QUOTED;
703 if (my_mbcharlen(cs, lip->yyGetLast()) > 1)
705 int l = my_ismbchar(cs,
707 lip->get_end_of_query());
712 lip->skip_binary(l - 1);
714 while (ident_map[c=lip->yyGet()])
716 if (my_mbcharlen(cs, c) > 1)
718 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
721 lip->skip_binary(l-1);
727 for (result_state= c; ident_map[c= lip->yyGet()]; result_state|= c) {};
729 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
731 length= lip->yyLength();
732 start= lip->get_ptr();
733 if (lip->ignore_space)
739 for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
741 if (start == lip->get_ptr() && c ==
'.' && ident_map[(uint8_t)lip->yyPeek()])
742 lip->next_state=MY_LEX_IDENT_SEP;
746 if ((tokval = find_keyword(lip, length, c ==
'(')))
748 lip->next_state= MY_LEX_START;
753 yylval->lex_str= get_token(lip, 0, length);
755 lip->body_utf8_append(lip->m_cpp_text_start);
757 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
759 return(result_state);
761 case MY_LEX_IDENT_SEP:
762 yylval->lex_str.assign(lip->get_ptr(), 1);
764 lip->next_state= MY_LEX_IDENT_START;
765 if (!ident_map[(uint8_t)lip->yyPeek()])
766 lip->next_state= MY_LEX_START;
769 case MY_LEX_NUMBER_IDENT:
770 if (lip->yyGetLast() ==
'0')
775 while (cs->isxdigit((c = lip->yyGet()))) ;
776 if ((lip->yyLength() >= 3) && !ident_map[c])
779 yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
783 state= MY_LEX_IDENT_START;
788 while ((c= lip->yyGet()) ==
'0' || c ==
'1') {};
789 if ((lip->yyLength() >= 3) && !ident_map[c])
792 yylval->lex_str= get_token(lip, 2, lip->yyLength()-2);
796 state= MY_LEX_IDENT_START;
802 while (cs->isdigit((c = lip->yyGet()))) ;
805 state=MY_LEX_INT_OR_REAL;
808 if (c ==
'e' || c ==
'E')
811 if (cs->isdigit(lip->yyPeek()) ||
812 (c=(lip->yyGet())) ==
'+' || c ==
'-')
814 if (cs->isdigit(lip->yyPeek()))
817 while (cs->isdigit(lip->yyGet())) ;
818 yylval->lex_str= get_token(lip, 0, lip->yyLength());
825 case MY_LEX_IDENT_START:
829 result_state= IDENT_QUOTED;
830 while (ident_map[c=lip->yyGet()])
832 if (my_mbcharlen(cs, c) > 1)
834 int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
837 lip->skip_binary(l-1);
843 for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
845 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
847 if (c ==
'.' && ident_map[(uint8_t)lip->yyPeek()])
848 lip->next_state=MY_LEX_IDENT_SEP;
850 yylval->lex_str= get_token(lip, 0, lip->yyLength());
852 lip->body_utf8_append(lip->m_cpp_text_start);
853 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
855 return(result_state);
857 case MY_LEX_USER_VARIABLE_DELIMITER:
859 uint32_t double_quotes= 0;
861 while ((c=lip->yyGet()))
864 if ((var_length= my_mbcharlen(cs, c)) == 1)
868 if (lip->yyPeek() != quote_char)
875 else if (var_length < 1)
877 lip->skip_binary(var_length-1);
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);
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);
889 case MY_LEX_INT_OR_REAL:
892 yylval->lex_str=get_token(lip, 0, lip->yyLength());
893 return int_token(yylval->lex_str.data(), yylval->lex_str.size());
897 while (cs->isdigit(c = lip->yyGet())) ;
899 if (c ==
'e' || c ==
'E')
902 if (c ==
'-' || c ==
'+')
909 while (cs->isdigit(lip->yyGet())) ;
910 yylval->lex_str=get_token(lip, 0, lip->yyLength());
913 yylval->lex_str=get_token(lip, 0, lip->yyLength());
916 case MY_LEX_HEX_NUMBER:
918 while (cs->isxdigit((c= lip->yyGet()))) ;
922 length= lip->yyLength();
925 yylval->lex_str=get_token(lip,
930 case MY_LEX_BIN_NUMBER:
932 while ((c= lip->yyGet()) ==
'0' || c ==
'1') {};
936 length= lip->yyLength();
937 yylval->lex_str= get_token(lip,
943 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
944 state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
946 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
948 lip->next_state= MY_LEX_START;
954 case MY_LEX_LONG_CMP_OP:
955 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP ||
956 state_map[(uint8_t)lip->yyPeek()] == MY_LEX_LONG_CMP_OP)
959 if (state_map[(uint8_t)lip->yyPeek()] == MY_LEX_CMP_OP)
962 if ((tokval = find_keyword(lip, lip->yyLength() + 1, 0)))
964 lip->next_state= MY_LEX_START;
971 if (c != lip->yyPeek())
977 tokval = find_keyword(lip,2,0);
978 lip->next_state= MY_LEX_START;
981 case MY_LEX_STRING_OR_DELIMITER:
984 state= MY_LEX_USER_VARIABLE_DELIMITER;
989 if (!(yylval->lex_str.str_ = get_text(lip, 1, 1)))
994 yylval->lex_str.assign(yylval->lex_str.data(), lip->yytoklen);
996 lip->body_utf8_append(lip->m_cpp_text_start);
997 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
999 lex->text_string_is_7bit= (lip->tok_bitmap & 0x80) ? 0 : 1;
1000 return(TEXT_STRING);
1002 case MY_LEX_COMMENT:
1003 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1004 while ((c = lip->yyGet()) !=
'\n' && c) ;
1006 state = MY_LEX_START;
1009 case MY_LEX_LONG_COMMENT:
1010 if (lip->yyPeek() !=
'*')
1015 lex->select_lex.options|= OPTION_FOUND_COMMENT;
1019 if (lip->yyPeekn(2) ==
'!')
1021 lip->in_comment= DISCARD_COMMENT;
1023 lip->set_echo(
false);
1033 const int MAX_VERSION_SIZE= 16;
1034 char version_str[MAX_VERSION_SIZE];
1039 version_str[pos]= lip->yyPeekn(pos);
1041 }
while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1042 version_str[pos]= 0;
1048 version=strtoll(version_str, NULL, 10);
1051 lip->yySkipn(pos-1);
1053 if (version <= DRIZZLE_VERSION_ID)
1056 lip->set_echo(
true);
1064 lip->set_echo(
true);
1070 lip->in_comment= PRESERVE_COMMENT;
1082 comment_closed=
false;
1083 while (! lip->eof())
1088 if (lip->yyPeek() ==
'/')
1091 comment_closed=
true;
1092 state = MY_LEX_START;
1100 if (! comment_closed)
1102 state = MY_LEX_START;
1103 lip->in_comment= NO_COMMENT;
1104 lip->set_echo(
true);
1107 case MY_LEX_END_LONG_COMMENT:
1108 if ((lip->in_comment != NO_COMMENT) && lip->yyPeek() ==
'/')
1113 lip->set_echo(lip->in_comment == PRESERVE_COMMENT);
1116 lip->set_echo(
true);
1117 lip->in_comment=NO_COMMENT;
1124 case MY_LEX_SET_VAR:
1125 if (lip->yyPeek() !=
'=')
1133 case MY_LEX_SEMICOLON:
1139 lip->next_state=MY_LEX_END;
1140 return(END_OF_INPUT);
1146 lip->set_echo(
false);
1148 lip->set_echo(
true);
1150 if (lip->in_comment != NO_COMMENT)
1152 lip->next_state=MY_LEX_END;
1153 return(END_OF_INPUT);
1159 lip->next_state=MY_LEX_END;
1164 case MY_LEX_REAL_OR_POINT:
1165 if (cs->isdigit(lip->yyPeek()))
1169 state= MY_LEX_IDENT_SEP;
1174 case MY_LEX_USER_END:
1175 switch (state_map[(uint8_t)lip->yyPeek()]) {
1177 case MY_LEX_USER_VARIABLE_DELIMITER:
1178 case MY_LEX_STRING_OR_DELIMITER:
1180 case MY_LEX_USER_END:
1181 lip->next_state=MY_LEX_SYSTEM_VAR;
1184 lip->next_state=MY_LEX_HOSTNAME;
1187 yylval->lex_str.assign(lip->get_ptr(), 1);
1190 case MY_LEX_HOSTNAME:
1191 for (c=lip->yyGet() ;
1192 cs->isalnum(c) || c ==
'.' || c ==
'_' || c ==
'$';
1194 yylval->lex_str=get_token(lip, 0, lip->yyLength());
1195 return(LEX_HOSTNAME);
1197 case MY_LEX_SYSTEM_VAR:
1198 yylval->lex_str.assign(lip->get_ptr(), 1);
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);
1206 case MY_LEX_IDENT_OR_KEYWORD:
1213 for (result_state= 0; ident_map[c= lip->yyGet()]; result_state|= c) {};
1215 result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1218 lip->next_state=MY_LEX_IDENT_SEP;
1220 length= lip->yyLength();
1224 if ((tokval= find_keyword(lip, length,0)))
1229 yylval->lex_str=get_token(lip, 0, length);
1231 lip->body_utf8_append(lip->m_cpp_text_start);
1232 lip->body_utf8_append_literal(yylval->lex_str, lip->m_cpp_text_end);
1234 return result_state;
1242 void Select_Lex_Node::init_query()
1245 linkage= UNSPECIFIED_TYPE;
1246 no_error= no_table_names_allowed= 0;
1247 uncacheable.reset();
1250 void Select_Lex_Node::init_select()
1254 void Select_Lex_Unit::init_query()
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;
1262 prepared= optimized= executed= 0;
1270 found_rows_for_union= 0;
1273 void Select_Lex::init_query()
1275 Select_Lex_Node::init_query();
1277 top_join_list.clear();
1278 join_list= &top_join_list;
1279 embedding= leaf_tables= 0;
1283 olap= UNSPECIFIED_OLAP_TYPE;
1284 having_fix_field= 0;
1285 context.select_lex=
this;
1296 parent_lex->push_context(&context);
1297 cond_count= between_count= with_wild= 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;
1310 void Select_Lex::init_select()
1316 in_sum_expr= with_wild= 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;
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();
1343 void Select_Lex_Node::include_down(Select_Lex_Node *upper)
1345 if ((next= upper->slave))
1347 prev= &upper->slave;
1361 void Select_Lex_Node::include_standalone(Select_Lex_Node *upper,
1362 Select_Lex_Node **ref)
1371 void Select_Lex_Node::include_neighbour(Select_Lex_Node *before)
1373 if ((next= before->next))
1375 prev= &before->next;
1377 master= before->master;
1382 void Select_Lex_Node::include_global(Select_Lex_Node **plink)
1384 if ((link_next= *plink))
1385 link_next->link_prev= &link_next;
1391 void Select_Lex_Node::fast_exclude()
1395 if ((*link_prev= link_next))
1396 link_next->link_prev= link_prev;
1399 for (; slave; slave= slave->next)
1400 slave->fast_exclude();
1408 void Select_Lex_Node::exclude()
1433 void Select_Lex_Unit::exclude_level()
1435 Select_Lex_Unit *units= 0, **units_last= &units;
1436 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1439 if (sl->link_prev && (*sl->link_prev= sl->link_next))
1440 sl->link_next->link_prev= sl->link_prev;
1443 Select_Lex_Unit **last= 0;
1444 for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1447 last= (Select_Lex_Unit**)&(u->next);
1451 (*units_last)= sl->first_inner_unit();
1459 (*units_last)= (Select_Lex_Unit*)next;
1461 next->prev= (Select_Lex_Node**)units_last;
1476 void Select_Lex_Unit::exclude_tree()
1478 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1481 if (sl->link_prev && (*sl->link_prev= sl->link_next))
1482 sl->link_next->link_prev= sl->link_prev;
1485 for (Select_Lex_Unit *u= sl->first_inner_unit(); u; u= u->next_unit())
1503 void Select_Lex::mark_as_dependent(Select_Lex *last)
1509 for (Select_Lex *s=
this;
1511 s= s->outer_select())
1513 if (! (s->uncacheable.test(UNCACHEABLE_DEPENDENT)))
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())
1524 ! (sl->uncacheable.test(UNCACHEABLE_DEPENDENT) && sl->uncacheable.test(UNCACHEABLE_UNITED)))
1525 sl->uncacheable.set(UNCACHEABLE_UNITED);
1528 s->is_correlated=
true;
1529 Item_subselect *subquery_predicate= s->master_unit()->item;
1530 if (subquery_predicate)
1531 subquery_predicate->is_correlated=
true;
1535 bool Select_Lex_Node::set_braces(
bool)
1538 bool Select_Lex_Node::inc_in_sum_expr()
1541 uint32_t Select_Lex_Node::get_in_sum_expr()
1544 TableList* Select_Lex_Node::get_table_list()
1547 List<Item>* Select_Lex_Node::get_item_list()
1550 TableList *Select_Lex_Node::add_table_to_list(Session *,
1553 const bitset<NUM_OF_TABLE_OPTIONS>&,
1565 bool Select_Lex::test_limit()
1567 if (select_limit != 0)
1569 my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1570 "LIMIT & IN/ALL/ANY/SOME subquery");
1576 Select_Lex_Unit* Select_Lex_Unit::master_unit()
1581 Select_Lex* Select_Lex_Unit::outer_select()
1583 return (Select_Lex*) master;
1586 void Select_Lex::add_order_to_list(Session *session, Item *item,
bool asc)
1591 void Select_Lex::add_item_to_list(Session *, Item *item)
1593 item_list.push_back(item);
1596 void Select_Lex::add_group_to_list(Session *session, Item *item,
bool asc)
1601 Select_Lex_Unit* Select_Lex::master_unit()
1603 return (Select_Lex_Unit*) master;
1606 Select_Lex* Select_Lex::outer_select()
1608 return (Select_Lex*) master->get_master();
1611 bool Select_Lex::set_braces(
bool value)
1617 bool Select_Lex::inc_in_sum_expr()
1623 uint32_t Select_Lex::get_in_sum_expr()
1628 TableList* Select_Lex::get_table_list()
1630 return (TableList*) table_list.first;
1633 List<Item>* Select_Lex::get_item_list()
1638 void Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
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)];
1644 void Select_Lex_Unit::print(String *str)
1646 bool union_all= !union_distinct;
1647 for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1649 if (sl != first_select())
1651 str->append(STRING_WITH_LEN(
" union "));
1653 str->append(STRING_WITH_LEN(
"all "));
1654 else if (union_distinct == sl)
1659 sl->print(session, str);
1663 if (fake_select_lex == global_parameters)
1665 if (fake_select_lex->order_list.elements)
1667 str->append(STRING_WITH_LEN(
" order by "));
1668 fake_select_lex->print_order(
1670 (Order *) fake_select_lex->order_list.first);
1672 fake_select_lex->print_limit(session, str);
1676 void Select_Lex::print_order(String *str,
1679 for (; order; order= order->next)
1681 if (order->counter_used)
1684 uint32_t length= snprintf(buffer, 20,
"%d", order->counter);
1685 str->append(buffer, length);
1688 (*order->item)->print(str);
1690 str->append(STRING_WITH_LEN(
" desc"));
1696 void Select_Lex::print_limit(Session *, String *str)
1698 Select_Lex_Unit *unit= master_unit();
1699 Item_subselect *item= unit->item;
1701 if (item && unit->global_parameters ==
this)
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)
1708 assert(!item->fixed ||
1713 (((subs_type == Item_subselect::IN_SUBS) &&
1714 ((Item_in_subselect*)item)->exec_method ==
1715 Item_in_subselect::MATERIALIZATION) ?
1717 (select_limit->val_int() == 1L) &&
1718 offset_limit == 0));
1724 str->append(STRING_WITH_LEN(
" limit "));
1727 offset_limit->print(str);
1730 select_limit->print(str);
1736 delete _create_table;
1737 delete _alter_table;
1757 void Query_tables_list::reset_query_tables_list(
bool init)
1759 if (!init && query_tables)
1761 TableList *table= query_tables;
1764 if (query_tables_last == &table->next_global ||
1765 !(table= table->next_global))
1770 query_tables_last= &query_tables;
1771 query_tables_own_last= 0;
1794 sql_command(SQLCOM_END),
1796 option_type(OPT_DEFAULT),
1799 sum_expr_used(false),
1800 _create_table(NULL),
1802 _create_field(NULL),
1805 reset_query_tables_list(
true);
1815 void Select_Lex_Unit::set_limit(Select_Lex *sl)
1817 ha_rows select_limit_val;
1820 val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
1821 select_limit_val= (ha_rows)val;
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() :
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;
1853 TableList *LEX::unlink_first_table(
bool *link_to_local)
1856 if ((first= query_tables))
1861 if ((query_tables= query_tables->next_global))
1862 query_tables->prev_global= &query_tables;
1864 query_tables_last= &query_tables;
1865 first->next_global= 0;
1870 if ((*link_to_local= test(select_lex.table_list.first)))
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--;
1876 first->next_local= 0;
1881 first_lists_tables_same();
1902 void LEX::first_lists_tables_same()
1904 TableList *first_table= (TableList*) select_lex.table_list.first;
1905 if (query_tables != first_table && first_table != 0)
1908 if (query_tables_last == &first_table->next_global)
1909 query_tables_last= first_table->prev_global;
1911 if ((next= *first_table->prev_global= first_table->next_global))
1912 next->prev_global= first_table->prev_global;
1914 first_table->next_global= query_tables;
1920 query_tables->prev_global= &first_table->next_global;
1921 first_table->prev_global= &query_tables;
1922 query_tables= first_table;
1936 void LEX::link_first_table_back(TableList *first,
bool link_to_local)
1940 if ((first->next_global= query_tables))
1941 query_tables->prev_global= &first->next_global;
1943 query_tables_last= &first->next_global;
1944 query_tables= first;
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++;
1967 void LEX::cleanup_after_one_table_open()
1977 if (all_selects_list != &select_lex)
1981 for (Select_Lex_Unit *un= select_lex.first_inner_unit();
1983 un= un->next_unit())
1986 all_selects_list= &select_lex;
1988 select_lex.cut_subtree();
2019 void Select_Lex::set_index_hint_type(index_hint_type type_arg, index_clause_map clause)
2021 current_index_hint_type= type_arg;
2022 current_index_hint_clause= clause;
2032 void Select_Lex::alloc_index_hints (Session *session)
2034 index_hints=
new (session->mem_root) List<Index_hint>();
2050 void Select_Lex::add_index_hint(Session *session,
const char *str)
2052 index_hints->push_front(
new (session->mem_root) Index_hint(current_index_hint_type, current_index_hint_clause, str));
2055 message::AlterTable *LEX::alter_table()
2057 if (not _alter_table)
2058 _alter_table=
new message::AlterTable;
2060 return _alter_table;