@@ -2170,6 +2170,20 @@ bool st_select_lex_unit::exec()
21702170 DBUG_ENTER (" st_select_lex_unit::exec" );
21712171 bool was_executed= executed;
21722172 int error;
2173+ bool limit_rows_was_activated= false ;
2174+
2175+ // Restore current_select on scope exit
2176+ SCOPE_EXIT ([this , lex_select_save]() {
2177+ thd->lex ->current_select = lex_select_save;
2178+ });
2179+
2180+ // Handle cleanup on scope exit
2181+ SCOPE_EXIT ([this , &limit_rows_was_activated, &examined_rows]() {
2182+ if (limit_rows_was_activated)
2183+ thd->lex ->activate_limit_rows_examined ();
2184+ if (!saved_error)
2185+ thd->inc_examined_row_count (examined_rows);
2186+ });
21732187
21742188 if (executed && !uncacheable && !describe)
21752189 DBUG_RETURN (FALSE );
@@ -2289,14 +2303,12 @@ bool st_select_lex_unit::exec()
22892303 thd->set_examined_row_count (0 );
22902304 if (union_result->flush ())
22912305 {
2292- thd->lex ->current_select = lex_select_save;
22932306 DBUG_RETURN (1 );
22942307 }
22952308 }
22962309 }
22972310 if (unlikely (saved_error))
22982311 {
2299- thd->lex ->current_select = lex_select_save;
23002312 DBUG_RETURN (saved_error);
23012313 }
23022314 if (fake_select_lex != NULL )
@@ -2340,21 +2352,18 @@ bool st_select_lex_unit::exec()
23402352
23412353 DBUG_EXECUTE_IF (" show_explain_probe_union_read" ,
23422354 dbug_serve_apcs (thd, 1 ););
2343- bool limit_rows_was_activated;
2344- {
2345- List<Item_func_match> empty_list;
2346- empty_list.empty ();
2347- /*
2348- Deactivate LIMIT ROWS EXAMINED to avoid producing potentially incomplete
2349- result of the UNION due to exceeding of the limit.
2350- */
2351- limit_rows_was_activated= thd->lex ->deactivate_limit_rows_examined ();
2355+ /*
2356+ Temporarily deactivate LIMIT ROWS EXAMINED to avoid producing potentially
2357+ incomplete result of the UNION due to exceeding of the limit. It will be
2358+ re-activated upon the function exit (see SCOPE_EXIT macro above)
2359+ */
2360+ limit_rows_was_activated= thd->lex ->deactivate_limit_rows_examined ();
23522361
2353- // Check if EOM
2354- if (fake_select_lex != NULL && likely (!thd->is_fatal_error ))
2355- {
2356- /* Send result to 'result' */
2357- saved_error= true ;
2362+ // Check if EOM
2363+ if (fake_select_lex != NULL && likely (!thd->is_fatal_error ))
2364+ {
2365+ /* Send result to 'result' */
2366+ saved_error= true ;
23582367
23592368 set_limit (global_parameters ());
23602369 init_prepare_fake_select_lex (thd, first_execution);
@@ -2375,7 +2384,7 @@ bool st_select_lex_unit::exec()
23752384 result))))
23762385 {
23772386 fake_select_lex->table_list .empty ();
2378- goto err ;
2387+ DBUG_RETURN ( TRUE ) ;
23792388 }
23802389 fake_select_lex->join ->no_const_tables = TRUE ;
23812390
@@ -2445,13 +2454,6 @@ bool st_select_lex_unit::exec()
24452454 indexes efficiently
24462455 */
24472456 }
2448- }
2449- thd->lex ->current_select = lex_select_save;
2450- err:
2451- if (limit_rows_was_activated)
2452- thd->lex ->activate_limit_rows_examined ();
2453- if (likely (!saved_error))
2454- thd->inc_examined_row_count (examined_rows);
24552457 DBUG_RETURN (saved_error);
24562458}
24572459
0 commit comments