Match ORDER BY terms to columns using names in compound queries. Make sure
this works for subqueries, especially in the right-hand side of an IN
operator. Ticket #2296. (CVS 3842)
FossilOrigin-Name: cfc6f933dc60ca88ae848f7f0c402e820437c2ff
diff --git a/src/expr.c b/src/expr.c
index 934f988..f4237b4 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -12,7 +12,7 @@
** This file contains routines used for analyzing expressions and
** for generating VDBE code that evaluates expressions in SQLite.
**
-** $Id: expr.c,v 1.283 2007/03/27 13:36:37 drh Exp $
+** $Id: expr.c,v 1.284 2007/04/13 16:06:33 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@@ -1420,7 +1420,9 @@
int iParm = pExpr->iTable + (((int)affinity)<<16);
ExprList *pEList;
assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
- sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0);
+ if( sqlite3Select(pParse, pExpr->pSelect, SRT_Set, iParm, 0, 0, 0, 0) ){
+ return;
+ }
pEList = pExpr->pSelect->pEList;
if( pEList && pEList->nExpr>0 ){
keyInfo.aColl[0] = binaryCompareCollSeq(pParse, pExpr->pLeft,
@@ -1491,7 +1493,9 @@
}
sqlite3ExprDelete(pSel->pLimit);
pSel->pLimit = sqlite3Expr(TK_INTEGER, 0, 0, &one);
- sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0);
+ if( sqlite3Select(pParse, pSel, sop, iMem, 0, 0, 0, 0) ){
+ return;
+ }
break;
}
}
diff --git a/src/select.c b/src/select.c
index 041b9c8..bc0c0ff 100644
--- a/src/select.c
+++ b/src/select.c
@@ -12,7 +12,7 @@
** This file contains C code routines that are called by the parser
** to handle SELECT statements in SQLite.
**
-** $Id: select.c,v 1.335 2007/04/12 21:25:02 drh Exp $
+** $Id: select.c,v 1.336 2007/04/13 16:06:33 drh Exp $
*/
#include "sqliteInt.h"
@@ -1401,8 +1401,11 @@
}
pEList = pSelect->pEList;
for(i=0; i<pOrderBy->nExpr; i++){
+ struct ExprList_item *pItem;
Expr *pE = pOrderBy->a[i].pExpr;
int iCol = -1;
+ char *zLabel;
+
if( pOrderBy->a[i].done ) continue;
if( sqlite3ExprIsInteger(pE, &iCol) ){
if( iCol<=0 || iCol>pEList->nExpr ){
@@ -1415,20 +1418,21 @@
if( !mustComplete ) continue;
iCol--;
}
- for(j=0; iCol<0 && j<pEList->nExpr; j++){
- if( pEList->a[j].zName && (pE->op==TK_ID || pE->op==TK_STRING) ){
- char *zName, *zLabel;
- zName = pEList->a[j].zName;
- zLabel = sqlite3NameFromToken(&pE->token);
- assert( zLabel!=0 );
- if( sqlite3StrICmp(zName, zLabel)==0 ){
- iCol = j;
+ if( iCol<0 && (zLabel = sqlite3NameFromToken(&pE->token))!=0 ){
+ for(j=0, pItem=pEList->a; j<pEList->nExpr; j++, pItem++){
+ char *zName;
+ if( pItem->zName ){
+ zName = sqlite3StrDup(pItem->zName);
+ }else{
+ zName = sqlite3NameFromToken(&pItem->pExpr->token);
}
- sqliteFree(zLabel);
+ if( zName && sqlite3StrICmp(zName, zLabel)==0 ){
+ iCol = j;
+ break;
+ }
+ sqliteFree(zName);
}
- if( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){
- iCol = j;
- }
+ sqliteFree(zLabel);
}
if( iCol>=0 ){
pE->op = TK_COLUMN;
@@ -1436,8 +1440,7 @@
pE->iTable = iTable;
pE->iAgg = -1;
pOrderBy->a[i].done = 1;
- }
- if( iCol<0 && mustComplete ){
+ }else if( mustComplete ){
sqlite3ErrorMsg(pParse,
"ORDER BY term number %d does not match any result column", i+1);
nErr++;