]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Include WHERE terms in the cursor-hint passed to a cursor opened for the rhs of a...
authordan <dan@noemail.net>
Fri, 17 Jun 2016 19:27:13 +0000 (19:27 +0000)
committerdan <dan@noemail.net>
Fri, 17 Jun 2016 19:27:13 +0000 (19:27 +0000)
FossilOrigin-Name: 998095aba01b75f685ed981b377e1dfe650d9bbf

manifest
manifest.uuid
src/wherecode.c
test/cursorhint2.test

index 788c218d3b9038a6adbc09fb9a3f7163a70c8847..be6f9cf0da25cac9054167723e2a5f834ac4a6e4 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Fix\sa\sduplicate\stest\sname\sin\scursorhint2.test.
-D 2016-06-17T14:59:40.619
+C Include\sWHERE\sterms\sin\sthe\scursor-hint\spassed\sto\sa\scursor\sopened\sfor\sthe\srhs\sof\sa\sLEFT\sJOIN\siff\swe\scan\sbe\ssure\sthat\sthose\sterms\swill\snot\sevaluate\sto\strue\sif\sthe\sLEFT\sJOIN\sgenerates\sa\srow\sof\sNULLs.
+D 2016-06-17T19:27:13.114
 F Makefile.in f3f7d2060ce03af4584e711ef3a626ef0b1d6340
 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434
 F Makefile.msc 50149765ef72f4e652b9a0f1f6462c4784bb9423
@@ -464,7 +464,7 @@ F src/wal.h 2f7c831cf3b071fa548bf2d5cac640846a7ff19c
 F src/walker.c 0f142b5bd3ed2041fc52d773880748b212e63354
 F src/where.c 74f0798525b6306682d7234f230ea93f86959b9b
 F src/whereInt.h e5b939701a7ceffc5a3a8188a37f9746416ebcd0
-F src/wherecode.c b9d5f7d8ebed2e09376fd785a111ffd9dfd59ffc
+F src/wherecode.c c1b11ad09468577afd3c36cd8c854fb6dd77f258
 F src/whereexpr.c c32d47085dbaca0b8fd013210f56693c7d220d48
 F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd
@@ -617,7 +617,7 @@ F test/cse.test 277350a26264495e86b1785f34d2d0c8600e021c
 F test/csv01.test 0929a9ce47021519512be92861f29e32d2538e5f
 F test/ctime.test 7bd009071e242aac4f18521581536b652b789a47
 F test/cursorhint.test 7bc346788390475e77a345da2b92270d04d35856
-F test/cursorhint2.test aa52c25d84366531af871f0bed91a7147b2b37e8
+F test/cursorhint2.test cef69bab25b9141071b4239fa5c3b4ce18eeafd0
 F test/date.test 984ac1e3e5e031386866f034006148d3972b4a65
 F test/dbstatus.test 8de104bb5606f19537d23cd553b41349b5ab1204
 F test/dbstatus2.test e93ab03bfae6d62d4d935f20de928c19ca0ed0ab
@@ -1502,7 +1502,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 c1a5a57cf10dca91082963dcbd3e3ffebc3707ef
-R ae6a6e72981ae73e95b7048747b6cd4f
+P fcd12b69cee1335224a65aa6d22c4e302b889398
+R aa7c436ce5d8105be03298dc84afa8de
 U dan
-Z 130058fba07c74a7ae7dc91c3a4fa1a9
+Z a2cf93e232f8c6f0bcf6400999626f67
index 8c6ce154cd6b6222f88d3b17362cd014fcc5916f..efb8ac03359248cffcc53b681c3fe37a00e09b34 100644 (file)
@@ -1 +1 @@
-fcd12b69cee1335224a65aa6d22c4e302b889398
\ No newline at end of file
+998095aba01b75f685ed981b377e1dfe650d9bbf
\ No newline at end of file
index 94139b6543ee5eeeb6d0663be520ad0269a71880..e6294de5321159b6009c7bbbc89b1a1cc51ef0be 100644 (file)
@@ -626,6 +626,31 @@ static int codeCursorHintCheckExpr(Walker *pWalker, Expr *pExpr){
   return WRC_Continue;
 }
 
+/*
+** Test whether or not expression pExpr, which was part of a WHERE clause,
+** should be included in the cursor-hint for a table that is on the rhs
+** of a LEFT JOIN. Set Walker.eCode to non-zero before returning if the 
+** expression is not suitable.
+**
+** An expression is unsuitable if it might evaluate to non NULL even if
+** a TK_COLUMN node that does affect the value of the expression is set
+** to NULL. For example:
+**
+**   col IS NULL
+**   col IS NOT NULL
+**   coalesce(col, 1)
+**   CASE WHEN col THEN 0 ELSE 1 END
+*/
+static int codeCursorHintIsOrFunction(Walker *pWalker, Expr *pExpr){
+  if( pExpr->op==TK_IS || pExpr->op==TK_FUNCTION 
+   || pExpr->op==TK_ISNULL || pExpr->op==TK_ISNOT 
+   || pExpr->op==TK_NOTNULL || pExpr->op==TK_CASE 
+  ){
+    pWalker->eCode = 1;
+  }
+  return WRC_Continue;
+}
+
 
 /*
 ** This function is called on every node of an expression tree used as an
@@ -714,22 +739,33 @@ static void codeCursorHint(
     ** JOIN for which the current table is not the rhs are omitted
     ** from the cursor-hint. 
     **
-    ** If this table is the rhs of a LEFT JOIN, only terms that were
-    ** specified as part of the ON(...) clause may be included in the 
-    ** hint. This is to address the following:
+    ** If this table is the rhs of a LEFT JOIN, "IS" or "IS NULL" terms 
+    ** that were specified as part of the WHERE clause must be excluded.
+    ** This is to address the following:
     **
     **   SELECT ... t1 LEFT JOIN t2 ON (t1.a=t2.b) WHERE t2.c IS NULL;
     **
-    ** If the (t2.c IS NULL) constraint is pushed down to the cursor, it
-    ** might filter out all rows that match (t1.a=t2.b), causing SQLite
-    ** to add a row of NULL values to the output that should not be
-    ** present (since the ON clause does actually match rows within t2).
+    ** Say there is a single row in t2 that matches (t1.a=t2.b), but its
+    ** t2.c values is not NULL. If the (t2.c IS NULL) constraint is 
+    ** pushed down to the cursor, this row is filtered out, causing
+    ** SQLite to synthesize a row of NULL values. Which does match the
+    ** WHERE clause, and so the query returns a row. Which is incorrect.
+    **
+    ** For the same reason, WHERE terms such as:
+    **
+    **   WHERE 1 = (t2.c IS NULL)
+    **
+    ** are also excluded. See codeCursorHintIsOrFunction() for details.
     */
     if( pTabItem->fg.jointype & JT_LEFT ){
-      if( !ExprHasProperty(pTerm->pExpr, EP_FromJoin) 
-       || pTerm->pExpr->iRightJoinTable!=pTabItem->iCursor
+      Expr *pExpr = pTerm->pExpr;
+      if( !ExprHasProperty(pExpr, EP_FromJoin) 
+       || pExpr->iRightJoinTable!=pTabItem->iCursor
       ){
-        continue;
+        sWalker.eCode = 0;
+        sWalker.xExprCallback = codeCursorHintIsOrFunction;
+        sqlite3WalkExpr(&sWalker, pTerm->pExpr);
+        if( sWalker.eCode ) continue;
       }
     }else{
       if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) continue;
index b6ebda2244c117e911fb2e1314d93dfe807dcc76..b42227cdfb89a8906c3e596f8db4e2033430c3ef 100644 (file)
@@ -94,7 +94,74 @@ do_extract_hints_test 1.6 {
 do_extract_hints_test 1.7 {
   SELECT * FROM t1 LEFT JOIN t2 ON (a=c AND d=e) LEFT JOIN t3 ON (d=f);
 } {
-  t2 {EQ(r[2],c0)} t3 {EQ(r[6],c1)}
+  t2 {EQ(r[2],c0)} t3 {AND(EQ(r[6],c0),EQ(r[7],c1))}
+}
+
+#-------------------------------------------------------------------------
+#
+do_execsql_test 2.0 {
+  CREATE TABLE x1(x, y);
+  CREATE TABLE x2(a, b);
+}
+
+do_extract_hints_test 2.1 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NULL;
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.2 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS +NULL;
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.3 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = (b IS NULL)
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.4 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.5 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 1 = coalesce(b, 1)
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.6 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT NULL)
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.7 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE 0 = (b IS NOT +NULL)
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.8 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE b IS NOT +NULL
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.9 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE CASE b WHEN 0 THEN 0 ELSE 1 END;
+} {
+  x2 {EQ(c0,r[2])}
+}
+
+do_extract_hints_test 2.10 {
+  SELECT * FROM x1 LEFT JOIN x2 ON (a=x) WHERE x2.b = 32+32
+} {
+  x2 {AND(EQ(c1,ADD(32,32)),EQ(c0,r[2]))}
 }
 
 finish_test