Open
Conversation
SQLite does not support ORDER BY and LIMIT on DELETE unless
compiled with SQLITE_ENABLE_UPDATE_DELETE_LIMIT, which is not
the case for standard builds. Rewrite single-table DELETE
statements using ORDER BY and/or LIMIT to drive deletion off a
rowid subquery, mirroring the approach already used for UPDATE:
DELETE FROM t WHERE c = 1 ORDER BY c LIMIT 10
becomes
DELETE FROM t WHERE rowid IN (
SELECT rowid FROM t WHERE c = 1 ORDER BY c LIMIT 10
)
The subquery forwards tableRef, tableAlias, whereClause,
orderClause, and simpleLimitClause, so DELETEs that reference
an aliased table (e.g. DELETE FROM t AS a WHERE a.c = 1 LIMIT 1)
keep the alias in scope inside the subquery.
Multi-table DELETE is unchanged; MySQL's grammar disallows
ORDER BY/LIMIT there.
Fixes #100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds support for single-table
DELETEstatements withORDER BYand/orLIMITclauses, which SQLite rejects as a syntax error unless it was compiled withSQLITE_ENABLE_UPDATE_DELETE_LIMIT(not the case for standard builds, including the one bundled with WordPress Playground).The fix mirrors the existing
UPDATELIMIT rewrite: whenORDER BYorLIMITis present, the statement is translated to drive deletion off arowidsubquery.The subquery forwards
tableRef,tableAlias,whereClause,orderClause, andsimpleLimitClause, so queries that reference an aliased table keep the alias in scope inside the subquery (e.g.DELETE FROM t AS a WHERE a.c = 1 LIMIT 1works).Scope
DELETEwithORDER BY,LIMIT, or both — with or withoutWHEREand alias.DELETE— MySQL's grammar disallowsORDER BY/LIMITthere, so there's nothing to add.Tests
LIMIT,WHERE+LIMIT,ORDER BY+LIMIT,WHERE+ORDER BY+LIMIT, and alias +WHERE+LIMIT.DELETE ... LIMITthrough the driver, and assert on the surviving rows — including a case that matches multiple rows to verifyLIMITis actually enforced, and an alias case.Fixes #100