Blob Blame History Raw
From 0990c415f65d2556a5e4122cbe5727d500411aeb Mon Sep 17 00:00:00 2001
From: drh <drh@noemail.net>
Date: Sun, 23 Feb 2020 17:34:45 +0000
Subject: [PATCH] Fix a problem with ALTER TABLE for views that have a nested
 FROM clause. Ticket [f50af3e8a565776b].

FossilOrigin-Name: c431b3fd8fd0f6a6974bba3e9366b0430ec003d570e7ce70ceefbcff5fe4b6fa
---
 src/select.c       |  2 +-
 test/altertab.test | 17 +++++++++++++++++
 4 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/src/select.c b/src/select.c
index c60ff2700..fe0229ca8 100644
--- a/src/select.c
+++ b/src/select.c
@@ -5046,7 +5046,7 @@ static int selectExpander(Walker *pWalker, Select *p){
             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
             sqlite3TokenInit(&sColname, zColname);
             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
-            if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
+            if( pNew && (p->selFlags & SF_NestedFrom)!=0 && !IN_RENAME_OBJECT){
               struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
               if( pSub ){
                 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);

diff --git a/test/altertab.test b/test/altertab.test
index a3642070e..520502c3d 100644
--- a/test/altertab.test
+++ b/test/altertab.test
@@ -578,5 +578,21 @@ ifcapable fts3 {
     SELECT * FROM z1_segments;
   }
 }
+# 2020-02-23 ticket f50af3e8a565776b
+reset_db
+do_execsql_test 19.100 {
+  CREATE TABLE t1(x);
+  CREATE VIEW t2 AS SELECT 1 FROM t1, (t1 AS a0, t1);
+  ALTER TABLE t1 RENAME TO t3;
+  SELECT sql FROM sqlite_master;
+} {{CREATE TABLE "t3"(x)} {CREATE VIEW t2 AS SELECT 1 FROM "t3", ("t3" AS a0, "t3")}}
+do_execsql_test 19.110 {
+  INSERT INTO t3(x) VALUES(123);
+  SELECT * FROM t2;
+} {1}
+do_execsql_test 19.120 {
+  INSERT INTO t3(x) VALUES('xyz');
+  SELECT * FROM t2;
+} {1 1 1 1 1 1 1 1}

 finish_test
--
2.37.3