From 4decc03b285d4a1c42750d6f29c5f0c3a25cd1c4 Mon Sep 17 00:00:00 2001
From: Dmitry Shulga <dmitry.shulga@mariadb.com>
Date: Thu, 6 May 2021 13:55:57 +0700
Subject: [PATCH] MDEV-16708: Unsupported commands for prepared statements

Fixed the test main.view
---
 mysql-test/main/view.test | 4 ++++
 sql/item.cc               | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/mysql-test/main/view.test b/mysql-test/main/view.test
index 805afedc8ae..73322d97591 100644
--- a/mysql-test/main/view.test
+++ b/mysql-test/main/view.test
@@ -3081,7 +3081,9 @@ create view v1 as select * from t1 order by f2;
 select * from v1;
 explain extended select * from v1;
 select * from v1 order by f1;
+--enable_prepare_warnings
 explain extended select * from v1 order by f1;
+--disable_prepare_warnings
 drop view v1;
 drop table t1;
 
@@ -4421,7 +4423,9 @@ CREATE ALGORITHM=TEMPTABLE VIEW v1 AS SELECT f1 FROM t1;
 CREATE ALGORITHM=MERGE VIEW v2 AS SELECT f1 FROM v1 ORDER BY f1;
 
 SELECT * FROM v2 AS a1, v2 AS a2;
+--enable_prepare_warnings
 EXPLAIN EXTENDED SELECT * FROM v2 AS a1, v2 AS a2;
+--disable_prepare_warnings
 
 DROP VIEW v1, v2;
 DROP TABLE t1;
diff --git a/sql/item.cc b/sql/item.cc
index 3cede11a415..252135f50ad 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -5531,9 +5531,12 @@ Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
   */
   Name_resolution_context *last_checked_context= context;
   Item **ref= (Item **) not_found_item;
+  SELECT_LEX *current_sel= thd->lex->current_select;
   Name_resolution_context *outer_context= 0;
   SELECT_LEX *select= 0;
-  outer_context= context->outer_context;
+
+  if (current_sel->master_unit()->outer_select())
+    outer_context= context->outer_context;
 
   /*
     This assert is to ensure we have an outer contex when *from_field
-- 
2.30.9