xref: /OK3568_Linux_fs/buildroot/package/oracle-mysql/0005-bison_3_breaks_mysql_server_build.patch (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1fix the yacc code in mysql
2
3Signed-off-by: Marcelo Gutierrez (UTN/FRH) <kuyurix@gmail.com>
4---
5diff -uNr mysql-5.1.73.orig/sql/sql_lex.cc mysql-5.1.73/sql/sql_lex.cc
6--- mysql-5.1.73.orig/sql/sql_lex.cc	2013-11-04 18:52:27.000000000 +0000
7+++ mysql-5.1.73/sql/sql_lex.cc	2014-02-12 14:12:04.244111625 +0000
8@@ -775,14 +775,13 @@
9 				(which can't be followed by a signed number)
10 */
11
12-int MYSQLlex(void *arg, void *yythd)
13+int MYSQLlex(void *arg, THD *thd)
14 {
15   reg1	uchar c= 0;
16   bool comment_closed;
17   int	tokval, result_state;
18   uint length;
19   enum my_lex_states state;
20-  THD *thd= (THD *)yythd;
21   Lex_input_stream *lip= & thd->m_parser_state->m_lip;
22   LEX *lex= thd->lex;
23   YYSTYPE *yylval=(YYSTYPE*) arg;
24diff -uNr mysql-5.1.73.orig/sql/sql_lex.h mysql-5.1.73/sql/sql_lex.h
25--- mysql-5.1.73.orig/sql/sql_lex.h	2013-11-04 18:52:27.000000000 +0000
26+++ mysql-5.1.73/sql/sql_lex.h	2014-02-12 14:17:19.424106423 +0000
27@@ -2072,7 +2072,7 @@
28 extern void lex_free(void);
29 extern void lex_start(THD *thd);
30 extern void lex_end(LEX *lex);
31-extern int MYSQLlex(void *arg, void *yythd);
32+extern int MYSQLlex(void *arg, THD *thd);
33
34 extern void trim_whitespace(CHARSET_INFO *cs, LEX_STRING *str);
35
36diff -uNr mysql-5.1.73.orig/sql/sql_parse.cc mysql-5.1.73/sql/sql_parse.cc
37--- mysql-5.1.73.orig/sql/sql_parse.cc	2013-11-04 18:52:27.000000000 +0000
38+++ mysql-5.1.73/sql/sql_parse.cc	2014-02-12 14:19:20.424104427 +0000
39@@ -8012,7 +8012,7 @@
40 }
41
42
43-extern int MYSQLparse(void *thd); // from sql_yacc.cc
44+extern int MYSQLparse(THD *thd); // from sql_yacc.cc
45
46
47 /**
48diff -uNr mysql-5.1.73.orig/sql/sql_yacc.yy mysql-5.1.73/sql/sql_yacc.yy
49--- mysql-5.1.73.orig/sql/sql_yacc.yy	2013-11-04 18:52:27.000000000 +0000
50+++ mysql-5.1.73/sql/sql_yacc.yy	2014-02-12 20:17:06.707750140 +0000
51@@ -23,19 +23,13 @@
52 */
53
54 %{
55-/* thd is passed as an argument to yyparse(), and subsequently to yylex().
56-** The type will be void*, so it must be  cast to (THD*) when used.
57-** Use the YYTHD macro for this.
58-*/
59-#define YYPARSE_PARAM yythd
60-#define YYLEX_PARAM yythd
61-#define YYTHD ((THD *)yythd)
62-#define YYLIP (& YYTHD->m_parser_state->m_lip)
63+
64+#define YYLIP (& thd->m_parser_state->m_lip)
65
66 #define MYSQL_YACC
67 #define YYINITDEPTH 100
68 #define YYMAXDEPTH 3200                        /* Because of 64K stack */
69-#define Lex (YYTHD->lex)
70+#define Lex (thd->lex)
71 #define Select Lex->current_select
72 #include "mysql_priv.h"
73 #include "slave.h"
74@@ -55,7 +49,7 @@
75 #pragma warning (disable : 4065)
76 #endif
77
78-int yylex(void *yylval, void *yythd);
79+int yylex(void *yylval, THD *thd);
80
81 const LEX_STRING null_lex_str= {0,0};
82
83@@ -64,7 +58,7 @@
84     ulong val= *(F);                          \
85     if (my_yyoverflow((B), (D), &val))        \
86     {                                         \
87-      yyerror((char*) (A));                   \
88+      yyerror(current_thd, (char*) (A));      \
89       return 2;                               \
90     }                                         \
91     else                                      \
92@@ -76,7 +70,7 @@
93 #define MYSQL_YYABORT                         \
94   do                                          \
95   {                                           \
96-    LEX::cleanup_lex_after_parse_error(YYTHD);\
97+    LEX::cleanup_lex_after_parse_error(thd);\
98     YYABORT;                                  \
99   } while (0)
100
101@@ -159,9 +153,8 @@
102   to abort from the parser.
103 */
104
105-void MYSQLerror(const char *s)
106+void MYSQLerror(THD *thd, const char *s)
107 {
108-  THD *thd= current_thd;
109
110   /*
111     Restore the original LEX if it was replaced when parsing
112@@ -675,7 +668,10 @@
113 bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize);
114 %}
115
116-%pure_parser                                    /* We have threads */
117+/* We have threads */
118+%define api.pure
119+%parse-param { THD *thd }
120+%lex-param { THD *thd }
121 /*
122   Currently there are 169 shift/reduce conflicts.
123   We should not introduce new conflicts any more.
124@@ -1516,7 +1512,6 @@
125 query:
126           END_OF_INPUT
127           {
128-            THD *thd= YYTHD;
129             if (!thd->bootstrap &&
130               (!(thd->lex->select_lex.options & OPTION_FOUND_COMMENT)))
131             {
132@@ -1530,7 +1525,7 @@
133           {
134             Lex_input_stream *lip = YYLIP;
135
136-            if ((YYTHD->client_capabilities & CLIENT_MULTI_QUERIES) &&
137+            if ((thd->client_capabilities & CLIENT_MULTI_QUERIES) &&
138                 ! lip->stmt_prepare_mode &&
139                 ! lip->eof())
140             {
141@@ -1626,7 +1621,6 @@
142 deallocate:
143           deallocate_or_drop PREPARE_SYM ident
144           {
145-            THD *thd= YYTHD;
146             LEX *lex= thd->lex;
147             lex->sql_command= SQLCOM_DEALLOCATE_PREPARE;
148             lex->prepared_stmt_name= $3;
149@@ -1641,7 +1635,6 @@
150 prepare:
151           PREPARE_SYM ident FROM prepare_src
152           {
153-            THD *thd= YYTHD;
154             LEX *lex= thd->lex;
155             lex->sql_command= SQLCOM_PREPARE;
156             lex->prepared_stmt_name= $2;
157@@ -1651,14 +1644,12 @@
158 prepare_src:
159           TEXT_STRING_sys
160           {
161-            THD *thd= YYTHD;
162             LEX *lex= thd->lex;
163             lex->prepared_stmt_code= $1;
164             lex->prepared_stmt_code_is_varref= FALSE;
165           }
166         | '@' ident_or_text
167           {
168-            THD *thd= YYTHD;
169             LEX *lex= thd->lex;
170             lex->prepared_stmt_code= $2;
171             lex->prepared_stmt_code_is_varref= TRUE;
172@@ -1668,7 +1659,6 @@
173 execute:
174           EXECUTE_SYM ident
175           {
176-            THD *thd= YYTHD;
177             LEX *lex= thd->lex;
178             lex->sql_command= SQLCOM_EXECUTE;
179             lex->prepared_stmt_name= $2;
180@@ -1826,7 +1816,6 @@
181 create:
182           CREATE opt_table_options TABLE_SYM opt_if_not_exists table_ident
183           {
184-            THD *thd= YYTHD;
185             LEX *lex= thd->lex;
186             lex->sql_command= SQLCOM_CREATE_TABLE;
187             if (!lex->select_lex.add_table_to_list(thd, $5, NULL,
188@@ -1844,13 +1833,13 @@
189           }
190           create2
191           {
192-            LEX *lex= YYTHD->lex;
193+            LEX *lex= thd->lex;
194             lex->current_select= &lex->select_lex;
195             if ((lex->create_info.used_fields & HA_CREATE_USED_ENGINE) &&
196                 !lex->create_info.db_type)
197             {
198-              lex->create_info.db_type= ha_default_handlerton(YYTHD);
199-              push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN,
200+              lex->create_info.db_type= ha_default_handlerton(thd);
201+              push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
202                                   ER_WARN_USING_OTHER_HANDLER,
203                                   ER(ER_WARN_USING_OTHER_HANDLER),
204                                   ha_resolve_storage_engine_name(lex->create_info.db_type),
205@@ -1979,7 +1968,7 @@
206 event_tail:
207           remember_name EVENT_SYM opt_if_not_exists sp_name
208           {
209-            THD *thd= YYTHD;
210+            THD *thd= thd;
211             LEX *lex=Lex;
212
213             lex->stmt_definition_begin= $1;
214@@ -2046,7 +2035,7 @@
215 ev_starts:
216           /* empty */
217           {
218-            Item *item= new (YYTHD->mem_root) Item_func_now_local();
219+            Item *item= new (thd->mem_root) Item_func_now_local();
220             if (item == NULL)
221               MYSQL_YYABORT;
222             Lex->event_parse_data->item_starts= item;
223@@ -2096,7 +2085,6 @@
224
225 ev_sql_stmt:
226           {
227-            THD *thd= YYTHD;
228             LEX *lex= thd->lex;
229             Lex_input_stream *lip= YYLIP;
230
231@@ -2139,7 +2127,6 @@
232           }
233           ev_sql_stmt_inner
234           {
235-            THD *thd= YYTHD;
236             LEX *lex= thd->lex;
237
238             /* return back to the original memory root ASAP */
239@@ -2198,11 +2185,10 @@
240             $$= new sp_name($1, $3, true);
241             if ($$ == NULL)
242               MYSQL_YYABORT;
243-            $$->init_qname(YYTHD);
244+            $$->init_qname(thd);
245           }
246         | ident
247           {
248-            THD *thd= YYTHD;
249             LEX *lex= thd->lex;
250             LEX_STRING db;
251             if (check_routine_name(&$1))
252@@ -2272,7 +2258,7 @@
253             lex->sql_command= SQLCOM_CALL;
254             lex->spname= $2;
255             lex->value_list.empty();
256-            sp_add_used_routine(lex, YYTHD, $2, TYPE_ENUM_PROCEDURE);
257+            sp_add_used_routine(lex, thd, $2, TYPE_ENUM_PROCEDURE);
258           }
259           opt_sp_cparam_list {}
260         ;
261@@ -2345,7 +2331,7 @@
262                                                      (enum enum_field_types)$3,
263                                                      sp_param_in);
264
265-            if (lex->sphead->fill_field_definition(YYTHD, lex,
266+            if (lex->sphead->fill_field_definition(thd, lex,
267                                                    (enum enum_field_types) $3,
268                                                    &spvar->field_def))
269             {
270@@ -2382,7 +2368,7 @@
271                                                      (enum enum_field_types)$4,
272                                                      (sp_param_mode_t)$1);
273
274-            if (lex->sphead->fill_field_definition(YYTHD, lex,
275+            if (lex->sphead->fill_field_definition(thd, lex,
276                                                    (enum enum_field_types) $4,
277                                                    &spvar->field_def))
278             {
279@@ -2445,13 +2431,12 @@
280           {
281             LEX *lex= Lex;
282
283-            lex->sphead->reset_lex(YYTHD);
284+            lex->sphead->reset_lex(thd);
285             lex->spcont->declare_var_boundary($2);
286           }
287           type
288           sp_opt_default
289           {
290-            THD *thd= YYTHD;
291             LEX *lex= Lex;
292             sp_pcontext *pctx= lex->spcont;
293             uint num_vars= pctx->context_var_count();
294@@ -2477,7 +2462,7 @@
295               spvar->type= var_type;
296               spvar->dflt= dflt_value_item;
297
298-              if (lex->sphead->fill_field_definition(YYTHD, lex, var_type,
299+              if (lex->sphead->fill_field_definition(thd, lex, var_type,
300                                                      &spvar->field_def))
301               {
302                 MYSQL_YYABORT;
303@@ -2501,7 +2486,7 @@
304             }
305
306             pctx->declare_var_boundary(0);
307-            if (lex->sphead->restore_lex(YYTHD))
308+            if (lex->sphead->restore_lex(thd))
309               MYSQL_YYABORT;
310             $$.vars= $2;
311             $$.conds= $$.hndlrs= $$.curs= 0;
312@@ -2516,7 +2501,7 @@
313 	      my_error(ER_SP_DUP_COND, MYF(0), $2.str);
314 	      MYSQL_YYABORT;
315 	    }
316-	    if(YYTHD->lex->spcont->push_cond(&$2, $5))
317+	    if(thd->lex->spcont->push_cond(&$2, $5))
318               MYSQL_YYABORT;
319             $$.vars= $$.hndlrs= $$.curs= 0;
320             $$.conds= 1;
321@@ -2602,7 +2587,7 @@
322
323 sp_cursor_stmt:
324           {
325-            Lex->sphead->reset_lex(YYTHD);
326+            Lex->sphead->reset_lex(thd);
327           }
328           select
329           {
330@@ -2618,7 +2603,7 @@
331             }
332             lex->sp_lex_in_use= TRUE;
333             $$= lex;
334-            if (lex->sphead->restore_lex(YYTHD))
335+            if (lex->sphead->restore_lex(thd))
336               MYSQL_YYABORT;
337           }
338         ;
339@@ -2662,7 +2647,7 @@
340 sp_cond:
341           ulong_num
342           { /* mysql errno */
343-            $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
344+            $$= (sp_cond_type_t *)thd->alloc(sizeof(sp_cond_type_t));
345             if ($$ == NULL)
346               MYSQL_YYABORT;
347             $$->type= sp_cond_type_t::number;
348@@ -2675,7 +2660,7 @@
349               my_error(ER_SP_BAD_SQLSTATE, MYF(0), $3.str);
350               MYSQL_YYABORT;
351             }
352-            $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
353+            $$= (sp_cond_type_t *)thd->alloc(sizeof(sp_cond_type_t));
354             if ($$ == NULL)
355               MYSQL_YYABORT;
356             $$->type= sp_cond_type_t::state;
357@@ -2705,21 +2690,21 @@
358           }
359         | SQLWARNING_SYM /* SQLSTATEs 01??? */
360           {
361-            $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
362+            $$= (sp_cond_type_t *)thd->alloc(sizeof(sp_cond_type_t));
363             if ($$ == NULL)
364               MYSQL_YYABORT;
365             $$->type= sp_cond_type_t::warning;
366           }
367         | not FOUND_SYM /* SQLSTATEs 02??? */
368           {
369-            $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
370+            $$= (sp_cond_type_t *)thd->alloc(sizeof(sp_cond_type_t));
371             if ($$ == NULL)
372               MYSQL_YYABORT;
373             $$->type= sp_cond_type_t::notfound;
374           }
375         | SQLEXCEPTION_SYM /* All other SQLSTATEs */
376           {
377-            $$= (sp_cond_type_t *)YYTHD->alloc(sizeof(sp_cond_type_t));
378+            $$= (sp_cond_type_t *)thd->alloc(sizeof(sp_cond_type_t));
379             if ($$ == NULL)
380               MYSQL_YYABORT;
381             $$->type= sp_cond_type_t::exception;
382@@ -2789,7 +2774,6 @@
383
384 sp_proc_stmt_statement:
385           {
386-            THD *thd= YYTHD;
387             LEX *lex= thd->lex;
388             Lex_input_stream *lip= YYLIP;
389
390@@ -2798,7 +2782,6 @@
391           }
392           statement
393           {
394-            THD *thd= YYTHD;
395             LEX *lex= thd->lex;
396             Lex_input_stream *lip= YYLIP;
397             sp_head *sp= lex->sphead;
398@@ -2845,7 +2828,7 @@
399
400 sp_proc_stmt_return:
401           RETURN_SYM
402-          { Lex->sphead->reset_lex(YYTHD); }
403+          { Lex->sphead->reset_lex(thd); }
404           expr
405           {
406             LEX *lex= Lex;
407@@ -2867,7 +2850,7 @@
408                 MYSQL_YYABORT;
409               sp->m_flags|= sp_head::HAS_RETURN;
410             }
411-            if (sp->restore_lex(YYTHD))
412+            if (sp->restore_lex(thd))
413               MYSQL_YYABORT;
414           }
415         ;
416@@ -3094,7 +3077,7 @@
417         ;
418
419 sp_if:
420-          { Lex->sphead->reset_lex(YYTHD); }
421+          { Lex->sphead->reset_lex(thd); }
422           expr THEN_SYM
423           {
424             LEX *lex= Lex;
425@@ -3108,7 +3091,7 @@
426                 sp->add_cont_backpatch(i) ||
427                 sp->add_instr(i))
428               MYSQL_YYABORT;
429-            if (sp->restore_lex(YYTHD))
430+            if (sp->restore_lex(thd))
431               MYSQL_YYABORT;
432           }
433           sp_proc_stmts1
434@@ -3147,7 +3130,7 @@
435           {
436             LEX *lex= Lex;
437             case_stmt_action_case(lex);
438-            lex->sphead->reset_lex(YYTHD); /* For expr $3 */
439+            lex->sphead->reset_lex(thd); /* For expr $3 */
440           }
441           expr
442           {
443@@ -3156,7 +3139,7 @@
444               MYSQL_YYABORT;
445
446             /* For expr $3 */
447-            if (lex->sphead->restore_lex(YYTHD))
448+            if (lex->sphead->restore_lex(thd))
449               MYSQL_YYABORT;
450           }
451           simple_when_clause_list
452@@ -3198,7 +3181,7 @@
453 simple_when_clause:
454           WHEN_SYM
455           {
456-            Lex->sphead->reset_lex(YYTHD); /* For expr $3 */
457+            Lex->sphead->reset_lex(thd); /* For expr $3 */
458           }
459           expr
460           {
461@@ -3208,7 +3191,7 @@
462             if (case_stmt_action_when(lex, $3, true))
463               MYSQL_YYABORT;
464             /* For expr $3 */
465-            if (lex->sphead->restore_lex(YYTHD))
466+            if (lex->sphead->restore_lex(thd))
467               MYSQL_YYABORT;
468           }
469           THEN_SYM
470@@ -3223,7 +3206,7 @@
471 searched_when_clause:
472           WHEN_SYM
473           {
474-            Lex->sphead->reset_lex(YYTHD); /* For expr $3 */
475+            Lex->sphead->reset_lex(thd); /* For expr $3 */
476           }
477           expr
478           {
479@@ -3231,7 +3214,7 @@
480             if (case_stmt_action_when(lex, $3, false))
481               MYSQL_YYABORT;
482             /* For expr $3 */
483-            if (lex->sphead->restore_lex(YYTHD))
484+            if (lex->sphead->restore_lex(thd))
485               MYSQL_YYABORT;
486           }
487           THEN_SYM
488@@ -3395,7 +3378,7 @@
489               MYSQL_YYABORT;
490 	  }
491         | WHILE_SYM
492-          { Lex->sphead->reset_lex(YYTHD); }
493+          { Lex->sphead->reset_lex(thd); }
494           expr DO_SYM
495           {
496             LEX *lex= Lex;
497@@ -3409,7 +3392,7 @@
498                 sp->new_cont_backpatch(i) ||
499                 sp->add_instr(i))
500               MYSQL_YYABORT;
501-            if (sp->restore_lex(YYTHD))
502+            if (sp->restore_lex(thd))
503               MYSQL_YYABORT;
504           }
505           sp_proc_stmts1 END WHILE_SYM
506@@ -3424,7 +3407,7 @@
507             lex->sphead->do_cont_backpatch();
508           }
509         | REPEAT_SYM sp_proc_stmts1 UNTIL_SYM
510-          { Lex->sphead->reset_lex(YYTHD); }
511+          { Lex->sphead->reset_lex(thd); }
512           expr END REPEAT_SYM
513           {
514             LEX *lex= Lex;
515@@ -3436,7 +3419,7 @@
516             if (i == NULL ||
517                 lex->sphead->add_instr(i))
518               MYSQL_YYABORT;
519-            if (lex->sphead->restore_lex(YYTHD))
520+            if (lex->sphead->restore_lex(thd))
521               MYSQL_YYABORT;
522             /* We can shortcut the cont_backpatch here */
523             i->m_cont_dest= ip+1;
524@@ -3859,7 +3842,6 @@
525           create3 {}
526         | LIKE table_ident
527           {
528-            THD *thd= YYTHD;
529             TABLE_LIST *src_table;
530             LEX *lex= thd->lex;
531
532@@ -3873,7 +3855,6 @@
533           }
534         | '(' LIKE table_ident ')'
535           {
536-            THD *thd= YYTHD;
537             TABLE_LIST *src_table;
538             LEX *lex= thd->lex;
539
540@@ -4342,7 +4323,6 @@
541           bit_expr
542           {
543             Item *part_expr= $1;
544-            THD *thd= YYTHD;
545             LEX *lex= thd->lex;
546             Name_resolution_context *context= &lex->current_select->context;
547             TABLE_LIST *save_list= context->table_list;
548@@ -4364,7 +4344,7 @@
549               my_error(ER_PARTITION_FUNCTION_IS_NOT_ALLOWED, MYF(0));
550               MYSQL_YYABORT;
551             }
552-            if (part_expr->fix_fields(YYTHD, (Item**)0) ||
553+            if (part_expr->fix_fields(thd, (Item**)0) ||
554                 ((context->table_list= save_list), FALSE) ||
555                 (!part_expr->const_item()) ||
556                 (!lex->safe_to_cache_query))
557@@ -4629,7 +4609,7 @@
558         | TYPE_SYM opt_equal storage_engines
559           {
560             Lex->create_info.db_type= $3;
561-            WARN_DEPRECATED(yythd, "6.0", "TYPE=storage_engine",
562+            WARN_DEPRECATED(thd, "6.0", "TYPE=storage_engine",
563                             "'ENGINE=storage_engine'");
564             Lex->create_info.used_fields|= HA_CREATE_USED_ENGINE;
565           }
566@@ -4791,19 +4771,19 @@
567 storage_engines:
568           ident_or_text
569           {
570-            plugin_ref plugin= ha_resolve_by_name(YYTHD, &$1);
571+            plugin_ref plugin= ha_resolve_by_name(thd, &$1);
572
573             if (plugin)
574               $$= plugin_data(plugin, handlerton*);
575             else
576             {
577-              if (YYTHD->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION)
578+              if (thd->variables.sql_mode & MODE_NO_ENGINE_SUBSTITUTION)
579               {
580                 my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), $1.str);
581                 MYSQL_YYABORT;
582               }
583               $$= 0;
584-              push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_WARN,
585+              push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
586                                   ER_UNKNOWN_STORAGE_ENGINE,
587                                   ER(ER_UNKNOWN_STORAGE_ENGINE),
588                                   $1.str);
589@@ -4815,7 +4795,7 @@
590           ident_or_text
591           {
592             plugin_ref plugin;
593-            if ((plugin= ha_resolve_by_name(YYTHD, &$1)))
594+            if ((plugin= ha_resolve_by_name(thd, &$1)))
595               $$= plugin_data(plugin, handlerton*);
596             else
597             {
598@@ -5043,7 +5023,7 @@
599               {
600                 char buff[sizeof("YEAR()") + MY_INT64_NUM_DECIMAL_DIGITS + 1];
601                 my_snprintf(buff, sizeof(buff), "YEAR(%lu)", length);
602-                push_warning_printf(YYTHD, MYSQL_ERROR::WARN_LEVEL_NOTE,
603+                push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
604                                     ER_WARN_DEPRECATED_SYNTAX,
605                                     ER(ER_WARN_DEPRECATED_SYNTAX),
606                                     buff, "YEAR(4)");
607@@ -5057,7 +5037,7 @@
608           { $$=MYSQL_TYPE_TIME; }
609         | TIMESTAMP opt_field_length
610           {
611-            if (YYTHD->variables.sql_mode & MODE_MAXDB)
612+            if (thd->variables.sql_mode & MODE_MAXDB)
613               $$=MYSQL_TYPE_DATETIME;
614             else
615             {
616@@ -5189,7 +5169,7 @@
617 real_type:
618           REAL
619           {
620-            $$= YYTHD->variables.sql_mode & MODE_REAL_AS_FLOAT ?
621+            $$= thd->variables.sql_mode & MODE_REAL_AS_FLOAT ?
622               MYSQL_TYPE_FLOAT : MYSQL_TYPE_DOUBLE;
623           }
624         | DOUBLE_SYM
625@@ -5263,7 +5243,7 @@
626         | DEFAULT now_or_signed_literal { Lex->default_value=$2; }
627         | ON UPDATE_SYM NOW_SYM optional_braces
628           {
629-            Item *item= new (YYTHD->mem_root) Item_func_now_local();
630+            Item *item= new (thd->mem_root) Item_func_now_local();
631             if (item == NULL)
632               MYSQL_YYABORT;
633             Lex->on_update_value= item;
634@@ -5312,7 +5292,7 @@
635 now_or_signed_literal:
636           NOW_SYM optional_braces
637           {
638-            $$= new (YYTHD->mem_root) Item_func_now_local();
639+            $$= new (thd->mem_root) Item_func_now_local();
640             if ($$ == NULL)
641               MYSQL_YYABORT;
642           }
643@@ -5673,7 +5653,6 @@
644 alter:
645           ALTER opt_ignore TABLE_SYM table_ident
646           {
647-            THD *thd= YYTHD;
648             LEX *lex= thd->lex;
649             lex->name.str= 0;
650             lex->name.length= 0;
651@@ -5799,7 +5778,7 @@
652               Event_parse_data.
653             */
654
655-            if (!(Lex->event_parse_data= Event_parse_data::new_instance(YYTHD)))
656+            if (!(Lex->event_parse_data= Event_parse_data::new_instance(thd)))
657               MYSQL_YYABORT;
658             Lex->event_parse_data->identifier= $4;
659
660@@ -6192,7 +6171,6 @@
661           {
662             if (!$4)
663             {
664-              THD *thd= YYTHD;
665               $4= thd->variables.collation_database;
666             }
667             $5= $5 ? $5 : $4;
668@@ -6556,7 +6534,7 @@
669 assign_to_keycache:
670           table_ident cache_keys_spec
671           {
672-            if (!Select->add_table_to_list(YYTHD, $1, NULL, 0, TL_READ,
673+            if (!Select->add_table_to_list(thd, $1, NULL, 0, TL_READ,
674                                            Select->pop_index_hints()))
675               MYSQL_YYABORT;
676           }
677@@ -6585,7 +6563,7 @@
678 preload_keys:
679           table_ident cache_keys_spec opt_ignore_leaves
680           {
681-            if (!Select->add_table_to_list(YYTHD, $1, NULL, $3, TL_READ,
682+            if (!Select->add_table_to_list(thd, $1, NULL, $3, TL_READ,
683                                            Select->pop_index_hints()))
684               MYSQL_YYABORT;
685           }
686@@ -6593,7 +6571,7 @@
687
688 cache_keys_spec:
689           {
690-            Lex->select_lex.alloc_index_hints(YYTHD);
691+            Lex->select_lex.alloc_index_hints(thd);
692             Select->set_index_hint_type(INDEX_HINT_USE,
693                                         global_system_variables.old_mode ?
694                                         INDEX_HINT_MASK_JOIN :
695@@ -6813,7 +6791,6 @@
696         | select_item
697         | '*'
698           {
699-            THD *thd= YYTHD;
700             Item *item= new (thd->mem_root)
701                           Item_field(&thd->lex->current_select->context,
702                                      NULL, NULL, "*");
703@@ -6828,7 +6805,6 @@
704 select_item:
705           remember_name select_item2 remember_end select_alias
706           {
707-            THD *thd= YYTHD;
708             DBUG_ASSERT($1 < $3);
709
710             if (add_item_to_list(thd, $2))
711@@ -6929,7 +6905,7 @@
712             else
713             {
714               /* X OR Y */
715-              $$ = new (YYTHD->mem_root) Item_cond_or($1, $3);
716+              $$ = new (thd->mem_root) Item_cond_or($1, $3);
717               if ($$ == NULL)
718                 MYSQL_YYABORT;
719             }
720@@ -6937,7 +6913,7 @@
721         | expr XOR expr %prec XOR
722           {
723             /* XOR is a proprietary extension */
724-            $$ = new (YYTHD->mem_root) Item_cond_xor($1, $3);
725+            $$ = new (thd->mem_root) Item_cond_xor($1, $3);
726             if ($$ == NULL)
727               MYSQL_YYABORT;
728           }
729@@ -6979,50 +6955,50 @@
730             else
731             {
732               /* X AND Y */
733-              $$ = new (YYTHD->mem_root) Item_cond_and($1, $3);
734+              $$ = new (thd->mem_root) Item_cond_and($1, $3);
735               if ($$ == NULL)
736                 MYSQL_YYABORT;
737             }
738           }
739         | NOT_SYM expr %prec NOT_SYM
740           {
741-            $$= negate_expression(YYTHD, $2);
742+            $$= negate_expression(thd, $2);
743             if ($$ == NULL)
744               MYSQL_YYABORT;
745           }
746         | bool_pri IS TRUE_SYM %prec IS
747           {
748-            $$= new (YYTHD->mem_root) Item_func_istrue($1);
749+            $$= new (thd->mem_root) Item_func_istrue($1);
750             if ($$ == NULL)
751               MYSQL_YYABORT;
752           }
753         | bool_pri IS not TRUE_SYM %prec IS
754           {
755-            $$= new (YYTHD->mem_root) Item_func_isnottrue($1);
756+            $$= new (thd->mem_root) Item_func_isnottrue($1);
757             if ($$ == NULL)
758               MYSQL_YYABORT;
759           }
760         | bool_pri IS FALSE_SYM %prec IS
761           {
762-            $$= new (YYTHD->mem_root) Item_func_isfalse($1);
763+            $$= new (thd->mem_root) Item_func_isfalse($1);
764             if ($$ == NULL)
765               MYSQL_YYABORT;
766           }
767         | bool_pri IS not FALSE_SYM %prec IS
768           {
769-            $$= new (YYTHD->mem_root) Item_func_isnotfalse($1);
770+            $$= new (thd->mem_root) Item_func_isnotfalse($1);
771             if ($$ == NULL)
772               MYSQL_YYABORT;
773           }
774         | bool_pri IS UNKNOWN_SYM %prec IS
775           {
776-            $$= new (YYTHD->mem_root) Item_func_isnull($1);
777+            $$= new (thd->mem_root) Item_func_isnull($1);
778             if ($$ == NULL)
779               MYSQL_YYABORT;
780           }
781         | bool_pri IS not UNKNOWN_SYM %prec IS
782           {
783-            $$= new (YYTHD->mem_root) Item_func_isnotnull($1);
784+            $$= new (thd->mem_root) Item_func_isnotnull($1);
785             if ($$ == NULL)
786               MYSQL_YYABORT;
787           }
788@@ -7032,19 +7008,19 @@
789 bool_pri:
790           bool_pri IS NULL_SYM %prec IS
791           {
792-            $$= new (YYTHD->mem_root) Item_func_isnull($1);
793+            $$= new (thd->mem_root) Item_func_isnull($1);
794             if ($$ == NULL)
795               MYSQL_YYABORT;
796           }
797         | bool_pri IS not NULL_SYM %prec IS
798           {
799-            $$= new (YYTHD->mem_root) Item_func_isnotnull($1);
800+            $$= new (thd->mem_root) Item_func_isnotnull($1);
801             if ($$ == NULL)
802               MYSQL_YYABORT;
803           }
804         | bool_pri EQUAL_SYM predicate %prec EQUAL_SYM
805           {
806-            $$= new (YYTHD->mem_root) Item_func_equal($1,$3);
807+            $$= new (thd->mem_root) Item_func_equal($1,$3);
808             if ($$ == NULL)
809               MYSQL_YYABORT;
810           }
811@@ -7066,13 +7042,12 @@
812 predicate:
813           bit_expr IN_SYM '(' subselect ')'
814           {
815-            $$= new (YYTHD->mem_root) Item_in_subselect($1, $4);
816+            $$= new (thd->mem_root) Item_in_subselect($1, $4);
817             if ($$ == NULL)
818               MYSQL_YYABORT;
819           }
820         | bit_expr not IN_SYM '(' subselect ')'
821           {
822-            THD *thd= YYTHD;
823             Item *item= new (thd->mem_root) Item_in_subselect($1, $5);
824             if (item == NULL)
825               MYSQL_YYABORT;
826@@ -7082,7 +7057,7 @@
827           }
828         | bit_expr IN_SYM '(' expr ')'
829           {
830-            $$= handle_sql2003_note184_exception(YYTHD, $1, true, $4);
831+            $$= handle_sql2003_note184_exception(thd, $1, true, $4);
832             if ($$ == NULL)
833               MYSQL_YYABORT;
834           }
835@@ -7090,13 +7065,13 @@
836           {
837             $6->push_front($4);
838             $6->push_front($1);
839-            $$= new (YYTHD->mem_root) Item_func_in(*$6);
840+            $$= new (thd->mem_root) Item_func_in(*$6);
841             if ($$ == NULL)
842               MYSQL_YYABORT;
843           }
844         | bit_expr not IN_SYM '(' expr ')'
845           {
846-            $$= handle_sql2003_note184_exception(YYTHD, $1, false, $5);
847+            $$= handle_sql2003_note184_exception(thd, $1, false, $5);
848             if ($$ == NULL)
849               MYSQL_YYABORT;
850           }
851@@ -7104,7 +7079,7 @@
852           {
853             $7->push_front($5);
854             $7->push_front($1);
855-            Item_func_in *item = new (YYTHD->mem_root) Item_func_in(*$7);
856+            Item_func_in *item = new (thd->mem_root) Item_func_in(*$7);
857             if (item == NULL)
858               MYSQL_YYABORT;
859             item->negate();
860@@ -7112,14 +7087,14 @@
861           }
862         | bit_expr BETWEEN_SYM bit_expr AND_SYM predicate
863           {
864-            $$= new (YYTHD->mem_root) Item_func_between($1,$3,$5);
865+            $$= new (thd->mem_root) Item_func_between($1,$3,$5);
866             if ($$ == NULL)
867               MYSQL_YYABORT;
868           }
869         | bit_expr not BETWEEN_SYM bit_expr AND_SYM predicate
870           {
871             Item_func_between *item;
872-            item= new (YYTHD->mem_root) Item_func_between($1,$4,$6);
873+            item= new (thd->mem_root) Item_func_between($1,$4,$6);
874             if (item == NULL)
875               MYSQL_YYABORT;
876             item->negate();
877@@ -7127,42 +7102,42 @@
878           }
879         | bit_expr SOUNDS_SYM LIKE bit_expr
880           {
881-            Item *item1= new (YYTHD->mem_root) Item_func_soundex($1);
882-            Item *item4= new (YYTHD->mem_root) Item_func_soundex($4);
883+            Item *item1= new (thd->mem_root) Item_func_soundex($1);
884+            Item *item4= new (thd->mem_root) Item_func_soundex($4);
885             if ((item1 == NULL) || (item4 == NULL))
886               MYSQL_YYABORT;
887-            $$= new (YYTHD->mem_root) Item_func_eq(item1, item4);
888+            $$= new (thd->mem_root) Item_func_eq(item1, item4);
889             if ($$ == NULL)
890               MYSQL_YYABORT;
891           }
892         | bit_expr LIKE simple_expr opt_escape
893           {
894-            $$= new (YYTHD->mem_root) Item_func_like($1,$3,$4,Lex->escape_used);
895+            $$= new (thd->mem_root) Item_func_like($1,$3,$4,Lex->escape_used);
896             if ($$ == NULL)
897               MYSQL_YYABORT;
898           }
899         | bit_expr not LIKE simple_expr opt_escape
900           {
901-            Item *item= new (YYTHD->mem_root) Item_func_like($1,$4,$5,
902+            Item *item= new (thd->mem_root) Item_func_like($1,$4,$5,
903                                                              Lex->escape_used);
904             if (item == NULL)
905               MYSQL_YYABORT;
906-            $$= new (YYTHD->mem_root) Item_func_not(item);
907+            $$= new (thd->mem_root) Item_func_not(item);
908             if ($$ == NULL)
909               MYSQL_YYABORT;
910           }
911         | bit_expr REGEXP bit_expr
912           {
913-            $$= new (YYTHD->mem_root) Item_func_regex($1,$3);
914+            $$= new (thd->mem_root) Item_func_regex($1,$3);
915             if ($$ == NULL)
916               MYSQL_YYABORT;
917           }
918         | bit_expr not REGEXP bit_expr
919           {
920-            Item *item= new (YYTHD->mem_root) Item_func_regex($1,$4);
921+            Item *item= new (thd->mem_root) Item_func_regex($1,$4);
922             if (item == NULL)
923               MYSQL_YYABORT;
924-            $$= negate_expression(YYTHD, item);
925+            $$= negate_expression(thd, item);
926             if ($$ == NULL)
927               MYSQL_YYABORT;
928           }
929@@ -7172,85 +7147,85 @@
930 bit_expr:
931           bit_expr '|' bit_expr %prec '|'
932           {
933-            $$= new (YYTHD->mem_root) Item_func_bit_or($1,$3);
934+            $$= new (thd->mem_root) Item_func_bit_or($1,$3);
935             if ($$ == NULL)
936               MYSQL_YYABORT;
937           }
938         | bit_expr '&' bit_expr %prec '&'
939           {
940-            $$= new (YYTHD->mem_root) Item_func_bit_and($1,$3);
941+            $$= new (thd->mem_root) Item_func_bit_and($1,$3);
942             if ($$ == NULL)
943               MYSQL_YYABORT;
944           }
945         | bit_expr SHIFT_LEFT bit_expr %prec SHIFT_LEFT
946           {
947-            $$= new (YYTHD->mem_root) Item_func_shift_left($1,$3);
948+            $$= new (thd->mem_root) Item_func_shift_left($1,$3);
949             if ($$ == NULL)
950               MYSQL_YYABORT;
951           }
952         | bit_expr SHIFT_RIGHT bit_expr %prec SHIFT_RIGHT
953           {
954-            $$= new (YYTHD->mem_root) Item_func_shift_right($1,$3);
955+            $$= new (thd->mem_root) Item_func_shift_right($1,$3);
956             if ($$ == NULL)
957               MYSQL_YYABORT;
958           }
959         | bit_expr '+' bit_expr %prec '+'
960           {
961-            $$= new (YYTHD->mem_root) Item_func_plus($1,$3);
962+            $$= new (thd->mem_root) Item_func_plus($1,$3);
963             if ($$ == NULL)
964               MYSQL_YYABORT;
965           }
966         | bit_expr '-' bit_expr %prec '-'
967           {
968-            $$= new (YYTHD->mem_root) Item_func_minus($1,$3);
969+            $$= new (thd->mem_root) Item_func_minus($1,$3);
970             if ($$ == NULL)
971               MYSQL_YYABORT;
972           }
973         | bit_expr '+' INTERVAL_SYM expr interval %prec '+'
974           {
975-            $$= new (YYTHD->mem_root) Item_date_add_interval($1,$4,$5,0);
976+            $$= new (thd->mem_root) Item_date_add_interval($1,$4,$5,0);
977             if ($$ == NULL)
978               MYSQL_YYABORT;
979           }
980         | bit_expr '-' INTERVAL_SYM expr interval %prec '-'
981           {
982-            $$= new (YYTHD->mem_root) Item_date_add_interval($1,$4,$5,1);
983+            $$= new (thd->mem_root) Item_date_add_interval($1,$4,$5,1);
984             if ($$ == NULL)
985               MYSQL_YYABORT;
986           }
987         | bit_expr '*' bit_expr %prec '*'
988           {
989-            $$= new (YYTHD->mem_root) Item_func_mul($1,$3);
990+            $$= new (thd->mem_root) Item_func_mul($1,$3);
991             if ($$ == NULL)
992               MYSQL_YYABORT;
993           }
994         | bit_expr '/' bit_expr %prec '/'
995           {
996-            $$= new (YYTHD->mem_root) Item_func_div($1,$3);
997+            $$= new (thd->mem_root) Item_func_div($1,$3);
998             if ($$ == NULL)
999               MYSQL_YYABORT;
1000           }
1001         | bit_expr '%' bit_expr %prec '%'
1002           {
1003-            $$= new (YYTHD->mem_root) Item_func_mod($1,$3);
1004+            $$= new (thd->mem_root) Item_func_mod($1,$3);
1005             if ($$ == NULL)
1006               MYSQL_YYABORT;
1007           }
1008         | bit_expr DIV_SYM bit_expr %prec DIV_SYM
1009           {
1010-            $$= new (YYTHD->mem_root) Item_func_int_div($1,$3);
1011+            $$= new (thd->mem_root) Item_func_int_div($1,$3);
1012             if ($$ == NULL)
1013               MYSQL_YYABORT;
1014           }
1015         | bit_expr MOD_SYM bit_expr %prec MOD_SYM
1016           {
1017-            $$= new (YYTHD->mem_root) Item_func_mod($1,$3);
1018+            $$= new (thd->mem_root) Item_func_mod($1,$3);
1019             if ($$ == NULL)
1020               MYSQL_YYABORT;
1021           }
1022         | bit_expr '^' bit_expr
1023           {
1024-            $$= new (YYTHD->mem_root) Item_func_bit_xor($1,$3);
1025+            $$= new (thd->mem_root) Item_func_bit_xor($1,$3);
1026             if ($$ == NULL)
1027               MYSQL_YYABORT;
1028           }
1029@@ -7299,7 +7274,6 @@
1030         | function_call_conflict
1031         | simple_expr COLLATE_SYM ident_or_text %prec NEG
1032           {
1033-            THD *thd= YYTHD;
1034             Item *i1= new (thd->mem_root) Item_string($3.str,
1035                                                       $3.length,
1036                                                       thd->charset());
1037@@ -7315,7 +7289,7 @@
1038         | sum_expr
1039         | simple_expr OR_OR_SYM simple_expr
1040           {
1041-            $$= new (YYTHD->mem_root) Item_func_concat($1, $3);
1042+            $$= new (thd->mem_root) Item_func_concat($1, $3);
1043             if ($$ == NULL)
1044               MYSQL_YYABORT;
1045           }
1046@@ -7325,25 +7299,25 @@
1047           }
1048         | '-' simple_expr %prec NEG
1049           {
1050-            $$= new (YYTHD->mem_root) Item_func_neg($2);
1051+            $$= new (thd->mem_root) Item_func_neg($2);
1052             if ($$ == NULL)
1053               MYSQL_YYABORT;
1054           }
1055         | '~' simple_expr %prec NEG
1056           {
1057-            $$= new (YYTHD->mem_root) Item_func_bit_neg($2);
1058+            $$= new (thd->mem_root) Item_func_bit_neg($2);
1059             if ($$ == NULL)
1060               MYSQL_YYABORT;
1061           }
1062         | not2 simple_expr %prec NEG
1063           {
1064-            $$= negate_expression(YYTHD, $2);
1065+            $$= negate_expression(thd, $2);
1066             if ($$ == NULL)
1067               MYSQL_YYABORT;
1068           }
1069         | '(' subselect ')'
1070           {
1071-            $$= new (YYTHD->mem_root) Item_singlerow_subselect($2);
1072+            $$= new (thd->mem_root) Item_singlerow_subselect($2);
1073             if ($$ == NULL)
1074               MYSQL_YYABORT;
1075           }
1076@@ -7352,20 +7326,20 @@
1077         | '(' expr ',' expr_list ')'
1078           {
1079             $4->push_front($2);
1080-            $$= new (YYTHD->mem_root) Item_row(*$4);
1081+            $$= new (thd->mem_root) Item_row(*$4);
1082             if ($$ == NULL)
1083               MYSQL_YYABORT;
1084           }
1085         | ROW_SYM '(' expr ',' expr_list ')'
1086           {
1087             $5->push_front($3);
1088-            $$= new (YYTHD->mem_root) Item_row(*$5);
1089+            $$= new (thd->mem_root) Item_row(*$5);
1090             if ($$ == NULL)
1091               MYSQL_YYABORT;
1092           }
1093         | EXISTS '(' subselect ')'
1094           {
1095-            $$= new (YYTHD->mem_root) Item_exists_subselect($3);
1096+            $$= new (thd->mem_root) Item_exists_subselect($3);
1097             if ($$ == NULL)
1098               MYSQL_YYABORT;
1099           }
1100@@ -7374,7 +7348,7 @@
1101         | MATCH ident_list_arg AGAINST '(' bit_expr fulltext_options ')'
1102           {
1103             $2->push_front($5);
1104-            Item_func_match *i1= new (YYTHD->mem_root) Item_func_match(*$2, $6);
1105+            Item_func_match *i1= new (thd->mem_root) Item_func_match(*$2, $6);
1106             if (i1 == NULL)
1107               MYSQL_YYABORT;
1108             Select->add_ftfunc_to_list(i1);
1109@@ -7382,7 +7356,7 @@
1110           }
1111         | BINARY simple_expr %prec NEG
1112           {
1113-            $$= create_func_cast(YYTHD, $2, ITEM_CAST_CHAR, NULL, NULL,
1114+            $$= create_func_cast(thd, $2, ITEM_CAST_CHAR, NULL, NULL,
1115                                  &my_charset_bin);
1116             if ($$ == NULL)
1117               MYSQL_YYABORT;
1118@@ -7390,27 +7364,27 @@
1119         | CAST_SYM '(' expr AS cast_type ')'
1120           {
1121             LEX *lex= Lex;
1122-            $$= create_func_cast(YYTHD, $3, $5, lex->length, lex->dec,
1123+            $$= create_func_cast(thd, $3, $5, lex->length, lex->dec,
1124                                  lex->charset);
1125             if ($$ == NULL)
1126               MYSQL_YYABORT;
1127           }
1128         | CASE_SYM opt_expr when_list opt_else END
1129           {
1130-            $$= new (YYTHD->mem_root) Item_func_case(* $3, $2, $4 );
1131+            $$= new (thd->mem_root) Item_func_case(* $3, $2, $4 );
1132             if ($$ == NULL)
1133               MYSQL_YYABORT;
1134           }
1135         | CONVERT_SYM '(' expr ',' cast_type ')'
1136           {
1137-            $$= create_func_cast(YYTHD, $3, $5, Lex->length, Lex->dec,
1138+            $$= create_func_cast(thd, $3, $5, Lex->length, Lex->dec,
1139                                  Lex->charset);
1140             if ($$ == NULL)
1141               MYSQL_YYABORT;
1142           }
1143         | CONVERT_SYM '(' expr USING charset_name ')'
1144           {
1145-            $$= new (YYTHD->mem_root) Item_func_conv_charset($3,$5);
1146+            $$= new (thd->mem_root) Item_func_conv_charset($3,$5);
1147             if ($$ == NULL)
1148               MYSQL_YYABORT;
1149           }
1150@@ -7423,14 +7397,14 @@
1151               my_error(ER_WRONG_COLUMN_NAME, MYF(0), il->my_name()->str);
1152               MYSQL_YYABORT;
1153             }
1154-            $$= new (YYTHD->mem_root) Item_default_value(Lex->current_context(),
1155+            $$= new (thd->mem_root) Item_default_value(Lex->current_context(),
1156                                                          $3);
1157             if ($$ == NULL)
1158               MYSQL_YYABORT;
1159           }
1160         | VALUES '(' simple_ident_nospvar ')'
1161           {
1162-            $$= new (YYTHD->mem_root) Item_insert_value(Lex->current_context(),
1163+            $$= new (thd->mem_root) Item_insert_value(Lex->current_context(),
1164                                                         $3);
1165             if ($$ == NULL)
1166               MYSQL_YYABORT;
1167@@ -7438,7 +7412,7 @@
1168         | INTERVAL_SYM expr interval '+' expr %prec INTERVAL_SYM
1169           /* we cannot put interval before - */
1170           {
1171-            $$= new (YYTHD->mem_root) Item_date_add_interval($5,$2,$3,0);
1172+            $$= new (thd->mem_root) Item_date_add_interval($5,$2,$3,0);
1173             if ($$ == NULL)
1174               MYSQL_YYABORT;
1175           }
1176@@ -7453,19 +7427,19 @@
1177 function_call_keyword:
1178           CHAR_SYM '(' expr_list ')'
1179           {
1180-            $$= new (YYTHD->mem_root) Item_func_char(*$3);
1181+            $$= new (thd->mem_root) Item_func_char(*$3);
1182             if ($$ == NULL)
1183               MYSQL_YYABORT;
1184           }
1185         | CHAR_SYM '(' expr_list USING charset_name ')'
1186           {
1187-            $$= new (YYTHD->mem_root) Item_func_char(*$3, $5);
1188+            $$= new (thd->mem_root) Item_func_char(*$3, $5);
1189             if ($$ == NULL)
1190               MYSQL_YYABORT;
1191           }
1192         | CURRENT_USER optional_braces
1193           {
1194-            $$= new (YYTHD->mem_root) Item_func_current_user(Lex->current_context());
1195+            $$= new (thd->mem_root) Item_func_current_user(Lex->current_context());
1196             if ($$ == NULL)
1197               MYSQL_YYABORT;
1198             Lex->set_stmt_unsafe();
1199@@ -7473,31 +7447,30 @@
1200           }
1201         | DATE_SYM '(' expr ')'
1202           {
1203-            $$= new (YYTHD->mem_root) Item_date_typecast($3);
1204+            $$= new (thd->mem_root) Item_date_typecast($3);
1205             if ($$ == NULL)
1206               MYSQL_YYABORT;
1207           }
1208         | DAY_SYM '(' expr ')'
1209           {
1210-            $$= new (YYTHD->mem_root) Item_func_dayofmonth($3);
1211+            $$= new (thd->mem_root) Item_func_dayofmonth($3);
1212             if ($$ == NULL)
1213               MYSQL_YYABORT;
1214           }
1215         | HOUR_SYM '(' expr ')'
1216           {
1217-            $$= new (YYTHD->mem_root) Item_func_hour($3);
1218+            $$= new (thd->mem_root) Item_func_hour($3);
1219             if ($$ == NULL)
1220               MYSQL_YYABORT;
1221           }
1222         | INSERT '(' expr ',' expr ',' expr ',' expr ')'
1223           {
1224-            $$= new (YYTHD->mem_root) Item_func_insert($3,$5,$7,$9);
1225+            $$= new (thd->mem_root) Item_func_insert($3,$5,$7,$9);
1226             if ($$ == NULL)
1227               MYSQL_YYABORT;
1228           }
1229         | INTERVAL_SYM '(' expr ',' expr ')' %prec INTERVAL_SYM
1230           {
1231-            THD *thd= YYTHD;
1232             List<Item> *list= new (thd->mem_root) List<Item>;
1233             if (list == NULL)
1234               MYSQL_YYABORT;
1235@@ -7512,7 +7485,6 @@
1236           }
1237         | INTERVAL_SYM '(' expr ',' expr ',' expr_list ')' %prec INTERVAL_SYM
1238           {
1239-            THD *thd= YYTHD;
1240             $7->push_front($5);
1241             $7->push_front($3);
1242             Item_row *item= new (thd->mem_root) Item_row(*$7);
1243@@ -7524,103 +7496,103 @@
1244           }
1245         | LEFT '(' expr ',' expr ')'
1246           {
1247-            $$= new (YYTHD->mem_root) Item_func_left($3,$5);
1248+            $$= new (thd->mem_root) Item_func_left($3,$5);
1249             if ($$ == NULL)
1250               MYSQL_YYABORT;
1251           }
1252         | MINUTE_SYM '(' expr ')'
1253           {
1254-            $$= new (YYTHD->mem_root) Item_func_minute($3);
1255+            $$= new (thd->mem_root) Item_func_minute($3);
1256             if ($$ == NULL)
1257               MYSQL_YYABORT;
1258           }
1259         | MONTH_SYM '(' expr ')'
1260           {
1261-            $$= new (YYTHD->mem_root) Item_func_month($3);
1262+            $$= new (thd->mem_root) Item_func_month($3);
1263             if ($$ == NULL)
1264               MYSQL_YYABORT;
1265           }
1266         | RIGHT '(' expr ',' expr ')'
1267           {
1268-            $$= new (YYTHD->mem_root) Item_func_right($3,$5);
1269+            $$= new (thd->mem_root) Item_func_right($3,$5);
1270             if ($$ == NULL)
1271               MYSQL_YYABORT;
1272           }
1273         | SECOND_SYM '(' expr ')'
1274           {
1275-            $$= new (YYTHD->mem_root) Item_func_second($3);
1276+            $$= new (thd->mem_root) Item_func_second($3);
1277             if ($$ == NULL)
1278               MYSQL_YYABORT;
1279           }
1280         | TIME_SYM '(' expr ')'
1281           {
1282-            $$= new (YYTHD->mem_root) Item_time_typecast($3);
1283+            $$= new (thd->mem_root) Item_time_typecast($3);
1284             if ($$ == NULL)
1285               MYSQL_YYABORT;
1286           }
1287         | TIMESTAMP '(' expr ')'
1288           {
1289-            $$= new (YYTHD->mem_root) Item_datetime_typecast($3);
1290+            $$= new (thd->mem_root) Item_datetime_typecast($3);
1291             if ($$ == NULL)
1292               MYSQL_YYABORT;
1293           }
1294         | TIMESTAMP '(' expr ',' expr ')'
1295           {
1296-            $$= new (YYTHD->mem_root) Item_func_add_time($3, $5, 1, 0);
1297+            $$= new (thd->mem_root) Item_func_add_time($3, $5, 1, 0);
1298             if ($$ == NULL)
1299               MYSQL_YYABORT;
1300           }
1301         | TRIM '(' expr ')'
1302           {
1303-            $$= new (YYTHD->mem_root) Item_func_trim($3);
1304+            $$= new (thd->mem_root) Item_func_trim($3);
1305             if ($$ == NULL)
1306               MYSQL_YYABORT;
1307           }
1308         | TRIM '(' LEADING expr FROM expr ')'
1309           {
1310-            $$= new (YYTHD->mem_root) Item_func_ltrim($6,$4);
1311+            $$= new (thd->mem_root) Item_func_ltrim($6,$4);
1312             if ($$ == NULL)
1313               MYSQL_YYABORT;
1314           }
1315         | TRIM '(' TRAILING expr FROM expr ')'
1316           {
1317-            $$= new (YYTHD->mem_root) Item_func_rtrim($6,$4);
1318+            $$= new (thd->mem_root) Item_func_rtrim($6,$4);
1319             if ($$ == NULL)
1320               MYSQL_YYABORT;
1321           }
1322         | TRIM '(' BOTH expr FROM expr ')'
1323           {
1324-            $$= new (YYTHD->mem_root) Item_func_trim($6,$4);
1325+            $$= new (thd->mem_root) Item_func_trim($6,$4);
1326             if ($$ == NULL)
1327               MYSQL_YYABORT;
1328           }
1329         | TRIM '(' LEADING FROM expr ')'
1330           {
1331-            $$= new (YYTHD->mem_root) Item_func_ltrim($5);
1332+            $$= new (thd->mem_root) Item_func_ltrim($5);
1333             if ($$ == NULL)
1334               MYSQL_YYABORT;
1335           }
1336         | TRIM '(' TRAILING FROM expr ')'
1337           {
1338-            $$= new (YYTHD->mem_root) Item_func_rtrim($5);
1339+            $$= new (thd->mem_root) Item_func_rtrim($5);
1340             if ($$ == NULL)
1341               MYSQL_YYABORT;
1342           }
1343         | TRIM '(' BOTH FROM expr ')'
1344           {
1345-            $$= new (YYTHD->mem_root) Item_func_trim($5);
1346+            $$= new (thd->mem_root) Item_func_trim($5);
1347             if ($$ == NULL)
1348               MYSQL_YYABORT;
1349           }
1350         | TRIM '(' expr FROM expr ')'
1351           {
1352-            $$= new (YYTHD->mem_root) Item_func_trim($5,$3);
1353+            $$= new (thd->mem_root) Item_func_trim($5,$3);
1354             if ($$ == NULL)
1355               MYSQL_YYABORT;
1356           }
1357         | USER '(' ')'
1358           {
1359-            $$= new (YYTHD->mem_root) Item_func_user();
1360+            $$= new (thd->mem_root) Item_func_user();
1361             if ($$ == NULL)
1362               MYSQL_YYABORT;
1363             Lex->set_stmt_unsafe();
1364@@ -7628,7 +7600,7 @@
1365           }
1366         | YEAR_SYM '(' expr ')'
1367           {
1368-            $$= new (YYTHD->mem_root) Item_func_year($3);
1369+            $$= new (thd->mem_root) Item_func_year($3);
1370             if ($$ == NULL)
1371               MYSQL_YYABORT;
1372           }
1373@@ -7649,34 +7621,34 @@
1374 function_call_nonkeyword:
1375           ADDDATE_SYM '(' expr ',' expr ')'
1376           {
1377-            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $5,
1378+            $$= new (thd->mem_root) Item_date_add_interval($3, $5,
1379                                                              INTERVAL_DAY, 0);
1380             if ($$ == NULL)
1381               MYSQL_YYABORT;
1382           }
1383         | ADDDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
1384           {
1385-            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $6, $7, 0);
1386+            $$= new (thd->mem_root) Item_date_add_interval($3, $6, $7, 0);
1387             if ($$ == NULL)
1388               MYSQL_YYABORT;
1389           }
1390         | CURDATE optional_braces
1391           {
1392-            $$= new (YYTHD->mem_root) Item_func_curdate_local();
1393+            $$= new (thd->mem_root) Item_func_curdate_local();
1394             if ($$ == NULL)
1395               MYSQL_YYABORT;
1396             Lex->safe_to_cache_query=0;
1397           }
1398         | CURTIME optional_braces
1399           {
1400-            $$= new (YYTHD->mem_root) Item_func_curtime_local();
1401+            $$= new (thd->mem_root) Item_func_curtime_local();
1402             if ($$ == NULL)
1403               MYSQL_YYABORT;
1404             Lex->safe_to_cache_query=0;
1405           }
1406         | CURTIME '(' expr ')'
1407           {
1408-            $$= new (YYTHD->mem_root) Item_func_curtime_local($3);
1409+            $$= new (thd->mem_root) Item_func_curtime_local($3);
1410             if ($$ == NULL)
1411               MYSQL_YYABORT;
1412             Lex->safe_to_cache_query=0;
1413@@ -7684,83 +7656,83 @@
1414         | DATE_ADD_INTERVAL '(' expr ',' INTERVAL_SYM expr interval ')'
1415           %prec INTERVAL_SYM
1416           {
1417-            $$= new (YYTHD->mem_root) Item_date_add_interval($3,$6,$7,0);
1418+            $$= new (thd->mem_root) Item_date_add_interval($3,$6,$7,0);
1419             if ($$ == NULL)
1420               MYSQL_YYABORT;
1421           }
1422         | DATE_SUB_INTERVAL '(' expr ',' INTERVAL_SYM expr interval ')'
1423           %prec INTERVAL_SYM
1424           {
1425-            $$= new (YYTHD->mem_root) Item_date_add_interval($3,$6,$7,1);
1426+            $$= new (thd->mem_root) Item_date_add_interval($3,$6,$7,1);
1427             if ($$ == NULL)
1428               MYSQL_YYABORT;
1429           }
1430         | EXTRACT_SYM '(' interval FROM expr ')'
1431           {
1432-            $$=new (YYTHD->mem_root) Item_extract( $3, $5);
1433+            $$=new (thd->mem_root) Item_extract( $3, $5);
1434             if ($$ == NULL)
1435               MYSQL_YYABORT;
1436           }
1437         | GET_FORMAT '(' date_time_type  ',' expr ')'
1438           {
1439-            $$= new (YYTHD->mem_root) Item_func_get_format($3, $5);
1440+            $$= new (thd->mem_root) Item_func_get_format($3, $5);
1441             if ($$ == NULL)
1442               MYSQL_YYABORT;
1443           }
1444         | NOW_SYM optional_braces
1445           {
1446-            $$= new (YYTHD->mem_root) Item_func_now_local();
1447+            $$= new (thd->mem_root) Item_func_now_local();
1448             if ($$ == NULL)
1449               MYSQL_YYABORT;
1450             Lex->safe_to_cache_query=0;
1451           }
1452         | NOW_SYM '(' expr ')'
1453           {
1454-            $$= new (YYTHD->mem_root) Item_func_now_local($3);
1455+            $$= new (thd->mem_root) Item_func_now_local($3);
1456             if ($$ == NULL)
1457               MYSQL_YYABORT;
1458             Lex->safe_to_cache_query=0;
1459           }
1460         | POSITION_SYM '(' bit_expr IN_SYM expr ')'
1461           {
1462-            $$ = new (YYTHD->mem_root) Item_func_locate($5,$3);
1463+            $$ = new (thd->mem_root) Item_func_locate($5,$3);
1464             if ($$ == NULL)
1465               MYSQL_YYABORT;
1466           }
1467         | SUBDATE_SYM '(' expr ',' expr ')'
1468           {
1469-            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $5,
1470+            $$= new (thd->mem_root) Item_date_add_interval($3, $5,
1471                                                              INTERVAL_DAY, 1);
1472             if ($$ == NULL)
1473               MYSQL_YYABORT;
1474           }
1475         | SUBDATE_SYM '(' expr ',' INTERVAL_SYM expr interval ')'
1476           {
1477-            $$= new (YYTHD->mem_root) Item_date_add_interval($3, $6, $7, 1);
1478+            $$= new (thd->mem_root) Item_date_add_interval($3, $6, $7, 1);
1479             if ($$ == NULL)
1480               MYSQL_YYABORT;
1481           }
1482         | SUBSTRING '(' expr ',' expr ',' expr ')'
1483           {
1484-            $$= new (YYTHD->mem_root) Item_func_substr($3,$5,$7);
1485+            $$= new (thd->mem_root) Item_func_substr($3,$5,$7);
1486             if ($$ == NULL)
1487               MYSQL_YYABORT;
1488           }
1489         | SUBSTRING '(' expr ',' expr ')'
1490           {
1491-            $$= new (YYTHD->mem_root) Item_func_substr($3,$5);
1492+            $$= new (thd->mem_root) Item_func_substr($3,$5);
1493             if ($$ == NULL)
1494               MYSQL_YYABORT;
1495           }
1496         | SUBSTRING '(' expr FROM expr FOR_SYM expr ')'
1497           {
1498-            $$= new (YYTHD->mem_root) Item_func_substr($3,$5,$7);
1499+            $$= new (thd->mem_root) Item_func_substr($3,$5,$7);
1500             if ($$ == NULL)
1501               MYSQL_YYABORT;
1502           }
1503         | SUBSTRING '(' expr FROM expr ')'
1504           {
1505-            $$= new (YYTHD->mem_root) Item_func_substr($3,$5);
1506+            $$= new (thd->mem_root) Item_func_substr($3,$5);
1507             if ($$ == NULL)
1508               MYSQL_YYABORT;
1509           }
1510@@ -7775,9 +7747,9 @@
1511             */
1512             Lex->set_stmt_unsafe();
1513             if (global_system_variables.sysdate_is_now == 0)
1514-              $$= new (YYTHD->mem_root) Item_func_sysdate_local();
1515+              $$= new (thd->mem_root) Item_func_sysdate_local();
1516             else
1517-              $$= new (YYTHD->mem_root) Item_func_now_local();
1518+              $$= new (thd->mem_root) Item_func_now_local();
1519             if ($$ == NULL)
1520               MYSQL_YYABORT;
1521             Lex->safe_to_cache_query=0;
1522@@ -7785,42 +7757,42 @@
1523         | SYSDATE '(' expr ')'
1524           {
1525             if (global_system_variables.sysdate_is_now == 0)
1526-              $$= new (YYTHD->mem_root) Item_func_sysdate_local($3);
1527+              $$= new (thd->mem_root) Item_func_sysdate_local($3);
1528             else
1529-              $$= new (YYTHD->mem_root) Item_func_now_local($3);
1530+              $$= new (thd->mem_root) Item_func_now_local($3);
1531             if ($$ == NULL)
1532               MYSQL_YYABORT;
1533             Lex->safe_to_cache_query=0;
1534           }
1535         | TIMESTAMP_ADD '(' interval_time_stamp ',' expr ',' expr ')'
1536           {
1537-            $$= new (YYTHD->mem_root) Item_date_add_interval($7,$5,$3,0);
1538+            $$= new (thd->mem_root) Item_date_add_interval($7,$5,$3,0);
1539             if ($$ == NULL)
1540               MYSQL_YYABORT;
1541           }
1542         | TIMESTAMP_DIFF '(' interval_time_stamp ',' expr ',' expr ')'
1543           {
1544-            $$= new (YYTHD->mem_root) Item_func_timestamp_diff($5,$7,$3);
1545+            $$= new (thd->mem_root) Item_func_timestamp_diff($5,$7,$3);
1546             if ($$ == NULL)
1547               MYSQL_YYABORT;
1548           }
1549         | UTC_DATE_SYM optional_braces
1550           {
1551-            $$= new (YYTHD->mem_root) Item_func_curdate_utc();
1552+            $$= new (thd->mem_root) Item_func_curdate_utc();
1553             if ($$ == NULL)
1554               MYSQL_YYABORT;
1555             Lex->safe_to_cache_query=0;
1556           }
1557         | UTC_TIME_SYM optional_braces
1558           {
1559-            $$= new (YYTHD->mem_root) Item_func_curtime_utc();
1560+            $$= new (thd->mem_root) Item_func_curtime_utc();
1561             if ($$ == NULL)
1562               MYSQL_YYABORT;
1563             Lex->safe_to_cache_query=0;
1564           }
1565         | UTC_TIMESTAMP_SYM optional_braces
1566           {
1567-            $$= new (YYTHD->mem_root) Item_func_now_utc();
1568+            $$= new (thd->mem_root) Item_func_now_utc();
1569             if ($$ == NULL)
1570               MYSQL_YYABORT;
1571             Lex->safe_to_cache_query=0;
1572@@ -7835,62 +7807,61 @@
1573 function_call_conflict:
1574           ASCII_SYM '(' expr ')'
1575           {
1576-            $$= new (YYTHD->mem_root) Item_func_ascii($3);
1577+            $$= new (thd->mem_root) Item_func_ascii($3);
1578             if ($$ == NULL)
1579               MYSQL_YYABORT;
1580           }
1581         | CHARSET '(' expr ')'
1582           {
1583-            $$= new (YYTHD->mem_root) Item_func_charset($3);
1584+            $$= new (thd->mem_root) Item_func_charset($3);
1585             if ($$ == NULL)
1586               MYSQL_YYABORT;
1587           }
1588         | COALESCE '(' expr_list ')'
1589           {
1590-            $$= new (YYTHD->mem_root) Item_func_coalesce(* $3);
1591+            $$= new (thd->mem_root) Item_func_coalesce(* $3);
1592             if ($$ == NULL)
1593               MYSQL_YYABORT;
1594           }
1595         | COLLATION_SYM '(' expr ')'
1596           {
1597-            $$= new (YYTHD->mem_root) Item_func_collation($3);
1598+            $$= new (thd->mem_root) Item_func_collation($3);
1599             if ($$ == NULL)
1600               MYSQL_YYABORT;
1601           }
1602         | DATABASE '(' ')'
1603           {
1604-            $$= new (YYTHD->mem_root) Item_func_database();
1605+            $$= new (thd->mem_root) Item_func_database();
1606             if ($$ == NULL)
1607               MYSQL_YYABORT;
1608             Lex->safe_to_cache_query=0;
1609           }
1610         | IF '(' expr ',' expr ',' expr ')'
1611           {
1612-            $$= new (YYTHD->mem_root) Item_func_if($3,$5,$7);
1613+            $$= new (thd->mem_root) Item_func_if($3,$5,$7);
1614             if ($$ == NULL)
1615               MYSQL_YYABORT;
1616           }
1617         | MICROSECOND_SYM '(' expr ')'
1618           {
1619-            $$= new (YYTHD->mem_root) Item_func_microsecond($3);
1620+            $$= new (thd->mem_root) Item_func_microsecond($3);
1621             if ($$ == NULL)
1622               MYSQL_YYABORT;
1623           }
1624         | MOD_SYM '(' expr ',' expr ')'
1625           {
1626-            $$ = new (YYTHD->mem_root) Item_func_mod($3, $5);
1627+            $$ = new (thd->mem_root) Item_func_mod($3, $5);
1628             if ($$ == NULL)
1629               MYSQL_YYABORT;
1630           }
1631         | OLD_PASSWORD '(' expr ')'
1632           {
1633-            $$=  new (YYTHD->mem_root) Item_func_old_password($3);
1634+            $$=  new (thd->mem_root) Item_func_old_password($3);
1635             if ($$ == NULL)
1636               MYSQL_YYABORT;
1637           }
1638         | PASSWORD '(' expr ')'
1639           {
1640-            THD *thd= YYTHD;
1641             Item* i1;
1642             if (thd->variables.old_passwords)
1643               i1= new (thd->mem_root) Item_func_old_password($3);
1644@@ -7902,31 +7873,30 @@
1645           }
1646         | QUARTER_SYM '(' expr ')'
1647           {
1648-            $$ = new (YYTHD->mem_root) Item_func_quarter($3);
1649+            $$ = new (thd->mem_root) Item_func_quarter($3);
1650             if ($$ == NULL)
1651               MYSQL_YYABORT;
1652           }
1653         | REPEAT_SYM '(' expr ',' expr ')'
1654           {
1655-            $$= new (YYTHD->mem_root) Item_func_repeat($3,$5);
1656+            $$= new (thd->mem_root) Item_func_repeat($3,$5);
1657             if ($$ == NULL)
1658               MYSQL_YYABORT;
1659           }
1660         | REPLACE '(' expr ',' expr ',' expr ')'
1661           {
1662-            $$= new (YYTHD->mem_root) Item_func_replace($3,$5,$7);
1663+            $$= new (thd->mem_root) Item_func_replace($3,$5,$7);
1664             if ($$ == NULL)
1665               MYSQL_YYABORT;
1666           }
1667         | TRUNCATE_SYM '(' expr ',' expr ')'
1668           {
1669-            $$= new (YYTHD->mem_root) Item_func_round($3,$5,1);
1670+            $$= new (thd->mem_root) Item_func_round($3,$5,1);
1671             if ($$ == NULL)
1672               MYSQL_YYABORT;
1673           }
1674         | WEEK_SYM '(' expr ')'
1675           {
1676-            THD *thd= YYTHD;
1677             Item *i1= new (thd->mem_root) Item_int((char*) "0",
1678                                            thd->variables.default_week_format,
1679                                                    1);
1680@@ -7938,7 +7908,7 @@
1681           }
1682         | WEEK_SYM '(' expr ',' expr ')'
1683           {
1684-            $$= new (YYTHD->mem_root) Item_func_week($3,$5);
1685+            $$= new (thd->mem_root) Item_func_week($3,$5);
1686             if ($$ == NULL)
1687               MYSQL_YYABORT;
1688           }
1689@@ -7960,52 +7930,52 @@
1690 geometry_function:
1691           CONTAINS_SYM '(' expr ',' expr ')'
1692           {
1693-            $$= GEOM_NEW(YYTHD,
1694+            $$= GEOM_NEW(thd,
1695                          Item_func_spatial_rel($3, $5,
1696                                                Item_func::SP_CONTAINS_FUNC));
1697           }
1698         | GEOMETRYCOLLECTION '(' expr_list ')'
1699           {
1700-            $$= GEOM_NEW(YYTHD,
1701+            $$= GEOM_NEW(thd,
1702                          Item_func_spatial_collection(* $3,
1703                            Geometry::wkb_geometrycollection,
1704                            Geometry::wkb_point));
1705           }
1706         | LINESTRING '(' expr_list ')'
1707           {
1708-            $$= GEOM_NEW(YYTHD,
1709+            $$= GEOM_NEW(thd,
1710                          Item_func_spatial_collection(* $3,
1711                            Geometry::wkb_linestring,
1712                            Geometry::wkb_point));
1713           }
1714         | MULTILINESTRING '(' expr_list ')'
1715           {
1716-            $$= GEOM_NEW(YYTHD,
1717+            $$= GEOM_NEW(thd,
1718                          Item_func_spatial_collection(* $3,
1719                            Geometry::wkb_multilinestring,
1720                            Geometry::wkb_linestring));
1721           }
1722         | MULTIPOINT '(' expr_list ')'
1723           {
1724-            $$= GEOM_NEW(YYTHD,
1725+            $$= GEOM_NEW(thd,
1726                          Item_func_spatial_collection(* $3,
1727                            Geometry::wkb_multipoint,
1728                            Geometry::wkb_point));
1729           }
1730         | MULTIPOLYGON '(' expr_list ')'
1731           {
1732-            $$= GEOM_NEW(YYTHD,
1733+            $$= GEOM_NEW(thd,
1734                          Item_func_spatial_collection(* $3,
1735                            Geometry::wkb_multipolygon,
1736                            Geometry::wkb_polygon));
1737           }
1738         | POINT_SYM '(' expr ',' expr ')'
1739           {
1740-            $$= GEOM_NEW(YYTHD, Item_func_point($3,$5));
1741+            $$= GEOM_NEW(thd, Item_func_point($3,$5));
1742           }
1743         | POLYGON '(' expr_list ')'
1744           {
1745-            $$= GEOM_NEW(YYTHD,
1746+            $$= GEOM_NEW(thd,
1747                          Item_func_spatial_collection(* $3,
1748                            Geometry::wkb_polygon,
1749                            Geometry::wkb_linestring));
1750@@ -8043,7 +8013,6 @@
1751           }
1752           opt_udf_expr_list ')'
1753           {
1754-            THD *thd= YYTHD;
1755             Create_func *builder;
1756             Item *item= NULL;
1757
1758@@ -8097,7 +8066,6 @@
1759           }
1760         | ident '.' ident '(' opt_expr_list ')'
1761           {
1762-            THD *thd= YYTHD;
1763             Create_qfunc *builder;
1764             Item *item= NULL;
1765
1766@@ -8161,7 +8129,7 @@
1767 udf_expr_list:
1768           udf_expr
1769           {
1770-            $$= new (YYTHD->mem_root) List<Item>;
1771+            $$= new (thd->mem_root) List<Item>;
1772             if ($$ == NULL)
1773               MYSQL_YYABORT;
1774             $$->push_back($1);
1775@@ -8194,7 +8162,7 @@
1776                remember_name we may get quoted or escaped names.
1777             */
1778             else if ($2->type() != Item::FIELD_ITEM)
1779-              $2->set_name($1, (uint) ($3 - $1), YYTHD->charset());
1780+              $2->set_name($1, (uint) ($3 - $1), thd->charset());
1781             $$= $2;
1782           }
1783         ;
1784@@ -8202,46 +8170,46 @@
1785 sum_expr:
1786           AVG_SYM '(' in_sum_expr ')'
1787           {
1788-            $$= new (YYTHD->mem_root) Item_sum_avg($3);
1789+            $$= new (thd->mem_root) Item_sum_avg($3);
1790             if ($$ == NULL)
1791               MYSQL_YYABORT;
1792           }
1793         | AVG_SYM '(' DISTINCT in_sum_expr ')'
1794           {
1795-            $$= new (YYTHD->mem_root) Item_sum_avg_distinct($4);
1796+            $$= new (thd->mem_root) Item_sum_avg_distinct($4);
1797             if ($$ == NULL)
1798               MYSQL_YYABORT;
1799           }
1800         | BIT_AND  '(' in_sum_expr ')'
1801           {
1802-            $$= new (YYTHD->mem_root) Item_sum_and($3);
1803+            $$= new (thd->mem_root) Item_sum_and($3);
1804             if ($$ == NULL)
1805               MYSQL_YYABORT;
1806           }
1807         | BIT_OR  '(' in_sum_expr ')'
1808           {
1809-            $$= new (YYTHD->mem_root) Item_sum_or($3);
1810+            $$= new (thd->mem_root) Item_sum_or($3);
1811             if ($$ == NULL)
1812               MYSQL_YYABORT;
1813           }
1814         | BIT_XOR  '(' in_sum_expr ')'
1815           {
1816-            $$= new (YYTHD->mem_root) Item_sum_xor($3);
1817+            $$= new (thd->mem_root) Item_sum_xor($3);
1818             if ($$ == NULL)
1819               MYSQL_YYABORT;
1820           }
1821         | COUNT_SYM '(' opt_all '*' ')'
1822           {
1823-            Item *item= new (YYTHD->mem_root) Item_int((int32) 0L,1);
1824+            Item *item= new (thd->mem_root) Item_int((int32) 0L,1);
1825             if (item == NULL)
1826               MYSQL_YYABORT;
1827-            $$= new (YYTHD->mem_root) Item_sum_count(item);
1828+            $$= new (thd->mem_root) Item_sum_count(item);
1829             if ($$ == NULL)
1830               MYSQL_YYABORT;
1831           }
1832         | COUNT_SYM '(' in_sum_expr ')'
1833           {
1834-            $$= new (YYTHD->mem_root) Item_sum_count($3);
1835+            $$= new (thd->mem_root) Item_sum_count($3);
1836             if ($$ == NULL)
1837               MYSQL_YYABORT;
1838           }
1839@@ -8251,13 +8219,13 @@
1840           { Select->in_sum_expr--; }
1841           ')'
1842           {
1843-            $$= new (YYTHD->mem_root) Item_sum_count_distinct(* $5);
1844+            $$= new (thd->mem_root) Item_sum_count_distinct(* $5);
1845             if ($$ == NULL)
1846               MYSQL_YYABORT;
1847           }
1848         | MIN_SYM '(' in_sum_expr ')'
1849           {
1850-            $$= new (YYTHD->mem_root) Item_sum_min($3);
1851+            $$= new (thd->mem_root) Item_sum_min($3);
1852             if ($$ == NULL)
1853               MYSQL_YYABORT;
1854           }
1855@@ -8268,55 +8236,55 @@
1856         */
1857         | MIN_SYM '(' DISTINCT in_sum_expr ')'
1858           {
1859-            $$= new (YYTHD->mem_root) Item_sum_min($4);
1860+            $$= new (thd->mem_root) Item_sum_min($4);
1861             if ($$ == NULL)
1862               MYSQL_YYABORT;
1863           }
1864         | MAX_SYM '(' in_sum_expr ')'
1865           {
1866-            $$= new (YYTHD->mem_root) Item_sum_max($3);
1867+            $$= new (thd->mem_root) Item_sum_max($3);
1868             if ($$ == NULL)
1869               MYSQL_YYABORT;
1870           }
1871         | MAX_SYM '(' DISTINCT in_sum_expr ')'
1872           {
1873-            $$= new (YYTHD->mem_root) Item_sum_max($4);
1874+            $$= new (thd->mem_root) Item_sum_max($4);
1875             if ($$ == NULL)
1876               MYSQL_YYABORT;
1877           }
1878         | STD_SYM '(' in_sum_expr ')'
1879           {
1880-            $$= new (YYTHD->mem_root) Item_sum_std($3, 0);
1881+            $$= new (thd->mem_root) Item_sum_std($3, 0);
1882             if ($$ == NULL)
1883               MYSQL_YYABORT;
1884           }
1885         | VARIANCE_SYM '(' in_sum_expr ')'
1886           {
1887-            $$= new (YYTHD->mem_root) Item_sum_variance($3, 0);
1888+            $$= new (thd->mem_root) Item_sum_variance($3, 0);
1889             if ($$ == NULL)
1890               MYSQL_YYABORT;
1891           }
1892         | STDDEV_SAMP_SYM '(' in_sum_expr ')'
1893           {
1894-            $$= new (YYTHD->mem_root) Item_sum_std($3, 1);
1895+            $$= new (thd->mem_root) Item_sum_std($3, 1);
1896             if ($$ == NULL)
1897               MYSQL_YYABORT;
1898           }
1899         | VAR_SAMP_SYM '(' in_sum_expr ')'
1900           {
1901-            $$= new (YYTHD->mem_root) Item_sum_variance($3, 1);
1902+            $$= new (thd->mem_root) Item_sum_variance($3, 1);
1903             if ($$ == NULL)
1904               MYSQL_YYABORT;
1905           }
1906         | SUM_SYM '(' in_sum_expr ')'
1907           {
1908-            $$= new (YYTHD->mem_root) Item_sum_sum($3);
1909+            $$= new (thd->mem_root) Item_sum_sum($3);
1910             if ($$ == NULL)
1911               MYSQL_YYABORT;
1912           }
1913         | SUM_SYM '(' DISTINCT in_sum_expr ')'
1914           {
1915-            $$= new (YYTHD->mem_root) Item_sum_sum_distinct($4);
1916+            $$= new (thd->mem_root) Item_sum_sum_distinct($4);
1917             if ($$ == NULL)
1918               MYSQL_YYABORT;
1919           }
1920@@ -8328,7 +8296,7 @@
1921           {
1922             SELECT_LEX *sel= Select;
1923             sel->in_sum_expr--;
1924-            $$= new (YYTHD->mem_root)
1925+            $$= new (thd->mem_root)
1926                   Item_func_group_concat(Lex->current_context(), $3, $5,
1927                                          sel->gorder_list, $7);
1928             if ($$ == NULL)
1929@@ -8357,7 +8325,7 @@
1930           ident_or_text SET_VAR expr
1931           {
1932             Item_func_set_user_var *item;
1933-            $$= item= new (YYTHD->mem_root) Item_func_set_user_var($1, $3);
1934+            $$= item= new (thd->mem_root) Item_func_set_user_var($1, $3);
1935             if ($$ == NULL)
1936               MYSQL_YYABORT;
1937             LEX *lex= Lex;
1938@@ -8366,7 +8334,7 @@
1939           }
1940         | ident_or_text
1941           {
1942-            $$= new (YYTHD->mem_root) Item_func_get_user_var($1);
1943+            $$= new (thd->mem_root) Item_func_get_user_var($1);
1944             if ($$ == NULL)
1945               MYSQL_YYABORT;
1946             LEX *lex= Lex;
1947@@ -8380,7 +8348,7 @@
1948               my_parse_error(ER(ER_SYNTAX_ERROR));
1949               MYSQL_YYABORT;
1950             }
1951-            if (!($$= get_system_var(YYTHD, $2, $3, $4)))
1952+            if (!($$= get_system_var(thd, $2, $3, $4)))
1953               MYSQL_YYABORT;
1954             if (!((Item_func_get_system_var*) $$)->is_written_to_binlog())
1955               Lex->set_stmt_unsafe();
1956@@ -8395,7 +8363,7 @@
1957 opt_gconcat_separator:
1958           /* empty */
1959           {
1960-            $$= new (YYTHD->mem_root) String(",", 1, &my_charset_latin1);
1961+            $$= new (thd->mem_root) String(",", 1, &my_charset_latin1);
1962             if ($$ == NULL)
1963               MYSQL_YYABORT;
1964           }
1965@@ -8422,9 +8390,9 @@
1966
1967 gorder_list:
1968           gorder_list ',' order_ident order_dir
1969-          { if (add_gorder_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
1970+          { if (add_gorder_to_list(thd, $3,(bool) $4)) MYSQL_YYABORT; }
1971         | order_ident order_dir
1972-          { if (add_gorder_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; }
1973+          { if (add_gorder_to_list(thd, $1,(bool) $2)) MYSQL_YYABORT; }
1974         ;
1975
1976 in_sum_expr:
1977@@ -8477,7 +8445,7 @@
1978 expr_list:
1979           expr
1980           {
1981-            $$= new (YYTHD->mem_root) List<Item>;
1982+            $$= new (thd->mem_root) List<Item>;
1983             if ($$ == NULL)
1984               MYSQL_YYABORT;
1985             $$->push_back($1);
1986@@ -8497,7 +8465,7 @@
1987 ident_list:
1988           simple_ident
1989           {
1990-            $$= new (YYTHD->mem_root) List<Item>;
1991+            $$= new (thd->mem_root) List<Item>;
1992             if ($$ == NULL)
1993               MYSQL_YYABORT;
1994             $$->push_back($1);
1995@@ -8595,7 +8563,7 @@
1996           {
1997             MYSQL_YYABORT_UNLESS($1 && $3);
1998             /* Change the current name resolution context to a local context. */
1999-            if (push_new_name_resolution_context(YYTHD, $1, $3))
2000+            if (push_new_name_resolution_context(thd, $1, $3))
2001               MYSQL_YYABORT;
2002             Select->parsing_place= IN_ON;
2003           }
2004@@ -8610,7 +8578,7 @@
2005           {
2006             MYSQL_YYABORT_UNLESS($1 && $3);
2007             /* Change the current name resolution context to a local context. */
2008-            if (push_new_name_resolution_context(YYTHD, $1, $3))
2009+            if (push_new_name_resolution_context(thd, $1, $3))
2010               MYSQL_YYABORT;
2011             Select->parsing_place= IN_ON;
2012           }
2013@@ -8640,7 +8608,7 @@
2014           {
2015             MYSQL_YYABORT_UNLESS($1 && $5);
2016             /* Change the current name resolution context to a local context. */
2017-            if (push_new_name_resolution_context(YYTHD, $1, $5))
2018+            if (push_new_name_resolution_context(thd, $1, $5))
2019               MYSQL_YYABORT;
2020             Select->parsing_place= IN_ON;
2021           }
2022@@ -8676,7 +8644,7 @@
2023           {
2024             MYSQL_YYABORT_UNLESS($1 && $5);
2025             /* Change the current name resolution context to a local context. */
2026-            if (push_new_name_resolution_context(YYTHD, $1, $5))
2027+            if (push_new_name_resolution_context(thd, $1, $5))
2028               MYSQL_YYABORT;
2029             Select->parsing_place= IN_ON;
2030           }
2031@@ -8724,7 +8692,7 @@
2032           }
2033           table_ident opt_table_alias opt_key_definition
2034           {
2035-            if (!($$= Select->add_table_to_list(YYTHD, $2, $3,
2036+            if (!($$= Select->add_table_to_list(thd, $2, $3,
2037                                                 Select->get_table_join_options(),
2038                                                 Lex->lock_option,
2039                                                 Select->pop_index_hints())))
2040@@ -8922,7 +8890,7 @@
2041
2042 opt_index_hints_list:
2043           /* empty */
2044-        | { Select->alloc_index_hints(YYTHD); } index_hints_list
2045+        | { Select->alloc_index_hints(thd); } index_hints_list
2046         ;
2047
2048 opt_key_definition:
2049@@ -8931,15 +8899,15 @@
2050         ;
2051
2052 opt_key_usage_list:
2053-          /* empty */ { Select->add_index_hint(YYTHD, NULL, 0); }
2054+          /* empty */ { Select->add_index_hint(thd, NULL, 0); }
2055         | key_usage_list {}
2056         ;
2057
2058 key_usage_element:
2059           ident
2060-          { Select->add_index_hint(YYTHD, $1.str, $1.length); }
2061+          { Select->add_index_hint(thd, $1.str, $1.length); }
2062         | PRIMARY_SYM
2063-          { Select->add_index_hint(YYTHD, (char *)"PRIMARY", 7); }
2064+          { Select->add_index_hint(thd, (char *)"PRIMARY", 7); }
2065         ;
2066
2067 key_usage_list:
2068@@ -8952,7 +8920,7 @@
2069           {
2070             if (!($$= new List<String>))
2071               MYSQL_YYABORT;
2072-            String *s= new (YYTHD->mem_root) String((const char *) $1.str,
2073+            String *s= new (thd->mem_root) String((const char *) $1.str,
2074                                                     $1.length,
2075                                                     system_charset_info);
2076             if (s == NULL)
2077@@ -8961,7 +8929,7 @@
2078           }
2079         | using_list ',' ident
2080           {
2081-            String *s= new (YYTHD->mem_root) String((const char *) $3.str,
2082+            String *s= new (thd->mem_root) String((const char *) $3.str,
2083                                                     $3.length,
2084                                                     system_charset_info);
2085             if (s == NULL)
2086@@ -9002,7 +8970,7 @@
2087                                     implementation without changing its
2088                                     resolution.
2089                                   */
2090-                                  WARN_DEPRECATED(yythd, VER_CELOSIA, "FRAC_SECOND", "MICROSECOND");
2091+                                  WARN_DEPRECATED(thd, VER_CELOSIA, "FRAC_SECOND", "MICROSECOND");
2092                                 }
2093 	;
2094
2095@@ -9086,7 +9054,6 @@
2096           }
2097         | /* empty */
2098           {
2099-            THD *thd= YYTHD;
2100             Lex->escape_used= FALSE;
2101             $$= ((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) ?
2102                  new (thd->mem_root) Item_string("", 0, &my_charset_latin1) :
2103@@ -9107,9 +9074,9 @@
2104
2105 group_list:
2106           group_list ',' order_ident order_dir
2107-          { if (add_group_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
2108+          { if (add_group_to_list(thd, $3,(bool) $4)) MYSQL_YYABORT; }
2109         | order_ident order_dir
2110-          { if (add_group_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; }
2111+          { if (add_group_to_list(thd, $1,(bool) $2)) MYSQL_YYABORT; }
2112         ;
2113
2114 olap_opt:
2115@@ -9156,7 +9123,6 @@
2116 alter_order_item:
2117           simple_ident_nospvar order_dir
2118           {
2119-            THD *thd= YYTHD;
2120             bool ascending= ($2 == 1) ? true : false;
2121             if (add_order_to_list(thd, $1, ascending))
2122               MYSQL_YYABORT;
2123@@ -9209,9 +9175,9 @@
2124
2125 order_list:
2126           order_list ',' order_ident order_dir
2127-          { if (add_order_to_list(YYTHD, $3,(bool) $4)) MYSQL_YYABORT; }
2128+          { if (add_order_to_list(thd, $3,(bool) $4)) MYSQL_YYABORT; }
2129         | order_ident order_dir
2130-          { if (add_order_to_list(YYTHD, $1,(bool) $2)) MYSQL_YYABORT; }
2131+          { if (add_order_to_list(thd, $1,(bool) $2)) MYSQL_YYABORT; }
2132         ;
2133
2134 order_dir:
2135@@ -9271,19 +9237,19 @@
2136         }
2137         | ULONGLONG_NUM
2138           {
2139-            $$= new (YYTHD->mem_root) Item_uint($1.str, $1.length);
2140+            $$= new (thd->mem_root) Item_uint($1.str, $1.length);
2141             if ($$ == NULL)
2142               MYSQL_YYABORT;
2143           }
2144         | LONG_NUM
2145           {
2146-            $$= new (YYTHD->mem_root) Item_uint($1.str, $1.length);
2147+            $$= new (thd->mem_root) Item_uint($1.str, $1.length);
2148             if ($$ == NULL)
2149               MYSQL_YYABORT;
2150           }
2151         | NUM
2152           {
2153-            $$= new (YYTHD->mem_root) Item_uint($1.str, $1.length);
2154+            $$= new (thd->mem_root) Item_uint($1.str, $1.length);
2155             if ($$ == NULL)
2156               MYSQL_YYABORT;
2157           }
2158@@ -9365,7 +9331,7 @@
2159             lex->proc_list.elements=0;
2160             lex->proc_list.first=0;
2161             lex->proc_list.next= &lex->proc_list.first;
2162-            Item_field *item= new (YYTHD->mem_root)
2163+            Item_field *item= new (thd->mem_root)
2164                                 Item_field(&lex->current_select->context,
2165                                            NULL, NULL, $2.str);
2166             if (item == NULL)
2167@@ -9390,8 +9356,7 @@
2168 procedure_item:
2169           remember_name expr remember_end
2170           {
2171-            THD *thd= YYTHD;
2172-
2173+
2174             if (add_proc_to_list(thd, $2))
2175               MYSQL_YYABORT;
2176             if (!$2->name)
2177@@ -9560,7 +9525,6 @@
2178           }
2179         | DROP FUNCTION_SYM if_exists ident '.' ident
2180           {
2181-            THD *thd= YYTHD;
2182             LEX *lex= thd->lex;
2183             sp_name *spname;
2184             if ($4.str && check_db_name(&$4))
2185@@ -9583,7 +9547,6 @@
2186           }
2187         | DROP FUNCTION_SYM if_exists ident
2188           {
2189-            THD *thd= YYTHD;
2190             LEX *lex= thd->lex;
2191             LEX_STRING db= {0, 0};
2192             sp_name *spname;
2193@@ -9664,7 +9627,7 @@
2194 table_name:
2195           table_ident
2196           {
2197-            if (!Select->add_table_to_list(YYTHD, $1, NULL, TL_OPTION_UPDATING))
2198+            if (!Select->add_table_to_list(thd, $1, NULL, TL_OPTION_UPDATING))
2199               MYSQL_YYABORT;
2200           }
2201         ;
2202@@ -9677,7 +9640,7 @@
2203 table_alias_ref:
2204           table_ident_opt_wild
2205           {
2206-            if (!Select->add_table_to_list(YYTHD, $1, NULL,
2207+            if (!Select->add_table_to_list(thd, $1, NULL,
2208                                            TL_OPTION_UPDATING | TL_OPTION_ALIAS,
2209                                            Lex->lock_option ))
2210               MYSQL_YYABORT;
2211@@ -9868,7 +9831,7 @@
2212           expr { $$= $1;}
2213         | DEFAULT
2214           {
2215-            $$= new (YYTHD->mem_root) Item_default_value(Lex->current_context());
2216+            $$= new (thd->mem_root) Item_default_value(Lex->current_context());
2217             if ($$ == NULL)
2218               MYSQL_YYABORT;
2219           }
2220@@ -9922,7 +9885,7 @@
2221 update_elem:
2222           simple_ident_nospvar equal expr_or_default
2223           {
2224-            if (add_item_to_list(YYTHD, $1) || add_value_to_list(YYTHD, $3))
2225+            if (add_item_to_list(thd, $1) || add_value_to_list(thd, $3))
2226               MYSQL_YYABORT;
2227           }
2228         ;
2229@@ -9965,7 +9928,7 @@
2230 single_multi:
2231           FROM table_ident
2232           {
2233-            if (!Select->add_table_to_list(YYTHD, $2, NULL, TL_OPTION_UPDATING,
2234+            if (!Select->add_table_to_list(thd, $2, NULL, TL_OPTION_UPDATING,
2235                                            Lex->lock_option))
2236               MYSQL_YYABORT;
2237           }
2238@@ -9998,7 +9961,7 @@
2239             Table_ident *ti= new Table_ident($1);
2240             if (ti == NULL)
2241               MYSQL_YYABORT;
2242-            if (!Select->add_table_to_list(YYTHD,
2243+            if (!Select->add_table_to_list(thd,
2244                                            ti,
2245                                            $3,
2246                                            TL_OPTION_UPDATING | TL_OPTION_ALIAS,
2247@@ -10007,10 +9970,10 @@
2248           }
2249         | ident '.' ident opt_wild opt_table_alias
2250           {
2251-            Table_ident *ti= new Table_ident(YYTHD, $1, $3, 0);
2252+            Table_ident *ti= new Table_ident(thd, $1, $3, 0);
2253             if (ti == NULL)
2254               MYSQL_YYABORT;
2255-            if (!Select->add_table_to_list(YYTHD,
2256+            if (!Select->add_table_to_list(thd,
2257                                            ti,
2258                                            $5,
2259                                            TL_OPTION_UPDATING | TL_OPTION_ALIAS,
2260@@ -10130,7 +10093,7 @@
2261            {
2262              LEX *lex= Lex;
2263              lex->sql_command= SQLCOM_SHOW_DATABASES;
2264-             if (prepare_schema_table(YYTHD, lex, 0, SCH_SCHEMATA))
2265+             if (prepare_schema_table(thd, lex, 0, SCH_SCHEMATA))
2266                MYSQL_YYABORT;
2267            }
2268          | opt_full TABLES opt_db wild_and_where
2269@@ -10138,7 +10101,7 @@
2270              LEX *lex= Lex;
2271              lex->sql_command= SQLCOM_SHOW_TABLES;
2272              lex->select_lex.db= $3;
2273-             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLE_NAMES))
2274+             if (prepare_schema_table(thd, lex, 0, SCH_TABLE_NAMES))
2275                MYSQL_YYABORT;
2276            }
2277          | opt_full TRIGGERS_SYM opt_db wild_and_where
2278@@ -10146,7 +10109,7 @@
2279              LEX *lex= Lex;
2280              lex->sql_command= SQLCOM_SHOW_TRIGGERS;
2281              lex->select_lex.db= $3;
2282-             if (prepare_schema_table(YYTHD, lex, 0, SCH_TRIGGERS))
2283+             if (prepare_schema_table(thd, lex, 0, SCH_TRIGGERS))
2284                MYSQL_YYABORT;
2285            }
2286          | EVENTS_SYM opt_db wild_and_where
2287@@ -10154,7 +10117,7 @@
2288              LEX *lex= Lex;
2289              lex->sql_command= SQLCOM_SHOW_EVENTS;
2290              lex->select_lex.db= $2;
2291-             if (prepare_schema_table(YYTHD, lex, 0, SCH_EVENTS))
2292+             if (prepare_schema_table(thd, lex, 0, SCH_EVENTS))
2293                MYSQL_YYABORT;
2294            }
2295          | TABLE_SYM STATUS_SYM opt_db wild_and_where
2296@@ -10162,7 +10125,7 @@
2297              LEX *lex= Lex;
2298              lex->sql_command= SQLCOM_SHOW_TABLE_STATUS;
2299              lex->select_lex.db= $3;
2300-             if (prepare_schema_table(YYTHD, lex, 0, SCH_TABLES))
2301+             if (prepare_schema_table(thd, lex, 0, SCH_TABLES))
2302                MYSQL_YYABORT;
2303            }
2304         | OPEN_SYM TABLES opt_db wild_and_where
2305@@ -10170,22 +10133,22 @@
2306             LEX *lex= Lex;
2307             lex->sql_command= SQLCOM_SHOW_OPEN_TABLES;
2308             lex->select_lex.db= $3;
2309-            if (prepare_schema_table(YYTHD, lex, 0, SCH_OPEN_TABLES))
2310+            if (prepare_schema_table(thd, lex, 0, SCH_OPEN_TABLES))
2311               MYSQL_YYABORT;
2312           }
2313         | opt_full PLUGIN_SYM
2314           {
2315             LEX *lex= Lex;
2316-            WARN_DEPRECATED(yythd, "6.0", "SHOW PLUGIN", "'SHOW PLUGINS'");
2317+            WARN_DEPRECATED(thd, "6.0", "SHOW PLUGIN", "'SHOW PLUGINS'");
2318             lex->sql_command= SQLCOM_SHOW_PLUGINS;
2319-            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
2320+            if (prepare_schema_table(thd, lex, 0, SCH_PLUGINS))
2321               MYSQL_YYABORT;
2322           }
2323         | PLUGINS_SYM
2324           {
2325             LEX *lex= Lex;
2326             lex->sql_command= SQLCOM_SHOW_PLUGINS;
2327-            if (prepare_schema_table(YYTHD, lex, 0, SCH_PLUGINS))
2328+            if (prepare_schema_table(thd, lex, 0, SCH_PLUGINS))
2329               MYSQL_YYABORT;
2330           }
2331         | ENGINE_SYM known_storage_engines show_engine_param
2332@@ -10198,7 +10161,7 @@
2333             lex->sql_command= SQLCOM_SHOW_FIELDS;
2334             if ($5)
2335               $4->change_db($5);
2336-            if (prepare_schema_table(YYTHD, lex, $4, SCH_COLUMNS))
2337+            if (prepare_schema_table(thd, lex, $4, SCH_COLUMNS))
2338               MYSQL_YYABORT;
2339           }
2340         | NEW_SYM MASTER_SYM FOR_SYM SLAVE
2341@@ -10233,7 +10196,7 @@
2342             lex->sql_command= SQLCOM_SHOW_KEYS;
2343             if ($4)
2344               $3->change_db($4);
2345-            if (prepare_schema_table(YYTHD, lex, $3, SCH_STATISTICS))
2346+            if (prepare_schema_table(thd, lex, $3, SCH_STATISTICS))
2347               MYSQL_YYABORT;
2348           }
2349         | COLUMN_SYM TYPES_SYM
2350@@ -10245,15 +10208,15 @@
2351           {
2352             LEX *lex=Lex;
2353             lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
2354-            WARN_DEPRECATED(yythd, "6.0", "SHOW TABLE TYPES", "'SHOW [STORAGE] ENGINES'");
2355-            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
2356+            WARN_DEPRECATED(thd, "6.0", "SHOW TABLE TYPES", "'SHOW [STORAGE] ENGINES'");
2357+            if (prepare_schema_table(thd, lex, 0, SCH_ENGINES))
2358               MYSQL_YYABORT;
2359           }
2360         | opt_storage ENGINES_SYM
2361           {
2362             LEX *lex=Lex;
2363             lex->sql_command= SQLCOM_SHOW_STORAGE_ENGINES;
2364-            if (prepare_schema_table(YYTHD, lex, 0, SCH_ENGINES))
2365+            if (prepare_schema_table(thd, lex, 0, SCH_ENGINES))
2366               MYSQL_YYABORT;
2367           }
2368         | AUTHORS_SYM
2369@@ -10285,7 +10248,7 @@
2370           {
2371             LEX *lex= Lex;
2372             lex->sql_command= SQLCOM_SHOW_PROFILE;
2373-            if (prepare_schema_table(YYTHD, lex, NULL, SCH_PROFILES) != 0)
2374+            if (prepare_schema_table(thd, lex, NULL, SCH_PROFILES) != 0)
2375               YYABORT;
2376           }
2377         | opt_var_type STATUS_SYM wild_and_where
2378@@ -10293,7 +10256,7 @@
2379             LEX *lex= Lex;
2380             lex->sql_command= SQLCOM_SHOW_STATUS;
2381             lex->option_type= $1;
2382-            if (prepare_schema_table(YYTHD, lex, 0, SCH_STATUS))
2383+            if (prepare_schema_table(thd, lex, 0, SCH_STATUS))
2384               MYSQL_YYABORT;
2385           }
2386         | INNOBASE_SYM STATUS_SYM
2387@@ -10301,24 +10264,24 @@
2388             LEX *lex= Lex;
2389             lex->sql_command = SQLCOM_SHOW_ENGINE_STATUS;
2390             if (!(lex->create_info.db_type=
2391-                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
2392+                  ha_resolve_by_legacy_type(thd, DB_TYPE_INNODB)))
2393             {
2394               my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
2395               MYSQL_YYABORT;
2396             }
2397-            WARN_DEPRECATED(yythd, "6.0", "SHOW INNODB STATUS", "'SHOW ENGINE INNODB STATUS'");
2398+            WARN_DEPRECATED(thd, "6.0", "SHOW INNODB STATUS", "'SHOW ENGINE INNODB STATUS'");
2399           }
2400         | MUTEX_SYM STATUS_SYM
2401           {
2402             LEX *lex= Lex;
2403             lex->sql_command = SQLCOM_SHOW_ENGINE_MUTEX;
2404             if (!(lex->create_info.db_type=
2405-                  ha_resolve_by_legacy_type(YYTHD, DB_TYPE_INNODB)))
2406+                  ha_resolve_by_legacy_type(thd, DB_TYPE_INNODB)))
2407             {
2408               my_error(ER_UNKNOWN_STORAGE_ENGINE, MYF(0), "InnoDB");
2409               MYSQL_YYABORT;
2410             }
2411-            WARN_DEPRECATED(yythd, "6.0", "SHOW MUTEX STATUS", "'SHOW ENGINE INNODB MUTEX'");
2412+            WARN_DEPRECATED(thd, "6.0", "SHOW MUTEX STATUS", "'SHOW ENGINE INNODB MUTEX'");
2413           }
2414         | opt_full PROCESSLIST_SYM
2415           { Lex->sql_command= SQLCOM_SHOW_PROCESSLIST;}
2416@@ -10327,21 +10290,21 @@
2417             LEX *lex= Lex;
2418             lex->sql_command= SQLCOM_SHOW_VARIABLES;
2419             lex->option_type= $1;
2420-            if (prepare_schema_table(YYTHD, lex, 0, SCH_VARIABLES))
2421+            if (prepare_schema_table(thd, lex, 0, SCH_VARIABLES))
2422               MYSQL_YYABORT;
2423           }
2424         | charset wild_and_where
2425           {
2426             LEX *lex= Lex;
2427             lex->sql_command= SQLCOM_SHOW_CHARSETS;
2428-            if (prepare_schema_table(YYTHD, lex, 0, SCH_CHARSETS))
2429+            if (prepare_schema_table(thd, lex, 0, SCH_CHARSETS))
2430               MYSQL_YYABORT;
2431           }
2432         | COLLATION_SYM wild_and_where
2433           {
2434             LEX *lex= Lex;
2435             lex->sql_command= SQLCOM_SHOW_COLLATIONS;
2436-            if (prepare_schema_table(YYTHD, lex, 0, SCH_COLLATIONS))
2437+            if (prepare_schema_table(thd, lex, 0, SCH_COLLATIONS))
2438               MYSQL_YYABORT;
2439           }
2440         | GRANTS
2441@@ -10371,7 +10334,7 @@
2442           {
2443             LEX *lex= Lex;
2444             lex->sql_command = SQLCOM_SHOW_CREATE;
2445-            if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL,0))
2446+            if (!lex->select_lex.add_table_to_list(thd, $3, NULL,0))
2447               MYSQL_YYABORT;
2448             lex->only_view= 0;
2449             lex->create_info.storage_media= HA_SM_DEFAULT;
2450@@ -10380,7 +10343,7 @@
2451           {
2452             LEX *lex= Lex;
2453             lex->sql_command = SQLCOM_SHOW_CREATE;
2454-            if (!lex->select_lex.add_table_to_list(YYTHD, $3, NULL, 0))
2455+            if (!lex->select_lex.add_table_to_list(thd, $3, NULL, 0))
2456               MYSQL_YYABORT;
2457             lex->only_view= 1;
2458           }
2459@@ -10416,14 +10379,14 @@
2460           {
2461             LEX *lex= Lex;
2462             lex->sql_command= SQLCOM_SHOW_STATUS_PROC;
2463-            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
2464+            if (prepare_schema_table(thd, lex, 0, SCH_PROCEDURES))
2465               MYSQL_YYABORT;
2466           }
2467         | FUNCTION_SYM STATUS_SYM wild_and_where
2468           {
2469             LEX *lex= Lex;
2470             lex->sql_command= SQLCOM_SHOW_STATUS_FUNC;
2471-            if (prepare_schema_table(YYTHD, lex, 0, SCH_PROCEDURES))
2472+            if (prepare_schema_table(thd, lex, 0, SCH_PROCEDURES))
2473               MYSQL_YYABORT;
2474           }
2475         | PROCEDURE CODE_SYM sp_name
2476@@ -10501,7 +10464,7 @@
2477           /* empty */
2478         | LIKE TEXT_STRING_sys
2479           {
2480-            Lex->wild= new (YYTHD->mem_root) String($2.str, $2.length,
2481+            Lex->wild= new (thd->mem_root) String($2.str, $2.length,
2482                                                     system_charset_info);
2483             if (Lex->wild == NULL)
2484               MYSQL_YYABORT;
2485@@ -10525,7 +10488,7 @@
2486             lex->sql_command= SQLCOM_SHOW_FIELDS;
2487             lex->select_lex.db= 0;
2488             lex->verbose= 0;
2489-            if (prepare_schema_table(YYTHD, lex, $2, SCH_COLUMNS))
2490+            if (prepare_schema_table(thd, lex, $2, SCH_COLUMNS))
2491               MYSQL_YYABORT;
2492           }
2493           opt_describe_column {}
2494@@ -10554,7 +10517,7 @@
2495         | text_string { Lex->wild= $1; }
2496         | ident
2497           {
2498-            Lex->wild= new (YYTHD->mem_root) String((const char*) $1.str,
2499+            Lex->wild= new (thd->mem_root) String((const char*) $1.str,
2500                                                     $1.length,
2501                                                     system_charset_info);
2502             if (Lex->wild == NULL)
2503@@ -10697,7 +10660,6 @@
2504 load:
2505           LOAD DATA_SYM
2506           {
2507-            THD *thd= YYTHD;
2508             LEX *lex= thd->lex;
2509
2510             if (lex->sphead)
2511@@ -10711,7 +10673,7 @@
2512         | LOAD TABLE_SYM table_ident FROM MASTER_SYM
2513           {
2514             LEX *lex=Lex;
2515-            WARN_DEPRECATED(yythd, "6.0", "LOAD TABLE FROM MASTER",
2516+            WARN_DEPRECATED(thd, "6.0", "LOAD TABLE FROM MASTER",
2517                             "MySQL Administrator (mysqldump, mysql)");
2518             if (lex->sphead)
2519             {
2520@@ -10719,7 +10681,7 @@
2521               MYSQL_YYABORT;
2522             }
2523             lex->sql_command = SQLCOM_LOAD_MASTER_TABLE;
2524-            if (!Select->add_table_to_list(YYTHD, $3, NULL, TL_OPTION_UPDATING))
2525+            if (!Select->add_table_to_list(thd, $3, NULL, TL_OPTION_UPDATING))
2526               MYSQL_YYABORT;
2527           }
2528         ;
2529@@ -10739,7 +10701,7 @@
2530           opt_duplicate INTO TABLE_SYM table_ident
2531           {
2532             LEX *lex=Lex;
2533-            if (!Select->add_table_to_list(YYTHD, $9, NULL, TL_OPTION_UPDATING,
2534+            if (!Select->add_table_to_list(thd, $9, NULL, TL_OPTION_UPDATING,
2535                                            lex->lock_option))
2536               MYSQL_YYABORT;
2537             lex->field_list.empty();
2538@@ -10754,7 +10716,7 @@
2539         | FROM MASTER_SYM
2540           {
2541             Lex->sql_command = SQLCOM_LOAD_MASTER_DATA;
2542-            WARN_DEPRECATED(yythd, "6.0", "LOAD DATA FROM MASTER",
2543+            WARN_DEPRECATED(thd, "6.0", "LOAD DATA FROM MASTER",
2544                             "mysqldump or future "
2545                             "BACKUP/RESTORE DATABASE facility");
2546           }
2547@@ -10872,7 +10834,7 @@
2548           simple_ident_nospvar {$$= $1;}
2549         | '@' ident_or_text
2550           {
2551-            $$= new (YYTHD->mem_root) Item_user_var_as_out_param($2);
2552+            $$= new (thd->mem_root) Item_user_var_as_out_param($2);
2553             if ($$ == NULL)
2554               MYSQL_YYABORT;
2555           }
2556@@ -10889,7 +10851,6 @@
2557           TEXT_STRING
2558           {
2559             LEX_STRING tmp;
2560-            THD *thd= YYTHD;
2561             CHARSET_INFO *cs_con= thd->variables.collation_connection;
2562             CHARSET_INFO *cs_cli= thd->variables.character_set_client;
2563             uint repertoire= thd->lex->text_string_is_7bit &&
2564@@ -10915,7 +10876,7 @@
2565             uint repertoire= Lex->text_string_is_7bit ?
2566                              MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
2567             DBUG_ASSERT(my_charset_is_ascii_based(national_charset_info));
2568-            $$= new (YYTHD->mem_root) Item_string($1.str, $1.length,
2569+            $$= new (thd->mem_root) Item_string($1.str, $1.length,
2570                                                   national_charset_info,
2571                                                   DERIVATION_COERCIBLE,
2572                                                   repertoire);
2573@@ -10924,7 +10885,7 @@
2574           }
2575         | UNDERSCORE_CHARSET TEXT_STRING
2576           {
2577-            Item_string *str= new (YYTHD->mem_root) Item_string($2.str,
2578+            Item_string *str= new (thd->mem_root) Item_string($2.str,
2579                                                                 $2.length, $1);
2580             if (str == NULL)
2581               MYSQL_YYABORT;
2582@@ -10943,7 +10904,7 @@
2583                  If the string has been pure ASCII so far,
2584                  check the new part.
2585               */
2586-              CHARSET_INFO *cs= YYTHD->variables.collation_connection;
2587+              CHARSET_INFO *cs= thd->variables.collation_connection;
2588               item->collation.repertoire|= my_string_repertoire(cs,
2589                                                                 $2.str,
2590                                                                 $2.length);
2591@@ -10954,15 +10915,15 @@
2592 text_string:
2593           TEXT_STRING_literal
2594           {
2595-            $$= new (YYTHD->mem_root) String($1.str,
2596+            $$= new (thd->mem_root) String($1.str,
2597                                              $1.length,
2598-                                             YYTHD->variables.collation_connection);
2599+                                             thd->variables.collation_connection);
2600             if ($$ == NULL)
2601               MYSQL_YYABORT;
2602           }
2603         | HEX_NUM
2604           {
2605-            Item *tmp= new (YYTHD->mem_root) Item_hex_string($1.str, $1.length);
2606+            Item *tmp= new (thd->mem_root) Item_hex_string($1.str, $1.length);
2607             if (tmp == NULL)
2608               MYSQL_YYABORT;
2609             /*
2610@@ -10974,7 +10935,7 @@
2611           }
2612         | BIN_NUM
2613           {
2614-            Item *tmp= new (YYTHD->mem_root) Item_bin_string($1.str, $1.length);
2615+            Item *tmp= new (thd->mem_root) Item_bin_string($1.str, $1.length);
2616             if (tmp == NULL)
2617               MYSQL_YYABORT;
2618             /*
2619@@ -10989,7 +10950,6 @@
2620 param_marker:
2621           PARAM_MARKER
2622           {
2623-            THD *thd= YYTHD;
2624             LEX *lex= thd->lex;
2625             Lex_input_stream *lip= YYLIP;
2626             Item_param *item;
2627@@ -11022,38 +10982,38 @@
2628         | NUM_literal { $$ = $1; }
2629         | NULL_SYM
2630           {
2631-            $$ = new (YYTHD->mem_root) Item_null();
2632+            $$ = new (thd->mem_root) Item_null();
2633             if ($$ == NULL)
2634               MYSQL_YYABORT;
2635             YYLIP->next_state= MY_LEX_OPERATOR_OR_IDENT;
2636           }
2637         | FALSE_SYM
2638           {
2639-            $$= new (YYTHD->mem_root) Item_int((char*) "FALSE",0,1);
2640+            $$= new (thd->mem_root) Item_int((char*) "FALSE",0,1);
2641             if ($$ == NULL)
2642               MYSQL_YYABORT;
2643           }
2644         | TRUE_SYM
2645           {
2646-            $$= new (YYTHD->mem_root) Item_int((char*) "TRUE",1,1);
2647+            $$= new (thd->mem_root) Item_int((char*) "TRUE",1,1);
2648             if ($$ == NULL)
2649               MYSQL_YYABORT;
2650           }
2651         | HEX_NUM
2652           {
2653-            $$ = new (YYTHD->mem_root) Item_hex_string($1.str, $1.length);
2654+            $$ = new (thd->mem_root) Item_hex_string($1.str, $1.length);
2655             if ($$ == NULL)
2656               MYSQL_YYABORT;
2657           }
2658         | BIN_NUM
2659           {
2660-            $$= new (YYTHD->mem_root) Item_bin_string($1.str, $1.length);
2661+            $$= new (thd->mem_root) Item_bin_string($1.str, $1.length);
2662             if ($$ == NULL)
2663               MYSQL_YYABORT;
2664           }
2665         | UNDERSCORE_CHARSET HEX_NUM
2666           {
2667-            Item *tmp= new (YYTHD->mem_root) Item_hex_string($2.str, $2.length);
2668+            Item *tmp= new (thd->mem_root) Item_hex_string($2.str, $2.length);
2669             if (tmp == NULL)
2670               MYSQL_YYABORT;
2671             /*
2672@@ -11064,7 +11024,7 @@
2673             String *str= tmp->val_str((String*) 0);
2674
2675             Item_string *item_str;
2676-            item_str= new (YYTHD->mem_root)
2677+            item_str= new (thd->mem_root)
2678                         Item_string(NULL, /* name will be set in select_item */
2679                                     str ? str->ptr() : "",
2680                                     str ? str->length() : 0,
2681@@ -11082,7 +11042,7 @@
2682           }
2683         | UNDERSCORE_CHARSET BIN_NUM
2684           {
2685-            Item *tmp= new (YYTHD->mem_root) Item_bin_string($2.str, $2.length);
2686+            Item *tmp= new (thd->mem_root) Item_bin_string($2.str, $2.length);
2687             if (tmp == NULL)
2688               MYSQL_YYABORT;
2689             /*
2690@@ -11093,7 +11053,7 @@
2691             String *str= tmp->val_str((String*) 0);
2692
2693             Item_string *item_str;
2694-            item_str= new (YYTHD->mem_root)
2695+            item_str= new (thd->mem_root)
2696                         Item_string(NULL, /* name will be set in select_item */
2697                                     str ? str->ptr() : "",
2698                                     str ? str->length() : 0,
2699@@ -11117,7 +11077,7 @@
2700           NUM
2701           {
2702             int error;
2703-            $$= new (YYTHD->mem_root)
2704+            $$= new (thd->mem_root)
2705                   Item_int($1.str,
2706                            (longlong) my_strtoll10($1.str, NULL, &error),
2707                            $1.length);
2708@@ -11127,7 +11087,7 @@
2709         | LONG_NUM
2710           {
2711             int error;
2712-            $$= new (YYTHD->mem_root)
2713+            $$= new (thd->mem_root)
2714                   Item_int($1.str,
2715                            (longlong) my_strtoll10($1.str, NULL, &error),
2716                            $1.length);
2717@@ -11136,23 +11096,23 @@
2718           }
2719         | ULONGLONG_NUM
2720           {
2721-            $$= new (YYTHD->mem_root) Item_uint($1.str, $1.length);
2722+            $$= new (thd->mem_root) Item_uint($1.str, $1.length);
2723             if ($$ == NULL)
2724               MYSQL_YYABORT;
2725           }
2726         | DECIMAL_NUM
2727           {
2728-            $$= new (YYTHD->mem_root) Item_decimal($1.str, $1.length,
2729-                                                   YYTHD->charset());
2730-            if (($$ == NULL) || (YYTHD->is_error()))
2731+            $$= new (thd->mem_root) Item_decimal($1.str, $1.length,
2732+                                                   thd->charset());
2733+            if (($$ == NULL) || (thd->is_error()))
2734             {
2735               MYSQL_YYABORT;
2736             }
2737           }
2738         | FLOAT_NUM
2739           {
2740-            $$= new (YYTHD->mem_root) Item_float($1.str, $1.length);
2741-            if (($$ == NULL) || (YYTHD->is_error()))
2742+            $$= new (thd->mem_root) Item_float($1.str, $1.length);
2743+            if (($$ == NULL) || (thd->is_error()))
2744             {
2745               MYSQL_YYABORT;
2746             }
2747@@ -11172,7 +11132,7 @@
2748           ident '.' '*'
2749           {
2750             SELECT_LEX *sel= Select;
2751-            $$= new (YYTHD->mem_root) Item_field(Lex->current_context(),
2752+            $$= new (thd->mem_root) Item_field(Lex->current_context(),
2753                                                  NullS, $1.str, "*");
2754             if ($$ == NULL)
2755               MYSQL_YYABORT;
2756@@ -11180,7 +11140,6 @@
2757           }
2758         | ident '.' ident '.' '*'
2759           {
2760-            THD *thd= YYTHD;
2761             SELECT_LEX *sel= Select;
2762             const char* schema= thd->client_capabilities & CLIENT_NO_SCHEMA ?
2763                                   NullS : $1.str;
2764@@ -11200,7 +11159,6 @@
2765 simple_ident:
2766           ident
2767           {
2768-            THD *thd= YYTHD;
2769             LEX *lex= thd->lex;
2770             Lex_input_stream *lip= YYLIP;
2771             sp_variable_t *spv;
2772@@ -11251,7 +11209,6 @@
2773 simple_ident_nospvar:
2774           ident
2775           {
2776-            THD *thd= YYTHD;
2777             SELECT_LEX *sel=Select;
2778             if ((sel->parsing_place != IN_HAVING) ||
2779                 (sel->get_in_sum_expr() > 0))
2780@@ -11273,7 +11230,6 @@
2781 simple_ident_q:
2782           ident '.' ident
2783           {
2784-            THD *thd= YYTHD;
2785             LEX *lex= thd->lex;
2786
2787             /*
2788@@ -11352,7 +11308,6 @@
2789           }
2790         | '.' ident '.' ident
2791           {
2792-            THD *thd= YYTHD;
2793             LEX *lex= thd->lex;
2794             SELECT_LEX *sel= lex->current_select;
2795             if (sel->no_table_names_allowed)
2796@@ -11377,7 +11332,6 @@
2797           }
2798         | ident '.' ident '.' ident
2799           {
2800-            THD *thd= YYTHD;
2801             LEX *lex= thd->lex;
2802             SELECT_LEX *sel= lex->current_select;
2803             const char* schema= (thd->client_capabilities & CLIENT_NO_SCHEMA ?
2804@@ -11445,7 +11399,7 @@
2805           }
2806         | ident '.' ident
2807           {
2808-            $$= new Table_ident(YYTHD, $1,$3,0);
2809+            $$= new Table_ident(thd, $1,$3,0);
2810             if ($$ == NULL)
2811               MYSQL_YYABORT;
2812           }
2813@@ -11467,7 +11421,7 @@
2814           }
2815         | ident '.' ident opt_wild
2816           {
2817-            $$= new Table_ident(YYTHD, $1,$3,0);
2818+            $$= new Table_ident(thd, $1,$3,0);
2819             if ($$ == NULL)
2820               MYSQL_YYABORT;
2821           }
2822@@ -11477,7 +11431,7 @@
2823           ident
2824           {
2825             LEX_STRING db={(char*) any_db,3};
2826-            $$= new Table_ident(YYTHD, db,$1,0);
2827+            $$= new Table_ident(thd, db,$1,0);
2828             if ($$ == NULL)
2829               MYSQL_YYABORT;
2830           }
2831@@ -11487,8 +11441,7 @@
2832           IDENT { $$= $1; }
2833         | IDENT_QUOTED
2834           {
2835-            THD *thd= YYTHD;
2836-
2837+
2838             if (thd->charset_is_system_charset)
2839             {
2840               CHARSET_INFO *cs= system_charset_info;
2841@@ -11516,8 +11469,6 @@
2842 TEXT_STRING_sys:
2843           TEXT_STRING
2844           {
2845-            THD *thd= YYTHD;
2846-
2847             if (thd->charset_is_system_charset)
2848               $$= $1;
2849             else
2850@@ -11532,8 +11483,6 @@
2851 TEXT_STRING_literal:
2852           TEXT_STRING
2853           {
2854-            THD *thd= YYTHD;
2855-
2856             if (thd->charset_is_collation_connection)
2857               $$= $1;
2858             else
2859@@ -11548,8 +11497,6 @@
2860 TEXT_STRING_filesystem:
2861           TEXT_STRING
2862           {
2863-            THD *thd= YYTHD;
2864-
2865             if (thd->charset_is_character_set_filesystem)
2866               $$= $1;
2867             else
2868@@ -11566,7 +11513,6 @@
2869           IDENT_sys    { $$=$1; }
2870         | keyword
2871           {
2872-            THD *thd= YYTHD;
2873             $$.str= thd->strmake($1.str, $1.length);
2874             if ($$.str == NULL)
2875               MYSQL_YYABORT;
2876@@ -11578,7 +11524,6 @@
2877           IDENT_sys    { $$=$1; }
2878         | keyword_sp
2879           {
2880-            THD *thd= YYTHD;
2881             $$.str= thd->strmake($1.str, $1.length);
2882             if ($$.str == NULL)
2883               MYSQL_YYABORT;
2884@@ -11595,7 +11540,6 @@
2885 user:
2886           ident_or_text
2887           {
2888-            THD *thd= YYTHD;
2889             if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
2890               MYSQL_YYABORT;
2891             $$->user = $1;
2892@@ -11609,7 +11553,6 @@
2893           }
2894         | ident_or_text '@' ident_or_text
2895           {
2896-            THD *thd= YYTHD;
2897             if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
2898               MYSQL_YYABORT;
2899             $$->user = $1; $$->host=$3;
2900@@ -11628,7 +11571,7 @@
2901           }
2902         | CURRENT_USER optional_braces
2903           {
2904-            if (!($$=(LEX_USER*) YYTHD->alloc(sizeof(st_lex_user))))
2905+            if (!($$=(LEX_USER*) thd->alloc(sizeof(st_lex_user))))
2906               MYSQL_YYABORT;
2907             /*
2908               empty LEX_USER means current_user and
2909@@ -11991,7 +11934,6 @@
2910
2911 option_type_value:
2912           {
2913-            THD *thd= YYTHD;
2914             LEX *lex= thd->lex;
2915             Lex_input_stream *lip= YYLIP;
2916
2917@@ -12022,7 +11964,6 @@
2918           }
2919           ext_option_value
2920           {
2921-            THD *thd= YYTHD;
2922             LEX *lex= thd->lex;
2923             Lex_input_stream *lip= YYLIP;
2924
2925@@ -12105,7 +12046,6 @@
2926 sys_option_value:
2927           option_type internal_variable_name equal set_expr_or_default
2928           {
2929-            THD *thd= YYTHD;
2930             LEX *lex= Lex;
2931             LEX_STRING *name= &$2.base_name;
2932
2933@@ -12117,7 +12057,7 @@
2934                 my_parse_error(ER(ER_SYNTAX_ERROR));
2935                 MYSQL_YYABORT;
2936               }
2937-              if (set_trigger_new_row(YYTHD, name, $4))
2938+              if (set_trigger_new_row(thd, name, $4))
2939                 MYSQL_YYABORT;
2940             }
2941             else if ($2.var)
2942@@ -12147,7 +12087,6 @@
2943           }
2944         | option_type TRANSACTION_SYM ISOLATION LEVEL_SYM isolation_types
2945           {
2946-            THD *thd= YYTHD;
2947             LEX *lex=Lex;
2948             lex->option_type= $1;
2949             Item *item= new (thd->mem_root) Item_int((int32) $5);
2950@@ -12167,7 +12106,7 @@
2951           '@' ident_or_text equal expr
2952           {
2953             Item_func_set_user_var *item;
2954-            item= new (YYTHD->mem_root) Item_func_set_user_var($2, $4);
2955+            item= new (thd->mem_root) Item_func_set_user_var($2, $4);
2956             if (item == NULL)
2957               MYSQL_YYABORT;
2958             set_var_user *var= new set_var_user(item);
2959@@ -12177,7 +12116,6 @@
2960           }
2961         | '@' '@' opt_var_ident_type internal_variable_name equal set_expr_or_default
2962           {
2963-            THD *thd= YYTHD;
2964             struct sys_var_with_base tmp= $4;
2965             /* Lookup if necessary: must be a system variable. */
2966             if (tmp.var == NULL)
2967@@ -12190,7 +12128,6 @@
2968           }
2969         | charset old_or_new_charset_name_or_default
2970           {
2971-            THD *thd= YYTHD;
2972             LEX *lex= thd->lex;
2973             CHARSET_INFO *cs2;
2974             cs2= $2 ? $2: global_system_variables.character_set_client;
2975@@ -12238,7 +12175,6 @@
2976           }
2977         | PASSWORD equal text_or_password
2978           {
2979-            THD *thd= YYTHD;
2980             LEX *lex= thd->lex;
2981             LEX_USER *user;
2982             sp_pcontext *spc= lex->spcont;
2983@@ -12278,7 +12214,6 @@
2984 internal_variable_name:
2985           ident
2986           {
2987-            THD *thd= YYTHD;
2988             sp_pcontext *spc= thd->lex->spcont;
2989             sp_variable_t *spv;
2990
2991@@ -12337,7 +12272,7 @@
2992             }
2993             else
2994             {
2995-              sys_var *tmp=find_sys_var(YYTHD, $3.str, $3.length);
2996+              sys_var *tmp=find_sys_var(thd, $3.str, $3.length);
2997               if (!tmp)
2998                 MYSQL_YYABORT;
2999               if (!tmp->is_struct())
3000@@ -12348,7 +12283,7 @@
3001           }
3002         | DEFAULT '.' ident
3003           {
3004-            sys_var *tmp=find_sys_var(YYTHD, $3.str, $3.length);
3005+            sys_var *tmp=find_sys_var(thd, $3.str, $3.length);
3006             if (!tmp)
3007               MYSQL_YYABORT;
3008             if (!tmp->is_struct())
3009@@ -12370,16 +12305,16 @@
3010           TEXT_STRING { $$=$1.str;}
3011         | PASSWORD '(' TEXT_STRING ')'
3012           {
3013-            $$= $3.length ? YYTHD->variables.old_passwords ?
3014-              Item_func_old_password::alloc(YYTHD, $3.str, $3.length) :
3015-              Item_func_password::alloc(YYTHD, $3.str, $3.length) :
3016+            $$= $3.length ? thd->variables.old_passwords ?
3017+              Item_func_old_password::alloc(thd, $3.str, $3.length) :
3018+              Item_func_password::alloc(thd, $3.str, $3.length) :
3019               $3.str;
3020             if ($$ == NULL)
3021               MYSQL_YYABORT;
3022           }
3023         | OLD_PASSWORD '(' TEXT_STRING ')'
3024           {
3025-            $$= $3.length ? Item_func_old_password::alloc(YYTHD, $3.str,
3026+            $$= $3.length ? Item_func_old_password::alloc(thd, $3.str,
3027                                                           $3.length) :
3028               $3.str;
3029             if ($$ == NULL)
3030@@ -12393,19 +12328,19 @@
3031         | DEFAULT { $$=0; }
3032         | ON
3033           {
3034-            $$=new (YYTHD->mem_root) Item_string("ON",  2, system_charset_info);
3035+            $$=new (thd->mem_root) Item_string("ON",  2, system_charset_info);
3036             if ($$ == NULL)
3037               MYSQL_YYABORT;
3038           }
3039         | ALL
3040           {
3041-            $$=new (YYTHD->mem_root) Item_string("ALL", 3, system_charset_info);
3042+            $$=new (thd->mem_root) Item_string("ALL", 3, system_charset_info);
3043             if ($$ == NULL)
3044               MYSQL_YYABORT;
3045           }
3046         | BINARY
3047           {
3048-            $$=new (YYTHD->mem_root) Item_string("binary", 6, system_charset_info);
3049+            $$=new (thd->mem_root) Item_string("binary", 6, system_charset_info);
3050             if ($$ == NULL)
3051               MYSQL_YYABORT;
3052           }
3053@@ -12443,7 +12378,7 @@
3054           table_ident opt_table_alias lock_option
3055           {
3056             thr_lock_type lock_type= (thr_lock_type) $3;
3057-            if (!Select->add_table_to_list(YYTHD, $1, $2, 0, lock_type))
3058+            if (!Select->add_table_to_list(thd, $1, $2, 0, lock_type))
3059               MYSQL_YYABORT;
3060             /* If table is to be write locked, protect from a impending GRL. */
3061             if (lock_type >= TL_WRITE_ALLOW_WRITE)
3062@@ -12514,7 +12449,7 @@
3063             lex->expr_allows_subselect= FALSE;
3064             lex->sql_command = SQLCOM_HA_READ;
3065             lex->ha_rkey_mode= HA_READ_KEY_EXACT; /* Avoid purify warnings */
3066-            Item *one= new (YYTHD->mem_root) Item_int((int32) 1);
3067+            Item *one= new (thd->mem_root) Item_int((int32) 1);
3068             if (one == NULL)
3069               MYSQL_YYABORT;
3070             lex->current_select->select_limit= one;
3071@@ -12836,10 +12771,10 @@
3072             $$=$1; $1->password=$4;
3073             if ($4.length)
3074             {
3075-              if (YYTHD->variables.old_passwords)
3076+              if (thd->variables.old_passwords)
3077               {
3078                 char *buff=
3079-                  (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
3080+                  (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH_323+1);
3081                 if (buff == NULL)
3082                   MYSQL_YYABORT;
3083                 my_make_scrambled_password_323(buff, $4.str, $4.length);
3084@@ -12849,7 +12784,7 @@
3085               else
3086               {
3087                 char *buff=
3088-                  (char *) YYTHD->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
3089+                  (char *) thd->alloc(SCRAMBLED_PASSWORD_CHAR_LENGTH+1);
3090                 if (buff == NULL)
3091                   MYSQL_YYABORT;
3092                 my_make_scrambled_password(buff, $4.str, $4.length);
3093@@ -12881,7 +12816,7 @@
3094 column_list_id:
3095           ident
3096           {
3097-            String *new_str = new (YYTHD->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
3098+            String *new_str = new (thd->mem_root) String((const char*) $1.str,$1.length,system_charset_info);
3099             if (new_str == NULL)
3100               MYSQL_YYABORT;
3101             List_iterator <LEX_COLUMN> iter(Lex->columns);
3102@@ -12981,14 +12916,14 @@
3103
3104 opt_chain:
3105           /* empty */
3106-          { $$= (YYTHD->variables.completion_type == 1); }
3107+          { $$= (thd->variables.completion_type == 1); }
3108         | AND_SYM NO_SYM CHAIN_SYM { $$=0; }
3109         | AND_SYM CHAIN_SYM        { $$=1; }
3110         ;
3111
3112 opt_release:
3113           /* empty */
3114-          { $$= (YYTHD->variables.completion_type == 2); }
3115+          { $$= (thd->variables.completion_type == 2); }
3116         | RELEASE_SYM        { $$=1; }
3117         | NO_SYM RELEASE_SYM { $$=0; }
3118 ;
3119@@ -13102,7 +13037,6 @@
3120
3121 union_order_or_limit:
3122           {
3123-            THD *thd= YYTHD;
3124             LEX *lex= thd->lex;
3125             DBUG_ASSERT(lex->current_select->linkage != GLOBAL_OPTIONS_TYPE);
3126             SELECT_LEX *sel= lex->current_select;
3127@@ -13118,7 +13052,6 @@
3128           }
3129           order_or_limit
3130           {
3131-            THD *thd= YYTHD;
3132             thd->lex->current_select->no_table_names_allowed= 0;
3133             thd->where= "";
3134           }
3135@@ -13255,14 +13188,14 @@
3136               from older master servers (i.e. to create non-suid trigger in this
3137               case).
3138             */
3139-            YYTHD->lex->definer= 0;
3140+            thd->lex->definer= 0;
3141           }
3142         ;
3143
3144 definer:
3145           DEFINER_SYM EQ user
3146           {
3147-            YYTHD->lex->definer= get_current_user(YYTHD, $3);
3148+            thd->lex->definer= get_current_user(thd, $3);
3149           }
3150         ;
3151
3152@@ -13307,7 +13240,6 @@
3153 view_tail:
3154           view_suid VIEW_SYM table_ident
3155           {
3156-            THD *thd= YYTHD;
3157             LEX *lex= thd->lex;
3158             lex->sql_command= SQLCOM_CREATE_VIEW;
3159             /* first table in list is target VIEW name */
3160@@ -13347,7 +13279,6 @@
3161           }
3162           view_select_aux view_check_option
3163           {
3164-            THD *thd= YYTHD;
3165             LEX *lex= Lex;
3166             uint len= YYLIP->get_cpp_ptr() - lex->create_view_select.str;
3167             void *create_view_select= thd->memdup(lex->create_view_select.str, len);
3168@@ -13403,7 +13334,6 @@
3169           EACH_SYM
3170           ROW_SYM
3171           { /* $15 */
3172-            THD *thd= YYTHD;
3173             LEX *lex= thd->lex;
3174             Lex_input_stream *lip= YYLIP;
3175             sp_head *sp;
3176@@ -13437,8 +13367,8 @@
3177             sp_head *sp= lex->sphead;
3178
3179             lex->sql_command= SQLCOM_CREATE_TRIGGER;
3180-            sp->set_stmt_end(YYTHD);
3181-            sp->restore_thd_mem_root(YYTHD);
3182+            sp->set_stmt_end(thd);
3183+            sp->restore_thd_mem_root(thd);
3184
3185             if (sp->is_not_allowed_in_function("trigger"))
3186               MYSQL_YYABORT;
3187@@ -13448,7 +13378,7 @@
3188               sp_proc_stmt alternatives are not saving/restoring LEX, so
3189               lex->query_tables can be wiped out.
3190             */
3191-            if (!lex->select_lex.add_table_to_list(YYTHD, $9,
3192+            if (!lex->select_lex.add_table_to_list(thd, $9,
3193                                                    (LEX_STRING*) 0,
3194                                                    TL_OPTION_UPDATING,
3195                                                    TL_IGNORE))
3196@@ -13466,7 +13396,6 @@
3197           AGGREGATE_SYM remember_name FUNCTION_SYM ident
3198           RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
3199           {
3200-            THD *thd= YYTHD;
3201             LEX *lex= thd->lex;
3202             if (is_native_function(thd, & $4))
3203             {
3204@@ -13484,7 +13413,6 @@
3205         | remember_name FUNCTION_SYM ident
3206           RETURNS_SYM udf_type SONAME_SYM TEXT_STRING_sys
3207           {
3208-            THD *thd= YYTHD;
3209             LEX *lex= thd->lex;
3210             if (is_native_function(thd, & $3))
3211             {
3212@@ -13507,7 +13435,6 @@
3213           sp_name /* $3 */
3214           '(' /* $4 */
3215           { /* $5 */
3216-            THD *thd= YYTHD;
3217             LEX *lex= thd->lex;
3218             Lex_input_stream *lip= YYLIP;
3219             sp_head *sp;
3220@@ -13565,7 +13492,7 @@
3221               MYSQL_YYABORT;
3222             }
3223
3224-            if (sp->fill_field_definition(YYTHD, lex,
3225+            if (sp->fill_field_definition(thd, lex,
3226                                           (enum enum_field_types) $11,
3227                                           &sp->m_return_field_def))
3228               MYSQL_YYABORT;
3229@@ -13574,7 +13501,6 @@
3230           }
3231           sp_c_chistics /* $13 */
3232           { /* $14 */
3233-            THD *thd= YYTHD;
3234             LEX *lex= thd->lex;
3235             Lex_input_stream *lip= YYLIP;
3236
3237@@ -13583,7 +13509,6 @@
3238           }
3239           sp_proc_stmt /* $15 */
3240           {
3241-            THD *thd= YYTHD;
3242             LEX *lex= thd->lex;
3243             sp_head *sp= lex->sphead;
3244
3245@@ -13654,10 +13579,10 @@
3246             sp= new sp_head();
3247             if (sp == NULL)
3248               MYSQL_YYABORT;
3249-            sp->reset_thd_mem_root(YYTHD);
3250+            sp->reset_thd_mem_root(thd);
3251             sp->init(lex);
3252             sp->m_type= TYPE_ENUM_PROCEDURE;
3253-            sp->init_sp_name(YYTHD, $3);
3254+            sp->init_sp_name(thd, $3);
3255
3256             lex->sphead= sp;
3257           }
3258@@ -13672,7 +13597,6 @@
3259           sp_pdparam_list
3260           ')'
3261           {
3262-            THD *thd= YYTHD;
3263             LEX *lex= thd->lex;
3264
3265             lex->sphead->m_param_end= YYLIP->get_cpp_tok_start();
3266@@ -13680,7 +13604,6 @@
3267           }
3268           sp_c_chistics
3269           {
3270-            THD *thd= YYTHD;
3271             LEX *lex= thd->lex;
3272
3273             lex->sphead->m_chistics= &lex->sp_chistics;
3274@@ -13691,9 +13614,9 @@
3275             LEX *lex= Lex;
3276             sp_head *sp= lex->sphead;
3277
3278-            sp->set_stmt_end(YYTHD);
3279+            sp->set_stmt_end(thd);
3280             lex->sql_command= SQLCOM_CREATE_PROCEDURE;
3281-            sp->restore_thd_mem_root(YYTHD);
3282+            sp->restore_thd_mem_root(thd);
3283           }
3284         ;
3285
3286@@ -13730,21 +13653,21 @@
3287           text_string
3288           {
3289             MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE);
3290-            if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
3291+            if (!(Lex->xid=(XID *)thd->alloc(sizeof(XID))))
3292               MYSQL_YYABORT;
3293             Lex->xid->set(1L, $1->ptr(), $1->length(), 0, 0);
3294           }
3295           | text_string ',' text_string
3296           {
3297             MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
3298-            if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
3299+            if (!(Lex->xid=(XID *)thd->alloc(sizeof(XID))))
3300               MYSQL_YYABORT;
3301             Lex->xid->set(1L, $1->ptr(), $1->length(), $3->ptr(), $3->length());
3302           }
3303           | text_string ',' text_string ',' ulong_num
3304           {
3305             MYSQL_YYABORT_UNLESS($1->length() <= MAXGTRIDSIZE && $3->length() <= MAXBQUALSIZE);
3306-            if (!(Lex->xid=(XID *)YYTHD->alloc(sizeof(XID))))
3307+            if (!(Lex->xid=(XID *)thd->alloc(sizeof(XID))))
3308               MYSQL_YYABORT;
3309             Lex->xid->set($5, $1->ptr(), $1->length(), $3->ptr(), $3->length());
3310           }
3311