mirror of
https://github.com/sqlite/sqlite.git
synced 2025-08-08 14:02:16 +03:00
Make the value of an explicit LIMIT clause on a scalar sub-query available to xBestIndex for simple "LIMIT 0" and "LIMIT 1" queries.
FossilOrigin-Name: 33b6a63caafccc61b3193714911cd8b5dd9b7f1798054b8c7845b23688d531f2
This commit is contained in:
26
src/expr.c
26
src/expr.c
@@ -3896,17 +3896,23 @@ int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){
|
||||
VdbeComment((v, "Init EXISTS result"));
|
||||
}
|
||||
if( pSel->pLimit ){
|
||||
/* The subquery already has a limit. If the pre-existing limit is X
|
||||
** then make the new limit X<>0 so that the new limit is either 1 or 0 */
|
||||
sqlite3 *db = pParse->db;
|
||||
pLimit = sqlite3Expr(db, TK_INTEGER, "0");
|
||||
if( pLimit ){
|
||||
pLimit->affExpr = SQLITE_AFF_NUMERIC;
|
||||
pLimit = sqlite3PExpr(pParse, TK_NE,
|
||||
sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
|
||||
/* The subquery already has a limit. If the pre-existing limit X is
|
||||
** not already integer value 1 or 0, then make the new limit X<>0 so that
|
||||
** the new limit is either 1 or 0 */
|
||||
Expr *pLeft = pSel->pLimit->pLeft;
|
||||
if( ExprHasProperty(pLeft, EP_IntValue)==0
|
||||
|| (pLeft->u.iValue!=1 && pLeft->u.iValue!=0)
|
||||
){
|
||||
sqlite3 *db = pParse->db;
|
||||
pLimit = sqlite3Expr(db, TK_INTEGER, "0");
|
||||
if( pLimit ){
|
||||
pLimit->affExpr = SQLITE_AFF_NUMERIC;
|
||||
pLimit = sqlite3PExpr(pParse, TK_NE,
|
||||
sqlite3ExprDup(db, pLeft, 0), pLimit);
|
||||
}
|
||||
sqlite3ExprDeferredDelete(pParse, pLeft);
|
||||
pSel->pLimit->pLeft = pLimit;
|
||||
}
|
||||
sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
|
||||
pSel->pLimit->pLeft = pLimit;
|
||||
}else{
|
||||
/* If there is no pre-existing limit add a limit of 1 */
|
||||
pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");
|
||||
|
Reference in New Issue
Block a user