lede-packages-rs

git clone git://archive.git.mtrnord.blog/MTRNord/lede-packages-rs.git
Log | Files | Refs | README | LICENSE

120-bison-compat.patch (121850B)


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