45
37
const LEX_STRING null_lex_str= {NULL, 0};
39
/* Longest standard keyword name */
41
#define TOCK_NAME_LENGTH 24
44
The following data is based on the latin1 character set, and is only
45
used when comparing keywords
48
static uchar to_upper_lex[]=
50
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
52
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
53
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
54
64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
55
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
56
96, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
57
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,123,124,125,126,127,
58
128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
59
144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
60
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
61
176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
62
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
63
208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
64
192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
65
208,209,210,211,212,213,214,247,216,217,218,219,220,221,222,255
69
Names of the index hints (for error messages). Keep in sync with
73
const char * index_hint_type_name[] =
80
inline int lex_casecmp(const char *s, const char *t, uint len)
83
to_upper_lex[(uchar) *s++] == to_upper_lex[(uchar) *t++]) ;
93
DBUG_ENTER("lex_init");
94
for (i=0 ; i < array_elements(symbols) ; i++)
95
symbols[i].length=(uchar) strlen(symbols[i].name);
96
for (i=0 ; i < array_elements(sql_functions) ; i++)
97
sql_functions[i].length=(uchar) strlen(sql_functions[i].name);
104
{ // Call this when daemon ends
105
DBUG_ENTER("lex_free");
48
111
st_parsing_options::reset()
50
allows_select_procedure= true;
113
allows_variable= TRUE;
114
allows_select_into= TRUE;
115
allows_select_procedure= TRUE;
116
allows_derived= TRUE;
53
Lex_input_stream::Lex_input_stream(Session *session,
119
Lex_input_stream::Lex_input_stream(THD *thd,
54
120
const char* buffer,
55
121
unsigned int length)
263
333
lex->sql_command= SQLCOM_END;
264
334
lex->duplicates= DUP_ERROR;
266
lex->escape_used= false;
336
lex->proc_list.first= 0;
337
lex->escape_used= FALSE;
267
338
lex->query_tables= 0;
268
lex->reset_query_tables_list(false);
269
lex->expr_allows_subselect= true;
270
lex->use_only_table_context= false;
271
lex->parse_vcol_expr= false;
339
lex->reset_query_tables_list(FALSE);
340
lex->expr_allows_subselect= TRUE;
341
lex->use_only_table_context= FALSE;
273
343
lex->name.str= 0;
274
344
lex->name.length= 0;
275
345
lex->nest_level=0 ;
276
346
lex->allow_sum_func= 0;
277
347
lex->in_sum_func= NULL;
349
ok, there must be a better solution for this, long-term
350
I tried "bzero" in the sql_yacc.yy code, but that for
351
some reason made the values zero, even if they were set
353
lex->server_options.server_name= 0;
354
lex->server_options.server_name_length= 0;
355
lex->server_options.host= 0;
356
lex->server_options.db= 0;
357
lex->server_options.username= 0;
358
lex->server_options.password= 0;
359
lex->server_options.scheme= 0;
360
lex->server_options.socket= 0;
361
lex->server_options.owner= 0;
362
lex->server_options.port= -1;
279
lex->is_lex_started= true;
364
lex->is_lex_started= TRUE;
282
368
void lex_end(LEX *lex)
370
DBUG_ENTER("lex_end");
371
DBUG_PRINT("enter", ("lex: 0x%lx", (long) lex));
284
372
if (lex->yacc_yyss)
286
free(lex->yacc_yyss);
287
free(lex->yacc_yyvs);
374
my_free(lex->yacc_yyss, MYF(0));
375
my_free(lex->yacc_yyvs, MYF(0));
288
376
lex->yacc_yyss= 0;
289
377
lex->yacc_yyvs= 0;
380
/* release used plugins */
381
plugin_unlock_list(0, (plugin_ref*)lex->plugins.buffer,
382
lex->plugins.elements);
383
reset_dynamic(&lex->plugins);
297
static int find_keyword(Lex_input_stream *lip, uint32_t len, bool function)
389
static int find_keyword(Lex_input_stream *lip, uint len, bool function)
299
/* Plenty of memory for the largest lex symbol we have */
301
391
const char *tok= lip->get_tok_start();
303
for (;tok_pos<len && tok_pos<63;tok_pos++)
304
tok_upper[tok_pos]=my_toupper(system_charset_info, tok[tok_pos]);
305
tok_upper[tok_pos]=0;
307
const SYMBOL *symbol= lookup_symbol(tok_upper, len, function);
393
SYMBOL *symbol= get_hash_symbol(tok, len, function);
310
396
lip->yylval->symbol.symbol=symbol;
510
** Calc type of integer; long integer, int64_t integer or real.
614
** Calc type of integer; long integer, longlong integer or real.
511
615
** Returns smallest type that match the string.
512
** When using uint64_t values the result is converted to a real
616
** When using unsigned long long values the result is converted to a real
513
617
** because else they will be unexpected sign changes because all calculation
514
** is done with int64_t or double.
618
** is done with longlong or double.
517
621
static const char *long_str="2147483647";
518
static const uint32_t long_len=10;
622
static const uint long_len=10;
519
623
static const char *signed_long_str="-2147483648";
520
static const char *int64_t_str="9223372036854775807";
521
static const uint32_t int64_t_len=19;
522
static const char *signed_int64_t_str="-9223372036854775808";
523
static const uint32_t signed_int64_t_len=19;
524
static const char *unsigned_int64_t_str="18446744073709551615";
525
static const uint32_t unsigned_int64_t_len=20;
624
static const char *longlong_str="9223372036854775807";
625
static const uint longlong_len=19;
626
static const char *signed_longlong_str="-9223372036854775808";
627
static const uint signed_longlong_len=19;
628
static const char *unsigned_longlong_str="18446744073709551615";
629
static const uint unsigned_longlong_len=20;
527
static inline uint32_t int_token(const char *str,uint32_t length)
631
static inline uint int_token(const char *str,uint length)
529
633
if (length < long_len) // quick normal case
1096
1251
lip->in_comment= DISCARD_COMMENT;
1097
1252
/* Accept '/' '*' '!', but do not keep this marker. */
1098
lip->set_echo(false);
1253
lip->set_echo(FALSE);
1104
1259
The special comment format is very strict:
1105
'/' '*' '!', followed by digits ended by a non-digit.
1106
There must be at least 5 digits for it to count
1260
'/' '*' '!', followed by exactly
1261
1 digit (major), 2 digits (minor), then 2 digits (dot).
1108
const int MAX_VERSION_SIZE= 16;
1109
char version_str[MAX_VERSION_SIZE];
1114
version_str[pos]= lip->yyPeekn(pos);
1116
} while ((pos < MAX_VERSION_SIZE-1) && isdigit(version_str[pos-1]));
1117
version_str[pos]= 0;
1119
/* To keep some semblance of compatibility, we impose a 5 digit floor */
1123
version=strtoll(version_str, NULL, 10);
1266
char version_str[6];
1267
version_str[0]= lip->yyPeekn(0);
1268
version_str[1]= lip->yyPeekn(1);
1269
version_str[2]= lip->yyPeekn(2);
1270
version_str[3]= lip->yyPeekn(3);
1271
version_str[4]= lip->yyPeekn(4);
1273
if ( my_isdigit(cs, version_str[0])
1274
&& my_isdigit(cs, version_str[1])
1275
&& my_isdigit(cs, version_str[2])
1276
&& my_isdigit(cs, version_str[3])
1277
&& my_isdigit(cs, version_str[4])
1281
version=strtol(version_str, NULL, 10);
1125
1283
/* Accept 'M' 'm' 'm' 'd' 'd' */
1126
lip->yySkipn(pos-1);
1128
if (version <= DRIZZLE_VERSION_ID)
1286
if (version <= MYSQL_VERSION_ID)
1130
1288
/* Expand the content of the special comment as real code */
1131
lip->set_echo(true);
1289
lip->set_echo(TRUE);
1132
1290
state=MY_LEX_START;
1672
1834
// Select is dependent of outer select
1673
1835
s->uncacheable= (s->uncacheable & ~UNCACHEABLE_UNITED) |
1674
1836
UNCACHEABLE_DEPENDENT;
1675
Select_Lex_Unit *munit= s->master_unit();
1837
SELECT_LEX_UNIT *munit= s->master_unit();
1676
1838
munit->uncacheable= (munit->uncacheable & ~UNCACHEABLE_UNITED) |
1677
1839
UNCACHEABLE_DEPENDENT;
1678
for (Select_Lex *sl= munit->first_select(); sl ; sl= sl->next_select())
1840
for (SELECT_LEX *sl= munit->first_select(); sl ; sl= sl->next_select())
1681
1843
!(sl->uncacheable & (UNCACHEABLE_DEPENDENT | UNCACHEABLE_UNITED)))
1682
1844
sl->uncacheable|= UNCACHEABLE_UNITED;
1685
s->is_correlated= true;
1847
s->is_correlated= TRUE;
1686
1848
Item_subselect *subquery_predicate= s->master_unit()->item;
1687
1849
if (subquery_predicate)
1688
subquery_predicate->is_correlated= true;
1850
subquery_predicate->is_correlated= TRUE;
1692
bool Select_Lex_Node::set_braces(bool)
1694
bool Select_Lex_Node::inc_in_sum_expr() { return 1; }
1695
uint32_t Select_Lex_Node::get_in_sum_expr() { return 0; }
1696
TableList* Select_Lex_Node::get_table_list() { return 0; }
1697
List<Item>* Select_Lex_Node::get_item_list() { return 0; }
1698
TableList *Select_Lex_Node::add_table_to_list (Session *, Table_ident *, LEX_STRING *, uint32_t,
1699
thr_lock_type, List<Index_hint> *, LEX_STRING *)
1854
bool st_select_lex_node::set_braces(bool value) { return 1; }
1855
bool st_select_lex_node::inc_in_sum_expr() { return 1; }
1856
uint st_select_lex_node::get_in_sum_expr() { return 0; }
1857
TABLE_LIST* st_select_lex_node::get_table_list() { return 0; }
1858
List<Item>* st_select_lex_node::get_item_list() { return 0; }
1859
TABLE_LIST *st_select_lex_node::add_table_to_list (THD *thd, Table_ident *table,
1861
ulong table_join_options,
1862
thr_lock_type flags,
1863
List<Index_hint> *hints,
1703
uint32_t Select_Lex_Node::get_table_join_options()
1868
ulong st_select_lex_node::get_table_join_options()
1723
Select_Lex_Unit* Select_Lex_Unit::master_unit()
1888
st_select_lex_unit* st_select_lex_unit::master_unit()
1729
Select_Lex* Select_Lex_Unit::outer_select()
1731
return (Select_Lex*) master;
1735
bool Select_Lex::add_order_to_list(Session *session, Item *item, bool asc)
1737
return add_to_list(session, order_list, item, asc);
1741
bool Select_Lex::add_item_to_list(Session *, Item *item)
1743
return(item_list.push_back(item));
1747
bool Select_Lex::add_group_to_list(Session *session, Item *item, bool asc)
1749
return add_to_list(session, group_list, item, asc);
1753
Select_Lex_Unit* Select_Lex::master_unit()
1755
return (Select_Lex_Unit*) master;
1759
Select_Lex* Select_Lex::outer_select()
1761
return (Select_Lex*) master->get_master();
1765
bool Select_Lex::set_braces(bool value)
1894
st_select_lex* st_select_lex_unit::outer_select()
1896
return (st_select_lex*) master;
1900
bool st_select_lex::add_order_to_list(THD *thd, Item *item, bool asc)
1902
return add_to_list(thd, order_list, item, asc);
1906
bool st_select_lex::add_item_to_list(THD *thd, Item *item)
1908
DBUG_ENTER("st_select_lex::add_item_to_list");
1909
DBUG_PRINT("info", ("Item: 0x%lx", (long) item));
1910
DBUG_RETURN(item_list.push_back(item));
1914
bool st_select_lex::add_group_to_list(THD *thd, Item *item, bool asc)
1916
return add_to_list(thd, group_list, item, asc);
1920
st_select_lex_unit* st_select_lex::master_unit()
1922
return (st_select_lex_unit*) master;
1926
st_select_lex* st_select_lex::outer_select()
1928
return (st_select_lex*) master->get_master();
1932
bool st_select_lex::set_braces(bool value)
1772
bool Select_Lex::inc_in_sum_expr()
1939
bool st_select_lex::inc_in_sum_expr()
1779
uint32_t Select_Lex::get_in_sum_expr()
1946
uint st_select_lex::get_in_sum_expr()
1781
1948
return in_sum_expr;
1785
TableList* Select_Lex::get_table_list()
1952
TABLE_LIST* st_select_lex::get_table_list()
1787
return (TableList*) table_list.first;
1954
return (TABLE_LIST*) table_list.first;
1790
List<Item>* Select_Lex::get_item_list()
1957
List<Item>* st_select_lex::get_item_list()
1792
1959
return &item_list;
1795
uint32_t Select_Lex::get_table_join_options()
1962
ulong st_select_lex::get_table_join_options()
1797
1964
return table_join_options;
1801
bool Select_Lex::setup_ref_array(Session *session, uint32_t order_group_num)
1968
bool st_select_lex::setup_ref_array(THD *thd, uint order_group_num)
1803
1970
if (ref_pointer_array)
1974
We have to create array in prepared statement memory if it is
1977
Query_arena *arena= thd->stmt_arena;
1806
1978
return (ref_pointer_array=
1807
(Item **)session->alloc(sizeof(Item*) * (n_child_sum_items +
1979
(Item **)arena->alloc(sizeof(Item*) * (n_child_sum_items +
1808
1980
item_list.elements +
1809
1981
select_n_having_items +
1810
1982
select_n_where_fields +
1975
2166
Initialize LEX object.
1981
2172
LEX object initialized with this constructor can be used as part of
1982
Session object for which one can safely call open_tables(), lock_tables()
2173
THD object for which one can safely call open_tables(), lock_tables()
1983
2174
and close_thread_tables() functions. But it is not yet ready for
1984
2175
statement parsing. On should use lex_start() function to prepare LEX
1989
2180
:result(0), yacc_yyss(0), yacc_yyvs(0),
1990
2181
sql_command(SQLCOM_END), option_type(OPT_DEFAULT), is_lex_started(0)
1993
reset_query_tables_list(true);
2184
my_init_dynamic_array2(&plugins, sizeof(plugin_ref),
2185
plugins_static_buffer,
2186
INITIAL_LEX_PLUGIN_LIST_SIZE,
2187
INITIAL_LEX_PLUGIN_LIST_SIZE);
2188
reset_query_tables_list(TRUE);
2193
Check whether the merging algorithm can be used on this VIEW
2196
st_lex::can_be_merged()
2199
We can apply merge algorithm if it is single SELECT view with
2200
subqueries only in WHERE clause (we do not count SELECTs of underlying
2201
views, and second level subqueries) and we have not grpouping, ordering,
2202
HAVING clause, aggregate functions, DISTINCT clause, LIMIT clause and
2203
several underlying tables.
2206
FALSE - only temporary table algorithm can be used
2207
TRUE - merge algorithm can be used
2210
bool st_lex::can_be_merged()
2212
// TODO: do not forget implement case when select_lex.table_list.elements==0
2214
/* find non VIEW subqueries/unions */
2215
bool selects_allow_merge= select_lex.next_select() == 0;
2216
if (selects_allow_merge)
2218
for (SELECT_LEX_UNIT *tmp_unit= select_lex.first_inner_unit();
2220
tmp_unit= tmp_unit->next_unit())
2222
if (tmp_unit->first_select()->parent_lex == this &&
2223
(tmp_unit->item == 0 ||
2224
(tmp_unit->item->place() != IN_WHERE &&
2225
tmp_unit->item->place() != IN_ON)))
2227
selects_allow_merge= 0;
2233
return (selects_allow_merge &&
2234
select_lex.group_list.elements == 0 &&
2235
select_lex.having == 0 &&
2236
select_lex.with_sum_func == 0 &&
2237
select_lex.table_list.elements >= 1 &&
2238
!(select_lex.options & SELECT_DISTINCT) &&
2239
select_lex.select_limit == 0);
2244
check if command can use VIEW with MERGE algorithm (for top VIEWs)
2247
st_lex::can_use_merged()
2250
Only listed here commands can use merge algorithm in top level
2251
SELECT_LEX (for subqueries will be used merge algorithm if
2252
st_lex::can_not_use_merged() is not TRUE).
2255
FALSE - command can't use merged VIEWs
2256
TRUE - VIEWs with MERGE algorithms can be used
2259
bool st_lex::can_use_merged()
2261
switch (sql_command)
2264
case SQLCOM_CREATE_TABLE:
2266
case SQLCOM_UPDATE_MULTI:
2268
case SQLCOM_DELETE_MULTI:
2270
case SQLCOM_INSERT_SELECT:
2271
case SQLCOM_REPLACE:
2272
case SQLCOM_REPLACE_SELECT:
2281
Check if command can't use merged views in any part of command
2284
st_lex::can_not_use_merged()
2287
Temporary table algorithm will be used on all SELECT levels for queries
2288
listed here (see also st_lex::can_use_merged()).
2291
FALSE - command can't use merged VIEWs
2292
TRUE - VIEWs with MERGE algorithms can be used
2295
bool st_lex::can_not_use_merged()
2297
switch (sql_command)
2300
SQLCOM_SHOW_FIELDS is necessary to make
2301
information schema tables working correctly with views.
2302
see get_schema_tables_result function
2304
case SQLCOM_SHOW_FIELDS:
2058
2395
This method is needed to support this rule.
2060
@return true in case of error (parsing should be aborted, false in
2397
@return TRUE in case of error (parsing should be aborted, FALSE in
2061
2398
case of success
2065
LEX::copy_db_to(char **p_db, size_t *p_db_length) const
2402
st_lex::copy_db_to(char **p_db, size_t *p_db_length) const
2067
return session->copy_db_to(p_db, p_db_length);
2404
return thd->copy_db_to(p_db, p_db_length);
2071
2408
initialize limit counters
2074
Select_Lex_Unit::set_limit()
2075
values - Select_Lex with initial values for counters
2411
st_select_lex_unit::set_limit()
2412
values - SELECT_LEX with initial values for counters
2078
void Select_Lex_Unit::set_limit(Select_Lex *sl)
2415
void st_select_lex_unit::set_limit(st_select_lex *sl)
2080
2417
ha_rows select_limit_val;
2083
2420
val= sl->select_limit ? sl->select_limit->val_uint() : HA_POS_ERROR;
2084
2421
select_limit_val= (ha_rows)val;
2086
Check for overflow : ha_rows can be smaller then uint64_t if
2424
Check for overflow : ha_rows can be smaller then ulonglong if
2087
2425
BIG_TABLES is off.
2089
if (val != (uint64_t)select_limit_val)
2427
if (val != (ulonglong)select_limit_val)
2090
2428
select_limit_val= HA_POS_ERROR;
2091
2430
offset_limit_cnt= (ha_rows)(sl->offset_limit ? sl->offset_limit->val_uint() :
2093
2432
select_limit_cnt= select_limit_val + offset_limit_cnt;
2094
2433
if (select_limit_cnt < select_limit_val)
2095
2434
select_limit_cnt= HA_POS_ERROR; // no limit
2280
static void fix_prepare_info_in_table_list(Session *session, TableList *tbl)
2668
static void fix_prepare_info_in_table_list(THD *thd, TABLE_LIST *tbl)
2282
2670
for (; tbl; tbl= tbl->next_local)
2284
2672
if (tbl->on_expr)
2286
2674
tbl->prep_on_expr= tbl->on_expr;
2287
tbl->on_expr= tbl->on_expr->copy_andor_structure(session);
2289
fix_prepare_info_in_table_list(session, tbl->merge_underlying_list);
2295
There are Select_Lex::add_table_to_list &
2296
Select_Lex::set_lock_for_tables are in sql_parse.cc
2298
Select_Lex::print is in sql_select.cc
2300
Select_Lex_Unit::prepare, Select_Lex_Unit::exec,
2301
Select_Lex_Unit::cleanup, Select_Lex_Unit::reinit_exec_mechanism,
2302
Select_Lex_Unit::change_result
2675
tbl->on_expr= tbl->on_expr->copy_andor_structure(thd);
2677
fix_prepare_info_in_table_list(thd, tbl->merge_underlying_list);
2683
Save WHERE/HAVING/ON clauses and replace them with disposable copies
2686
st_select_lex::fix_prepare_information
2688
conds in/out pointer to WHERE condition to be met at execution
2689
having_conds in/out pointer to HAVING condition to be met at execution
2692
The passed WHERE and HAVING are to be saved for the future executions.
2693
This function saves it, and returns a copy which can be thrashed during
2694
this execution of the statement. By saving/thrashing here we mean only
2696
The function also calls fix_prepare_info_in_table_list that saves all
2700
void st_select_lex::fix_prepare_information(THD *thd, Item **conds,
2701
Item **having_conds)
2703
if (thd->stmt_arena->is_conventional() == false && first_execution)
2709
*conds= where= prep_where->copy_andor_structure(thd);
2713
prep_having= *having_conds;
2714
*having_conds= having= prep_having->copy_andor_structure(thd);
2716
fix_prepare_info_in_table_list(thd, (TABLE_LIST *)table_list.first);
2722
There are st_select_lex::add_table_to_list &
2723
st_select_lex::set_lock_for_tables are in sql_parse.cc
2725
st_select_lex::print is in sql_select.cc
2727
st_select_lex_unit::prepare, st_select_lex_unit::exec,
2728
st_select_lex_unit::cleanup, st_select_lex_unit::reinit_exec_mechanism,
2729
st_select_lex_unit::change_result
2303
2730
are in sql_union.cc