diff options
author | drh <drh@noemail.net> | 2019-09-23 11:55:22 +0000 |
---|---|---|
committer | drh <drh@noemail.net> | 2019-09-23 11:55:22 +0000 |
commit | 7ca1347f59d33015047fad6ca2cf878f2488eb22 (patch) | |
tree | 1a3343901bee7ce28092801c0167db4d2d4fb62b /src/expr.c | |
parent | b7fc7c8556e5745dca30d561ec46776a7d293e93 (diff) | |
download | sqlite-7ca1347f59d33015047fad6ca2cf878f2488eb22.tar.gz sqlite-7ca1347f59d33015047fad6ca2cf878f2488eb22.zip |
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
Diffstat (limited to 'src/expr.c')
-rw-r--r-- | src/expr.c | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/src/expr.c b/src/expr.c index 3656782c4..9310f0003 100644 --- a/src/expr.c +++ b/src/expr.c @@ -2959,11 +2959,21 @@ int sqlite3CodeSubselect(Parse *pParse, Expr *pExpr){ 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; |