Index: ext/fts3/fts3_hash.c ================================================================== --- ext/fts3/fts3_hash.c +++ ext/fts3/fts3_hash.c @@ -94,17 +94,17 @@ /* ** Hash and comparison functions when the mode is FTS3_HASH_STRING */ static int fts3StrHash(const void *pKey, int nKey){ const char *z = (const char *)pKey; - int h = 0; + unsigned h = 0; if( nKey<=0 ) nKey = (int) strlen(z); while( nKey > 0 ){ h = (h<<3) ^ h ^ *z++; nKey--; } - return h & 0x7fffffff; + return (int)(h & 0x7fffffff); } static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){ if( n1!=n2 ) return 1; return strncmp((const char*)pKey1,(const char*)pKey2,n1); } Index: src/delete.c ================================================================== --- src/delete.c +++ src/delete.c @@ -641,11 +641,12 @@ /* Populate the OLD.* pseudo-table register array. These values will be ** used by any BEFORE and AFTER triggers that exist. */ sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld); for(iCol=0; iColnCol; iCol++){ - if( mask==0xffffffff || mask&(1<a[i].pExpr) ){ - constMask |= (1<funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){ pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr); } } Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -135,11 +135,11 @@ UNUSED_PARAMETER(argc); switch( sqlite3_value_type(argv[0]) ){ case SQLITE_INTEGER: { i64 iVal = sqlite3_value_int64(argv[0]); if( iVal<0 ){ - if( (iVal<<1)==0 ){ + if( iVal==SMALLEST_INT64 ){ /* IMP: R-31676-45509 If X is the integer -9223372036854775808 ** then abs(X) throws an integer overflow error since there is no ** equivalent positive 64-bit two complement value. */ sqlite3_result_error(context, "integer overflow", -1); return; Index: src/hash.c ================================================================== --- src/hash.c +++ src/hash.c @@ -51,11 +51,11 @@ /* ** The hashing function. */ static unsigned int strHash(const char *z, int nKey){ - int h = 0; + unsigned int h = 0; assert( nKey>=0 ); while( nKey > 0 ){ h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++]; nKey--; } Index: src/main.c ================================================================== --- src/main.c +++ src/main.c @@ -3322,11 +3322,11 @@ ** database files might have arbitrary corruption. Setting the flag during ** testing causes certain assert() statements in the code to be activated ** that demonstrat invariants on well-formed database files. */ case SQLITE_TESTCTRL_NEVER_CORRUPT: { - sqlite3Config.neverCorrupt = va_arg(ap, int); + sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int); break; } } va_end(ap); Index: src/os_unix.c ================================================================== --- src/os_unix.c +++ src/os_unix.c @@ -4095,11 +4095,11 @@ /* Update the global lock state and do debug tracing */ #ifdef SQLITE_DEBUG { u16 mask; OSTRACE(("SHM-LOCK ")); - mask = ofst>31 ? 0xffffffff : (1<<(ofst+n)) - (1<31 ? 0xffff : (1<<(ofst+n)) - (1<exclMask &= ~mask; pShmNode->sharedMask &= ~mask; Index: src/pcache1.c ================================================================== --- src/pcache1.c +++ src/pcache1.c @@ -718,10 +718,11 @@ unsigned int nPinned; PCache1 *pCache = (PCache1 *)p; PGroup *pGroup; PgHdr1 *pPage = 0; + assert( offsetof(PgHdr1,page)==0 ); assert( pCache->bPurgeable || createFlag!=1 ); assert( pCache->bPurgeable || pCache->nMin==0 ); assert( pCache->bPurgeable==0 || pCache->nMin==10 ); assert( pCache->nMin==0 || pCache->bPurgeable ); pcache1EnterMutex(pGroup = pCache->pGroup); @@ -823,11 +824,11 @@ fetch_out: if( pPage && iKey>pCache->iMaxKey ){ pCache->iMaxKey = iKey; } pcache1LeaveMutex(pGroup); - return &pPage->page; + return (sqlite3_pcache_page*)pPage; } /* ** Implementation of the sqlite3_pcache.xUnpin method. Index: src/select.c ================================================================== --- src/select.c +++ src/select.c @@ -1723,11 +1723,48 @@ if( pRet==0 && iColpEList->nExpr ){ pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr); } return pRet; } -#endif /* SQLITE_OMIT_COMPOUND_SELECT */ + +/* +** The select statement passed as the second parameter is a compound SELECT +** with an ORDER BY clause. This function allocates and returns a KeyInfo +** structure suitable for implementing the ORDER BY. +** +** Space to hold the KeyInfo structure is obtained from malloc. The calling +** function is responsible for ensuring that this structure is eventually +** freed. +*/ +static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){ + ExprList *pOrderBy = p->pOrderBy; + int nOrderBy = p->pOrderBy->nExpr; + sqlite3 *db = pParse->db; + KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1); + if( pRet ){ + int i; + for(i=0; ia[i]; + Expr *pTerm = pItem->pExpr; + CollSeq *pColl; + + if( pTerm->flags & EP_Collate ){ + pColl = sqlite3ExprCollSeq(pParse, pTerm); + }else{ + pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1); + if( pColl==0 ) pColl = db->pDfltColl; + pOrderBy->a[i].pExpr = + sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName); + } + assert( sqlite3KeyInfoIsWriteable(pRet) ); + pRet->aColl[i] = pColl; + pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder; + } + } + + return pRet; +} #ifndef SQLITE_OMIT_CTE /* ** This routine generates VDBE code to compute the content of a WITH RECURSIVE ** query of the form: @@ -1797,23 +1834,20 @@ pOffset = p->pOffset; regLimit = p->iLimit; regOffset = p->iOffset; p->pLimit = p->pOffset = 0; p->iLimit = p->iOffset = 0; + pOrderBy = p->pOrderBy; /* Locate the cursor number of the Current table */ for(i=0; ALWAYS(inSrc); i++){ if( pSrc->a[i].isRecursive ){ iCurrent = pSrc->a[i].iCursor; break; } } - /* Detach the ORDER BY clause from the compound SELECT */ - pOrderBy = p->pOrderBy; - p->pOrderBy = 0; - /* Allocate cursors numbers for Queue and Distinct. The cursor number for ** the Distinct table must be exactly one greater than Queue in order ** for the SRT_DistTable and SRT_DistQueue destinations to work. */ iQueue = pParse->nTab++; if( p->op==TK_UNION ){ @@ -1826,11 +1860,11 @@ /* Allocate cursors for Current, Queue, and Distinct. */ regCurrent = ++pParse->nMem; sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol); if( pOrderBy ){ - KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 1); + KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1); sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0, (char*)pKeyInfo, P4_KEYINFO); destQueue.pOrderBy = pOrderBy; }else{ sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol); @@ -1838,10 +1872,13 @@ VdbeComment((v, "Queue table")); if( iDistinct ){ p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0); p->selFlags |= SF_UsesEphemeral; } + + /* Detach the ORDER BY clause from the compound SELECT */ + p->pOrderBy = 0; /* Store the results of the setup-query in Queue. */ rc = sqlite3Select(pParse, pSetup, &destQueue); if( rc ) goto end_of_recursive_query; @@ -1881,21 +1918,20 @@ p->pOrderBy = pOrderBy; p->pLimit = pLimit; p->pOffset = pOffset; return; } -#endif +#endif /* SQLITE_OMIT_CTE */ /* Forward references */ static int multiSelectOrderBy( Parse *pParse, /* Parsing context */ Select *p, /* The right-most of SELECTs to be coded */ SelectDest *pDest /* What to do with query results */ ); -#ifndef SQLITE_OMIT_COMPOUND_SELECT /* ** This routine is called to process a compound query form from ** two or more separate queries using UNION, UNION ALL, EXCEPT, or ** INTERSECT ** @@ -2623,28 +2659,11 @@ for(i=0, pItem=pOrderBy->a; iu.x.iOrderByCol>0 && pItem->u.x.iOrderByCol<=p->pEList->nExpr ); aPermute[i] = pItem->u.x.iOrderByCol - 1; } - pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1); - if( pKeyMerge ){ - for(i=0; ia[i].pExpr; - if( pTerm->flags & EP_Collate ){ - pColl = sqlite3ExprCollSeq(pParse, pTerm); - }else{ - pColl = multiSelectCollSeq(pParse, p, aPermute[i]); - if( pColl==0 ) pColl = db->pDfltColl; - pOrderBy->a[i].pExpr = - sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName); - } - assert( sqlite3KeyInfoIsWriteable(pKeyMerge) ); - pKeyMerge->aColl[i] = pColl; - pKeyMerge->aSortOrder[i] = pOrderBy->a[i].sortOrder; - } - } + pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1); }else{ pKeyMerge = 0; } /* Reattach the ORDER BY clause to the query. Index: src/sqliteInt.h ================================================================== --- src/sqliteInt.h +++ src/sqliteInt.h @@ -1988,10 +1988,11 @@ /* ** A bit in a Bitmask */ #define MASKBIT(n) (((Bitmask)1)<<(n)) +#define MASKBIT32(n) (((unsigned int)1)<<(n)) /* ** The following structure describes the FROM clause of a SELECT statement. ** Each table or subquery in the FROM clause is a separate element of ** the SrcList.a[] array. Index: src/tclsqlite.c ================================================================== --- src/tclsqlite.c +++ src/tclsqlite.c @@ -915,11 +915,11 @@ const char *zArg1, const char *zArg2, const char *zArg3, const char *zArg4 ){ - char *zCode; + const char *zCode; Tcl_DString str; int rc; const char *zReply; SqliteDb *pDb = (SqliteDb*)pArg; if( pDb->disableAuth ) return SQLITE_OK; @@ -1041,11 +1041,11 @@ static int DbTransPostCmd( ClientData data[], /* data[0] is the Sqlite3Db* for $db */ Tcl_Interp *interp, /* Tcl interpreter */ int result /* Result of evaluating SCRIPT */ ){ - static const char *azEnd[] = { + static const char *const azEnd[] = { "RELEASE _tcl_transaction", /* rc==TCL_ERROR, nTransaction!=0 */ "COMMIT", /* rc!=TCL_ERROR, nTransaction==0 */ "ROLLBACK TO _tcl_transaction ; RELEASE _tcl_transaction", "ROLLBACK" /* rc==TCL_ERROR, nTransaction==0 */ }; @@ -2020,11 +2020,11 @@ }else if( objc==2 ){ if( pDb->zCommit ){ Tcl_AppendResult(interp, pDb->zCommit, 0); } }else{ - char *zCommit; + const char *zCommit; int len; if( pDb->zCommit ){ Tcl_Free(pDb->zCommit); } zCommit = Tcl_GetStringFromObj(objv[2], &len); @@ -2093,18 +2093,18 @@ int nSep; /* Number of bytes in zSep[] */ int nNull; /* Number of bytes in zNull[] */ char *zSql; /* An SQL statement */ char *zLine; /* A single line of input from the file */ char **azCol; /* zLine[] broken up into columns */ - char *zCommit; /* How to commit changes */ + const char *zCommit; /* How to commit changes */ FILE *in; /* The input file */ int lineno = 0; /* Line number of input file */ char zLineNum[80]; /* Line number print buffer */ Tcl_Obj *pResult; /* interp result */ - char *zSep; - char *zNull; + const char *zSep; + const char *zNull; if( objc<5 || objc>7 ){ Tcl_WrongNumArgs(interp, 2, objv, "CONFLICT-ALGORITHM TABLE FILENAME ?SEPARATOR? ?NULLINDICATOR?"); return TCL_ERROR; } @@ -3079,11 +3079,11 @@ #endif if( objc==2 ){ zArg = Tcl_GetStringFromObj(objv[1], 0); if( strcmp(zArg,"-version")==0 ){ - Tcl_AppendResult(interp,sqlite3_version,0); + Tcl_AppendResult(interp,sqlite3_libversion(),0); return TCL_OK; } if( strcmp(zArg,"-has-codec")==0 ){ #ifdef SQLITE_HAS_CODEC Tcl_AppendResult(interp,"1",0); @@ -3161,11 +3161,11 @@ return TCL_ERROR; } zErrMsg = 0; p = (SqliteDb*)Tcl_Alloc( sizeof(*p) ); if( p==0 ){ - Tcl_SetResult(interp, "malloc failed", TCL_STATIC); + Tcl_SetResult(interp, (char *)"malloc failed", TCL_STATIC); return TCL_ERROR; } memset(p, 0, sizeof(*p)); zFile = Tcl_GetStringFromObj(objv[2], 0); zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename); Index: src/test8.c ================================================================== --- src/test8.c +++ src/test8.c @@ -890,11 +890,11 @@ pIdxInfo->needToFreeIdxStr = 1; if( useCost ){ pIdxInfo->estimatedCost = cost; }else if( useIdx ){ /* Approximation of log2(nRow). */ - for( ii=0; ii<(sizeof(int)*8); ii++ ){ + for( ii=0; ii<(sizeof(int)*8)-1; ii++ ){ if( nRow & (1<estimatedCost = (double)ii; } } }else{ Index: src/update.c ================================================================== --- src/update.c +++ src/update.c @@ -465,13 +465,14 @@ oldmask |= sqlite3TriggerColmask(pParse, pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError ); for(i=0; inCol; i++){ if( oldmask==0xffffffff - || (i<32 && (oldmask & (1<aCol[i].colFlags & COLFLAG_PRIMKEY)!=0 ){ + testcase( oldmask!=0xffffffff && i==31 ); sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i); }else{ sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i); } } @@ -502,11 +503,11 @@ sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i); }else{ j = aXRef[i]; if( j>=0 ){ sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i); - }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<31 || (newmask & MASKBIT32(i)) ){ /* This branch loads the value of a column that will not be changed ** into a register. This is done if there are no BEFORE triggers, or ** if there are one or more BEFORE triggers that use this value via ** a new.* reference in a trigger program. */ Index: src/util.c ================================================================== --- src/util.c +++ src/util.c @@ -999,11 +999,12 @@ /* ** Read or write a four-byte big-endian integer value. */ u32 sqlite3Get4byte(const u8 *p){ - return (p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; + testcase( p[0]&0x80 ); + return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; } void sqlite3Put4byte(unsigned char *p, u32 v){ p[0] = (u8)(v>>24); p[1] = (u8)(v>>16); p[2] = (u8)(v>>8); Index: src/vdbe.c ================================================================== --- src/vdbe.c +++ src/vdbe.c @@ -6233,11 +6233,11 @@ #ifdef SQLITE_USE_FCNTL_TRACE zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql); if( zTrace ){ int i; for(i=0; inDb; i++){ - if( ((1<btreeMask)==0 ) continue; + if( MASKBIT(i) & p->btreeMask)==0 ) continue; sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace); } } #endif /* SQLITE_USE_FCNTL_TRACE */ #ifdef SQLITE_DEBUG Index: src/vdbeaux.c ================================================================== --- src/vdbeaux.c +++ src/vdbeaux.c @@ -2614,12 +2614,13 @@ void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){ AuxData **pp = &pVdbe->pAuxData; while( *pp ){ AuxData *pAux = *pp; if( (iOp<0) - || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & ((u32)1<iArg)))) + || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg)))) ){ + testcase( pAux->iArg==31 ); if( pAux->xDelete ){ pAux->xDelete(pAux->pAux); } *pp = pAux->pNext; sqlite3DbFree(pVdbe->db, pAux); @@ -2930,10 +2931,13 @@ u32 sqlite3VdbeSerialGet( const unsigned char *buf, /* Buffer to deserialize from */ u32 serial_type, /* Serial type to deserialize */ Mem *pMem /* Memory cell to write value into */ ){ + u64 x; + u32 y; + int i; switch( serial_type ){ case 10: /* Reserved for future use */ case 11: /* Reserved for future use */ case 0: { /* NULL */ pMem->flags = MEM_Null; @@ -2943,36 +2947,37 @@ pMem->u.i = (signed char)buf[0]; pMem->flags = MEM_Int; return 1; } case 2: { /* 2-byte signed integer */ - pMem->u.i = (((signed char)buf[0])<<8) | buf[1]; + i = 256*(signed char)buf[0] | buf[1]; + pMem->u.i = (i64)i; pMem->flags = MEM_Int; return 2; } case 3: { /* 3-byte signed integer */ - pMem->u.i = (((signed char)buf[0])<<16) | (buf[1]<<8) | buf[2]; + i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2]; + pMem->u.i = (i64)i; pMem->flags = MEM_Int; return 3; } case 4: { /* 4-byte signed integer */ - pMem->u.i = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; + y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; + pMem->u.i = (i64)*(int*)&y; pMem->flags = MEM_Int; return 4; } case 5: { /* 6-byte signed integer */ - u64 x = (((signed char)buf[0])<<8) | buf[1]; - u32 y = (buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; + x = 256*(signed char)buf[0] + buf[1]; + y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5]; x = (x<<32) | y; pMem->u.i = *(i64*)&x; pMem->flags = MEM_Int; return 6; } case 6: /* 8-byte signed integer */ case 7: { /* IEEE floating point */ - u64 x; - u32 y; #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT) /* Verify that integers and floating point values use the same ** byte order. Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is ** defined that 64-bit floating point values really are mixed ** endian. @@ -2981,13 +2986,12 @@ static const double r1 = 1.0; u64 t2 = t1; swapMixedEndianFloat(t2); assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 ); #endif - - x = (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; - y = (buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; + x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3]; + y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7]; x = (x<<32) | y; if( serial_type==6 ){ pMem->u.i = *(i64*)&x; pMem->flags = MEM_Int; }else{ Index: src/vdbemem.c ================================================================== --- src/vdbemem.c +++ src/vdbemem.c @@ -596,11 +596,11 @@ #endif /* SQLITE_DEBUG */ /* ** Size of struct Mem not including the Mem.zMalloc member. */ -#define MEMCELLSIZE (size_t)(&(((Mem *)0)->zMalloc)) +#define MEMCELLSIZE offsetof(Mem,zMalloc) /* ** Make an shallow copy of pFrom into pTo. Prior contents of ** pTo are freed. The pFrom->z field is not duplicated. If ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z Index: test/closure01.test ================================================================== --- test/closure01.test +++ test/closure01.test @@ -13,55 +13,72 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl set testprefix closure01 -ifcapable !vtab { finish_test ; return } +ifcapable !vtab||!cte { finish_test ; return } load_static_extension db closure do_execsql_test 1.0 { BEGIN; CREATE TABLE t1(x INTEGER PRIMARY KEY, y INTEGER); + WITH RECURSIVE + cnt(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM cnt LIMIT 131072) + INSERT INTO t1(x, y) SELECT i, nullif(i,1)/2 FROM cnt; CREATE INDEX t1y ON t1(y); - INSERT INTO t1(x) VALUES(1),(2); - INSERT INTO t1(x) SELECT x+2 FROM t1; - INSERT INTO t1(x) SELECT x+4 FROM t1; - INSERT INTO t1(x) SELECT x+8 FROM t1; - INSERT INTO t1(x) SELECT x+16 FROM t1; - INSERT INTO t1(x) SELECT x+32 FROM t1; - INSERT INTO t1(x) SELECT x+64 FROM t1; - INSERT INTO t1(x) SELECT x+128 FROM t1; - INSERT INTO t1(x) SELECT x+256 FROM t1; - INSERT INTO t1(x) SELECT x+512 FROM t1; - INSERT INTO t1(x) SELECT x+1024 FROM t1; - INSERT INTO t1(x) SELECT x+2048 FROM t1; - INSERT INTO t1(x) SELECT x+4096 FROM t1; - INSERT INTO t1(x) SELECT x+8192 FROM t1; - INSERT INTO t1(x) SELECT x+16384 FROM t1; - INSERT INTO t1(x) SELECT x+32768 FROM t1; - INSERT INTO t1(x) SELECT x+65536 FROM t1; - UPDATE t1 SET y=x/2 WHERE x>1; COMMIT; CREATE VIRTUAL TABLE cx USING transitive_closure(tablename=t1, idcolumn=x, parentcolumn=y); } {} # The entire table -do_execsql_test 1.1 { +do_timed_execsql_test 1.1 { SELECT count(*), depth FROM cx WHERE root=1 GROUP BY depth ORDER BY 1; } {/1 0 1 17 2 1 4 2 8 3 16 4 .* 65536 16/} +do_timed_execsql_test 1.1-cte { + WITH RECURSIVE + below(id,depth) AS ( + VALUES(1,0) + UNION ALL + SELECT t1.x, below.depth+1 + FROM t1 JOIN below on t1.y=below.id + ) + SELECT count(*), depth FROM below GROUP BY depth ORDER BY 1; +} {/1 0 1 17 2 1 4 2 8 3 16 4 .* 65536 16/} # descendents of 32768 -do_execsql_test 1.2 { +do_timed_execsql_test 1.2 { SELECT * FROM cx WHERE root=32768 ORDER BY id; } {32768 0 65536 1 65537 1 131072 2} +do_timed_execsql_test 1.2-cte { + WITH RECURSIVE + below(id,depth) AS ( + VALUES(32768,0) + UNION ALL + SELECT t1.x, below.depth+1 + FROM t1 JOIN below on t1.y=below.id + WHERE below.depth<2 + ) + SELECT id, depth FROM below ORDER BY id; +} {32768 0 65536 1 65537 1 131072 2} # descendents of 16384 -do_execsql_test 1.3 { +do_timed_execsql_test 1.3 { SELECT * FROM cx WHERE root=16384 AND depth<=2 ORDER BY id; } {16384 0 32768 1 32769 1 65536 2 65537 2 65538 2 65539 2} +do_timed_execsql_test 1.3-cte { + WITH RECURSIVE + below(id,depth) AS ( + VALUES(16384,0) + UNION ALL + SELECT t1.x, below.depth+1 + FROM t1 JOIN below on t1.y=below.id + WHERE below.depth<2 + ) + SELECT id, depth FROM below ORDER BY id; +} {16384 0 32768 1 32769 1 65536 2 65537 2 65538 2 65539 2} # children of 16384 do_execsql_test 1.4 { SELECT id, depth, root, tablename, idcolumn, parentcolumn FROM cx WHERE root=16384 @@ -68,23 +85,45 @@ AND depth=1 ORDER BY id; } {32768 1 {} t1 x y 32769 1 {} t1 x y} # great-grandparent of 16384 -do_execsql_test 1.5 { +do_timed_execsql_test 1.5 { SELECT id, depth, root, tablename, idcolumn, parentcolumn FROM cx WHERE root=16384 AND depth=3 AND idcolumn='Y' AND parentcolumn='X'; } {2048 3 {} t1 Y X} +do_timed_execsql_test 1.5-cte { + WITH RECURSIVE + above(id,depth) AS ( + VALUES(16384,0) + UNION ALL + SELECT t1.y, above.depth+1 + FROM t1 JOIN above ON t1.x=above.id + WHERE above.depth<3 + ) + SELECT id FROM above WHERE depth=3; +} {2048} # depth<5 -do_execsql_test 1.6 { +do_timed_execsql_test 1.6 { SELECT count(*), depth FROM cx WHERE root=1 AND depth<5 GROUP BY depth ORDER BY 1; } {1 0 2 1 4 2 8 3 16 4} +do_timed_execsql_test 1.6-cte { + WITH RECURSIVE + below(id,depth) AS ( + VALUES(1,0) + UNION ALL + SELECT t1.x, below.depth+1 + FROM t1 JOIN below ON t1.y=below.id + WHERE below.depth<4 + ) + SELECT count(*), depth FROM below GROUP BY depth ORDER BY 1; +} {1 0 2 1 4 2 8 3 16 4} # depth<=5 do_execsql_test 1.7 { SELECT count(*), depth FROM cx WHERE root=1 AND depth<=5 GROUP BY depth ORDER BY 1; @@ -101,13 +140,24 @@ SELECT count(*), depth FROM cx WHERE root=1 AND depth BETWEEN 3 AND 5 GROUP BY depth ORDER BY 1; } {8 3 16 4 32 5} # depth==5 with min() and max() -do_execsql_test 1.10 { +do_timed_execsql_test 1.10 { SELECT count(*), min(id), max(id) FROM cx WHERE root=1 AND depth=5; } {32 32 63} +do_timed_execsql_test 1.10-cte { + WITH RECURSIVE + below(id,depth) AS ( + VALUES(1,0) + UNION ALL + SELECT t1.x, below.depth+1 + FROM t1 JOIN below ON t1.y=below.id + WHERE below.depth<5 + ) + SELECT count(*), min(id), max(id) FROM below WHERE depth=5; +} {32 32 63} # Create a much smaller table t2 with only 32 elements db eval { CREATE TABLE t2(x INTEGER PRIMARY KEY, y INTEGER); INSERT INTO t2 SELECT x, y FROM t1 WHERE x<32; Index: test/corruptH.test ================================================================== --- test/corruptH.test +++ test/corruptH.test @@ -64,10 +64,11 @@ reset_db # Initialize the database. # do_execsql_test 2.1 { + PRAGMA auto_vacuum=0; PRAGMA page_size=1024; CREATE TABLE t1(a INTEGER PRIMARY KEY, b); INSERT INTO t1 VALUES(1, 'one'); INSERT INTO t1 VALUES(2, 'two'); @@ -93,23 +94,48 @@ hexio_write test.db 36 00000002 sqlite3 db test.db } {} + +# The corruption migration caused by the test case below does not +# cause corruption to be detected in mmap mode. +# +# The trick here is that the root page of the tree scanned by the outer +# query is also currently on the free-list. So while the first seek on +# the table (for a==1) works, by the time the second is attempted The +# "INSERT INTO t2..." statements have recycled the root page of t1 and +# used it as an index leaf. Normally, BtreeMovetoUnpacked() detects +# that the PgHdr object associated with said root page does not match +# the cursor (as it is now marked with PgHdr.intKey==0) and returns +# SQLITE_CORRUPT. +# +# However, in mmap mode, the outer query and the inner queries use +# different PgHdr objects (same data, but different PgHdr container +# objects). And so the corruption is not detected. Instead, the second +# seek fails to find anything and only a single row is returned. +# +set res23 {1 {database disk image is malformed}} +if {[permutation]=="mmap"} { + set res23 {0 one} +} do_test 2.3 { list [catch { + set res [list] db eval { SELECT * FROM t1 WHERE a IN (1, 2) } { db eval { INSERT INTO t2 SELECT randomblob(100) FROM t2; INSERT INTO t2 SELECT randomblob(100) FROM t2; INSERT INTO t2 SELECT randomblob(100) FROM t2; INSERT INTO t2 SELECT randomblob(100) FROM t2; INSERT INTO t2 SELECT randomblob(100) FROM t2; } + lappend res $b } + set res } msg] $msg -} {1 {database disk image is malformed}} +} $res23 #------------------------------------------------------------------------- reset_db # Initialize the database. Index: test/e_select.test ================================================================== --- test/e_select.test +++ test/e_select.test @@ -331,13 +331,13 @@ 4 "SELECT count(*)" {1} 5 "SELECT count(*) WHERE 0" {0} 6 "SELECT count(*) WHERE 1" {1} } -# EVIDENCE-OF: R-48114-33255 If there is only a single table in the -# join-source following the FROM clause, then the input data used by the -# SELECT statement is the contents of the named table. +# EVIDENCE-OF: R-45424-07352 If there is only a single table or subquery +# in the FROM clause, then the input data used by the SELECT statement +# is the contents of the named table. # # The results of the SELECT queries suggest that they are operating on the # contents of the table 'xx'. # do_execsql_test e_select-1.2.0 { @@ -355,14 +355,14 @@ 2 "SELECT count(*), count(x), count(y) FROM xx" {3 2 3} 3 "SELECT sum(x), sum(y) FROM xx" {-17.89 -16.87} } -# EVIDENCE-OF: R-23593-12456 If there is more than one table specified -# as part of the join-source following the FROM keyword, then the -# contents of each named table are joined into a single dataset for the -# simple SELECT statement to operate on. +# EVIDENCE-OF: R-28355-09804 If there is more than one table or subquery +# in FROM clause then the contents of all tables and/or subqueries are +# joined into a single dataset for the simple SELECT statement to +# operate on. # # There are more detailed tests for subsequent requirements that add # more detail to this idea. We just add a single test that shows that # data is coming from each of the three tables following the FROM clause # here to show that the statement, vague as it is, is not incorrect. @@ -381,14 +381,14 @@ # # The following block of tests - e_select-1.4.* - test that the description # of cartesian joins in the SELECT documentation is consistent with SQLite. # In doing so, we test the following three requirements as a side-effect: # -# EVIDENCE-OF: R-46122-14930 If the join-op is "CROSS JOIN", "INNER -# JOIN", "JOIN" or a comma (",") and there is no ON or USING clause, -# then the result of the join is simply the cartesian product of the -# left and right-hand datasets. +# EVIDENCE-OF: R-49872-03192 If the join-operator is "CROSS JOIN", +# "INNER JOIN", "JOIN" or a comma (",") and there is no ON or USING +# clause, then the result of the join is simply the cartesian product of +# the left and right-hand datasets. # # The tests are built on this assertion. Really, they test that the output # of a CROSS JOIN, JOIN, INNER JOIN or "," join matches the expected result # of calculating the cartesian product of the left and right-hand datasets. # @@ -511,15 +511,14 @@ 2 { SELECT * FROM t1 AS y CROSS JOIN t1 AS x } $t1_cross_t1 \ 3 { SELECT * FROM t1 INNER JOIN t2 } $t1_cross_t2 \ 4 { SELECT * FROM t1 AS y INNER JOIN t1 AS x } $t1_cross_t1 \ ] - -# EVIDENCE-OF: R-22775-56496 If there is an ON clause specified, then -# the ON expression is evaluated for each row of the cartesian product -# as a boolean expression. All rows for which the expression evaluates -# to false are excluded from the dataset. +# EVIDENCE-OF: R-38465-03616 If there is an ON clause then the ON +# expression is evaluated for each row of the cartesian product as a +# boolean expression. Only rows for which the expression evaluates to +# true are included from the dataset. # foreach {tn select res} [list \ 1 { SELECT * FROM t1 %JOIN% t2 ON (1) } $t1_cross_t2 \ 2 { SELECT * FROM t1 %JOIN% t2 ON (0) } [list] \ 3 { SELECT * FROM t1 %JOIN% t2 ON (NULL) } [list] \ @@ -538,26 +537,26 @@ {two I two II two III three I three II three III} \ ] { do_join_test e_select-1.3.$tn $select $res } -# EVIDENCE-OF: R-63358-54862 If there is a USING clause specified as -# part of the join-constraint, then each of the column names specified -# must exist in the datasets to both the left and right of the join-op. +# EVIDENCE-OF: R-49933-05137 If there is a USING clause then each of the +# column names specified must exist in the datasets to both the left and +# right of the join-operator. # do_select_tests e_select-1.4 -error { cannot join using column %s - column not present in both tables } { 1 { SELECT * FROM t1, t3 USING (b) } "b" 2 { SELECT * FROM t3, t1 USING (c) } "c" 3 { SELECT * FROM t3, (SELECT a AS b, b AS c FROM t1) USING (a) } "a" } -# EVIDENCE-OF: R-55987-04584 For each pair of namesake columns, the +# EVIDENCE-OF: R-22776-52830 For each pair of named columns, the # expression "lhs.X = rhs.X" is evaluated for each row of the cartesian -# product as a boolean expression. All rows for which one or more of the -# expressions evaluates to false are excluded from the result set. +# product as a boolean expression. Only rows for which all such +# expressions evaluates to true are included from the result set. # do_select_tests e_select-1.5 { 1 { SELECT * FROM t1, t3 USING (a) } {a one 1 b two 2} 2 { SELECT * FROM t3, t4 USING (a,c) } {b 2} } @@ -564,12 +563,12 @@ # EVIDENCE-OF: R-54046-48600 When comparing values as a result of a # USING clause, the normal rules for handling affinities, collation # sequences and NULL values in comparisons apply. # -# EVIDENCE-OF: R-35466-18578 The column from the dataset on the -# left-hand side of the join operator is considered to be on the +# EVIDENCE-OF: R-38422-04402 The column from the dataset on the +# left-hand side of the join-operator is considered to be on the # left-hand side of the comparison operator (=) for the purposes of # collation sequence and affinity precedence. # do_execsql_test e_select-1.6.0 { CREATE TABLE t5(a COLLATE nocase, b COLLATE binary); @@ -620,14 +619,13 @@ %JOIN% t5 ON (x.a=t5.a) } {aa cc AA cc bb DD BB dd} } { do_join_test e_select-1.7.$tn $select $res } - -# EVIDENCE-OF: R-41434-12448 If the join-op is a "LEFT JOIN" or "LEFT -# OUTER JOIN", then after the ON or USING filtering clauses have been -# applied, an extra row is added to the output for each row in the +# EVIDENCE-OF: R-42531-52874 If the join-operator is a "LEFT JOIN" or +# "LEFT OUTER JOIN", then after the ON or USING filtering clauses have +# been applied, an extra row is added to the output for each row in the # original left-hand input dataset that corresponds to no rows at all in # the composite dataset (if any). # do_execsql_test e_select-1.8.0 { CREATE TABLE t7(a, b, c); @@ -658,12 +656,12 @@ {x ex 24 x abc 24 y why 25 {} {} {}} 2a "SELECT * FROM t7 JOIN t8 USING (a)" {x ex 24 abc 24} 2b "SELECT * FROM t7 LEFT JOIN t8 USING (a)" {x ex 24 abc 24 y why 25 {} {}} } -# EVIDENCE-OF: R-01809-52134 If the NATURAL keyword is added to any of -# the join-ops, then an implicit USING clause is added to the +# EVIDENCE-OF: R-04932-55942 If the NATURAL keyword is in the +# join-operator then an implicit USING clause is added to the # join-constraints. The implicit USING clause contains each of the # column names that appear in both the left and right-hand input # datasets. # do_select_tests e_select-1-10 { Index: test/e_select2.test ================================================================== --- test/e_select2.test +++ test/e_select2.test @@ -342,14 +342,14 @@ catchsql { DROP INDEX i1 } catchsql { DROP INDEX i2 } catchsql { DROP INDEX i3 } execsql $indexes - # EVIDENCE-OF: R-46122-14930 If the join-op is "CROSS JOIN", "INNER - # JOIN", "JOIN" or a comma (",") and there is no ON or USING clause, - # then the result of the join is simply the cartesian product of the - # left and right-hand datasets. + # EVIDENCE-OF: R-49872-03192 If the join-operator is "CROSS JOIN", + # "INNER JOIN", "JOIN" or a comma (",") and there is no ON or USING + # clause, then the result of the join is simply the cartesian product of + # the left and right-hand datasets. # # EVIDENCE-OF: R-46256-57243 There is no difference between the "INNER # JOIN", "JOIN" and "," join operators. # # EVIDENCE-OF: R-25071-21202 The "CROSS JOIN" join operator produces the @@ -366,14 +366,14 @@ test_join $tn.1.9 "t2, t2 AS x" {t2 t2} test_join $tn.1.10 "t2 INNER JOIN t2 AS x" {t2 t2} test_join $tn.1.11 "t2 CROSS JOIN t2 AS x" {t2 t2} test_join $tn.1.12 "t2 JOIN t2 AS x" {t2 t2} - # EVIDENCE-OF: R-22775-56496 If there is an ON clause specified, then - # the ON expression is evaluated for each row of the cartesian product - # as a boolean expression. All rows for which the expression evaluates - # to false are excluded from the dataset. + # EVIDENCE-OF: R-38465-03616 If there is an ON clause then the ON + # expression is evaluated for each row of the cartesian product as a + # boolean expression. Only rows for which the expression evaluates to + # true are included from the dataset. # test_join $tn.2.1 "t1, t2 ON (t1.a=t2.a)" {t1 t2 -on {te_equals a a}} test_join $tn.2.2 "t2, t1 ON (t1.a=t2.a)" {t2 t1 -on {te_equals a a}} test_join $tn.2.3 "t2, t1 ON (1)" {t2 t1 -on te_true} test_join $tn.2.4 "t2, t1 ON (NULL)" {t2 t1 -on te_false} @@ -502,18 +502,18 @@ INSERT INTO t4 VALUES('2.0'); INSERT INTO t4 VALUES('TWO'); INSERT INTO t5 VALUES(2, 'two'); } {} -# EVIDENCE-OF: R-55824-40976 A sub-select specified in the join-source -# following the FROM clause in a simple SELECT statement is handled as -# if it was a table containing the data returned by executing the -# sub-select statement. -# -# EVIDENCE-OF: R-42612-06757 Each column of the sub-select dataset -# inherits the collation sequence and affinity of the corresponding -# expression in the sub-select statement. +# EVIDENCE-OF: R-59237-46742 A subquery specified in the +# table-or-subquery following the FROM clause in a simple SELECT +# statement is handled as if it was a table containing the data returned +# by executing the subquery statement. +# +# EVIDENCE-OF: R-27438-53558 Each column of the subquery has the +# collation sequence and affinity of the corresponding expression in the +# subquery statement. # foreach {tn subselect select spec} { 1 "SELECT * FROM t2" "SELECT * FROM t1 JOIN %ss%" {t1 %ss%} Index: test/pager4.test ================================================================== --- test/pager4.test +++ test/pager4.test @@ -7,18 +7,23 @@ # May you find forgiveness for yourself and forgive others. # May you share freely, never taking more than you give. # #*********************************************************************** # -# Tests for the SQLITE_IOERR_NODB error condition: the database file file +# Tests for the SQLITE_READONLY_DBMOVED error condition: the database file # is unlinked or renamed out from under SQLite. # if {$tcl_platform(platform)!="unix"} return set testdir [file dirname $argv0] source $testdir/tester.tcl + +if {[permutation]=="inmemory_journal"} { + finish_test + return +} # Create a database file for testing # do_execsql_test pager4-1.1 { CREATE TABLE t1(a,b,c); Index: test/pagerfault.test ================================================================== --- test/pagerfault.test +++ test/pagerfault.test @@ -1542,7 +1542,8 @@ catch { db2 close } } sqlite3_shutdown sqlite3_config_uri 0 +sqlite3_initialize finish_test Index: test/printf2.test ================================================================== --- test/printf2.test +++ test/printf2.test @@ -57,11 +57,11 @@ } {314159} do_execsql_test printf2-1.11 { SELECT printf('%lld%n',314159.2653,'hi'); } {314159} -# EVIDENCE-OF: R-20555-31089 The %z format is interchangable with %s. +# EVIDENCE-OF: R-17002-27534 The %z format is interchangeable with %s. # do_execsql_test printf2-1.12 { SELECT printf('%.*z',5,'abcdefghijklmnop'); } {abcde} do_execsql_test printf2-1.13 { Index: test/spellfix.test ================================================================== --- test/spellfix.test +++ test/spellfix.test @@ -232,39 +232,41 @@ } {keener {}} do_execsql_test 6.1.3 { SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner'; } {keener 300} -proc trace_callback {sql} { - if {[string range $sql 0 2] == "-- "} { - lappend ::trace [string range $sql 3 end] - } -} - -proc do_tracesql_test {tn sql {res {}}} { - set ::trace [list] - uplevel [list do_test $tn [subst -nocommands { - set vals [execsql {$sql}] - concat [set vals] [set ::trace] - }] [list {*}$res]] -} - -db trace trace_callback -do_tracesql_test 6.2.1 { - SELECT word FROM t3 WHERE rowid = 10; -} {keener - {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?} -} -do_tracesql_test 6.2.2 { - SELECT word, distance FROM t3 WHERE rowid = 10; -} {keener {} - {SELECT word, rank, NULL, langid, id FROM "main"."t3_vocab" WHERE rowid=?} -} -do_tracesql_test 6.2.3 { - SELECT word, distance FROM t3 WHERE rowid = 10 AND word MATCH 'kiiner'; -} {keener 300 - {SELECT id, word, rank, k1 FROM "main"."t3_vocab" WHERE langid=0 AND k2>=?1 AND k2=?1 AND k2