From: dan Date: Fri, 13 Aug 2010 16:38:48 +0000 (+0000) Subject: Do not apply the flattening optimization if the sub-query is DISTINCT. Fix for [e4b8a... X-Git-Tag: version-3.7.2~44 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=49ad330d677f613964c53d4ff2b5790dac59d5a4;p=thirdparty%2Fsqlite.git Do not apply the flattening optimization if the sub-query is DISTINCT. Fix for [e4b8a2ba6e]. FossilOrigin-Name: 497aafd8ed6a636a8bd5fa2cad265a20593ee34f --- diff --git a/manifest b/manifest index 4f8410dfe4..f9364ac70a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C If\san\serror\soccurs\sin\sPagerSetPagesize(),\sset\sthe\soutput\svariable\sto\sthe\sunmodified\spage-size\sbefore\sreturning. -D 2010-08-12T16:36:35 +C Do\snot\sapply\sthe\sflattening\soptimization\sif\sthe\ssub-query\sis\sDISTINCT.\sFix\sfor\s[e4b8a2ba6e]. +D 2010-08-13T16:38:49 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f F Makefile.in ec08dc838fd8110fe24c92e5130bcd91cbb1ff2e F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23 @@ -168,7 +168,7 @@ F src/printf.c 8ae5082dd38a1b5456030c3755ec3a392cd51506 F src/random.c cd4a67b3953b88019f8cd4ccd81394a8ddfaba50 F src/resolve.c 1c0f32b64f8e3f555fe1f732f9d6f501a7f05706 F src/rowset.c 69afa95a97c524ba6faf3805e717b5b7ae85a697 -F src/select.c 4b8f481406c7fd2672abf715a2acaed8f9cde046 +F src/select.c 8add6cab889fc02e1492eda8dba462ccf11f51dd F src/shell.c 8517fc1f9c59ae4007e6cc8b9af91ab231ea2056 F src/sqlite.h.in 2d72a6242df41c517e38eec8791abcf5484a36f1 F src/sqlite3ext.h 69dfb8116af51b84a029cddb3b35062354270c89 @@ -585,7 +585,7 @@ F test/select8.test 391de11bdd52339c30580dabbbbe97e3e9a3c79d F test/select9.test 74c0fb2c6eecb0219cbed0cbe3df136f8fbf9343 F test/selectA.test 06d1032fa9009314c95394f2ca2e60d9f7ae8532 F test/selectB.test f305cc6660804cb239aab4e2f26b0e288b59958b -F test/selectC.test 33bb5673a8141df193c6fd56e6de7fea38b8d2ee +F test/selectC.test f9bf1bc4581b5b8158caa6e4e4f682acb379fb25 F test/server1.test f5b790d4c0498179151ca8a7715a65a7802c859c F test/shared.test b9114eaea7e748a3a4c8ff7b9ca806c8f95cef3e F test/shared2.test d6ba4ca1827ea36a1ac23a99e3c36eeac9165450 @@ -843,7 +843,7 @@ F tool/speedtest2.tcl ee2149167303ba8e95af97873c575c3e0fab58ff F tool/speedtest8.c 2902c46588c40b55661e471d7a86e4dd71a18224 F tool/speedtest8inst1.c 293327bc76823f473684d589a8160bde1f52c14e F tool/vdbe-compress.tcl d70ea6d8a19e3571d7ab8c9b75cba86d1173ff0f -P 228c5b16af80c22e97d9d4deb351e0d3f4523f89 -R 639f1bec1c909cc7434c6e41850105f7 +P 02def8f92588b8a45dff3976d1e7f9e3f0359b3b +R 74ad88fe4f30f12e9ec66365eae8f2a6 U dan -Z 132d1fc4e540d643bf195378105a77c8 +Z 55affff7311e423c3bc737f8b0377763 diff --git a/manifest.uuid b/manifest.uuid index a47beca736..ab242a3c7c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -02def8f92588b8a45dff3976d1e7f9e3f0359b3b \ No newline at end of file +497aafd8ed6a636a8bd5fa2cad265a20593ee34f \ No newline at end of file diff --git a/src/select.c b/src/select.c index 4550be6005..17184b675a 100644 --- a/src/select.c +++ b/src/select.c @@ -2495,12 +2495,13 @@ static void substSelect( ** (2) The subquery is not an aggregate or the outer query is not a join. ** ** (3) The subquery is not the right operand of a left outer join -** (Originally ticket #306. Strenghtened by ticket #3300) +** (Originally ticket #306. Strengthened by ticket #3300) ** -** (4) The subquery is not DISTINCT or the outer query is not a join. +** (4) The subquery is not DISTINCT. ** -** (5) The subquery is not DISTINCT or the outer query does not use -** aggregates. +** (**) At one point restrictions (4) and (5) defined a subset of DISTINCT +** sub-queries that were excluded from this optimization. Restriction +** (4) has since been expanded to exclude all DISTINCT subqueries. ** ** (6) The subquery does not use aggregates or the outer query is not ** DISTINCT. @@ -2520,9 +2521,9 @@ static void substSelect( ** (**) Not implemented. Subsumed into restriction (3). Was previously ** a separate restriction deriving from ticket #350. ** -** (13) The subquery and outer query do not both use LIMIT +** (13) The subquery and outer query do not both use LIMIT. ** -** (14) The subquery does not use OFFSET +** (14) The subquery does not use OFFSET. ** ** (15) The outer query is not part of a compound select or the ** subquery does not have a LIMIT clause. @@ -2613,9 +2614,9 @@ static int flattenSubquery( return 0; /* Restriction (15) */ } if( pSubSrc->nSrc==0 ) return 0; /* Restriction (7) */ - if( ((pSub->selFlags & SF_Distinct)!=0 || pSub->pLimit) - && (pSrc->nSrc>1 || isAgg) ){ /* Restrictions (4)(5)(8)(9) */ - return 0; + if( pSub->selFlags & SF_Distinct ) return 0; /* Restriction (5) */ + if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){ + return 0; /* Restrictions (8)(9) */ } if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){ return 0; /* Restriction (6) */ diff --git a/test/selectC.test b/test/selectC.test index 45250ef30b..fd384053ce 100644 --- a/test/selectC.test +++ b/test/selectC.test @@ -210,4 +210,27 @@ do_test selectC-3.3 { } } {xxx abcxxx yyy defyyy} + +proc udf {} { incr ::udf } +set ::udf 0 +db function udf udf + +do_execsql_test selectC-4.1 { + create table t_distinct_bug (a, b, c); + insert into t_distinct_bug values ('1', '1', 'a'); + insert into t_distinct_bug values ('1', '2', 'b'); + insert into t_distinct_bug values ('1', '3', 'c'); + insert into t_distinct_bug values ('1', '1', 'd'); + insert into t_distinct_bug values ('1', '2', 'e'); + insert into t_distinct_bug values ('1', '3', 'f'); +} {} + +do_execsql_test selectC-4.2 { + select a from (select distinct a, b from t_distinct_bug) +} {1 1 1} + +do_execsql_test selectC-4.3 { + select a, udf() from (select distinct a, b from t_distinct_bug) +} {1 1 1 2 1 3} + finish_test