17
17
/* A lexical scanner on a temporary buffer with a yacc interface */
20
19
#define DRIZZLE_LEX 1
21
#include "drizzled/configmake.h"
22
#include "drizzled/item/num.h"
23
#include "drizzled/error.h"
24
#include "drizzled/session.h"
25
#include "drizzled/sql_base.h"
26
#include "drizzled/lookup_symbol.h"
27
#include "drizzled/index_hint.h"
33
/* Stay outside of the namespace because otherwise bison goes nuts */
34
int DRIZZLElex(void *arg, void *yysession);
39
static int lex_one_token(void *arg, void *yysession);
42
save order by and tables in own lists.
20
#include <drizzled/server_includes.h>
22
static int lex_one_token(void *arg, void *yythd);
25
We are using pointer to this variable for distinguishing between assignment
26
to NEW row field (when parsing trigger definition) and structured variable.
44
static bool add_to_list(Session *session, SQL_LIST &list, Item *item, bool asc)
47
if (!(order = (order_st *) session->alloc(sizeof(order_st))))
49
order->item_ptr= item;
50
order->item= &order->item_ptr;
54
order->counter_used= 0;
55
list.link_in_list((unsigned char*) order,(unsigned char**) &order->next);
29
sys_var *trg_new_row_fake_var= (sys_var*) 0x01;
60
32
LEX_STRING constant for null-string to be used in parser and other places.
62
34
const LEX_STRING null_lex_str= {NULL, 0};
64
Lex_input_stream::Lex_input_stream(Session *session,
36
/* Longest standard keyword name */
38
#define TOCK_NAME_LENGTH 24
41
The following data is based on the latin1 character set, and is only
42
used when comparing keywords
45
static unsigned char to_upper_lex[]=
47
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
48
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
49
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
50
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
51
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
52
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
53
96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
54
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
55
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
56
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
57
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
58
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
59
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
60
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
61
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
62
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
66
Names of the index hints (for error messages). Keep in sync with
70
const char * index_hint_type_name[] =
77
inline int lex_casecmp(const char *s, const char *t, uint32_t len)
80
to_upper_lex[(unsigned char) *s++] == to_upper_lex[(unsigned char) *t++]) ;
90
for (i=0 ; i < array_elements(symbols) ; i++)
91
symbols[i].length=(unsigned char) strlen(symbols[i].name);
92
for (i=0 ; i < array_elements(sql_functions) ; i++)
93
sql_functions[i].length=(unsigned char) strlen(sql_functions[i].name);
100
{ // Call this when daemon ends
106
st_parsing_options::reset()
108
allows_variable= true;
109
allows_select_into= true;
110
allows_select_procedure= true;
111
allows_derived= true;
114
Lex_input_stream::Lex_input_stream(THD *thd,
65
115
const char* buffer,
66
116
unsigned int length)
178
233
The operation converts the specified text literal to the utf8 and appends
179
234
the result to the utf8-body.
181
@param session Thread context.
236
@param thd Thread context.
182
237
@param txt Text literal.
183
238
@param txt_cs Character set of the text literal.
184
239
@param end_ptr Pointer in the pre-processed buffer, to which
185
240
m_cpp_utf8_processed_ptr will be set in the end of the
188
void Lex_input_stream::body_utf8_append_literal(const LEX_STRING *txt,
244
void Lex_input_stream::body_utf8_append_literal(THD *thd,
245
const LEX_STRING *txt,
246
const CHARSET_INFO * const txt_cs,
189
247
const char *end_ptr)
191
249
if (!m_cpp_utf8_processed_ptr)
254
if (!my_charset_same(txt_cs, &my_charset_utf8_general_ci))
256
thd->convert_string(&utf_txt,
257
&my_charset_utf8_general_ci,
258
txt->str, txt->length,
263
utf_txt.str= txt->str;
264
utf_txt.length= txt->length;
194
267
/* NOTE: utf_txt.length is in bytes, not in symbols. */
196
memcpy(m_body_utf8_ptr, txt->str, txt->length);
197
m_body_utf8_ptr += txt->length;
269
memcpy(m_body_utf8_ptr, utf_txt.str, utf_txt.length);
270
m_body_utf8_ptr += utf_txt.length;
198
271
*m_body_utf8_ptr= 0;
200
273
m_cpp_utf8_processed_ptr= end_ptr;
204
278
This is called before every query that is to be parsed.
205
279
Because of this, it's critical to not do too much things here.
206
280
(We already do too much here)
208
void lex_start(Session *session)
283
void lex_start(THD *thd)
210
LEX *lex= session->lex;
212
lex->session= lex->unit.session= session;
287
lex->thd= lex->unit.thd= thd;
214
289
lex->context_stack.empty();
215
290
lex->unit.init_query();
771
889
for (; state_map[c] == MY_LEX_SKIP ; c= lip->yyGet()) {};
773
891
if (start == lip->get_ptr() && c == '.' && ident_map[(uint8_t)lip->yyPeek()])
774
lip->next_state=MY_LEX_IDENT_SEP;
892
lip->next_state=MY_LEX_IDENT_SEP;
776
894
{ // '(' must follow directly if function
778
if ((tokval = find_keyword(lip, length, c == '(')))
780
lip->next_state= MY_LEX_START; // Allow signed numbers
781
return(tokval); // Was keyword
896
if ((tokval = find_keyword(lip, length, c == '(')))
898
lip->next_state= MY_LEX_START; // Allow signed numbers
899
return(tokval); // Was keyword
783
901
lip->yySkip(); // next state does a unget
785
903
yylval->lex_str=get_token(lip, 0, length);
906
Note: "SELECT _bla AS 'alias'"
907
_bla should be considered as a IDENT if charset haven't been found.
908
So we don't use MYF(MY_WME) with get_charset_by_csname to avoid
912
if (yylval->lex_str.str[0] == '_')
914
const CHARSET_INFO * const cs= get_charset_by_csname(yylval->lex_str.str + 1,
915
MY_CS_PRIMARY, MYF(0));
919
lip->m_underscore_cs= cs;
921
lip->body_utf8_append(lip->m_cpp_text_start,
922
lip->get_cpp_tok_start() + length);
923
return(UNDERSCORE_CHARSET);
787
927
lip->body_utf8_append(lip->m_cpp_text_start);
789
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
929
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
930
lip->m_cpp_text_end);
791
932
return(result_state); // IDENT or IDENT_QUOTED
835
976
while (my_isdigit(cs, (c = lip->yyGet()))) ;
836
977
if (!ident_map[c])
837
978
{ // Can't be identifier
838
state=MY_LEX_INT_OR_REAL;
979
state=MY_LEX_INT_OR_REAL;
841
982
if (c == 'e' || c == 'E')
843
// The following test is written this way to allow numbers of type 1e1
984
// The following test is written this way to allow numbers of type 1e1
844
985
if (my_isdigit(cs,lip->yyPeek()) ||
845
986
(c=(lip->yyGet())) == '+' || c == '-')
847
988
if (my_isdigit(cs,lip->yyPeek())) // Number must have digit after sign
850
991
while (my_isdigit(cs,lip->yyGet())) ;
851
992
yylval->lex_str=get_token(lip, 0, lip->yyLength());
858
999
case MY_LEX_IDENT_START: // We come here after '.'
859
1000
result_state= IDENT;
1001
#if defined(USE_MB) && defined(USE_MB_IDENT)
862
result_state= IDENT_QUOTED;
1004
result_state= IDENT_QUOTED;
863
1005
while (ident_map[c=lip->yyGet()])
865
1007
if (my_mbcharlen(cs, c) > 1)
867
int l= my_ismbchar(cs, lip->get_ptr() -1, lip->get_end_of_query());
1010
if ((l = my_ismbchar(cs,
1012
lip->get_end_of_query())) == 0)
870
1014
lip->skip_binary(l-1);
876
1021
for (result_state=0; ident_map[c= lip->yyGet()]; result_state|= c) {};
877
1022
/* If there were non-ASCII characters, mark that we must convert */
878
1023
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
880
1025
if (c == '.' && ident_map[(uint8_t)lip->yyPeek()])
881
lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
1026
lip->next_state=MY_LEX_IDENT_SEP;// Next is '.'
883
1028
yylval->lex_str= get_token(lip, 0, lip->yyLength());
885
1030
lip->body_utf8_append(lip->m_cpp_text_start);
887
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
1032
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1033
lip->m_cpp_text_end);
889
1035
return(result_state);
1242
1411
result_state= result_state & 0x80 ? IDENT_QUOTED : IDENT;
1245
lip->next_state=MY_LEX_IDENT_SEP;
1414
lip->next_state=MY_LEX_IDENT_SEP;
1246
1415
length= lip->yyLength();
1247
1416
if (length == 0)
1248
1417
return(ABORT_SYM); // Names must be nonempty.
1249
1418
if ((tokval= find_keyword(lip, length,0)))
1251
1420
lip->yyUnget(); // Put back 'c'
1252
return(tokval); // Was keyword
1421
return(tokval); // Was keyword
1254
1423
yylval->lex_str=get_token(lip, 0, length);
1256
1425
lip->body_utf8_append(lip->m_cpp_text_start);
1258
lip->body_utf8_append_literal(&yylval->lex_str, lip->m_cpp_text_end);
1427
lip->body_utf8_append_literal(thd, &yylval->lex_str, cs,
1428
lip->m_cpp_text_end);
1260
1430
return(result_state);
1437
Construct a copy of this object to be used for mysql_alter_table
1438
and mysql_create_table.
1440
Historically, these two functions modify their Alter_info
1441
arguments. This behaviour breaks re-execution of prepared
1442
statements and stored procedures and is compensated by always
1443
supplying a copy of Alter_info to these functions.
1445
@return You need to use check the error in THD for out
1446
of memory condition after calling this function.
1449
Alter_info::Alter_info(const Alter_info &rhs, MEM_ROOT *mem_root)
1450
:drop_list(rhs.drop_list, mem_root),
1451
alter_list(rhs.alter_list, mem_root),
1452
key_list(rhs.key_list, mem_root),
1453
create_list(rhs.create_list, mem_root),
1455
keys_onoff(rhs.keys_onoff),
1456
tablespace_op(rhs.tablespace_op),
1457
no_parts(rhs.no_parts),
1458
build_method(rhs.build_method),
1459
datetime_field(rhs.datetime_field),
1460
error_if_not_empty(rhs.error_if_not_empty)
1463
Make deep copies of used objects.
1464
This is not a fully deep copy - clone() implementations
1465
of Alter_drop, Alter_column, Key, foreign_key, Key_part_spec
1466
do not copy string constants. At the same length the only
1467
reason we make a copy currently is that ALTER/CREATE TABLE
1468
code changes input Alter_info definitions, but string
1469
constants never change.
1471
list_copy_and_replace_each_value(drop_list, mem_root);
1472
list_copy_and_replace_each_value(alter_list, mem_root);
1473
list_copy_and_replace_each_value(key_list, mem_root);
1474
list_copy_and_replace_each_value(create_list, mem_root);
1265
1478
void trim_whitespace(const CHARSET_INFO * const cs, LEX_STRING *str)
1611
1836
prohibit using LIMIT clause
1613
bool Select_Lex::test_limit()
1838
bool st_select_lex::test_limit()
1615
1840
if (select_limit != 0)
1617
1842
my_error(ER_NOT_SUPPORTED_YET, MYF(0),
1618
1843
"LIMIT & IN/ALL/ANY/SOME subquery");
1624
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1629
Select_Lex* Select_Lex_Unit::outer_select()
1631
return (Select_Lex*) master;
1634
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1636
return add_to_list(session, order_list, item, asc);
1639
bool Select_Lex::add_item_to_list(Session *, Item *item)
1850
st_select_lex_unit* st_select_lex_unit::master_unit()
1856
st_select_lex* st_select_lex_unit::outer_select()
1858
return (st_select_lex*) master;
1862
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
1864
return add_to_list(thd, order_list, item, asc);
1868
bool st_select_lex::add_item_to_list(THD *thd __attribute__((unused)),
1641
1871
return(item_list.push_back(item));
1644
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1646
return add_to_list(session, group_list, item, asc);
1649
Select_Lex_Unit* Select_Lex::master_unit()
1651
return (Select_Lex_Unit*) master;
1654
Select_Lex* Select_Lex::outer_select()
1656
return (Select_Lex*) master->get_master();
1659
bool Select_Lex::set_braces(bool value)
1875
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
1877
return add_to_list(thd, group_list, item, asc);
1881
st_select_lex_unit* st_select_lex::master_unit()
1883
return (st_select_lex_unit*) master;
1887
st_select_lex* st_select_lex::outer_select()
1889
return (st_select_lex*) master->get_master();
1893
bool st_select_lex::set_braces(bool value)
1665
bool Select_Lex::inc_in_sum_expr()
1900
bool st_select_lex::inc_in_sum_expr()
1671
uint32_t Select_Lex::get_in_sum_expr()
1907
uint32_t st_select_lex::get_in_sum_expr()
1673
1909
return in_sum_expr;
1676
TableList* Select_Lex::get_table_list()
1913
TableList* st_select_lex::get_table_list()
1678
1915
return (TableList*) table_list.first;
1681
List<Item>* Select_Lex::get_item_list()
1918
List<Item>* st_select_lex::get_item_list()
1683
1920
return &item_list;
1686
uint32_t Select_Lex::get_table_join_options()
1923
uint32_t st_select_lex::get_table_join_options()
1688
1925
return table_join_options;
1691
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1929
bool st_select_lex::setup_ref_array(THD *thd, uint32_t order_group_num)
1693
1931
if (ref_pointer_array)
1696
1934
return (ref_pointer_array=
1697
(Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1935
(Item **)thd->alloc(sizeof(Item*) * (n_child_sum_items +
1698
1936
item_list.elements +
1699
1937
select_n_having_items +
1700
1938
select_n_where_fields +
1701
1939
order_group_num)*5)) == 0;
1704
void Select_Lex_Unit::print(String *str, enum_query_type query_type)
1943
void st_select_lex_unit::print(String *str, enum_query_type query_type)
1706
1945
bool union_all= !union_distinct;
1707
for (Select_Lex *sl= first_select(); sl; sl= sl->next_select())
1946
for (SELECT_LEX *sl= first_select(); sl; sl= sl->next_select())
1709
1948
if (sl != first_select())
1841
2085
query_tables= 0;
1842
2086
query_tables_last= &query_tables;
1843
2087
query_tables_own_last= 0;
2091
We delay real initialization of hash (and therefore related
2092
memory allocation) until first insertion into this hash.
2094
hash_clear(&sroutines);
2096
else if (sroutines.records)
2098
/* Non-zero sroutines.records means that hash was initialized. */
2099
my_hash_reset(&sroutines);
2101
sroutines_list.empty();
2102
sroutines_list_own_last= sroutines_list.next;
2103
sroutines_list_own_elements= 0;
2104
binlog_stmt_flags= 0;
2109
Destroy Query_tables_list object with freeing all resources used by it.
2112
destroy_query_tables_list()
2115
void Query_tables_list::destroy_query_tables_list()
2117
hash_free(&sroutines);
1847
2122
Initialize LEX object.
1853
2128
LEX object initialized with this constructor can be used as part of
1854
Session object for which one can safely call open_tables(), lock_tables()
2129
THD object for which one can safely call open_tables(), lock_tables()
1855
2130
and close_thread_tables() functions. But it is not yet ready for
1856
2131
statement parsing. On should use lex_start() function to prepare LEX
1860
2136
:result(0), yacc_yyss(0), yacc_yyvs(0),
1861
2137
sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
2140
my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2141
plugins_static_buffer,
2142
INITIAL_LEX_PLUGIN_LIST_SIZE,
2143
INITIAL_LEX_PLUGIN_LIST_SIZE);
1863
2144
reset_query_tables_list(true);
2149
Check whether the merging algorithm can be used on this VIEW
2152
st_lex::can_be_merged()
2155
We can apply merge algorithm if it is single SELECT view with
2156
subqueries only in WHERE clause (we do not count SELECTs of underlying
2157
views, and second level subqueries) and we have not grpouping, ordering,
2158
HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2159
several underlying tables.
2162
false - only temporary table algorithm can be used
2163
true - merge algorithm can be used
2166
bool st_lex::can_be_merged()
2168
// TODO: do not forget implement case when select_lex.table_list.elements==0
2170
/* find non VIEW subqueries/unions */
2171
bool selects_allow_merge= select_lex.next_select() == 0;
2172
if (selects_allow_merge)
2174
for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2176
tmp_unit= tmp_unit->next_unit())
2178
if (tmp_unit->first_select()->parent_lex == this &&
2179
(tmp_unit->item == 0 ||
2180
(tmp_unit->item->place() != IN_WHERE &&
2181
tmp_unit->item->place() != IN_ON)))
2183
selects_allow_merge= 0;
2189
return (selects_allow_merge &&
2190
select_lex.group_list.elements == 0 &&
2191
select_lex.having == 0 &&
2192
select_lex.with_sum_func == 0 &&
2193
select_lex.table_list.elements >= 1 &&
2194
!(select_lex.options & SELECT_DISTINCT) &&
2195
select_lex.select_limit == 0);
2200
check if command can use VIEW with MERGE algorithm (for top VIEWs)
2203
st_lex::can_use_merged()
2206
Only listed here commands can use merge algorithm in top level
2207
SELECT_LEX (for subqueries will be used merge algorithm if
2208
st_lex::can_not_use_merged() is not true).
2211
false - command can't use merged VIEWs
2212
true - VIEWs with MERGE algorithms can be used
2215
bool st_lex::can_use_merged()
2217
switch (sql_command)
2220
case SQLCOM_CREATE_TABLE:
2222
case SQLCOM_UPDATE_MULTI:
2224
case SQLCOM_DELETE_MULTI:
2226
case SQLCOM_INSERT_SELECT:
2227
case SQLCOM_REPLACE:
2228
case SQLCOM_REPLACE_SELECT:
2237
Check if command can't use merged views in any part of command
2240
st_lex::can_not_use_merged()
2243
Temporary table algorithm will be used on all SELECT levels for queries
2244
listed here (see also st_lex::can_use_merged()).
2247
false - command can't use merged VIEWs
2248
true - VIEWs with MERGE algorithms can be used
2251
bool st_lex::can_not_use_merged()
2253
switch (sql_command)
2256
SQLCOM_SHOW_FIELDS is necessary to make
2257
information schema tables working correctly with views.
2258
see get_schema_tables_result function
2260
case SQLCOM_SHOW_FIELDS:
2111
There are Select_Lex::add_table_to_list &
2112
Select_Lex::set_lock_for_tables are in sql_parse.cc
2114
Select_Lex::print is in sql_select.cc
2116
Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2117
Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2118
Select_Lex_Unit::change_result
2539
Save current state of Query_tables_list for this LEX, and prepare it
2540
for processing of new statemnt.
2543
reset_n_backup_query_tables_list()
2544
backup Pointer to Query_tables_list instance to be used for backup
2547
void st_lex::reset_n_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2553
Restore state of Query_tables_list for this LEX from backup.
2556
restore_backup_query_tables_list()
2557
backup Pointer to Query_tables_list instance used for backup
2560
void st_lex::restore_backup_query_tables_list(Query_tables_list *backup __attribute__((unused)))
2566
Checks for usage of routines and/or tables in a parsed statement
2569
st_lex:table_or_sp_used()
2572
false No routines and tables used
2573
true Either or both routines and tables are used.
2576
bool st_lex::table_or_sp_used()
2578
if (sroutines.records || query_tables)
2586
Do end-of-prepare fixup for list of tables and their merge-VIEWed tables
2589
fix_prepare_info_in_table_list()
2591
tbl List of tables to process
2594
Perform end-end-of prepare fixup for list of tables, if any of the tables
2595
is a merge-algorithm VIEW, recursively fix up its underlying tables as
2600
static void fix_prepare_info_in_table_list(THD *thd, TableList *tbl)
2602
for (; tbl; tbl= tbl->next_local)
2606
tbl->prep_on_expr= tbl->on_expr;
2607
tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
2609
fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
2615
There are st_select_lex::add_table_to_list &
2616
st_select_lex::set_lock_for_tables are in sql_parse.cc
2618
st_select_lex::print is in sql_select.cc
2620
st_select_lex_unit::prepare, st_select_lex_unit::exec,
2621
st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2622
st_select_lex_unit::change_result
2119
2623
are in sql_union.cc