| | @@ -317,11 +317,11 @@ |
| 317 | 317 | ** [sqlite3_libversion_number()], [sqlite3_sourceid()], |
| 318 | 318 | ** [sqlite_version()] and [sqlite_source_id()]. |
| 319 | 319 | */ |
| 320 | 320 | #define SQLITE_VERSION "3.8.9" |
| 321 | 321 | #define SQLITE_VERSION_NUMBER 3008009 |
| 322 | | -#define SQLITE_SOURCE_ID "2015-04-03 20:33:33 4ae9a3acc4eeeb7998769eb856c97c2233476f72" |
| 322 | +#define SQLITE_SOURCE_ID "2015-04-08 12:16:33 8a8ffc862e96f57aa698f93de10dee28e69f6e09" |
| 323 | 323 | |
| 324 | 324 | /* |
| 325 | 325 | ** CAPI3REF: Run-Time Library Version Numbers |
| 326 | 326 | ** KEYWORDS: sqlite3_version, sqlite3_sourceid |
| 327 | 327 | ** |
| | @@ -12226,10 +12226,11 @@ |
| 12226 | 12226 | #define SF_AllValues 0x0100 /* All terms of compound are VALUES */ |
| 12227 | 12227 | #define SF_NestedFrom 0x0200 /* Part of a parenthesized FROM clause */ |
| 12228 | 12228 | #define SF_MaybeConvert 0x0400 /* Need convertCompoundSelectToSubquery() */ |
| 12229 | 12229 | #define SF_Recursive 0x0800 /* The recursive part of a recursive CTE */ |
| 12230 | 12230 | #define SF_MinMaxAgg 0x1000 /* Aggregate containing min() or max() */ |
| 12231 | +#define SF_Converted 0x2000 /* By convertCompoundSelectToSubquery() */ |
| 12231 | 12232 | |
| 12232 | 12233 | |
| 12233 | 12234 | /* |
| 12234 | 12235 | ** The results of a SELECT can be distributed in several ways, as defined |
| 12235 | 12236 | ** by one of the following macros. The "SRT" prefix means "SELECT Result |
| | @@ -21523,19 +21524,23 @@ |
| 21523 | 21524 | }else{ |
| 21524 | 21525 | width = va_arg(ap,int); |
| 21525 | 21526 | } |
| 21526 | 21527 | if( width<0 ){ |
| 21527 | 21528 | flag_leftjustify = 1; |
| 21528 | | - width = -width; |
| 21529 | + width = width >= -2147483647 ? -width : 0; |
| 21529 | 21530 | } |
| 21530 | 21531 | c = *++fmt; |
| 21531 | 21532 | }else{ |
| 21533 | + unsigned wx = 0; |
| 21532 | 21534 | while( c>='0' && c<='9' ){ |
| 21533 | | - width = width*10 + c - '0'; |
| 21535 | + wx = wx*10 + c - '0'; |
| 21534 | 21536 | c = *++fmt; |
| 21535 | 21537 | } |
| 21538 | + testcase( wx>0x7fffffff ); |
| 21539 | + width = wx & 0x7fffffff; |
| 21536 | 21540 | } |
| 21541 | + |
| 21537 | 21542 | /* Get the precision */ |
| 21538 | 21543 | if( c=='.' ){ |
| 21539 | 21544 | precision = 0; |
| 21540 | 21545 | c = *++fmt; |
| 21541 | 21546 | if( c=='*' ){ |
| | @@ -21542,17 +21547,22 @@ |
| 21542 | 21547 | if( bArgList ){ |
| 21543 | 21548 | precision = (int)getIntArg(pArgList); |
| 21544 | 21549 | }else{ |
| 21545 | 21550 | precision = va_arg(ap,int); |
| 21546 | 21551 | } |
| 21547 | | - if( precision<0 ) precision = -precision; |
| 21548 | 21552 | c = *++fmt; |
| 21553 | + if( precision<0 ){ |
| 21554 | + precision = precision >= -2147483647 ? -precision : -1; |
| 21555 | + } |
| 21549 | 21556 | }else{ |
| 21557 | + unsigned px = 0; |
| 21550 | 21558 | while( c>='0' && c<='9' ){ |
| 21551 | | - precision = precision*10 + c - '0'; |
| 21559 | + px = px*10 + c - '0'; |
| 21552 | 21560 | c = *++fmt; |
| 21553 | 21561 | } |
| 21562 | + testcase( px>0x7fffffff ); |
| 21563 | + precision = px & 0x7fffffff; |
| 21554 | 21564 | } |
| 21555 | 21565 | }else{ |
| 21556 | 21566 | precision = -1; |
| 21557 | 21567 | } |
| 21558 | 21568 | /* Get the conversion type modifier */ |
| | @@ -21712,11 +21722,12 @@ |
| 21712 | 21722 | if( flag_plussign ) prefix = '+'; |
| 21713 | 21723 | else if( flag_blanksign ) prefix = ' '; |
| 21714 | 21724 | else prefix = 0; |
| 21715 | 21725 | } |
| 21716 | 21726 | if( xtype==etGENERIC && precision>0 ) precision--; |
| 21717 | | - for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 21727 | + testcase( precision>0xfff ); |
| 21728 | + for(idx=precision&0xfff, rounder=0.5; idx>0; idx--, rounder*=0.1){} |
| 21718 | 21729 | if( xtype==etFLOAT ) realvalue += rounder; |
| 21719 | 21730 | /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */ |
| 21720 | 21731 | exp = 0; |
| 21721 | 21732 | if( sqlite3IsNaN((double)realvalue) ){ |
| 21722 | 21733 | bufpt = "NaN"; |
| | @@ -21767,12 +21778,13 @@ |
| 21767 | 21778 | if( xtype==etEXP ){ |
| 21768 | 21779 | e2 = 0; |
| 21769 | 21780 | }else{ |
| 21770 | 21781 | e2 = exp; |
| 21771 | 21782 | } |
| 21772 | | - if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){ |
| 21773 | | - bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 ); |
| 21783 | + if( MAX(e2,0)+(i64)precision+(i64)width > etBUFSIZE - 15 ){ |
| 21784 | + bufpt = zExtra |
| 21785 | + = sqlite3Malloc( MAX(e2,0)+(i64)precision+(i64)width+15 ); |
| 21774 | 21786 | if( bufpt==0 ){ |
| 21775 | 21787 | setStrAccumError(pAccum, STRACCUM_NOMEM); |
| 21776 | 21788 | return; |
| 21777 | 21789 | } |
| 21778 | 21790 | } |
| | @@ -22000,11 +22012,11 @@ |
| 22000 | 22012 | ** Return the number of bytes of text that StrAccum is able to accept |
| 22001 | 22013 | ** after the attempted enlargement. The value returned might be zero. |
| 22002 | 22014 | */ |
| 22003 | 22015 | static int sqlite3StrAccumEnlarge(StrAccum *p, int N){ |
| 22004 | 22016 | char *zNew; |
| 22005 | | - assert( p->nChar+N >= p->nAlloc ); /* Only called if really needed */ |
| 22017 | + assert( p->nChar+(i64)N >= p->nAlloc ); /* Only called if really needed */ |
| 22006 | 22018 | if( p->accError ){ |
| 22007 | 22019 | testcase(p->accError==STRACCUM_TOOBIG); |
| 22008 | 22020 | testcase(p->accError==STRACCUM_NOMEM); |
| 22009 | 22021 | return 0; |
| 22010 | 22022 | } |
| | @@ -22049,11 +22061,14 @@ |
| 22049 | 22061 | |
| 22050 | 22062 | /* |
| 22051 | 22063 | ** Append N copies of character c to the given string buffer. |
| 22052 | 22064 | */ |
| 22053 | 22065 | SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){ |
| 22054 | | - if( p->nChar+N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ) return; |
| 22066 | + testcase( p->nChar + (i64)N > 0x7fffffff ); |
| 22067 | + if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){ |
| 22068 | + return; |
| 22069 | + } |
| 22055 | 22070 | while( (N--)>0 ) p->zText[p->nChar++] = c; |
| 22056 | 22071 | } |
| 22057 | 22072 | |
| 22058 | 22073 | /* |
| 22059 | 22074 | ** The StrAccum "p" is not large enough to accept N new bytes of z[]. |
| | @@ -82115,10 +82130,24 @@ |
| 82115 | 82130 | sNC.pParse = pParse; |
| 82116 | 82131 | if( sqlite3ResolveExprNames(&sNC, p->pLimit) || |
| 82117 | 82132 | sqlite3ResolveExprNames(&sNC, p->pOffset) ){ |
| 82118 | 82133 | return WRC_Abort; |
| 82119 | 82134 | } |
| 82135 | + |
| 82136 | + /* If the SF_Converted flags is set, then this Select object was |
| 82137 | + ** was created by the convertCompoundSelectToSubquery() function. |
| 82138 | + ** In this case the ORDER BY clause (p->pOrderBy) should be resolved |
| 82139 | + ** as if it were part of the sub-query, not the parent. This block |
| 82140 | + ** moves the pOrderBy down to the sub-query. It will be moved back |
| 82141 | + ** after the names have been resolved. */ |
| 82142 | + if( p->selFlags & SF_Converted ){ |
| 82143 | + Select *pSub = p->pSrc->a[0].pSelect; |
| 82144 | + assert( p->pSrc->nSrc==1 && isCompound==0 && p->pOrderBy ); |
| 82145 | + assert( pSub->pPrior && pSub->pOrderBy==0 ); |
| 82146 | + pSub->pOrderBy = p->pOrderBy; |
| 82147 | + p->pOrderBy = 0; |
| 82148 | + } |
| 82120 | 82149 | |
| 82121 | 82150 | /* Recursively resolve names in all subqueries |
| 82122 | 82151 | */ |
| 82123 | 82152 | for(i=0; i<p->pSrc->nSrc; i++){ |
| 82124 | 82153 | struct SrcList_item *pItem = &p->pSrc->a[i]; |
| | @@ -82196,10 +82225,21 @@ |
| 82196 | 82225 | /* The ORDER BY and GROUP BY clauses may not refer to terms in |
| 82197 | 82226 | ** outer queries |
| 82198 | 82227 | */ |
| 82199 | 82228 | sNC.pNext = 0; |
| 82200 | 82229 | sNC.ncFlags |= NC_AllowAgg; |
| 82230 | + |
| 82231 | + /* If this is a converted compound query, move the ORDER BY clause from |
| 82232 | + ** the sub-query back to the parent query. At this point each term |
| 82233 | + ** within the ORDER BY clause has been transformed to an integer value. |
| 82234 | + ** These integers will be replaced by copies of the corresponding result |
| 82235 | + ** set expressions by the call to resolveOrderGroupBy() below. */ |
| 82236 | + if( p->selFlags & SF_Converted ){ |
| 82237 | + Select *pSub = p->pSrc->a[0].pSelect; |
| 82238 | + p->pOrderBy = pSub->pOrderBy; |
| 82239 | + pSub->pOrderBy = 0; |
| 82240 | + } |
| 82201 | 82241 | |
| 82202 | 82242 | /* Process the ORDER BY clause for singleton SELECT statements. |
| 82203 | 82243 | ** The ORDER BY clause for compounds SELECT statements is handled |
| 82204 | 82244 | ** below, after all of the result-sets for all of the elements of |
| 82205 | 82245 | ** the compound have been resolved. |
| | @@ -109909,10 +109949,12 @@ |
| 109909 | 109949 | pNew->pHaving = 0; |
| 109910 | 109950 | pNew->pOrderBy = 0; |
| 109911 | 109951 | p->pPrior = 0; |
| 109912 | 109952 | p->pNext = 0; |
| 109913 | 109953 | p->selFlags &= ~SF_Compound; |
| 109954 | + assert( (p->selFlags & SF_Converted)==0 ); |
| 109955 | + p->selFlags |= SF_Converted; |
| 109914 | 109956 | assert( pNew->pPrior!=0 ); |
| 109915 | 109957 | pNew->pPrior->pNext = pNew; |
| 109916 | 109958 | pNew->pLimit = 0; |
| 109917 | 109959 | pNew->pOffset = 0; |
| 109918 | 109960 | return WRC_Continue; |
| | @@ -134980,30 +135022,37 @@ |
| 134980 | 135022 | ** parameter bDescDoclist should be false. If they are sorted in ascending |
| 134981 | 135023 | ** order, it should be passed a non-zero value. |
| 134982 | 135024 | ** |
| 134983 | 135025 | ** The right-hand input doclist is overwritten by this function. |
| 134984 | 135026 | */ |
| 134985 | | -static void fts3DoclistPhraseMerge( |
| 135027 | +static int fts3DoclistPhraseMerge( |
| 134986 | 135028 | int bDescDoclist, /* True if arguments are desc */ |
| 134987 | 135029 | int nDist, /* Distance from left to right (1=adjacent) */ |
| 134988 | 135030 | char *aLeft, int nLeft, /* Left doclist */ |
| 134989 | | - char *aRight, int *pnRight /* IN/OUT: Right/output doclist */ |
| 135031 | + char **paRight, int *pnRight /* IN/OUT: Right/output doclist */ |
| 134990 | 135032 | ){ |
| 134991 | 135033 | sqlite3_int64 i1 = 0; |
| 134992 | 135034 | sqlite3_int64 i2 = 0; |
| 134993 | 135035 | sqlite3_int64 iPrev = 0; |
| 135036 | + char *aRight = *paRight; |
| 134994 | 135037 | char *pEnd1 = &aLeft[nLeft]; |
| 134995 | 135038 | char *pEnd2 = &aRight[*pnRight]; |
| 134996 | 135039 | char *p1 = aLeft; |
| 134997 | 135040 | char *p2 = aRight; |
| 134998 | 135041 | char *p; |
| 134999 | 135042 | int bFirstOut = 0; |
| 135000 | | - char *aOut = aRight; |
| 135043 | + char *aOut; |
| 135001 | 135044 | |
| 135002 | 135045 | assert( nDist>0 ); |
| 135003 | | - |
| 135046 | + if( bDescDoclist ){ |
| 135047 | + aOut = sqlite3_malloc(*pnRight + FTS3_VARINT_MAX); |
| 135048 | + if( aOut==0 ) return SQLITE_NOMEM; |
| 135049 | + }else{ |
| 135050 | + aOut = aRight; |
| 135051 | + } |
| 135004 | 135052 | p = aOut; |
| 135053 | + |
| 135005 | 135054 | fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1); |
| 135006 | 135055 | fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2); |
| 135007 | 135056 | |
| 135008 | 135057 | while( p1 && p2 ){ |
| 135009 | 135058 | sqlite3_int64 iDiff = DOCID_CMP(i1, i2); |
| | @@ -135028,10 +135077,16 @@ |
| 135028 | 135077 | fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2); |
| 135029 | 135078 | } |
| 135030 | 135079 | } |
| 135031 | 135080 | |
| 135032 | 135081 | *pnRight = (int)(p - aOut); |
| 135082 | + if( bDescDoclist ){ |
| 135083 | + sqlite3_free(aRight); |
| 135084 | + *paRight = aOut; |
| 135085 | + } |
| 135086 | + |
| 135087 | + return SQLITE_OK; |
| 135033 | 135088 | } |
| 135034 | 135089 | |
| 135035 | 135090 | /* |
| 135036 | 135091 | ** Argument pList points to a position list nList bytes in size. This |
| 135037 | 135092 | ** function checks to see if the position list contains any entries for |
| | @@ -135152,12 +135207,26 @@ |
| 135152 | 135207 | char *aDoclist, /* Pointer to doclist */ |
| 135153 | 135208 | int nDoclist /* Size of aDoclist in bytes */ |
| 135154 | 135209 | ){ |
| 135155 | 135210 | if( pTS->aaOutput[0]==0 ){ |
| 135156 | 135211 | /* If this is the first term selected, copy the doclist to the output |
| 135157 | | - ** buffer using memcpy(). */ |
| 135158 | | - pTS->aaOutput[0] = sqlite3_malloc(nDoclist); |
| 135212 | + ** buffer using memcpy(). |
| 135213 | + ** |
| 135214 | + ** Add FTS3_VARINT_MAX bytes of unused space to the end of the |
| 135215 | + ** allocation. This is so as to ensure that the buffer is big enough |
| 135216 | + ** to hold the current doclist AND'd with any other doclist. If the |
| 135217 | + ** doclists are stored in order=ASC order, this padding would not be |
| 135218 | + ** required (since the size of [doclistA AND doclistB] is always less |
| 135219 | + ** than or equal to the size of [doclistA] in that case). But this is |
| 135220 | + ** not true for order=DESC. For example, a doclist containing (1, -1) |
| 135221 | + ** may be smaller than (-1), as in the first example the -1 may be stored |
| 135222 | + ** as a single-byte delta, whereas in the second it must be stored as a |
| 135223 | + ** FTS3_VARINT_MAX byte varint. |
| 135224 | + ** |
| 135225 | + ** Similar padding is added in the fts3DoclistOrMerge() function. |
| 135226 | + */ |
| 135227 | + pTS->aaOutput[0] = sqlite3_malloc(nDoclist + FTS3_VARINT_MAX + 1); |
| 135159 | 135228 | pTS->anOutput[0] = nDoclist; |
| 135160 | 135229 | if( pTS->aaOutput[0] ){ |
| 135161 | 135230 | memcpy(pTS->aaOutput[0], aDoclist, nDoclist); |
| 135162 | 135231 | }else{ |
| 135163 | 135232 | return SQLITE_NOMEM; |
| | @@ -136409,18 +136478,21 @@ |
| 136409 | 136478 | ** It is merged into the main doclist stored in p->doclist.aAll/nAll. |
| 136410 | 136479 | ** |
| 136411 | 136480 | ** This function assumes that pList points to a buffer allocated using |
| 136412 | 136481 | ** sqlite3_malloc(). This function takes responsibility for eventually |
| 136413 | 136482 | ** freeing the buffer. |
| 136483 | +** |
| 136484 | +** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs. |
| 136414 | 136485 | */ |
| 136415 | | -static void fts3EvalPhraseMergeToken( |
| 136486 | +static int fts3EvalPhraseMergeToken( |
| 136416 | 136487 | Fts3Table *pTab, /* FTS Table pointer */ |
| 136417 | 136488 | Fts3Phrase *p, /* Phrase to merge pList/nList into */ |
| 136418 | 136489 | int iToken, /* Token pList/nList corresponds to */ |
| 136419 | 136490 | char *pList, /* Pointer to doclist */ |
| 136420 | 136491 | int nList /* Number of bytes in pList */ |
| 136421 | 136492 | ){ |
| 136493 | + int rc = SQLITE_OK; |
| 136422 | 136494 | assert( iToken!=p->iDoclistToken ); |
| 136423 | 136495 | |
| 136424 | 136496 | if( pList==0 ){ |
| 136425 | 136497 | sqlite3_free(p->doclist.aAll); |
| 136426 | 136498 | p->doclist.aAll = 0; |
| | @@ -136455,17 +136527,20 @@ |
| 136455 | 136527 | pLeft = pList; |
| 136456 | 136528 | nLeft = nList; |
| 136457 | 136529 | nDiff = p->iDoclistToken - iToken; |
| 136458 | 136530 | } |
| 136459 | 136531 | |
| 136460 | | - fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight); |
| 136532 | + rc = fts3DoclistPhraseMerge( |
| 136533 | + pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight |
| 136534 | + ); |
| 136461 | 136535 | sqlite3_free(pLeft); |
| 136462 | 136536 | p->doclist.aAll = pRight; |
| 136463 | 136537 | p->doclist.nAll = nRight; |
| 136464 | 136538 | } |
| 136465 | 136539 | |
| 136466 | 136540 | if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken; |
| 136541 | + return rc; |
| 136467 | 136542 | } |
| 136468 | 136543 | |
| 136469 | 136544 | /* |
| 136470 | 136545 | ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist |
| 136471 | 136546 | ** does not take deferred tokens into account. |
| | @@ -136487,11 +136562,11 @@ |
| 136487 | 136562 | if( pToken->pSegcsr ){ |
| 136488 | 136563 | int nThis = 0; |
| 136489 | 136564 | char *pThis = 0; |
| 136490 | 136565 | rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis); |
| 136491 | 136566 | if( rc==SQLITE_OK ){ |
| 136492 | | - fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis); |
| 136567 | + rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis); |
| 136493 | 136568 | } |
| 136494 | 136569 | } |
| 136495 | 136570 | assert( pToken->pSegcsr==0 ); |
| 136496 | 136571 | } |
| 136497 | 136572 | |
| | @@ -137289,13 +137364,17 @@ |
| 137289 | 137364 | Fts3PhraseToken *pToken = pTC->pToken; |
| 137290 | 137365 | int nList = 0; |
| 137291 | 137366 | char *pList = 0; |
| 137292 | 137367 | rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList); |
| 137293 | 137368 | assert( rc==SQLITE_OK || pList==0 ); |
| 137369 | + if( rc==SQLITE_OK ){ |
| 137370 | + rc = fts3EvalPhraseMergeToken( |
| 137371 | + pTab, pTC->pPhrase, pTC->iToken,pList,nList |
| 137372 | + ); |
| 137373 | + } |
| 137294 | 137374 | if( rc==SQLITE_OK ){ |
| 137295 | 137375 | int nCount; |
| 137296 | | - fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList); |
| 137297 | 137376 | nCount = fts3DoclistCountDocids( |
| 137298 | 137377 | pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll |
| 137299 | 137378 | ); |
| 137300 | 137379 | if( ii==0 || nCount<nMinEst ) nMinEst = nCount; |
| 137301 | 137380 | } |
| 137302 | 137381 | |