1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-10-24 09:53:10 +03:00

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
This commit is contained in:
drh
2007-04-13 16:06:32 +00:00
parent d215acf1f4
commit 94ccde58d0
5 changed files with 74 additions and 29 deletions

View File

@@ -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 @@ void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
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 @@ void sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
}
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;
}
}

View File

@@ -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 @@ static int matchOrderbyToColumn(
}
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 @@ static int matchOrderbyToColumn(
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( iCol<0 && sqlite3ExprCompare(pE, pEList->a[j].pExpr) ){
iCol = j;
if( zName && sqlite3StrICmp(zName, zLabel)==0 ){
iCol = j;
break;
}
sqliteFree(zName);
}
sqliteFree(zLabel);
}
if( iCol>=0 ){
pE->op = TK_COLUMN;
@@ -1436,8 +1440,7 @@ static int matchOrderbyToColumn(
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++;