]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Fix a crash in handling queries of the form "SELECT aggregate(DISTINCT tbl.col) FROM...
authordan <Dan Kennedy>
Sat, 3 Apr 2021 19:23:59 +0000 (19:23 +0000)
committerdan <Dan Kennedy>
Sat, 3 Apr 2021 19:23:59 +0000 (19:23 +0000)
FossilOrigin-Name: 0dcf808ddf23da834da724d88b1715ed06565f1f1290713ff42a3fcf6ffb802e

manifest
manifest.uuid
src/select.c
src/sqliteInt.h
src/where.c
test/distinctagg.test

index 302d604f68fb01a0fd753a5fc0fff3c946573137..8a026e082fda03e247f62684fded666e3fe82d15 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Correctly\scapture\sthe\serror\swhen\sa\sRETURNING\sclause\sappears\son\san\s\nattempt\sto\sUPDATE\san\seponymous\svirtual\stable.\ndbsqlfuzz\s486f791cbe2dc45839310073e71367a1d8ad22dd.
-D 2021-04-02T20:43:26.666
+C Fix\sa\scrash\sin\shandling\squeries\sof\sthe\sform\s"SELECT\saggregate(DISTINCT\stbl.col)\sFROM\s...\sLEFT\sJOIN\stbl\s...".\sFixes\sa\sproblem\sintroduced\sby\s[ef4ac0ddd297bbd3].
+D 2021-04-03T19:23:59.990
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -542,12 +542,12 @@ F src/printf.c 78fabb49b9ac9a12dd1c89d744abdc9b67fd3205e62967e158f78b965a29ec4b
 F src/random.c 80f5d666f23feb3e6665a6ce04c7197212a88384
 F src/resolve.c fc136d935f19966747663bed605ad7f06f84f9fe7bf7bf79e9bf844ef5c7556d
 F src/rowset.c ba9515a922af32abe1f7d39406b9d35730ed65efab9443dc5702693b60854c92
-F src/select.c 2d7a1f3d9f5970962b407cb4b38416fedc1a3b55efd010b40af375088653b6ce
+F src/select.c 06df4a2a94df253b580d38b32573f4b6462fbe39abc31101c9a9fa95215c8479
 F src/shell.c.in dcce260883836c9b58847505fbccce8d5546af925046f7dacd9443e922ece036
 F src/sqlite.h.in 18ec33e32001721fd4e9c4705a24a85dff04956ac2c0a21775058884ba845b09
 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
 F src/sqlite3ext.h 61b38c073d5e1e96a3d45271b257aef27d0d13da2bea5347692ae579475cd95e
-F src/sqliteInt.h c5cfae5891a6e643116f66f63769bcffeba89ca51f6278732da710eb0cf092b6
+F src/sqliteInt.h b6331a513c31ea39bf625ee7b7829aa674d8850df58d562689763cd408619f0e
 F src/sqliteLimit.h d7323ffea5208c6af2734574bae933ca8ed2ab728083caa117c9738581a31657
 F src/status.c 4b8bc2a6905163a38b739854a35b826c737333fab5b1f8e03fa7eb9a4799c4c1
 F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@@ -629,7 +629,7 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
 F src/wal.c 69e770e96fd56cc21608992bf2c6f1f3dc5cf2572d0495c6a643b06c3a679f14
 F src/wal.h c3aa7825bfa2fe0d85bef2db94655f99870a285778baa36307c0a16da32b226a
 F src/walker.c d42d6c80ea363ef689a462e65eefcfe87deab924c50de5baa37ecb6af7d7ddaa
-F src/where.c 07d1232df6fc4705815b0ef5182fe3e56bdba6ac09ce52bd66ecb207626aac2b
+F src/where.c f81b47aeed660808c315290aec02fefe21ffce55e988c603254a2c81d20a0eb5
 F src/whereInt.h 446e5e8018f83358ef917cf32d8e6a86dc8430113d0b17e720f1839d3faa44c4
 F src/wherecode.c c7354c9749f015f37bc32e15a4257a5155fa5d2b79f605f528bd09f3b9f1b4e6
 F src/whereexpr.c 4ba94b8ebbff97c658b212de6ccdb6d3a41847800bdf99e7a2a967c250269f2c
@@ -845,7 +845,7 @@ F test/descidx3.test 953c831df7ea219c73826dfbf2f6ee02d95040725aa88ccb4fa43d1a199
 F test/diskfull.test 106391384780753ea6896b7b4f005d10e9866b6e
 F test/distinct.test eefe5a8e47fcb761aaa745838e355ab28586f9f75786a1c098016881344b2a5e
 F test/distinct2.test cd1d15a4a2abf579298f7161e821ed50c0119136fe0424db85c52cf0adc230d1
-F test/distinctagg.test fd74c3783700f1d99c67a5d906bba97fb6650f4559f3ea7b1873f793ffc601c9
+F test/distinctagg.test 2ff06cbc65cbc25fff8c9b00004da3aa3431b7001601bdfc7d4eb700ece1c4d0
 F test/e_blobbytes.test 439a945953b35cb6948a552edaec4dc31fd70a05
 F test/e_blobclose.test 4b3c8c60c2171164d472059c73e9f3c1844bb66d
 F test/e_blobopen.test e95e1d40f995056f6f322cd5e1a1b83a27e1a145
@@ -1912,7 +1912,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P e16da5af822ef31d7e05992403cf9787fbb3d9abb0b5283aba55ea07e1830a72
-R 31cf23d11c71c31febdef8457989c710
-U drh
-Z bb6d981e49a44d67794ddc6bc2f09d25
+P 778a9a6e6f8d960fd55ac9be7eea20b1875a46192db85e63dddc61b632b30173
+R 8e7491f95f5def69f8f53350692c8afa
+U dan
+Z 2396774cc48a628f8ba66378b6a8f6cc
index 56dddf28125a44cc5c2fc355dade0afc9f303913..39197b7f16609e779a821720f9641e53d64202f4 100644 (file)
@@ -1 +1 @@
-778a9a6e6f8d960fd55ac9be7eea20b1875a46192db85e63dddc61b632b30173
\ No newline at end of file
+0dcf808ddf23da834da724d88b1715ed06565f1f1290713ff42a3fcf6ffb802e
\ No newline at end of file
index c6b36b6831e78996239117576aadcbed706175d0..7c84bd7d6dbab761b0d85b3d395644471195a0ae 100644 (file)
@@ -6856,7 +6856,7 @@ int sqlite3Select(
         pExpr = sqlite3ExprDup(db, pExpr, 0);
         pDistinct = sqlite3ExprListDup(db, pGroupBy, 0);
         pDistinct = sqlite3ExprListAppend(pParse, pDistinct, pExpr);
-        distFlag = pDistinct ? WHERE_WANT_DISTINCT : 0;
+        distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
       }
 
       /* If there is a GROUP BY clause we might need a sorting index to
@@ -7169,7 +7169,7 @@ int sqlite3Select(
           }
         }else if( pAggInfo->nFunc==1 && pAggInfo->aFunc[0].iDistinct>=0 ){
           pDistinct = pAggInfo->aFunc[0].pFExpr->x.pList;
-          distFlag = pDistinct ? WHERE_WANT_DISTINCT : 0;
+          distFlag = pDistinct ? (WHERE_WANT_DISTINCT|WHERE_AGG_DISTINCT) : 0;
         }
 
         /* This case runs if the aggregate has no GROUP BY clause.  The
index fcc50e15766ab0be309ed6c86648bb1906d563e8..783950fb058684303fb8965a263b241f42341c2c 100644 (file)
@@ -3019,7 +3019,7 @@ struct SrcList {
 #define WHERE_DISTINCTBY       0x0080 /* pOrderby is really a DISTINCT clause */
 #define WHERE_WANT_DISTINCT    0x0100 /* All output needs to be distinct */
 #define WHERE_SORTBYGROUP      0x0200 /* Support sqlite3WhereIsSorted() */
-                        /*     0x0400    not currently used */
+#define WHERE_AGG_DISTINCT     0x0400 /* Query is "SELECT agg(DISTINCT ...)" */
 #define WHERE_ORDERBY_LIMIT    0x0800 /* ORDERBY+LIMIT on the inner loop */
                         /*     0x1000    not currently used */
                         /*     0x2000    not currently used */
index ff6220b83d8e79d7718625acc9b62eb089a5691e..22ad55846596153c1a62316ac095109d7f205b76 100644 (file)
@@ -5025,9 +5025,7 @@ WhereInfo *sqlite3WhereBegin(
   /* Attempt to omit tables from the join that do not affect the result.
   ** For a table to not affect the result, the following must be true:
   **
-  **   1) The query must not be an aggregate. Or it must be an aggregate
-  **      that contains only one aggregate function with the DISTINCT 
-  **      qualifier. e.g. "SELECT count(DISTINCT ...) FROM".
+  **   1) The query must not be an aggregate.
   **   2) The table must be the RHS of a LEFT JOIN.
   **   3) Either the query must be DISTINCT, or else the ON or USING clause
   **      must contain a constraint that limits the scan of the table to 
@@ -5055,7 +5053,8 @@ WhereInfo *sqlite3WhereBegin(
   */
   notReady = ~(Bitmask)0;
   if( pWInfo->nLevel>=2
-   && pResultSet!=0               /* guarantees condition (1) above */
+   && pResultSet!=0                         /* these two combine to guarantee */
+   && 0==(wctrlFlags & WHERE_AGG_DISTINCT)  /* condition (1) above */
    && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
   ){
     int i;
index af2c26e02de3b7782d74b1a3b0a64d4b45293ea5..06f05d843529358478ebf1b7303b8571153a5fef 100644 (file)
@@ -174,10 +174,15 @@ foreach {tn use_t3 sql res} {
   1 1 "SELECT count(*) FROM t3"   2
   2 0 "SELECT count(*) FROM t1"   10
   2 1 "SELECT count(DISTINCT a) FROM t1, t3" 4
-  3 0 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3" 4
+  3 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3" 4
   4 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 4
   5 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0
-  6 0 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 ON (t3.x=0)"  4
+  6 1 "SELECT count(DISTINCT a) FROM t1 LEFT JOIN t3 ON (t3.x=0)"  4
+  7 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3" 2
+  8 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=1" 1
+  9 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 WHERE t3.x=0" 0
+ 10 1 "SELECT count(DISTINCT x) FROM t1 LEFT JOIN t3 ON (t3.x=0)"  0
+
 } {
   do_test 5.$tn.1 {
     set bUse 0
@@ -190,5 +195,18 @@ foreach {tn use_t3 sql res} {
   do_execsql_test 5.$tn.2 $sql $res
 }
 
+#-------------------------------------------------------------------------
+reset_db
+do_execsql_test 6.0 {
+  CREATE TABLE t1(a, b);
+  CREATE TABLE t2(c, d);
+  INSERT INTO t1 VALUES(123,456);
+  INSERT INTO t2 VALUES(123,456);
+}
+do_execsql_test 6.1 {
+  SELECT count(DISTINCT c) FROM t1 LEFT JOIN t2;
+} {1}
+
+
 finish_test