]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Do not flatten subqueries where the subquery has a LIMIT and the outer
authordrh <drh@noemail.net>
Tue, 26 Aug 2008 12:56:14 +0000 (12:56 +0000)
committerdrh <drh@noemail.net>
Tue, 26 Aug 2008 12:56:14 +0000 (12:56 +0000)
query has a WHERE clause.  Ticket #3334. (CVS 5613)

FossilOrigin-Name: 4995a1d1c9530be9ce647d338169620cd95a72eb

manifest
manifest.uuid
src/select.c
test/tkt3334.test [new file with mode: 0644]

index cc5f7f8f82f643f336defc7d89a0254e3204fd95..36e0c81690a649da321d053a574685b3a573c262 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sthe\sSQLITE_OPEN_FULLMUTEX\sdefinition\sto\ssqlite3.h.\s\sIt\scurrently\sis\snot\nvalid\sfor\sanything.\s\sThis\sis\smerely\sto\sreserve\sthe\snumber.\s(CVS\s5612)
-D 2008-08-25T21:23:02
+C Do\snot\sflatten\ssubqueries\swhere\sthe\ssubquery\shas\sa\sLIMIT\sand\sthe\souter\nquery\shas\sa\sWHERE\sclause.\s\sTicket\s#3334.\s(CVS\s5613)
+D 2008-08-26T12:56:14
 F Makefile.arm-wince-mingw32ce-gcc fcd5e9cd67fe88836360bb4f9ef4cb7f8e2fb5a0
 F Makefile.in 689e14735f862a5553bceef206d8c13e29504e44
 F Makefile.linux-gcc d53183f4aa6a9192d249731c90dbdffbd2c68654
@@ -145,7 +145,7 @@ F src/prepare.c c197041e0c4770672cda75e6bfe10242f885e510
 F src/printf.c 785f87120589c1db672e37c6eb1087c456e6f84d
 F src/random.c 5c754319d38abdd6acd74601ee0105504adc508a
 F src/resolve.c 74725e61c9eefb597a203631d921efd9005b7a88
-F src/select.c 1042eafb5c703ed4fc80ab3c3cbdfdb74fbcf2b4
+F src/select.c 8187927315ee592a8ee94d753b8a1a3625c33523
 F src/shell.c d83b578a8ccdd3e0e7fef4388a0887ce9f810967
 F src/sqlite.h.in c0e84a2d6e9f3263599174ff7261ba6daf730b4f
 F src/sqlite3ext.h 1e3887c9bd3ae66cb599e922824b04cd0d0f2c3e
@@ -551,6 +551,7 @@ F test/tkt3121.test 536df66a02838c26a12fe98639354ca1290ca68b
 F test/tkt3201.test 607d433ad2c1f6a8cb1af55aaca427f63c83191b
 F test/tkt3292.test 962465a0984a3b8c757efe59c2c59144871ee1dd
 F test/tkt3298.test a735582095ca2e90a0c1391c7e781a90de6c1f34
+F test/tkt3334.test ea13a53cb176e90571a76c86605b14a09efe366d
 F test/tokenize.test ce430a7aed48fc98301611429595883fdfcab5d7
 F test/trace.test 951cd0f5f571e7f36bf7bfe04be70f90fb16fb00
 F test/trans.test 2fd24cd7aa0b879d49a224cbd647d698f1e7ac5c
@@ -623,7 +624,7 @@ F tool/speedtest16.c c8a9c793df96db7e4933f0852abb7a03d48f2e81
 F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff
 F tool/speedtest8.c 1dbced29de5f59ba2ebf877edcadf171540374d1
 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e
-P aa92a1bec3d6bbcc59680ba13fed51ada9249d4c
-R bd25d74a7fe1cecd03149819abf1e756
+P 3b6ffb4492b6c43897692c49dcccbfb55963a46c
+R 0ea5c2219f101d8498478c6075393d46
 U drh
-Z 979b697b6c0ee9331c1f53da4ee2f6c2
+Z 5e5db29fb87e2cab5c4bf06a60b36872
index c7798a4d0d9fc9def1d2c4e87cb995dec0026c05..f8ab247a68aaac6e9bf012e2a16d3ff576739358 100644 (file)
@@ -1 +1 @@
-3b6ffb4492b6c43897692c49dcccbfb55963a46c
\ No newline at end of file
+4995a1d1c9530be9ce647d338169620cd95a72eb
\ No newline at end of file
index 8cd5325b3ad035275387b66211eaaa051554bce3..90611aa992fe2b73247d53479e245a40cc4da770 100644 (file)
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.470 2008/08/25 17:23:29 drh Exp $
+** $Id: select.c,v 1.471 2008/08/26 12:56:14 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -2525,6 +2525,9 @@ static void substSelect(
 **        ORDER by clause of the parent must be simple references to 
 **        columns of the sub-query.
 **
+**  (19)  The subquery does not use LIMIT or the outer query does not
+**        have a WHERE clause.
+**
 ** In this routine, the "p" parameter is a pointer to the outer query.
 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
@@ -2590,6 +2593,7 @@ static int flattenSubquery(
      return 0;                                           /* Restriction (11) */
   }
   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
+  if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
 
   /* OBSOLETE COMMENT 1:
   ** Restriction 3:  If the subquery is a join, make sure the subquery is 
diff --git a/test/tkt3334.test b/test/tkt3334.test
new file mode 100644 (file)
index 0000000..5473ab4
--- /dev/null
@@ -0,0 +1,84 @@
+# 2008 August 26
+#
+# The author disclaims copyright to this source code.  In place of
+# a legal notice, here is a blessing:
+#
+#    May you do good and not evil.
+#    May you find forgiveness for yourself and forgive others.
+#    May you share freely, never taking more than you give.
+#
+#***********************************************************************
+#
+# This file implements regression tests for SQLite library. 
+# Specifically, it tests that bug #3334 has been fixed by the
+# addition of restriction (19) to the subquery flattener optimization.
+#
+# $Id: tkt3334.test,v 1.1 2008/08/26 12:56:14 drh Exp $
+
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+
+do_test tkt3334-1.0 {
+  execsql {
+    CREATE TABLE t1(a,b);
+    INSERT INTO t1 VALUES(1,934);
+    INSERT INTO t1 VALUES(2,221);
+    INSERT INTO t1 VALUES(1,372);
+    INSERT INTO t1 VALUES(3,552);
+    INSERT INTO t1 VALUES(1,719);
+    INSERT INTO t1 VALUES(4,102);
+    SELECT * FROM t1 ORDER BY b;
+  }
+} {4 102 2 221 1 372 3 552 1 719 1 934}
+
+do_test tkt3334-1.1 {
+  execsql {
+    SELECT a FROM (SELECT a FROM t1 ORDER BY b LIMIT 2) WHERE a=1;
+  }
+} {}
+do_test tkt3334-1.2 {
+  execsql {
+    SELECT count(*) FROM (SELECT a FROM t1 ORDER BY b LIMIT 2) WHERE a=1;
+  }
+} {0}
+do_test tkt3334-1.3 {
+  execsql {
+    SELECT a FROM (SELECT a FROM t1 ORDER BY b LIMIT 3) WHERE a=1;
+  }
+} {1}
+do_test tkt3334-1.4 {
+  execsql {
+    SELECT count(*) FROM (SELECT a FROM t1 ORDER BY b LIMIT 3) WHERE a=1;
+  }
+} {1}
+do_test tkt3334-1.5 {
+  execsql {
+    SELECT a FROM (SELECT a FROM t1 ORDER BY b LIMIT 99) WHERE a=1;
+  }
+} {1 1 1}
+do_test tkt3334-1.6 {
+  execsql {
+    SELECT count(*) FROM (SELECT a FROM t1 ORDER BY b LIMIT 99) WHERE a=1;
+  }
+} {3}
+do_test tkt3334-1.7 {
+  execsql {
+    SELECT a FROM (SELECT a FROM t1 ORDER BY b) WHERE a=1;
+  }
+} {1 1 1}
+do_test tkt3334-1.8 {
+  execsql {
+    SELECT count(*) FROM (SELECT a FROM t1 ORDER BY b) WHERE a=1;
+  }
+} {3}
+do_test tkt3334-1.9 {
+  execsql {
+    SELECT a FROM (SELECT a FROM t1) WHERE a=1;
+  }
+} {1 1 1}
+do_test tkt3334-1.10 {
+  execsql {
+    SELECT count(*) FROM (SELECT a FROM t1) WHERE a=1;
+  }
+} {3}