Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2acc4e

Browse files
author
drh
committed
Changes to make the new constant expression factoring logic more general
and more testable. FossilOrigin-Name: d10fb49a92f5f6e93093ae83544e5aec7984361a
1 parent a0d6e3a commit c2acc4e

File tree

8 files changed

+53
-56
lines changed

8 files changed

+53
-56
lines changed

manifest

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
C Add\stest\scases\sfor\sINSERT\sINTO\s...\sDEFAULT\sVALUES\son\stables\swith\snumeric\nconstants\sin\sCHECK\sconstraints.
2-
D 2013-11-15T16:48:23.550
1+
C Changes\sto\smake\sthe\snew\sconstant\sexpression\sfactoring\slogic\smore\sgeneral\nand\smore\stestable.
2+
D 2013-11-15T18:15:19.953
33
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
44
F Makefile.in 8a07bebafbfda0eb67728f4bd15a36201662d1a1
55
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -169,13 +169,13 @@ F src/btmutex.c 976f45a12e37293e32cae0281b15a21d48a8aaa7
169169
F src/btree.c 260dedc13119e6fb7930380bd3d294b98362bf5a
170170
F src/btree.h bfe0e8c5759b4ec77b0d18390064a6ef3cdffaaf
171171
F src/btreeInt.h f038e818bfadf75afbd09819ed93c26a333d39e0
172-
F src/build.c 5b744617019f6011ee35a88fe046f3fdf6f85c53
172+
F src/build.c 07054d45319953e54a89d726e589a423e9c1c590
173173
F src/callback.c f99a8957ba2adf369645fac0db09ad8adcf1caa2
174174
F src/complete.c dc1d136c0feee03c2f7550bafc0d29075e36deac
175175
F src/ctime.c ea4b7f3623a0fcb1146e7f245d7410033e86859c
176176
F src/date.c 593c744b2623971e45affd0bde347631bdfa4625
177177
F src/delete.c ddb92f44595366c4817e576b5f11cad5a915c3ef
178-
F src/expr.c eb7eb7fa7848dc4e3b67a2f0bf32f794804ca38a
178+
F src/expr.c 016e9dccac357b32d3ef960fa1690c3b17625e1e
179179
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
180180
F src/fkey.c 78364daed38e26269c53ddb94c515bceac1063c6
181181
F src/func.c 96caa9dfd1febf9a4b720de4c43ccfb392a52b73
@@ -217,14 +217,14 @@ F src/pragma.c c8d70c47ec8d8ba93575d92e34d30ddff8e9b517
217217
F src/prepare.c 359d1a1e9c9bd4488e4dd3a1aaaf2d2ebb9bb768
218218
F src/printf.c da9119eb31a187a4b99f60aa4a225141c0ebb74b
219219
F src/random.c 0b2dbc37fdfbfa6bd455b091dfcef5bdb32dba68
220-
F src/resolve.c fc4673cc49b116e51e7f12de074c0acf8f2388f9
220+
F src/resolve.c 6fcceeb653a0020b14491975d567c989e794d408
221221
F src/rowset.c 64655f1a627c9c212d9ab497899e7424a34222e0
222-
F src/select.c 7317406831ecced390edba972818f3c5f82238c0
222+
F src/select.c 253cb683e4a05b0b56b0f9c816f3c4a4e5575ebb
223223
F src/shell.c b98e74123d6c2e20369607c1da2d23c71db633d9
224224
F src/sqlite.h.in 4dedcab5b32358bf7a596badffe7363be1f1a82d
225225
F src/sqlite3.rc 11094cc6a157a028b301a9f06b3d03089ea37c3e
226226
F src/sqlite3ext.h 886f5a34de171002ad46fae8c36a7d8051c190fc
227-
F src/sqliteInt.h 11b0dd04b1a2eb86a348fd818c7a63bf015cb0dd
227+
F src/sqliteInt.h e16eb6e62146234c05f34a4403a75cf242777ad7
228228
F src/sqliteLimit.h 164b0e6749d31e0daa1a4589a169d31c0dec7b3d
229229
F src/status.c 7ac05a5c7017d0b9f0b4bcd701228b784f987158
230230
F src/table.c 2cd62736f845d82200acfa1287e33feb3c15d62e
@@ -293,7 +293,7 @@ F src/vtab.c 21b932841e51ebd7d075e2d0ad1415dce8d2d5fd
293293
F src/wal.c 7dc3966ef98b74422267e7e6e46e07ff6c6eb1b4
294294
F src/wal.h df01efe09c5cb8c8e391ff1715cca294f89668a4
295295
F src/walker.c e9e593d5bb798c3e67fc3893dfe7055c9e7d8d74
296-
F src/where.c 6e7e932ee32a4b2a0e4741880c06fe7a545e0059
296+
F src/where.c c7d50e26bad54218d8fa109d3127b9769b9474ab
297297
F src/whereInt.h 96a75c61f1d2b9d4a8e4bb17d89deb0cf7cba358
298298
F test/8_3_names.test ebbb5cd36741350040fd28b432ceadf495be25b2
299299
F test/aggerror.test a867e273ef9e3d7919f03ef4f0e8c0d2767944f2
@@ -1139,7 +1139,7 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
11391139
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
11401140
F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
11411141
F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
1142-
P 835be656bb0e83c8108104869166aa9dd850d265
1143-
R eed45a05b059c0c6c1ee8ceb9dc6b477
1142+
P 79ec485b548fcfc508c4d5fa32ed0604e1b0c5d9
1143+
R 81b3d4bed81861e7b07996884febd358
11441144
U drh
1145-
Z 845e07c690cff6d1b0e87cc7b8d86036
1145+
Z 2fbf2a6281b456f08de0df7e1fd4f785

manifest.uuid

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
79ec485b548fcfc508c4d5fa32ed0604e1b0c5d9
1+
d10fb49a92f5f6e93093ae83544e5aec7984361a

src/build.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void sqlite3FinishCoding(Parse *pParse){
187187
ExprList *pEL = pParse->pConstExpr;
188188
pParse->cookieGoto = 0;
189189
for(i=0; i<pEL->nExpr; i++){
190-
sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].iAlias);
190+
sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
191191
}
192192
}
193193

src/expr.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,7 @@ ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
930930
pItem->sortOrder = pOldItem->sortOrder;
931931
pItem->done = 0;
932932
pItem->bSpanIsTab = pOldItem->bSpanIsTab;
933-
pItem->iOrderByCol = pOldItem->iOrderByCol;
934-
pItem->iAlias = pOldItem->iAlias;
933+
pItem->u = pOldItem->u;
935934
}
936935
return pNew;
937936
}
@@ -2908,7 +2907,6 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
29082907
if( (pX = pExpr->pLeft)!=0 ){
29092908
tempX = *pX;
29102909
testcase( pX->op==TK_COLUMN );
2911-
testcase( pX->op==TK_REGISTER );
29122910
exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
29132911
testcase( regFree1==0 );
29142912
opCompare.op = TK_EQ;
@@ -2932,7 +2930,6 @@ int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
29322930
testcase( pTest->op==TK_COLUMN );
29332931
sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
29342932
testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
2935-
testcase( aListelem[i+1].pExpr->op==TK_REGISTER );
29362933
sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
29372934
sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
29382935
sqlite3ExprCachePop(pParse, 1);
@@ -3000,7 +2997,6 @@ int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
30002997
int r2;
30012998
pExpr = sqlite3ExprSkipCollate(pExpr);
30022999
if( pParse->cookieGoto>0
3003-
&& pParse->nMem<32768
30043000
&& pExpr->op!=TK_REGISTER
30053001
&& sqlite3ExprIsConstantNotJoin(pExpr)
30063002
){
@@ -3010,14 +3006,14 @@ int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
30103006
if( p ){
30113007
for(i=0; i<p->nExpr; i++){
30123008
if( sqlite3ExprCompare(p->a[i].pExpr, pExpr, -1)==0 ){
3013-
return p->a[i].iAlias;
3009+
return p->a[i].u.iConstExprReg;
30143010
}
30153011
}
30163012
}
30173013
p = sqlite3ExprListAppend(pParse, p, sqlite3ExprDup(pParse->db, pExpr, 0));
30183014
pParse->pConstExpr = p;
30193015
r2 = ++pParse->nMem;
3020-
if( p ) p->a[p->nExpr-1].iAlias = r2;
3016+
if( p ) p->a[p->nExpr-1].u.iConstExprReg = r2;
30213017
}else{
30223018
int r1 = sqlite3GetTempReg(pParse);
30233019
r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
@@ -3069,12 +3065,13 @@ int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
30693065
int inReg;
30703066
inReg = sqlite3ExprCode(pParse, pExpr, target);
30713067
assert( target>0 );
3072-
/* This routine is called for terms to INSERT or UPDATE. And the only
3073-
** other place where expressions can be converted into TK_REGISTER is
3074-
** in WHERE clause processing. So as currently implemented, there is
3075-
** no way for a TK_REGISTER to exist here. But it seems prudent to
3076-
** keep the ALWAYS() in case the conditions above change with future
3077-
** modifications or enhancements. */
3068+
/* The only place, other than this routine, where expressions can be
3069+
** converted to TK_REGISTER is internal subexpressions in BETWEEN and
3070+
** CASE operators. Neither ever calls this routine. And this routine
3071+
** is never called twice on the same expression. Hence it is impossible
3072+
** for the input to this routine to already be a register. Nevertheless,
3073+
** it seems prudent to keep the ALWAYS() in case the conditions above
3074+
** change with future modifications or enhancements. */
30783075
if( ALWAYS(pExpr->op!=TK_REGISTER) ){
30793076
int iMem;
30803077
iMem = ++pParse->nMem;
@@ -3745,7 +3742,7 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
37453742
}
37463743
return 2;
37473744
}
3748-
if( pA->op!=pB->op /*&& (pA->op!=TK_REGISTER || pA->op2!=pB->op)*/ ){
3745+
if( pA->op!=pB->op ){
37493746
if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
37503747
return 1;
37513748
}
@@ -3760,8 +3757,6 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
37603757
}
37613758
}
37623759
if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
3763-
testcase( combinedFlags & EP_TokenOnly );
3764-
testcase( combinedFlags & EP_Reduced );
37653760
if( (combinedFlags & EP_TokenOnly)==0 ){
37663761
if( combinedFlags & EP_xIsSelect ) return 2;
37673762
if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
@@ -3770,7 +3765,6 @@ int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
37703765
if( (combinedFlags & EP_Reduced)==0 ){
37713766
if( pA->iColumn!=pB->iColumn ) return 2;
37723767
if( pA->iTable!=pB->iTable
3773-
&& pA->op!=TK_REGISTER
37743768
&& (pA->iTable!=iTab || pB->iTable>=0) ) return 2;
37753769
}
37763770
}

src/resolve.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ static void resolveAlias(
108108
pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
109109
if( pDup==0 ) return;
110110
ExprSetProperty(pDup, EP_Skip);
111-
if( pEList->a[iCol].iAlias==0 ){
112-
pEList->a[iCol].iAlias = (u16)(++pParse->nAlias);
111+
if( pEList->a[iCol].u.x.iAlias==0 ){
112+
pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
113113
}
114-
pDup->iTable = pEList->a[iCol].iAlias;
114+
pDup->iTable = pEList->a[iCol].u.x.iAlias;
115115
}
116116
if( pExpr->op==TK_COLLATE ){
117117
pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
@@ -976,7 +976,7 @@ static int resolveCompoundOrderBy(
976976
pItem->pExpr->pLeft = pNew;
977977
}
978978
sqlite3ExprDelete(db, pE);
979-
pItem->iOrderByCol = (u16)iCol;
979+
pItem->u.x.iOrderByCol = (u16)iCol;
980980
pItem->done = 1;
981981
}else{
982982
moreToDo = 1;
@@ -997,8 +997,8 @@ static int resolveCompoundOrderBy(
997997
/*
998998
** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
999999
** the SELECT statement pSelect. If any term is reference to a
1000-
** result set expression (as determined by the ExprList.a.iOrderByCol field)
1001-
** then convert that term into a copy of the corresponding result set
1000+
** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
1001+
** field) then convert that term into a copy of the corresponding result set
10021002
** column.
10031003
**
10041004
** If any errors are detected, add an error message to pParse and
@@ -1025,12 +1025,12 @@ int sqlite3ResolveOrderGroupBy(
10251025
pEList = pSelect->pEList;
10261026
assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
10271027
for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
1028-
if( pItem->iOrderByCol ){
1029-
if( pItem->iOrderByCol>pEList->nExpr ){
1028+
if( pItem->u.x.iOrderByCol ){
1029+
if( pItem->u.x.iOrderByCol>pEList->nExpr ){
10301030
resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
10311031
return 1;
10321032
}
1033-
resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType,0);
1033+
resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
10341034
}
10351035
}
10361036
return 0;
@@ -1079,7 +1079,7 @@ static int resolveOrderGroupBy(
10791079
** a copy of the iCol-th result-set column. The subsequent call to
10801080
** sqlite3ResolveOrderGroupBy() will convert the expression to a
10811081
** copy of the iCol-th result-set expression. */
1082-
pItem->iOrderByCol = (u16)iCol;
1082+
pItem->u.x.iOrderByCol = (u16)iCol;
10831083
continue;
10841084
}
10851085
}
@@ -1091,18 +1091,18 @@ static int resolveOrderGroupBy(
10911091
resolveOutOfRangeError(pParse, zType, i+1, nResult);
10921092
return 1;
10931093
}
1094-
pItem->iOrderByCol = (u16)iCol;
1094+
pItem->u.x.iOrderByCol = (u16)iCol;
10951095
continue;
10961096
}
10971097

10981098
/* Otherwise, treat the ORDER BY term as an ordinary expression */
1099-
pItem->iOrderByCol = 0;
1099+
pItem->u.x.iOrderByCol = 0;
11001100
if( sqlite3ResolveExprNames(pNC, pE) ){
11011101
return 1;
11021102
}
11031103
for(j=0; j<pSelect->pEList->nExpr; j++){
11041104
if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
1105-
pItem->iOrderByCol = j+1;
1105+
pItem->u.x.iOrderByCol = j+1;
11061106
}
11071107
}
11081108
}

src/select.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,16 +2372,16 @@ static int multiSelectOrderBy(
23722372
for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
23732373
struct ExprList_item *pItem;
23742374
for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
2375-
assert( pItem->iOrderByCol>0 );
2376-
if( pItem->iOrderByCol==i ) break;
2375+
assert( pItem->u.x.iOrderByCol>0 );
2376+
if( pItem->u.x.iOrderByCol==i ) break;
23772377
}
23782378
if( j==nOrderBy ){
23792379
Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
23802380
if( pNew==0 ) return SQLITE_NOMEM;
23812381
pNew->flags |= EP_IntValue;
23822382
pNew->u.iValue = i;
23832383
pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
2384-
if( pOrderBy ) pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
2384+
if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
23852385
}
23862386
}
23872387
}
@@ -2397,8 +2397,9 @@ static int multiSelectOrderBy(
23972397
if( aPermute ){
23982398
struct ExprList_item *pItem;
23992399
for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
2400-
assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
2401-
aPermute[i] = pItem->iOrderByCol - 1;
2400+
assert( pItem->u.x.iOrderByCol>0
2401+
&& pItem->u.x.iOrderByCol<=p->pEList->nExpr );
2402+
aPermute[i] = pItem->u.x.iOrderByCol - 1;
24022403
}
24032404
pKeyMerge = sqlite3KeyInfoAlloc(db, nOrderBy, 1);
24042405
if( pKeyMerge ){
@@ -2978,7 +2979,7 @@ static int flattenSubquery(
29782979
if( p->pOrderBy ){
29792980
int ii;
29802981
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
2981-
if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
2982+
if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
29822983
}
29832984
}
29842985
}
@@ -4385,10 +4386,10 @@ int sqlite3Select(
43854386
struct ExprList_item *pItem; /* For looping over expression in a list */
43864387

43874388
for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
4388-
pItem->iAlias = 0;
4389+
pItem->u.x.iAlias = 0;
43894390
}
43904391
for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
4391-
pItem->iAlias = 0;
4392+
pItem->u.x.iAlias = 0;
43924393
}
43934394
if( p->nSelectRow>100 ) p->nSelectRow = 100;
43944395
}else{

src/sqliteInt.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,8 +1900,13 @@ struct ExprList {
19001900
u8 sortOrder; /* 1 for DESC or 0 for ASC */
19011901
unsigned done :1; /* A flag to indicate when processing is finished */
19021902
unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
1903-
u16 iOrderByCol; /* For ORDER BY, column number in result set */
1904-
u16 iAlias; /* Index into Parse.aAlias[] for zName */
1903+
union {
1904+
struct {
1905+
u16 iOrderByCol; /* For ORDER BY, column number in result set */
1906+
u16 iAlias; /* Index into Parse.aAlias[] for zName */
1907+
} x;
1908+
int iConstExprReg; /* Register in which Expr value is cached */
1909+
} u;
19051910
} *a; /* Alloc a power of two greater or equal to nExpr */
19061911
};
19071912

src/where.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,6 @@ static int isLikeOrGlob(
669669

670670
pRight = pList->a[0].pExpr;
671671
op = pRight->op;
672-
if( op==TK_REGISTER ){
673-
op = pRight->op2;
674-
}
675672
if( op==TK_VARIABLE ){
676673
Vdbe *pReprepare = pParse->pReprepare;
677674
int iCol = pRight->iColumn;

0 commit comments

Comments
 (0)