diff --git a/sql/item.cc b/sql/item.cc
index 1624958..ef89bce 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -9731,7 +9731,7 @@ table_map Item_ref_null_helper::used_tables() const
 #ifndef DBUG_OFF
 
 /* Debugger help function */
-static char dbug_item_print_buf[256];
+static char dbug_item_print_buf[25600];
 
 const char *dbug_print_item(Item *item)
 {
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 0372a06..155c210 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -3347,7 +3347,8 @@ void JOIN::cleanup_item_list(List<Item> &items) const
 
 static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
 				      TABLE *table,
-				      const key_map *keys,ha_rows limit)
+				      const key_map *keys,ha_rows limit,
+                                      bool in_outer_join)
 {
   int error;
   DBUG_ENTER("get_quick_record_count");
@@ -3364,6 +3365,8 @@ static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
       DBUG_RETURN(select->quick->records);
     if (error == -1)
     {
+      if (in_outer_join)
+        DBUG_RETURN(1);
       table->reginfo.impossible_range=1;
       DBUG_RETURN(0);
     }
@@ -3955,36 +3958,58 @@ struct SARGABLE_PARAM
     */
     if ((!s->const_keys.is_clear_all() ||
 	 !bitmap_is_clear_all(&s->table->cond_set)) &&              // (1)
-        (!s->table->pos_in_table_list->embedding ||                 // (2)
+        /*(!s->table->pos_in_table_list->embedding ||                 // (2)
          (s->table->pos_in_table_list->embedding &&                 // (3)
-          s->table->pos_in_table_list->embedding->sj_on_expr)) &&   // (3)
+          s->table->pos_in_table_list->embedding->sj_on_expr)) &&   // (3)*/
         !s->table->is_filled_at_execution() &&                      // (4)
         !(s->table->pos_in_table_list->derived &&                   // (5)
           s->table->pos_in_table_list->is_materialized_derived()))  // (5)
     {
+      //////////////////////////////////////////////////
+      bool should_skip= 
+         (s->table->pos_in_table_list->embedding &&
+          !s->table->pos_in_table_list->embedding->sj_on_expr);
+
+      //////////////////////////////////////////////////
       bool impossible_range= FALSE;
       ha_rows records= HA_POS_ERROR;
       SQL_SELECT *select= 0;
+      bool in_nested_outer_join= false;
       if (!s->const_keys.is_clear_all())
       {
+        Item **cond;
+        if (*s->on_expr_ref)
+          cond= s->on_expr_ref;
+        else
+        {
+          if (s->table->pos_in_table_list->embedding &&
+              s->table->pos_in_table_list->embedding->on_expr)
+          {
+            in_nested_outer_join= true;
+            cond= &s->table->pos_in_table_list->embedding->on_expr;
+          }
+          else
+            cond= &join->conds;
+        }
+
+        /*if (!should_skip)*/ {
         select= make_select(s->table, found_const_table_map,
 			    found_const_table_map,
-			    *s->on_expr_ref ? *s->on_expr_ref : join->conds,
+                            *cond,
 			    1, &error);
         if (!select)
           goto error;
         records= get_quick_record_count(join->thd, select, s->table,
-				        &s->const_keys, join->row_limit);
-        /* Range analyzer could modify the condition. */
-        if (*s->on_expr_ref)
-          *s->on_expr_ref= select->cond;
-        else
-          join->conds= select->cond;
+				        &s->const_keys, join->row_limit,
+                                        should_skip);
+        /* Range analyzer could modify (but not remove) the condition. */
+        *cond= select->cond;
 
         s->quick=select->quick;
         s->needed_reg=select->needed_reg;
         select->quick=0;
         impossible_range= records == 0 && s->table->reginfo.impossible_range;
+        } // should_skip
       }
       if (!impossible_range)
       {
@@ -3998,7 +4023,7 @@ struct SARGABLE_PARAM
           records= 0;
         }
       }
-      if (impossible_range)
+      if (impossible_range && !in_nested_outer_join)
       {
 	/*
 	  Impossible WHERE or ON expression
@@ -4025,6 +4050,7 @@ struct SARGABLE_PARAM
       }
       if (select)
         delete select;
+    ///} // should_skip
     }
 
   }
