When a scalar subquery has a pre-existing "LIMIT X" then change it to
"LIMIT X<>0" rather than just "LIMIT 1" so that if X is 0 the limit
will still be zero. Ticket [99cd4807dc03f178]
FossilOrigin-Name: 82e5dcf5c1d500ed82f398b38fdae0f30033804e897fbab3c10f1e15e2abedef
diff --git a/src/expr.c b/src/expr.c
index 3656782..9310f00 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -2959,11 +2959,21 @@
sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
VdbeComment((v, "Init EXISTS result"));
}
- pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER,&sqlite3IntTokens[1], 0);
if( pSel->pLimit ){
- sqlite3ExprDelete(pParse->db, pSel->pLimit->pLeft);
+ /* 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 = sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
+ if( pLimit ){
+ pLimit->affExpr = SQLITE_AFF_NUMERIC;
+ pLimit = sqlite3PExpr(pParse, TK_NE,
+ sqlite3ExprDup(db, pSel->pLimit->pLeft, 0), pLimit);
+ }
+ sqlite3ExprDelete(db, pSel->pLimit->pLeft);
pSel->pLimit->pLeft = pLimit;
}else{
+ /* If there is no pre-existing limit add a limit of 1 */
+ pLimit = sqlite3ExprAlloc(pParse->db, TK_INTEGER, &sqlite3IntTokens[1], 0);
pSel->pLimit = sqlite3PExpr(pParse, TK_LIMIT, pLimit, 0);
}
pSel->iLimit = 0;