From b9d4c69135f8f11135793e22e554c3a9f6517983 Mon Sep 17 00:00:00 2001
From: Sergey Petrunya <psergey@askmonty.org>
Date: Tue, 22 Mar 2011 13:17:14 +0300
Subject: [PATCH] MWL#90: Address review feedback part #6: more clearer code in
 next_linear_tab() function

---
 sql/sql_select.cc | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index b5d6cc1acb..33690deffa 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -6389,21 +6389,31 @@ JOIN_TAB *first_linear_tab(JOIN *join, bool after_const_tables)
 JOIN_TAB *next_linear_tab(JOIN* join, JOIN_TAB* tab, bool include_bush_roots)
 {
   if (include_bush_roots && tab->bush_children)
+  {
+    /* This JOIN_TAB is a SJM nest; Start from first table in nest */
     return tab->bush_children->start;
+  }
 
   DBUG_ASSERT(!tab->last_leaf_in_bush || tab->bush_root_tab);
-  if (tab->last_leaf_in_bush)
-    tab= tab->bush_root_tab;
 
-  if (tab->bush_root_tab)
-    return ++tab;
+  if (tab->bush_root_tab)       /* Are we inside an SJM nest */
+  {
+    /* Inside SJM nest */
+    if (!tab->last_leaf_in_bush)
+      return tab+1;              /* Return next in nest */
+    /* Continue from the sjm on the top level */
+    tab= tab->bush_root_tab;
+  }
 
+  /* If no more JOIN_TAB's on the top level */
   if (++tab == join->join_tab + join->top_jtrange_tables)
     return NULL;
 
   if (!include_bush_roots && tab->bush_children)
+  {
+    /* This JOIN_TAB is a SJM nest; Start from first table in nest */
     tab= tab->bush_children->start;
-
+  }
   return tab;
 }
 
-- 
2.30.9