From 271896031628a161bc0c3937dc90af01029e8bdf Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 3 Sep 2016 15:31:20 +0000 Subject: [PATCH] Consider the affinity of "b" when using an "a IN (SELECT b ...)" expression with an index on "a". Fix for [199df416]. FossilOrigin-Name: f5e49855412e389a8a410db5d7ffb2e3634c5fa3 --- manifest | 15 ++--- manifest.uuid | 2 +- src/wherecode.c | 20 ++++-- test/rowvalue9.test | 144 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 test/rowvalue9.test diff --git a/manifest b/manifest index e6005476d0..fdd02d717d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Performance\soptimizations. -D 2016-09-03T01:46:15.276 +C Consider\sthe\saffinity\sof\s"b"\swhen\susing\san\s"a\sIN\s(SELECT\sb\s...)"\sexpression\swith\san\sindex\son\s"a".\sFix\sfor\s[199df416]. +D 2016-09-03T15:31:20.197 F Makefile.in cfd8fb987cd7a6af046daa87daa146d5aad0e088 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 5017381e4853b1472e01d5bb926be1268eba429c @@ -467,7 +467,7 @@ F src/wal.h 6dd221ed384afdc204bc61e25c23ef7fd5a511f2 F src/walker.c 2d2cc7fb0f320f7f415215d7247f3c584141ac09 F src/where.c 48d705e5196a0611a7be90698eade455ee238536 F src/whereInt.h 14dd243e13b81cbb0a66063d38b70f93a7d6e613 -F src/wherecode.c ee7b5353ff5f63548c206e3ecb4423ff0857d766 +F src/wherecode.c 8a9a53cb52dd8a75e07c85e3bc12c1604c735954 F src/whereexpr.c 7f9ada866d48d15d09754ae819c1c40efe3b2aff F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2 F test/affinity2.test a6d901b436328bd67a79b41bb0ac2663918fe3bd @@ -1028,6 +1028,7 @@ F test/rowvalue5.test a440d490c8c0bf606034c09d5c6bbf7840b98f95 F test/rowvalue6.test d19b54feb604d5601f8614b15e214e0774c01087 F test/rowvalue7.test 5d06ff19d9e6969e574a2e662a531dd0c67801a8 F test/rowvalue8.test 5900eddad9e2c3c2e26f1a95f74aafc1232ee5e0 +F test/rowvalue9.test ca4e07da993e455e2c34a11800cd5eecfa25dbea F test/rowvaluefault.test 7b16485e3f2b371f3e3d05455b8ded6d0c090244 F test/rtree.test 0c8d9dd458d6824e59683c19ab2ffa9ef946f798 F test/run-wordcount.sh 891e89c4c2d16e629cd45951d4ed899ad12afc09 @@ -1521,7 +1522,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 c7271fbde1aebb15daaedb7f1fa75fe410fd46f6 -R 617c8c8681735b3b6f061557d07c266d -U drh -Z dbe31dcf0712b654c7c10b1471579b28 +P f1d06c49ba0d814dc7ffb538aac3f4e6251fd8f0 +R 7e510355146cbd1276c808b956f1d4a6 +U dan +Z 2c951e6f5da67796ee0d56ffce2dd993 diff --git a/manifest.uuid b/manifest.uuid index eb5bf868a4..2759fbda66 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f1d06c49ba0d814dc7ffb538aac3f4e6251fd8f0 \ No newline at end of file +f5e49855412e389a8a410db5d7ffb2e3634c5fa3 \ No newline at end of file diff --git a/src/wherecode.c b/src/wherecode.c index 16ffb89db1..896509bc32 100644 --- a/src/wherecode.c +++ b/src/wherecode.c @@ -649,13 +649,21 @@ static int codeAllEqualityTerms( } testcase( pTerm->eOperator & WO_ISNULL ); testcase( pTerm->eOperator & WO_IN ); - if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){ - Expr *pRight = pTerm->pExpr->pRight; - if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ - sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); - VdbeCoverage(v); + if( (pTerm->eOperator & WO_ISNULL)==0 ){ + Expr *pRight = 0; + if( pTerm->eOperator & WO_IN ){ + if( pTerm->pExpr->flags & EP_xIsSelect ){ + int iField = pTerm->iField ? pTerm->iField-1 : 0; + pRight = pTerm->pExpr->x.pSelect->pEList->a[iField].pExpr; + } + }else{ + pRight = pTerm->pExpr->pRight; + if( (pTerm->wtFlags & TERM_IS)==0 && sqlite3ExprCanBeNull(pRight) ){ + sqlite3VdbeAddOp2(v, OP_IsNull, regBase+j, pLevel->addrBrk); + VdbeCoverage(v); + } } - if( zAff ){ + if( pRight && zAff ){ if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_BLOB ){ zAff[j] = SQLITE_AFF_BLOB; } diff --git a/test/rowvalue9.test b/test/rowvalue9.test new file mode 100644 index 0000000000..9e01e08932 --- /dev/null +++ b/test/rowvalue9.test @@ -0,0 +1,144 @@ +# 2016 September 3 +# +# 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. The +# focus of this file is testing SQL statements that use row value +# constructors. +# + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +set ::testprefix rowvalue9 + +do_execsql_test 1.0.1 { + CREATE TABLE a1(c, b INTEGER, a TEXT, PRIMARY KEY(a, b)); + + INSERT INTO a1 (rowid, c, b, a) VALUES(3, '0x03', 1, 1); + INSERT INTO a1 (rowid, c, b, a) VALUES(14, '0x0E', 2, 2); + INSERT INTO a1 (rowid, c, b, a) VALUES(15, '0x0F', 3, 3); + INSERT INTO a1 (rowid, c, b, a) VALUES(92, '0x5C', 4, 4); + + CREATE TABLE a2(x BLOB, y BLOB); + INSERT INTO a2(x, y) VALUES(1, 1); + INSERT INTO a2(x, y) VALUES(2, '2'); + INSERT INTO a2(x, y) VALUES('3', 3); + INSERT INTO a2(x, y) VALUES('4', '4'); +} + +do_execsql_test 1.0.2 { + SELECT x, typeof(x), y, typeof(y) FROM a2 ORDER BY rowid +} { + 1 integer 1 integer + 2 integer 2 text + 3 text 3 integer + 4 text 4 text +} + +do_execsql_test 1.1.1 { + SELECT (SELECT rowid FROM a1 WHERE a=x AND b=y) FROM a2 +} {{} {} 15 92} +do_execsql_test 1.1.2 { + SELECT (SELECT rowid FROM a1 WHERE (a, b) = (x, y)) FROM a2 +} {{} {} 15 92} + +do_execsql_test 1.2.3 { + SELECT a1.rowid FROM a1, a2 WHERE a=x AND b=y; +} {15 92} +do_execsql_test 1.2.4 { + SELECT a1.rowid FROM a1, a2 WHERE (a, b) = (x, y) +} {15 92} + + +do_execsql_test 1.3.1 { + SELECT a1.rowid FROM a1, a2 WHERE coalesce(NULL,x)=a AND coalesce(NULL,y)=b +} {3 14 15 92} +do_execsql_test 1.3.2 { + SELECT a1.rowid FROM a1, a2 + WHERE (coalesce(NULL,x), coalesce(NULL,y)) = (a, b) +} {3 14 15 92} + +do_execsql_test 1.4.1 { + SELECT a1.rowid FROM a1, a2 WHERE +x=a AND +y=b +} {3 14 15 92} +do_execsql_test 1.4.2 { + SELECT a1.rowid FROM a1, a2 WHERE (+x, +y) = (a, b) +} {3 14 15 92} + +do_execsql_test 1.5.1 { + SELECT (SELECT rowid FROM a1 WHERE a=+x AND b=+y) FROM a2 +} {3 14 15 92} +do_execsql_test 1.5.2 { + SELECT (SELECT rowid FROM a1 WHERE (a, b) = (+x, +y)) FROM a2 +} {3 14 15 92} +do_execsql_test 1.5.3 { + SELECT (SELECT rowid FROM a1 WHERE (+x, +y) = (a, b)) FROM a2 +} {3 14 15 92} + +do_execsql_test 1.6.1 { + SELECT a1.rowid FROM a1 WHERE (a, b) IN (SELECT x, y FROM a2) +} {15 92} + +do_execsql_test 1.6.2 { + SELECT a1.rowid FROM a1, a2 WHERE EXISTS ( + SELECT 1 FROM a1 WHERE a=x AND b=y + ) +} {3 14 15 92 3 14 15 92} + +do_execsql_test 2.1 { + CREATE TABLE b1(a TEXT); + CREATE TABLE b2(x BLOB); + + INSERT INTO b1 VALUES(1); + INSERT INTO b2 VALUES(1); +} + +do_execsql_test 2.2 { SELECT * FROM b1, b2 WHERE a=x; } {} +do_execsql_test 2.3 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {} + +do_execsql_test 2.4 { + CREATE UNIQUE INDEX b1a ON b1(a); +} +do_execsql_test 2.5 { SELECT * FROM b1 WHERE a IN (SELECT x FROM b2) } {} + +do_execsql_test 3.1 { + CREATE TABLE c1(a INTEGER, b TEXT); + INSERT INTO c1 VALUES(1, 1); + + CREATE TABLE c2(x BLOB, y BLOB); + INSERT INTO c2 VALUES(1, 1); +} +do_execsql_test 3.2 { + SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2) +} {} +do_execsql_test 3.3 { + CREATE UNIQUE INDEX c1ab ON c1(a, b); + SELECT * FROM c1 WHERE (a, b) IN (SELECT x, y FROM c2) +} {} + +do_execsql_test 4.0 { + CREATE TABLE d1(a TEXT); + CREATE TABLE d2(x BLOB); + INSERT INTO d1 VALUES(1); + INSERT INTO d2 VALUES(1); +} +do_execsql_test 4.1 { + SELECT * FROM d1 WHERE a IN (SELECT x FROM b2) +} {} +do_execsql_test 4.2 { + CREATE UNIQUE INDEX d1a ON d1(a); +} +do_execsql_test 4.3 { + SELECT * FROM d1 WHERE a IN (SELECT x FROM d2) +} {} + + +finish_test +