1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-29 08:01:23 +03:00

Add extra tests for the changes on this branch.

FossilOrigin-Name: a6365c778f2a6d6fa78e8520553373898f382ce73bf6496533e26291648ef5d1
This commit is contained in:
dan
2024-06-08 18:13:12 +00:00
parent 5525ac1049
commit b111f1cb2a
5 changed files with 328 additions and 28 deletions

View File

@ -1,5 +1,5 @@
C Experimental\soptimization\sto\srewrite\sa\sSELECT\swith\san\sEXISTS(...)\sexpression\sin\sthe\sWHERE\sclause\sas\sa\sjoin. C Add\sextra\stests\sfor\sthe\schanges\son\sthis\sbranch.
D 2024-06-07T21:00:42.970 D 2024-06-08T18:13:12.691
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -755,7 +755,7 @@ F src/printf.c 8b250972305e14b365561be5117ed0fd364e4fd58968776df1ce64c6280b90f9
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c 9c7786f032dea81487e7d94cb17849936f0e9b8891bfc91a6ac24ab193762804 F src/resolve.c 9c7786f032dea81487e7d94cb17849936f0e9b8891bfc91a6ac24ab193762804
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
F src/select.c 669cfc0392c8a0bd43e5a199cba5f796aead3423e2c529d09148adfa57ae1152 F src/select.c 06146768a11bfe8928fea0aabf8addedd4d146a8c4be70558f5c8ecdcc2ce338
F src/shell.c.in 77d12a0dab8724819e64a14d5fbaad91a934be2b22ad329708fba9ba78993f04 F src/shell.c.in 77d12a0dab8724819e64a14d5fbaad91a934be2b22ad329708fba9ba78993f04
F src/sqlite.h.in cbd3e4177791a61c056fd81e37a5b21bb6c8cb2ea8cac558c625974673f50acf F src/sqlite.h.in cbd3e4177791a61c056fd81e37a5b21bb6c8cb2ea8cac558c625974673f50acf
F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8 F src/sqlite3.rc 5121c9e10c3964d5755191c80dd1180c122fc3a8
@ -1114,7 +1114,8 @@ F test/exclusive.test 7ff63be7503990921838d5c9f77f6e33e68e48ed1a9d48cd28745bf650
F test/exclusive2.test 984090e8e9d1b331d2e8111daf6e5d61dda0bef7 F test/exclusive2.test 984090e8e9d1b331d2e8111daf6e5d61dda0bef7
F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7 F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7
F test/exists.test 79a75323c78f02bbe9c251ea502a092f9ef63dac F test/exists.test 79a75323c78f02bbe9c251ea502a092f9ef63dac
F test/existsexpr.test a86e8300a2e0cd26109ba5b0d552dbc598b9ad4b79dfd26cac255839c9d5cf6a F test/existsexpr.test b101143b177d623c35166589d3e6772021dfe4f625ac318c91726a998939cd19
F test/existsfault.test ff41c11f3052c1bbd4f8dd557802310026253d67d7c4e3a180c16d2f0862973e
F test/expr.test 5c06696478212e5a04e04b043f993373f6f8e5ce5a80f5548a84703b123b6caa F test/expr.test 5c06696478212e5a04e04b043f993373f6f8e5ce5a80f5548a84703b123b6caa
F test/expr2.test c27327ae9c017a7ff6280123f67aff496f912da74d78c888926d68b46ec75fd8 F test/expr2.test c27327ae9c017a7ff6280123f67aff496f912da74d78c888926d68b46ec75fd8
F test/exprfault.test da33606d799718e2f8e34efd0e5858884a1ad87f608774c552a7f5517cc27181 F test/exprfault.test da33606d799718e2f8e34efd0e5858884a1ad87f608774c552a7f5517cc27181
@ -2196,11 +2197,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P c4a9dda2809c6e0e3d928e11e5553ead82cd9df551bcd35b11a7d869ef80ab8e P 972a33db0b0e924b78d5309d222d8ea298bd59c72da14ea2d14e8e2caaad1e0a
R 7e4221204288c4c4593aaccf38fb6e40 R 20ea5e8daae9a69a6e09a05cc19a6a37
T *branch * exists-to-join
T *sym-exists-to-join *
T -sym-trunk *
U dan U dan
Z 8ebacdbf5bd3ee682966a7aab4962dc0 Z cbb8e2989ade033c119abad0907d9ec3
# Remove this line to create a well-formed Fossil manifest. # Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
972a33db0b0e924b78d5309d222d8ea298bd59c72da14ea2d14e8e2caaad1e0a a6365c778f2a6d6fa78e8520553373898f382ce73bf6496533e26291648ef5d1

View File

@ -7382,12 +7382,13 @@ static u64 findConstIdxTerms(
** WHERE S.sid = R.sid AND R.day = '2022-10-25'; ** WHERE S.sid = R.sid AND R.day = '2022-10-25';
*/ */
static void existsToJoin(Parse *pParse, Select *p, Expr *pWhere){ static void existsToJoin(Parse *pParse, Select *p, Expr *pWhere){
if( pWhere && p->pSrc->nSrc>0 ){ if( pWhere && p->pSrc->nSrc>0 && pParse->db->mallocFailed==0 ){
if( pWhere->op==TK_AND ){ if( pWhere->op==TK_AND ){
Expr *pRight = pWhere->pRight;
existsToJoin(pParse, p, pWhere->pLeft); existsToJoin(pParse, p, pWhere->pLeft);
existsToJoin(pParse, p, pWhere->pRight); existsToJoin(pParse, p, pRight);
} }
else if( pWhere->op==TK_EXISTS && (pWhere->flags & EP_xIsSelect) ){ else if( pWhere->op==TK_EXISTS ){
Select *pSub = pWhere->x.pSelect; Select *pSub = pWhere->x.pSelect;
if( pSub->pSrc->nSrc==1 if( pSub->pSrc->nSrc==1
&& (pSub->selFlags & (SF_Aggregate|SF_Correlated))==SF_Correlated && (pSub->selFlags & (SF_Aggregate|SF_Correlated))==SF_Correlated
@ -7401,28 +7402,26 @@ static void existsToJoin(Parse *pParse, Select *p, Expr *pWhere){
bTransform = 1; bTransform = 1;
} }
for(pIdx=pTab->pIndex; pIdx && bTransform==0; pIdx=pIdx->pNext){ for(pIdx=pTab->pIndex; pIdx && bTransform==0; pIdx=pIdx->pNext){
if( pIdx->onError && pIdx->nKeyCol<64 ){ if( pIdx->onError && pIdx->nKeyCol<=63 ){
u64 c = findConstIdxTerms(pParse, iCsr, pIdx, pSub->pWhere); u64 c = findConstIdxTerms(pParse, iCsr, pIdx, pSub->pWhere);
if( c==(1 << pIdx->nKeyCol)-1 ){ if( c==((u64)1 << pIdx->nKeyCol)-1 ){
bTransform = 1; bTransform = 1;
} }
} }
} }
if( bTransform ){ if( bTransform ){
p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
pSub->pSrc = 0;
if( p->pWhere ){
p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSub->pWhere);
}else{
p->pWhere = pSub->pWhere;
}
pSub->pWhere = 0;
sqlite3SelectDelete(pParse->db, pSub);
memset(pWhere, 0, sizeof(*pWhere)); memset(pWhere, 0, sizeof(*pWhere));
pWhere->op = TK_INTEGER; pWhere->op = TK_INTEGER;
pWhere->u.iValue = 1; pWhere->u.iValue = 1;
ExprSetProperty(pWhere, EP_IntValue); ExprSetProperty(pWhere, EP_IntValue);
assert( p->pWhere!=0 );
p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSub->pWhere);
pSub->pWhere = 0;
pSub->pSrc = 0;
sqlite3SelectDelete(pParse->db, pSub);
} }
} }
} }

View File

@ -17,9 +17,8 @@ set testprefix existsexpr
do_execsql_test 1.0 { do_execsql_test 1.0 {
CREATE TABLE x1(a, b); CREATE TABLE x1(a, b, PRIMARY KEY(a)) WITHOUT ROWID;
INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6); INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6);
CREATE UNIQUE INDEX x1a ON x1(a);
CREATE INDEX x1b ON x1(b); CREATE INDEX x1b ON x1(b);
CREATE TABLE x2(x, y); CREATE TABLE x2(x, y);
@ -111,6 +110,261 @@ do_execsql_test 2.5.1 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.rowid=a); SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.rowid=a);
} {~/SUBQUERY/} } {~/SUBQUERY/}
#-------------------------------------------------------------------------
proc do_subquery_test {tn bSub sql res} {
set r1(0) ~/SUBQUERY/
set r1(1) /SUBQUERY/
do_execsql_test $tn.1 "explain query plan $sql" $r1($bSub)
do_execsql_test $tn.2 $sql $res
}
do_execsql_test 3.0 {
CREATE TABLE y1(a, b, c);
CREATE TABLE y2(x, y, z);
CREATE UNIQUE INDEX y2zy ON y2(z, y);
INSERT INTO y1 VALUES(1, 1, 1);
INSERT INTO y1 VALUES(2, 2, 2);
INSERT INTO y1 VALUES(3, 3, 3);
INSERT INTO y1 VALUES(4, 4, 4);
INSERT INTO y2 VALUES(1, 1, 1);
INSERT INTO y2 VALUES(3, 3, 3);
}
do_subquery_test 3.1 0 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=a AND y=b AND x=z
)
} {
1 1 1 3 3 3
}
do_subquery_test 3.2 0 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=max(a,b) AND y=min(b,a) AND x=z
)
} {
1 1 1 3 3 3
}
do_subquery_test 3.3 0 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=max(a,b) AND y=min(b,a) AND c!=3
)
} {
1 1 1
}
do_subquery_test 3.4 1 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=max(a,b) AND b=3
)
} {
3 3 3
}
do_subquery_test 3.5 0 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=a-1 AND y=a-1
)
} {
2 2 2
4 4 4
}
do_subquery_test 3.6 1 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 WHERE z=a-1 AND y+1=a
)
} {
2 2 2
4 4 4
}
do_subquery_test 3.7 1 {
SELECT * FROM y1 WHERE EXISTS (
SELECT count(*) FROM y2 WHERE z=a-1 AND y=a-1
)
} {
1 1 1
2 2 2
3 3 3
4 4 4
}
do_subquery_test 3.8 1 {
SELECT * FROM y1 WHERE EXISTS ( SELECT a+1 FROM y2 )
} {
1 1 1
2 2 2
3 3 3
4 4 4
}
do_subquery_test 3.9 1 {
SELECT * FROM y1 WHERE EXISTS (
SELECT 1 FROM y2 one, y2 two WHERE one.z=a-1 AND one.y=a-1
)
} {
2 2 2
4 4 4
}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 4.0 {
CREATE TABLE tx1(a TEXT COLLATE nocase, b TEXT);
CREATE UNIQUE INDEX tx1ab ON tx1(a, b);
INSERT INTO tx1 VALUES('a', 'a');
INSERT INTO tx1 VALUES('B', 'b');
INSERT INTO tx1 VALUES('c', 'c');
INSERT INTO tx1 VALUES('D', 'd');
INSERT INTO tx1 VALUES('e', 'e');
CREATE TABLE tx2(x, y);
INSERT INTO tx2 VALUES('A', 'a');
INSERT INTO tx2 VALUES('b', 'b');
INSERT INTO tx2 VALUES('C', 'c');
INSERT INTO tx2 VALUES('D', 'd');
}
do_subquery_test 4.1 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x AND b=y
)
} {
A a
b b
C c
D d
}
do_subquery_test 4.2 1 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x AND b=y COLLATE nocase
)
} {
A a
b b
C c
D d
}
do_execsql_test 4.3 {
DROP INDEX tx1ab;
CREATE UNIQUE INDEX tx1ab ON tx1(a COLLATE binary, b);
}
do_subquery_test 4.4 1 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x AND b=y
)
} {
A a
b b
C c
D d
}
do_subquery_test 4.4 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x COLLATE binary AND b=y
)
} {
D d
}
do_subquery_test 4.4 1 {
SELECT EXISTS ( SELECT x FROM tx1 ) FROM tx2
} {
1 1 1 1
}
do_subquery_test 4.4 1 {
SELECT (SELECT EXISTS ( SELECT x FROM tx1 ) WHERE 1) FROM tx2
} {
1 1 1 1
}
#-------------------------------------------------------------------------
proc cols {s f} {
set lCols [list]
for {set i $s} {$i<=$f} {incr i} {
lappend lCols [format "c%02d" $i]
}
join $lCols ", "
}
proc vals {n val} {
set lVal [list]
for {set i 0} {$i<$n} {incr i} {
lappend lVal $val
}
join $lVal ", "
}
proc exprs {s f} {
set lExpr [list]
for {set i $s} {$i<=$f} {incr i} {
lappend lExpr [format "c%02d = o" $i]
}
join $lExpr " AND "
}
do_execsql_test 5.0 "
CREATE TABLE a1( [cols 0 99] );
"
do_execsql_test 5.1 "
-- 63 column index
CREATE UNIQUE INDEX a1idx1 ON a1( [cols 0 62] );
"
do_execsql_test 5.2 "
-- 64 column index
CREATE UNIQUE INDEX a1idx2 ON a1( [cols 10 73] );
"
do_execsql_test 5.2 "
-- 65 column index
CREATE UNIQUE INDEX a1idx3 ON a1( [cols 20 84] );
"
do_test 5.3 {
foreach v {1 2 3 4 5 6} {
execsql "INSERT INTO a1 VALUES( [vals 100 $v] )"
}
} {}
do_execsql_test 5.4 {
CREATE TABLE a2(o);
INSERT INTO a2 VALUES(2), (5);
}
do_subquery_test 5.5 0 "
SELECT o FROM a2 WHERE EXISTS (
SELECT 1 FROM a1 WHERE [exprs 0 62]
)
" {
2 5
}
do_subquery_test 5.6 1 "
SELECT o FROM a2 WHERE EXISTS (
SELECT 1 FROM a1 WHERE [exprs 10 73]
)
" {
2 5
}
do_subquery_test 5.7 1 "
SELECT o FROM a2 WHERE EXISTS (
SELECT 1 FROM a1 WHERE [exprs 20 84]
)
" {
2 5
}
finish_test finish_test

49
test/existsfault.test Normal file
View File

@ -0,0 +1,49 @@
# 2024 May 25
#
# 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.
#
#***********************************************************************
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl
set testprefix existsfault
db close
sqlite3_shutdown
sqlite3_config_lookaside 0 0
sqlite3_initialize
autoinstall_test_functions
sqlite3 db test.db
do_execsql_test 1.0 {
CREATE TABLE x1(a, b);
INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6);
CREATE UNIQUE INDEX x1a ON x1(a);
CREATE INDEX x1b ON x1(b);
CREATE TABLE x2(x, y);
INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6);
}
do_faultsim_test 1 -faults oom* -prep {
sqlite3 db test.db
execsql { SELECT * FROM sqlite_schema }
} -body {
execsql {
SELECT count(*) FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) AND y!=11
}
} -test {
faultsim_test_result {0 3}
}
finish_test