From c44b2e66a9f82595a5f8e1ddd55ae59a7e8566a7 Mon Sep 17 00:00:00 2001
From: Alexander Barkov <bar@mariadb.org>
Date: Fri, 20 May 2016 20:05:03 +0400
Subject: [PATCH] A derived_query_specification clean-up (to simplify further
 MDEV-8909 changes)

1. Better semantic readability:
- Moving get_select_lex_derived inside select_derived_init
  and decomposing it into get_select_lex and
  $1->init_nested_join(lex->thd)
-  Moving DBUG_ASSERT($1 == Lex->current_select) inside
   select_derived_init

Now init_nested_join() and end_nested_join() reside inside
the same rule select_derived_init.
(It's very likely that they can be further removed,
 as there are no any joins in this rule).

3. Better grammar readability:
  Moving SELECT_SYM from select_derived_init to derived_query_specification.
  It's easier to read a rule when it starts from a terminal symbol.
---
 sql/sql_yacc.yy | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index c6815058f2a..f53a7ee69f7 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -11194,12 +11194,7 @@ select_derived:
     SELECT * FROM (SELECT * FROM t1);
 */
 derived_query_specification:
-          get_select_lex_derived select_derived_init
-          {
-            // Now we have the same st_select_lex that we had in the beginning
-            DBUG_ASSERT($1 == Lex->current_select);
-          }
-          select_derived2
+          SELECT_SYM select_derived_init select_derived2
           {
             LEX *lex= Lex;
             SELECT_LEX *sel= lex->current_select;
@@ -11252,9 +11247,11 @@ get_select_lex_derived:
        ;
 
 select_derived_init:
-          SELECT_SYM
+          get_select_lex
           {
             LEX *lex= Lex;
+            if ($1->init_nested_join(lex->thd))
+              MYSQL_YYABORT;
 
             if (! lex->parsing_options.allows_derived)
             {
@@ -11274,6 +11271,8 @@ select_derived_init:
             $$= embedding &&
                 !embedding->nested_join->join_list.elements;
             /* return true if we are deeply nested */
+            // Now we have the same st_select_lex that we had in the beginning
+            DBUG_ASSERT($1 == Lex->current_select);
           }
         ;