aboutsummaryrefslogtreecommitdiff
path: root/src/expr.c
diff options
context:
space:
mode:
authordan <Dan Kennedy>2025-07-03 16:05:41 +0000
committerdan <Dan Kennedy>2025-07-03 16:05:41 +0000
commitbd05edd98f44e3430a8ecfecfdead510897e7582 (patch)
tree1ade001889add11bdd354e175771e5aeb22bfab4 /src/expr.c
parent1ff6f19d8b8199f49cd67171ae03873766d12c3f (diff)
parenta24a397b8bf509b7548cad3bb156102a5452526f (diff)
downloadsqlite-bd05edd98f44e3430a8ecfecfdead510897e7582.tar.gz
sqlite-bd05edd98f44e3430a8ecfecfdead510897e7582.zip
Fix a few cases where LIMIT clauses that were part of scalar sub-queries on virtual tables were not being passed to xBestIndex methods correctly.
FossilOrigin-Name: 960a8e6fc91f47add3a089dc6cef013109deadf809994c5149ad3bdfb3884de0
Diffstat (limited to 'src/expr.c')
-rw-r--r--src/expr.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/expr.c b/src/expr.c
index c8dfd3af3..ebac654a2 100644
--- a/src/expr.c
+++ b/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);
- }
- sqlite3ExprDeferredDelete(pParse, pSel->pLimit->pLeft);
- pSel->pLimit->pLeft = 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;
+ }
}else{
/* If there is no pre-existing limit add a limit of 1 */
pLimit = sqlite3Expr(pParse->db, TK_INTEGER, "1");