1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-08-07 02:42:48 +03:00

New optimizations to detect early when queries return no rows due to

tables being empty.  This includes the EXISTS-to-JOIN optimization that
tries to transform EXISTS constraints into additional terms of the FROM
clause.

FossilOrigin-Name: e33da6d5dc964db817d1bc63c9083aecd93d49ee14d5198600b47eaf7c5b9331
This commit is contained in:
drh
2025-07-08 19:53:36 +00:00
18 changed files with 781 additions and 38 deletions

View File

@@ -1,5 +1,5 @@
C Fix\sa\sproblem\swith\sthe\sfix\sin\s[5cb8e342e1].
D 2025-07-08T19:02:26.486
C New\soptimizations\sto\sdetect\searly\swhen\squeries\sreturn\sno\srows\sdue\sto\ntables\sbeing\sempty.\s\sThis\sincludes\sthe\sEXISTS-to-JOIN\soptimization\sthat\ntries\sto\stransform\sEXISTS\sconstraints\sinto\sadditional\sterms\sof\sthe\sFROM\nclause.
D 2025-07-08T19:53:36.480
F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -726,10 +726,10 @@ F src/auth.c 54ab9c6c5803b47c0d45b76ce27eff22a03b4b1f767c5945a3a4eb13aa4c78dc
F src/backup.c 5c97e8023aab1ce14a42387eb3ae00ba5a0644569e3476f38661fa6f824c3523
F src/bitvec.c e242d4496774dfc88fa278177dd23b607dce369ccafb3f61b41638eea2c9b399
F src/btmutex.c 30dada73a819a1ef5b7583786370dce1842e12e1ad941e4d05ac29695528daea
F src/btree.c 783f9999f9ca56846619ba902f5970e181d897c23cc923c915fef225af6dda8a
F src/btree.h 18e5e7b2124c23426a283523e5f31a4bff029131b795bb82391f9d2f3136fc50
F src/btree.c cb5b8ceb9baa02a63a2f83dec09c4153e1cfbdf9c2adef5c62c26d2160eeb067
F src/btree.h e823c46d87f63d904d735a24b76146d19f51f04445ea561f71cc3382fd1307f0
F src/btreeInt.h 9c0f9ea5c9b5f4dcaea18111d43efe95f2ac276cd86d770dce10fd99ccc93886
F src/build.c 67159d31bfc565e5f23a7be8159325bae599bddd19fc584ac2511b947cf341d3
F src/build.c cc4f287348790bbb7219f7e8dee13b1c345c3377fcdd98eca866e7457ecd07e7
F src/callback.c acae8c8dddda41ee85cfdf19b926eefe830f371069f8aadca3aa39adf5b1c859
F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e
F src/date.c 9db4d604e699a73e10b8e85a44db074a1f04c0591a77e2abfd77703f50dce1e9
@@ -783,14 +783,14 @@ F src/pragma.c 30b535d0a66348df844ee36f890617b4cf45e9a22dcbc47ec3ca92909c50aaf1
F src/prepare.c 1832be043fce7d489959aae6f994c452d023914714c4d5457beaed51c0f3d126
F src/printf.c 71b6d3a0093bf23f473e25480ca0024e8962681506c75f4ffd3d343a3f0ab113
F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c
F src/resolve.c d40fe18d7c2fd0339f5846ffcf7d6809866e380acdf14c76fb2af87e9fe13f64
F src/resolve.c d3ee7ed308d46f4ee6d3bb6316d8d6f87158f93a7fd616732138cc953cf364f0
F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97
F src/select.c 700e98061a61bf8e8b0f2707ed22ffc44c7a7b660dbf7c569430e04d2f95d8a5
F src/shell.c.in 4f14a1f5196b6006abc8e73cc8fd6c1a62cf940396f8ba909d6711f35f074bb6
F src/select.c 244f2fba5f73c7ea937333bd54280e83e218a0b652fc4540cbd72d33b0f7b4d8
F src/shell.c.in 73c0eeb7c265d59b99219d5aa055f412f07842088d8036b6d259927d85dd1bbf
F src/sqlite.h.in 5c54f2461a1ea529bab8499148a2b238e2d4bb571d59e8ea5322d0c190abb693
F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479
F src/sqlite3ext.h 0bfd049bb2088cc44c2ad54f2079d1c6e43091a4e1ce8868779b75f6c1484f1e
F src/sqliteInt.h 72bf74887e551d8adf0140bf20fbc321fda7f3cef2c6dc0c5aeefa584cd713a1
F src/sqliteInt.h 9c99d7565a839ad342cdda504c4b7921bb1a24c07227b8f50b7b131245a20693
F src/sqliteLimit.h 6d817c28a8f19af95e6f4921933b7fbbca48a962bce0eb0ec81e8bb3ef38e68b
F src/status.c 0e72e4f6be6ccfde2488eb63210297e75f569f3ce9920f6c3d77590ec6ce5ffd
F src/table.c 0f141b58a16de7e2fbe81c308379e7279f4c6b50eb08efeec5892794a0ba30d1
@@ -852,7 +852,7 @@ F src/upsert.c 215328c3f91623c520ec8672c44323553f12caeb4f01b1090ebdca99fdf7b4f1
F src/utf.c 7267c3fb9e2467020507601af3354c2446c61f444387e094c779dccd5ca62165
F src/util.c 36fb1150062957280777655976f3f9a75db236cb8207a0770ceae8d5ec17fcd3
F src/vacuum.c 1bacdd0a81d2b5dc1c508fbf0d938c89fa78dd8d5b46ec92686d44030d4f4789
F src/vdbe.c d2c13c0001f5ec40ec1f010a7f9badcff3ce09bbd7a78528682ad49d8566df54
F src/vdbe.c e505b8b879a330e8dafbe3ed9582eae2fc671b44a64748d1b58c07e4e0f527da
F src/vdbe.h 93761ed7c6b8bc19524912fd9b9b587d41bf4f1d0ade650a00dadc10518d8958
F src/vdbeInt.h 0bc581a9763be385e3af715e8c0a503ba8422c2b7074922faf4bb0d6ae31b15e
F src/vdbeapi.c f9a4881a9674fec3fa13da35044a1484d3c4b95f9ec891cc8ffb02ef2b7a41df
@@ -867,9 +867,9 @@ F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
F src/wal.c 20be6f0a25a80b7897cf2a5369bfd37ef198e6f0b6cdef16d83eee856056b159
F src/wal.h ba252daaa94f889f4b2c17c027e823d9be47ce39da1d3799886bbd51f0490452
F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014
F src/where.c 21b768d47fe28aafc7607a97228c261d7ccdca5cd2bff2221418566ca608e2c5
F src/where.c 6a9266dd1a559d48d8c7ca670a3e80143c7913153f7d1ceb0a4eca1087318951
F src/whereInt.h 8d94cb116c9e06205c3d5ac87af065fc044f8cf08bfdccd94b6ea1c1308e65da
F src/wherecode.c 8e375f7c2191e2a797a89ad34fca687509dc10a012877bc07f464b3cf5f21eb7
F src/wherecode.c 2a2d2993fd98c46f525f71b3bfd330fde73d8613aa0ff3e20402dd1fc63470af
F src/whereexpr.c d007dc41364de5902181739632380afd671e14f0c5cc9978e64a2c6df8f28c6c
F src/window.c d01227141f622f24fbe36ca105fbe6ef023f9fd98f1ccd65da95f88886565db5
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
@@ -1131,7 +1131,7 @@ F test/enc.test b5503a87b31cea8a5084c6e447383f9ca08933bd2f29d97b6b6201081b2343eb
F test/enc2.test 872afe58db772e7dfa1ad8e0759f8cc820e9efc8172d460fae83023101c2e435
F test/enc3.test 55ef64416d72975c66167310a51dc9fc544ba3ae4858b8d5ab22f4cb6500b087
F test/enc4.test c8f1ce3618508fd0909945beb8b8831feef2c020
F test/eqp.test 82f221e8cd588434d7f3bba9a0f4c78cbe7a541615a41632e12f50608bfb4a99
F test/eqp.test 746db9fe11629a0d00328e1721cc2a2e4726d574b677ab14de35fd914f54cc82
F test/eqp2.test 6e8996148de88f0e7670491e92e712a2920a369b4406f21a27c3c9b6a46b68dd
F test/errmsg.test eae9f091eb39ce7e20305de45d8e5d115b68fa856fba4ea6757b6ca3705ff7f9
F test/errofst1.test 6da78363739ba8991f498396ab331b5d64e7ab5c4172c12b5884683ef523ac53
@@ -1140,6 +1140,9 @@ F test/exclusive.test 7ff63be7503990921838d5c9f77f6e33e68e48ed1a9d48cd28745bf650
F test/exclusive2.test cd70b1d9c6fffd336f9795b711dcc5d9ceba133ad3f7001da3fda63615bdc91e
F test/exec.test e949714dc127eaa5ecc7d723efec1ec27118fdd7
F test/exists.test 79a75323c78f02bbe9c251ea502a092f9ef63dac
F test/existsexpr.test 40ddd9500109579dd949cd15bbb4e3a88f79f905d1f31905b9493651f60aacf6
F test/existsexpr2.test dc23e76389eff3d29f6488ff733012a3560cd67ec8cfaecbecd52cced5d5af11
F test/existsfault.test ff41c11f3052c1bbd4f8dd557802310026253d67d7c4e3a180c16d2f0862973e
F test/expr.test 4ada8eb822c45ef27a36851a258004d43c1e95e7c82585a1217e732084e4482c
F test/expr2.test c27327ae9c017a7ff6280123f67aff496f912da74d78c888926d68b46ec75fd8
F test/exprfault.test da33606d799718e2f8e34efd0e5858884a1ad87f608774c552a7f5517cc27181
@@ -1387,7 +1390,7 @@ F test/json/json-generator.tcl dc0dd0f393800c98658fc4c47eaa6af29d4e17527380cd286
F test/json/json-q1.txt 65f9d1cdcc4cffa9823fb73ed936aae5658700cd001fde448f68bfb91c807307
F test/json/json-speed-check.sh 7d5898808ce7542762318306ae6075a30f5e7ee115c4a409f487e123afe91d88 x
F test/json/jsonb-q1.txt 1e180fe6491efab307e318b22879e3a736ac9a96539bbde7911a13ee5b33abc7
F test/json101.test 8237a484c256965eab1678fd950a32ac56325bb7d0dadbd095a46b0ddd95d62b
F test/json101.test cf53254f0f0c1399a01b21fc58fee0e63a12a556be91b9ee9faccdb8b82c083c
F test/json102.test 9b2e5ada10845ff84853b3feaae2ce51ce7145ae458f74c6a6cecc6ef6ee3ae1
F test/json103.test 355746a6b66aa438f214b4fae454b13068fad2444b5f693e0d538ad1c059b264
F test/json104.test 1b844a70cddcfa2e4cd81a5db0657b2e61e7f00868310f24f56a9ba0114348c1
@@ -1494,7 +1497,7 @@ F test/notify1.test 669b2b743618efdc18ca4b02f45423d5d2304abf
F test/notify2.test 2ecabaa1305083856b7c39cf32816b612740c161
F test/notify3.test 796c7b7157f55c93b4e672b724e9c923a6fc6aa72ac419379a623e2350472e22
F test/notnull.test a37b663d5bb728d66fc182016613fb8e4a0a4bbf3d75b8876a7527f7d4ed3f18
F test/notnull2.test 2ac7b4e04917148c7a1a9ed36df20150175ce942f07f5714375b29acbaca7106
F test/notnull2.test 5b7dd6e82c409b2d011ad6acf19ae4bf0816a9c69ccf600b529d7405d7c49874
F test/notnullfault.test fc4bb7845582a2b3db376001ef49118393b1b11abe0d24adb03db057ee2b73d5
F test/null.test b7ff206a1c60fe01aa2abd33ef9ea83c93727d993ca8a613de86e925c9f2bc6f
F test/nulls1.test 7a5e4346ee4285034100b4cd20e6784f16a9d6c927e44ecdf10034086bbee9c9
@@ -2208,8 +2211,9 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350
F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd
F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
P 5cb8e342e1d33a0fd04fd022900eeada46a2ee23f15c6abca1b37633e77429d6
R 2f815a7ddd919cbdb9dea42abcf9e107
U dan
Z 1f7246259d459232db167600e0b56367
P 043ff54fb746c54bc6cfa6aa8c8a32c876c09d36163125916ad01024b98d447b 1b9b124f9a35ebd1ac4ea70ef1ee08a4c82c11da690d4164f6b785a6fd9730d9
R f2382be8a4dd59640ba7d3e608aa0208
T +closed 1b9b124f9a35ebd1ac4ea70ef1ee08a4c82c11da690d4164f6b785a6fd9730d9
U drh
Z 2faf96645b8f6e620da8ad84b2b3ac57
# Remove this line to create a well-formed Fossil manifest.

View File

@@ -1 +1 @@
043ff54fb746c54bc6cfa6aa8c8a32c876c09d36163125916ad01024b98d447b
e33da6d5dc964db817d1bc63c9083aecd93d49ee14d5198600b47eaf7c5b9331

View File

@@ -5667,6 +5667,30 @@ int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
return rc;
}
/* Set *pRes to 1 (true) if the BTree pointed to by cursor pCur contains zero
** rows of content. Set *pRes to 0 (false) if the table contains content.
** Return SQLITE_OK on success or some error code (ex: SQLITE_NOMEM) if
** something goes wrong.
*/
int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes){
int rc;
assert( cursorOwnsBtShared(pCur) );
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
if( pCur->eState==CURSOR_VALID ){
*pRes = 0;
return SQLITE_OK;
}
rc = moveToRoot(pCur);
if( rc==SQLITE_EMPTY ){
*pRes = 1;
rc = SQLITE_OK;
}else{
*pRes = 0;
}
return rc;
}
#ifdef SQLITE_DEBUG
/* The cursors is CURSOR_VALID and has BTCF_AtLast set. Verify that
** this flags are true for a consistent database.

View File

@@ -317,6 +317,7 @@ struct BtreePayload {
int sqlite3BtreeInsert(BtCursor*, const BtreePayload *pPayload,
int flags, int seekResult);
int sqlite3BtreeFirst(BtCursor*, int *pRes);
int sqlite3BtreeIsEmpty(BtCursor *pCur, int *pRes);
int sqlite3BtreeLast(BtCursor*, int *pRes);
int sqlite3BtreeNext(BtCursor*, int flags);
int sqlite3BtreeEof(BtCursor*);

View File

@@ -5137,16 +5137,22 @@ void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
** are deleted by this function.
*/
SrcList *sqlite3SrcListAppendList(Parse *pParse, SrcList *p1, SrcList *p2){
assert( p1 && p1->nSrc==1 );
assert( p1 );
assert( p2 || pParse->nErr );
assert( p2==0 || p2->nSrc>=1 );
testcase( p1->nSrc==0 );
if( p2 ){
SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, 1);
int nOld = p1->nSrc;
SrcList *pNew = sqlite3SrcListEnlarge(pParse, p1, p2->nSrc, nOld);
if( pNew==0 ){
sqlite3SrcListDelete(pParse->db, p2);
}else{
p1 = pNew;
memcpy(&p1->a[1], p2->a, p2->nSrc*sizeof(SrcItem));
memcpy(&p1->a[nOld], p2->a, p2->nSrc*sizeof(SrcItem));
assert( nOld==1 || (p2->a[0].fg.jointype & JT_LTORJ)==0 );
assert( p1->nSrc>=1 );
p1->a[0].fg.jointype |= (JT_LTORJ & p2->a[0].fg.jointype);
sqlite3DbFree(pParse->db, p2);
p1->a[0].fg.jointype |= (JT_LTORJ & p1->a[1].fg.jointype);
}
}
return p1;

View File

@@ -1358,11 +1358,13 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
return WRC_Prune;
}
#ifndef SQLITE_OMIT_SUBQUERY
case TK_EXISTS:
case TK_SELECT:
case TK_EXISTS: testcase( pExpr->op==TK_EXISTS );
#endif
case TK_IN: {
testcase( pExpr->op==TK_IN );
testcase( pExpr->op==TK_EXISTS );
testcase( pExpr->op==TK_SELECT );
if( ExprUseXSelect(pExpr) ){
int nRef = pNC->nRef;
testcase( pNC->ncFlags & NC_IsCheck );
@@ -1370,6 +1372,7 @@ static int resolveExprStep(Walker *pWalker, Expr *pExpr){
testcase( pNC->ncFlags & NC_IdxExpr );
testcase( pNC->ncFlags & NC_GenCol );
assert( pExpr->x.pSelect );
if( pExpr->op==TK_EXISTS ) pParse->bHasExists = 1;
if( pNC->ncFlags & NC_SelfRef ){
notValidImpl(pParse, pNC, "subqueries", pExpr, pExpr);
}else{

View File

@@ -384,7 +384,7 @@ static int tableAndColumnIndex(
int iEnd, /* Last member of pSrc->a[] to check */
const char *zCol, /* Name of the column we are looking for */
int *piTab, /* Write index of pSrc->a[] here */
int *piCol, /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
int *piCol, /* Write index of pSrc->a[*piTab].pSTab->aCol[] here */
int bIgnoreHidden /* Ignore hidden columns */
){
int i; /* For looping over tables in pSrc */
@@ -3036,8 +3036,10 @@ static int multiSelect(
int priorOp; /* The SRT_ operation to apply to prior selects */
Expr *pLimit; /* Saved values of p->nLimit */
int addr;
int emptyBypass = 0; /* IfEmpty opcode to bypass RHS */
SelectDest uniondest;
testcase( p->op==TK_EXCEPT );
testcase( p->op==TK_UNION );
priorOp = SRT_Union;
@@ -3075,6 +3077,8 @@ static int multiSelect(
*/
if( p->op==TK_EXCEPT ){
op = SRT_Except;
emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, unionTab);
VdbeCoverage(v);
}else{
assert( p->op==TK_UNION );
op = SRT_Union;
@@ -3095,6 +3099,7 @@ static int multiSelect(
if( p->op==TK_UNION ){
p->nSelectRow = sqlite3LogEstAdd(p->nSelectRow, pPrior->nSelectRow);
}
if( emptyBypass ) sqlite3VdbeJumpHere(v, emptyBypass);
sqlite3ExprDelete(db, p->pLimit);
p->pLimit = pLimit;
p->iLimit = 0;
@@ -3128,6 +3133,7 @@ static int multiSelect(
int addr;
SelectDest intersectdest;
int r1;
int emptyBypass;
/* INTERSECT is different from the others since it requires
** two temporary tables. Hence it has its own case. Begin
@@ -3151,6 +3157,7 @@ static int multiSelect(
if( rc ){
goto multi_select_end;
}
emptyBypass = sqlite3VdbeAddOp1(v, OP_IfEmpty, tab1); VdbeCoverage(v);
/* Code the current SELECT into temporary table "tab2"
*/
@@ -3182,7 +3189,7 @@ static int multiSelect(
iBreak = sqlite3VdbeMakeLabel(pParse);
iCont = sqlite3VdbeMakeLabel(pParse);
computeLimitRegisters(pParse, p, iBreak);
sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak); VdbeCoverage(v);
sqlite3VdbeAddOp1(v, OP_Rewind, tab1);
r1 = sqlite3GetTempReg(pParse);
iStart = sqlite3VdbeAddOp2(v, OP_RowData, tab1, r1);
sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
@@ -3194,6 +3201,7 @@ static int multiSelect(
sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart); VdbeCoverage(v);
sqlite3VdbeResolveLabel(v, iBreak);
sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
sqlite3VdbeJumpHere(v, emptyBypass);
sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
break;
}
@@ -4650,7 +4658,7 @@ static int flattenSubquery(
** complete, since there may still exist Expr.pTab entries that
** refer to the subquery even after flattening. Ticket #3346.
**
** pSubitem->pTab is always non-NULL by test restrictions and tests above.
** pSubitem->pSTab is always non-NULL by test restrictions and tests above.
*/
if( ALWAYS(pSubitem->pSTab!=0) ){
Table *pTabToDel = pSubitem->pSTab;
@@ -5764,7 +5772,7 @@ With *sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
** CTE expression, through routine checks to see if the reference is
** a recursive reference to the CTE.
**
** If pFrom matches a CTE according to either of these two above, pFrom->pTab
** If pFrom matches a CTE according to either of these two above, pFrom->pSTab
** and other fields are populated accordingly.
**
** Return 0 if no match is found.
@@ -7391,6 +7399,82 @@ static int fromClauseTermCanBeCoroutine(
return 1;
}
/*
** Argument pWhere is the WHERE clause belonging to SELECT statement p. This
** function attempts to transform expressions of the form:
**
** EXISTS (SELECT ...)
**
** into joins. For example, given
**
** CREATE TABLE sailors(sid INTEGER PRIMARY KEY, name TEXT);
** CREATE TABLE reserves(sid INT, day DATE, PRIMARY KEY(sid, day));
**
** SELECT name FROM sailors AS S WHERE EXISTS (
** SELECT * FROM reserves AS R WHERE S.sid = R.sid AND R.day = '2022-10-25'
** );
**
** the SELECT statement may be transformed as follows:
**
** SELECT name FROM sailors AS S, reserves AS R
** WHERE S.sid = R.sid AND R.day = '2022-10-25';
**
** **Approximately**. Really, we have to ensure that the FROM-clause term
** that was formerly inside the EXISTS is only executed once. This is handled
** by setting the SrcItem.fg.fromExists flag, which then causes code in
** the where.c file to exit the corresponding loop after the first successful
** match (if any).
*/
static SQLITE_NOINLINE void existsToJoin(
Parse *pParse, /* Parsing context */
Select *p, /* The SELECT statement being optimized */
Expr *pWhere /* part of the WHERE clause currently being examined */
){
if( pWhere
&& !ExprHasProperty(pWhere, EP_OuterON|EP_InnerON)
&& p->pSrc->nSrc<BMS
&& pParse->db->mallocFailed==0
){
if( pWhere->op==TK_AND ){
Expr *pRight = pWhere->pRight;
existsToJoin(pParse, p, pWhere->pLeft);
existsToJoin(pParse, p, pRight);
}
else if( pWhere->op==TK_EXISTS ){
Select *pSub = pWhere->x.pSelect;
Expr *pSubWhere = pSub->pWhere;
if( pSub->pSrc->nSrc==1
&& (pSub->selFlags & SF_Aggregate)==0
&& !pSub->pSrc->a[0].fg.isSubquery
){
memset(pWhere, 0, sizeof(*pWhere));
pWhere->op = TK_INTEGER;
pWhere->u.iValue = 1;
ExprSetProperty(pWhere, EP_IntValue);
assert( p->pWhere!=0 );
pSub->pSrc->a[0].fg.fromExists = 1;
pSub->pSrc->a[0].fg.jointype |= JT_CROSS;
p->pSrc = sqlite3SrcListAppendList(pParse, p->pSrc, pSub->pSrc);
if( pSubWhere ){
p->pWhere = sqlite3PExpr(pParse, TK_AND, p->pWhere, pSubWhere);
pSub->pWhere = 0;
}
pSub->pSrc = 0;
sqlite3ParserAddCleanup(pParse, sqlite3SelectDeleteGeneric, pSub);
#if TREETRACE_ENABLED
if( sqlite3TreeTrace & 0x100000 ){
TREETRACE(0x100000,pParse,p,
("After EXISTS-to-JOIN optimization:\n"));
sqlite3TreeViewSelect(0, p, 0);
}
#endif
existsToJoin(pParse, p, pSubWhere);
}
}
}
}
/*
** Generate byte-code for the SELECT statement given in the p argument.
**
@@ -7759,6 +7843,13 @@ int sqlite3Select(
}
#endif
/* If there may be an "EXISTS (SELECT ...)" in the WHERE clause, attempt
** to change it into a join. */
if( pParse->bHasExists && OptimizationEnabled(db,SQLITE_ExistsToJoin) ){
existsToJoin(pParse, p, p->pWhere);
pTabList = p->pSrc;
}
/* Do the WHERE-clause constant propagation optimization if this is
** a join. No need to spend time on this operation for non-join queries
** as the equivalent optimization will be handled by query planner in

View File

@@ -11710,6 +11710,7 @@ static int do_meta_command(char *zLine, ShellState *p){
{ 0x08000000, 1, "OnePass" },
{ 0x10000000, 1, "OrderBySubq" },
{ 0x20000000, 1, "StarQuery" },
{ 0x40000000, 1, "ExistsToJoin" },
{ 0xffffffff, 0, "All" },
};
unsigned int curOpt;

View File

@@ -1154,6 +1154,7 @@ extern u32 sqlite3TreeTrace;
** 0x00040000 SELECT tree dump after all code has been generated
** 0x00080000 NOT NULL strength reduction
** 0x00100000 Pointers are all shown as zero
** 0x00200000 EXISTS-to-JOIN optimization
*/
/*
@@ -1926,6 +1927,7 @@ struct sqlite3 {
#define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
#define SQLITE_OrderBySubq 0x10000000 /* ORDER BY in subquery helps outer */
#define SQLITE_StarQuery 0x20000000 /* Heurists for star queries */
#define SQLITE_ExistsToJoin 0x40000000 /* The EXISTS-to-JOIN optimization */
#define SQLITE_AllOpts 0xffffffff /* All optimizations */
/*
@@ -3369,6 +3371,7 @@ struct SrcItem {
unsigned rowidUsed :1; /* The ROWID of this table is referenced */
unsigned fixedSchema :1; /* Uses u4.pSchema, not u4.zDatabase */
unsigned hadSchema :1; /* Had u4.zDatabase before u4.pSchema */
unsigned fromExists :1; /* Comes from WHERE EXISTS(...) */
} fg;
int iCursor; /* The VDBE cursor number used to access this table */
Bitmask colUsed; /* Bit N set if column N used. Details above for N>62 */
@@ -3899,6 +3902,7 @@ struct Parse {
u8 disableLookaside; /* Number of times lookaside has been disabled */
u8 prepFlags; /* SQLITE_PREPARE_* flags */
u8 withinRJSubrtn; /* Nesting level for RIGHT JOIN body subroutines */
u8 bHasExists; /* Has a correlated "EXISTS (SELECT ....)" expression */
u8 mSubrtnSig; /* mini Bloom filter on available SubrtnSig.selId */
u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
u8 bReturning; /* Coding a RETURNING trigger */

View File

@@ -6398,6 +6398,32 @@ case OP_Rewind: { /* jump0, ncycle */
break;
}
/* Opcode: IfEmpty P1 P2 * * *
** Synopsis: if( empty(P1) ) goto P2
**
** Check to see if the b-tree table that cursor P1 references is empty
** and jump to P2 if it is.
*/
case OP_IfEmpty: { /* jump */
VdbeCursor *pC;
BtCursor *pCrsr;
int res;
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
assert( pOp->p2>=0 && pOp->p2<p->nOp );
pC = p->apCsr[pOp->p1];
assert( pC!=0 );
assert( pC->eCurType==CURTYPE_BTREE );
pCrsr = pC->uc.pCursor;
assert( pCrsr );
rc = sqlite3BtreeIsEmpty(pCrsr, &res);
if( rc ) goto abort_due_to_error;
VdbeBranchTaken(res!=0,2);
if( res ) goto jump_to_p2;
break;
}
/* Opcode: Next P1 P2 P3 * P5
**
** Advance cursor P1 so that it points to the next key/data pair in its

View File

@@ -3532,6 +3532,7 @@ static int whereLoopAddBtreeIndex(
&& pProbe->hasStat1!=0
&& OptimizationEnabled(db, SQLITE_SkipScan)
&& pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
&& pSrc->fg.fromExists==0
&& (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
){
LogEst nIter;
@@ -7148,6 +7149,13 @@ WhereInfo *sqlite3WhereBegin(
sqlite3VdbeAddOp4Dup8(v, OP_ColumnsUsed, pTabItem->iCursor, 0, 0,
(const u8*)&pTabItem->colUsed, P4_INT64);
#endif
if( ii>=2
&& (pTabItem[0].fg.jointype & (JT_LTORJ|JT_LEFT))==0
&& pLevel->addrHalt==pWInfo->a[0].addrHalt
){
sqlite3VdbeAddOp2(v, OP_IfEmpty, pTabItem->iCursor, pWInfo->iBreak);
VdbeCoverage(v);
}
}else{
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
}
@@ -7404,6 +7412,9 @@ void sqlite3WhereEnd(WhereInfo *pWInfo){
sqlite3VdbeAddOp2(v, OP_Goto, 1, pLevel->p2);
}
#endif /* SQLITE_DISABLE_SKIPAHEAD_DISTINCT */
if( pTabList->a[pLevel->iFrom].fg.fromExists ){
sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3VdbeCurrentAddr(v)+2);
}
/* The common case: Advance to the next row */
if( pLevel->addrCont ) sqlite3VdbeResolveLabel(v, pLevel->addrCont);
sqlite3VdbeAddOp3(v, pLevel->op, pLevel->p1, pLevel->p2, pLevel->p3);

View File

@@ -126,7 +126,6 @@ void sqlite3WhereAddExplainText(
#endif
{
VdbeOp *pOp = sqlite3VdbeGetOp(pParse->pVdbe, addr);
SrcItem *pItem = &pTabList->a[pLevel->iFrom];
sqlite3 *db = pParse->db; /* Database handle */
int isSearch; /* True for a SEARCH. False for SCAN. */
@@ -149,7 +148,10 @@ void sqlite3WhereAddExplainText(
sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
str.printfFlags = SQLITE_PRINTF_INTERNAL;
sqlite3_str_appendf(&str, "%s %S", isSearch ? "SEARCH" : "SCAN", pItem);
sqlite3_str_appendf(&str, "%s %S%s",
isSearch ? "SEARCH" : "SCAN",
pItem,
pItem->fg.fromExists ? " EXISTS" : "");
if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
const char *zFmt = 0;
Index *pIdx;

View File

@@ -338,8 +338,7 @@ det 3.3.3 {
} {
QUERY PLAN
|--SCAN t1
`--CORRELATED SCALAR SUBQUERY xxxxxx
`--SCAN t2
`--SCAN t2 EXISTS
}
#-------------------------------------------------------------------------

426
test/existsexpr.test Normal file
View File

@@ -0,0 +1,426 @@
# 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
set testprefix existsexpr
do_execsql_test 1.0 {
CREATE TABLE x1(a, b, PRIMARY KEY(a)) WITHOUT ROWID;
INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6);
CREATE INDEX x1b ON x1(b);
CREATE TABLE x2(x, y);
INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6);
}
do_execsql_test 1.1 {
SELECT 1 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=5)
} {1}
do_execsql_test 1.2 {
SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x)
} {1 2 3 4 5 6}
# With "a=x", the UNIQUE index means the EXIST can be transformed to a join.
# So no "SUBQUERY". With "b=x", the index is not UNIQUE and so there is a
# "SUBQUERY".
do_execsql_test 1.3.1 {
EXPLAIN QUERY PLAN
SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x)
} {~/SUBQUERY/}
do_execsql_test 1.3.2 {
EXPLAIN QUERY PLAN
SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE b=x)
} {~/SUBQUERY/}
do_execsql_test 1.4.1 {
EXPLAIN QUERY PLAN
SELECT * FROM x2 WHERE x=1 AND EXISTS (SELECT 1 FROM x1 WHERE a=x)
} {~/SUBQUERY/}
do_execsql_test 1.4.2 {
EXPLAIN QUERY PLAN
SELECT * FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x) AND y=2
} {~/SUBQUERY/}
do_execsql_test 1.5 {
SELECT count(*) FROM x2 WHERE EXISTS (SELECT 1 FROM x1 WHERE a=x)
} {3}
#-------------------------------------------------------------------------
do_execsql_test 2.0 {
CREATE TABLE t1(a, b);
WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<1000
) INSERT INTO t1 SELECT i, i FROM s;
CREATE TABLE t2(c, d);
WITH s(i) AS (
SELECT 10 UNION ALL SELECT i+10 FROM s WHERE i<1000
) INSERT INTO t2 SELECT i, i FROM s;
}
do_execsql_test 2.1 {
SELECT count(*) FROM t1;
SELECT count(*) FROM t2;
} {1000 100}
do_execsql_test 2.2 {
SELECT count(*) FROM t1, t2 WHERE a=c;
} {100}
do_execsql_test 2.3 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a)
} {100}
do_eqp_test 2.4 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a)
} {SCAN t1}
do_execsql_test 2.4.0 {
CREATE UNIQUE INDEX t2c ON t2(c);
CREATE UNIQUE INDEX t1a ON t1(a);
}
do_eqp_test 2.4.1 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a);
} {SCAN t1*t2 EXISTS}
do_execsql_test 2.4.2 {
ANALYZE;
}
do_eqp_test 2.4.3 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a);
} {SCAN t1*t2 EXISTS}
do_execsql_test 2.4.4 {
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c=a);
} {100}
do_execsql_test 2.5.1 {
EXPLAIN QUERY PLAN
SELECT count(*) FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.rowid=a);
} {~/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 0 {
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 0 {
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 0 {
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.1.1 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE (a COLLATE nocase)=x AND b=y
)
} {
A a b b C c D d
}
do_subquery_test 4.1.2 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x AND (b COLLATE binary)=y
)
} {
A a b b C c D d
}
do_subquery_test 4.1.1 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE x=(a COLLATE nocase) AND b=y
)
} {
A a b b C c D d
}
do_subquery_test 4.1.2 0 {
SELECT * FROM tx2 WHERE EXISTS (
SELECT 1 FROM tx1 WHERE a=x AND y=(b COLLATE binary)
)
} {
A a b b C c D d
}
do_subquery_test 4.2 0 {
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 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.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 0 "
SELECT o FROM a2 WHERE EXISTS (
SELECT 1 FROM a1 WHERE [exprs 10 73]
)
" {
2 5
}
do_subquery_test 5.7 0 "
SELECT o FROM a2 WHERE EXISTS (
SELECT 1 FROM a1 WHERE [exprs 20 84]
)
" {
2 5
}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 6.0 {
CREATE TABLE t1(a, b UNIQUE, c UNIQUE);
CREATE TABLE t2(a INfEGER PRIMARY KEY, b);
CREATE UNIQUE INDEX t2b ON t2(b);
}
do_catchsql_test 6.1 {
SELECT a FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE c COLLATE f = a)
} {1 {no such collation sequence: f}}
#-------------------------------------------------------------------------
reset_db
do_execsql_test 7.0 {
CREATE TABLE t1(x);
CREATE TABLE t2(y UNIQUE);
INSERT INTO t1 VALUES(1), (2);
INSERT INTO t2 VALUES(1), (3);
SELECT * FROM t1 one LEFT JOIN t1 two ON (one.x=two.x AND EXISTS (
SELECT 1 FROM t2 WHERE y=one.x
));
} {
1 1
2 {}
}
finish_test

96
test/existsexpr2.test Normal file
View File

@@ -0,0 +1,96 @@
# 2024 June 14
#
# 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
set testprefix existsexpr2
do_execsql_test 1.0 {
CREATE TABLE x1(a, b, PRIMARY KEY(a)) WITHOUT ROWID;
INSERT INTO x1 VALUES(1, 2), (3, 4), (5, 6);
CREATE INDEX x1b ON x1(b);
CREATE TABLE x2(x, y);
INSERT INTO x2 VALUES(1, 2), (3, 4), (5, 6);
}
do_execsql_test 1.1 {
SELECT * FROM x1 WHERE EXISTS (SELECT 1 FROM x2 WHERE a!=123)
} {1 2 3 4 5 6}
do_execsql_test 1.2 {
CREATE TABLE x3(u, v);
CREATE INDEX x3u ON x3(u);
INSERT INTO x3 VALUES
(1, 1), (1, 2), (1, 3),
(2, 1), (2, 2), (2, 3);
}
do_execsql_test 1.3 {
SELECT * FROM x1 WHERE EXISTS (
SELECT 1 FROM x3 WHERE u IN (1, 2, 3, 4) AND v=b
);
} {
1 2
}
#-------------------------------------------------------------------------
#
reset_db
do_execsql_test 2.0 {
CREATE TABLE t1(a, b, c);
CREATE INDEX t1ab ON t1(a,b);
INSERT INTO t1 VALUES
('abc', 1, 1),
('abc', 2, 2),
('abc', 2, 3),
('def', 1, 1),
('def', 2, 2),
('def', 2, 3);
CREATE TABLE t2(x, y);
INSERT INTO t2 VALUES(1, 1), (2, 2), (3, 3);
ANALYZE;
DELETE FROM sqlite_stat1;
INSERT INTO sqlite_stat1 VALUES('t1','t1ab','10000 5000 2');
ANALYZE sqlite_master;
}
do_execsql_test 2.1 {
SELECT a,b,c FROM t1 WHERE b=2 ORDER BY a
} {
abc 2 2
abc 2 3
def 2 2
def 2 3
}
do_execsql_test 2.2 {
SELECT x, y FROM t2 WHERE EXISTS (
SELECT 1 FROM t1 WHERE b=x
)
} {
1 1
2 2
}
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

View File

@@ -892,15 +892,15 @@ do_execsql_test json101-13.100 {
INSERT INTO t2(id,json) VALUES(4,'{"value":4}');
INSERT INTO t2(id,json) VALUES(5,'{"value":5}');
INSERT INTO t2(id,json) VALUES(6,'{"value":6}');
SELECT * FROM t1 CROSS JOIN t2
SELECT *, 'NL' FROM t1 CROSS JOIN t2
WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
WHERE Z.value==t2.id);
} {1 {{"items":[3,5]}} 3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}}}
} {1 {{"items":[3,5]}} 3 {{"value":3}} NL 1 {{"items":[3,5]}} 5 {{"value":5}} NL}
do_execsql_test json101-13.110 {
SELECT * FROM t2 CROSS JOIN t1
SELECT *, 'NL' FROM t2 CROSS JOIN t1
WHERE EXISTS(SELECT 1 FROM json_each(t1.json,'$.items') AS Z
WHERE Z.value==t2.id);
} {3 {{"value":3}} 1 {{"items":[3,5]}} 5 {{"value":5}} 1 {{"items":[3,5]}}}
} {3 {{"value":3}} 1 {{"items":[3,5]}} NL 5 {{"value":5}} 1 {{"items":[3,5]}} NL}
# 2018-05-16
# Incorrect fullkey output from json_each()

View File

@@ -66,7 +66,7 @@ do_vmstep_test 1.5.2 {
SELECT count(*) FROM t2 WHERE EXISTS(
SELECT 1 FROM t1 WHERE t1.a=450 AND t2.c IS NULL
)
} +8000 {0}
} 4000 {0}
#-------------------------------------------------------------------------
reset_db