From 2bf90f1e64a3ef72b2d5deda424b2277fad42cfd Mon Sep 17 00:00:00 2001 From: drh Date: Tue, 9 Dec 2008 13:04:29 +0000 Subject: [PATCH] A subquery in the result set of a SELECT should be able to look into outer queries to find variable references. But a subquery in the FROM clause should not be able to do this. Ticket #3530. (CVS 5998) FossilOrigin-Name: be03fa0543044dc46952695e963d9b82afed892b --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/resolve.c | 4 ++-- src/sqliteInt.h | 8 ++++---- test/tkt3346.test | 20 +++++++++++++++++++- 5 files changed, 35 insertions(+), 17 deletions(-) diff --git a/manifest b/manifest index 994fb5775b..7714ff0c4b 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Removed\ssome\sharmless\scompiler\swarnings\sand\sconverted\ssome\s"double"\sops\sto\s"int"\sin\sdate.c.\s(CVS\s5997) -D 2008-12-09T04:59:00 +C A\ssubquery\sin\sthe\sresult\sset\sof\sa\sSELECT\sshould\sbe\sable\sto\slook\sinto\souter\nqueries\sto\sfind\svariable\sreferences.\s\sBut\sa\ssubquery\sin\sthe\sFROM\sclause\nshould\snot\sbe\sable\sto\sdo\sthis.\s\sTicket\s#3530.\s(CVS\s5998) +D 2008-12-09T13:04:29 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0 F Makefile.in f7e4c81c347b04f7b0f1c1b081a168645d7b8af7 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654 @@ -149,13 +149,13 @@ F src/pragma.c 539e28c90e782fa909a3b3a6849d18a9eb11a548 F src/prepare.c fcadb25d2ad722d87103517333c825b56b79a770 F src/printf.c e29d9475c63e1dbfae005b98da3a60e07b5c1ca5 F src/random.c 67bf41b20f1983a0e481e7abb98d64704ee26884 -F src/resolve.c 4af5391d2b4c1d6c583a6805ac6660181de4545b +F src/resolve.c 6c32f21b5835136591b86cce6a82cde5947027c4 F src/rowset.c 2256fa4a928f750e2f3d6fc733523034beceb1d6 F src/select.c 1d3616e4e48f6c15a0c69bf14bc5423f31624f87 F src/shell.c 484bddeefac9314be18d4a60d401e93848274ee8 F src/sqlite.h.in f13b2f27265b78226240cb0d2e07f165e2110bd9 F src/sqlite3ext.h 1db7d63ab5de4b3e6b83dd03d1a4e64fef6d2a17 -F src/sqliteInt.h 2f21e46629c5d1371d23e326debbc72099e07b8e +F src/sqliteInt.h e61d837e04b03c75bf860f06cbb327224e320fba F src/sqliteLimit.h f435e728c6b620ef7312814d660a81f9356eb5c8 F src/status.c 237b193efae0cf6ac3f0817a208de6c6c6ef6d76 F src/table.c caf335a98a1093a5e8e79557d17053755f9aa474 @@ -572,7 +572,7 @@ F test/tkt3201.test 607d433ad2c1f6a8cb1af55aaca427f63c83191b F test/tkt3292.test 962465a0984a3b8c757efe59c2c59144871ee1dd F test/tkt3298.test a735582095ca2e90a0c1391c7e781a90de6c1f34 F test/tkt3334.test ea13a53cb176e90571a76c86605b14a09efe366d -F test/tkt3346.test 2f9a2be8621a87cbdb6283177dd419c7c46dd2a1 +F test/tkt3346.test 6f405f1e102b616fc9f1ee4934995e9b45ff3934 F test/tkt3357.test b37a51a12ba5e143d6714778276438606f8f9e27 F test/tkt3419.test 1bbf36d7ea03b638c15804251287c2391f5c1f6b F test/tkt3424.test 3171193ce340cff6b7ea81c03b8fa1cbc34ec36e @@ -664,7 +664,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e -P 59ae0020683766993c38e2b76a436d78d3e4bd63 -R 17936af34c3e654d021393afaa888743 -U shane -Z 7b2c61a1e0b4dfcc6d4fa7adb8f20c33 +P 5eb648a0b599c83504b892e2a5cefe6a837d6017 +R 3a3286fc2d40dc730943a62d4d30dc31 +U drh +Z 7b793e8a0830b136a964fa8647b16f4c diff --git a/manifest.uuid b/manifest.uuid index 5715c0f6bf..b877362029 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -5eb648a0b599c83504b892e2a5cefe6a837d6017 \ No newline at end of file +be03fa0543044dc46952695e963d9b82afed892b \ No newline at end of file diff --git a/src/resolve.c b/src/resolve.c index a878b0248e..7a4984cd4d 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -14,7 +14,7 @@ ** resolve all identifiers by associating them with a particular ** table and column. ** -** $Id: resolve.c,v 1.11 2008/11/17 19:18:55 danielk1977 Exp $ +** $Id: resolve.c,v 1.12 2008/12/09 13:04:29 drh Exp $ */ #include "sqliteInt.h" #include @@ -963,7 +963,7 @@ static int resolveSelectStep(Walker *pWalker, Select *p){ if( pItem->pSelect ){ const char *zSavedContext = pParse->zAuthContext; if( pItem->zName ) pParse->zAuthContext = pItem->zName; - sqlite3ResolveSelectNames(pParse, pItem->pSelect, &sNC); + sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC /*&sNC*/); pParse->zAuthContext = zSavedContext; if( pParse->nErr || db->mallocFailed ) return WRC_Abort; } diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 75b2428824..ea558e19a1 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.806 2008/12/09 01:32:03 drh Exp $ +** @(#) $Id: sqliteInt.h,v 1.807 2008/12/09 13:04:29 drh Exp $ */ #ifndef _SQLITEINT_H_ #define _SQLITEINT_H_ @@ -2030,9 +2030,9 @@ int sqlite3WalkSelectFrom(Walker*, Select*); ** Return code from the parse-tree walking primitives and their ** callbacks. */ -#define WRC_Continue 0 -#define WRC_Prune 1 -#define WRC_Abort 2 +#define WRC_Continue 0 /* Continue down into children */ +#define WRC_Prune 1 /* Omit children but continue walking siblings */ +#define WRC_Abort 2 /* Abandon the tree walk */ /* ** Assuming zIn points to the first byte of a UTF-8 character, diff --git a/test/tkt3346.test b/test/tkt3346.test index 46ec07f80e..dac1eed02a 100644 --- a/test/tkt3346.test +++ b/test/tkt3346.test @@ -12,7 +12,7 @@ # This file implements regression tests for SQLite library. The # focus of this file is testing the fix for ticket #3346 # -# $Id: tkt3346.test,v 1.1 2008/09/01 15:52:11 drh Exp $ +# $Id: tkt3346.test,v 1.2 2008/12/09 13:04:29 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl @@ -28,4 +28,22 @@ do_test tkt3346-1.1 { } } {2 bob 0 1 alice 1 3 claire 0} +# Ticket #3530 +# +# As shown by ticket #3346 above (see also ticket #3298) it is important +# that a subquery in the result-set be able to look up through multiple +# FROM levels in order to view tables in the FROM clause at the top level. +# +# But ticket #3530 shows us that a subquery in the FROM clause should not +# be able to look up to higher levels: +# +do_test tkt3346-2.1 { + catchsql { + CREATE TABLE t2(a); + INSERT INTO t2 VALUES(1); + + SELECT * FROM (SELECT * FROM t1 WHERE 1=x.a) AS x; + } +} {1 {no such column: x.a}} + finish_test