Repository navigation
fix(query): bound the rows read, instead of editing the user's SQL - #565
Merged
Merged
Conversation
Closes #520. The setting was appended to any statement starting SELECT, SHOW, DESCRIBE or EXPLAIN. Three of those four are wrong, and the row limit is on by default — so typing SHOW TABLES in the editor was a syntax error out of the box. Confirmed against MySQL 8. No SHOW form takes a LIMIT: SHOW TABLES LIMIT 5 ERROR 1064 SHOW DATABASES LIMIT 5 ERROR 1064 SHOW CREATE TABLE users LIMIT 5 ERROR 1064 SHOW COLUMNS FROM users LIMIT 5 ERROR 1064 SHOW VARIABLES LIMIT 5 ERROR 1064 SHOW PROCESSLIST LIMIT 5 ERROR 1064 DESCRIBE users LIMIT 5 ERROR 1064 DESC users LIMIT 5 ERROR 1064 The issue says most SHOW forms accept a LIMIT. None do. I wrote that issue, and checked rather than trusted it. EXPLAIN does accept one, which is worse than refusing: the LIMIT binds to the inner statement, so the plan describes a query the user did not ask about. Not reachable today, since explain goes through its own command with no limit, but it was one caller away. The check was also wrong in the other direction. `starts_with("SELECT")` misses TABLE and VALUES, which do take a LIMIT and were coming back unbounded, and misses a CTE, a leading comment and a leading bracket — so `WITH x AS (...) SELECT ...` ignored the setting entirely. accepts_row_limit replaces the prefix test, going through effective_verb so all four of those cases resolve to the verb that decides. Its allowlist — SELECT, TABLE, VALUES — is the same one EXPLAIN uses for what it can safely ANALYZE, which is not a coincidence: both questions are "does this produce a result set that a LIMIT bounds". A LIMIT the user wrote still wins. The setting is a ceiling on what the app volunteers, not an override of what was asked for. Three unit tests and four integration tests. Three of the four fail against the previous behaviour: SHOW and DESCRIBE error, and TABLE and the CTE come back unbounded. Co-Authored-By: Claude Opus 5 <[email protected]>
Closes #520. The row limit was applied by appending `LIMIT n` to statements that looked row-returning. The toolbar control was right — a toggle on by default, an adjustable count — but the mechanism under it could not make its promise true, because it depended on parsing SQL correctly and every gap was either a broken query or a cap that silently did not apply. Demonstrated against MySQL 8. The statement stops parsing: SHOW TABLES / DATABASES / CREATE TABLE / COLUMNS / VARIABLES ERROR 1064 DESCRIBE users, DESC users ERROR 1064 SELECT ... FOR UPDATE ERROR 1064 — LIMIT must precede the locking clause, appending puts it after SELECT ... LOCK IN SHARE MODE ERROR 1064 SELECT ... INTO @var ERROR 1064 Or the cap silently does not apply, which is worse, because the toolbar goes on claiming it does: SELECT * FROM users -- note the appended LIMIT lands after the comment and is commented out. Confirmed: returns every row SELECT * FROM (SELECT ... LIMIT 5) x read as already-bounded, so nothing added TABLE / VALUES / WITH ... SELECT not matched at all The rows are now bounded while reading them. The user's SQL reaches the server exactly as written; the executor stops keeping rows at the limit. This is what DBeaver, DataGrip and psql do for SQL the user wrote. Appending remains correct where the app composes the statement itself, which is what the schema tree already does when browsing a table — that call site is unchanged, and is the case MySQL Workbench, HeidiSQL and TablePlus all handle by appending too. Two things follow that appending could not do. The limit now applies to statements whose syntax has no LIMIT at all — SHOW output, and the result sets a procedure returns. And a result that is exactly `limit` rows long is no longer reported as truncated: truncation is recorded when a row is actually withheld rather than inferred from the count, so the banner stops sending people to look for rows that do not exist. At the cap the loop stops accumulating but keeps draining, since breaking out would abandon the rest of a multi-statement batch. The server has already produced those rows either way; what is bounded is what the app holds and returns. has_limit_clause, strip_limit, find_limit_keyword and find_keyword_offset existed only to support the rewriting and are gone, with their tests — 187 lines of parsing that no longer has anything to parse. Six integration tests, all failing against the appending version, plus the four from the first attempt at this issue. Together: the statements that used to error now run, the two silent cases are bounded, SHOW is bounded, and an exactly-full result is not called truncated. Co-Authored-By: Claude Opus 5 <[email protected]>
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.
Closes #520. Rewritten after investigating how other clients do this — the first version of this PR narrowed which statements got a
LIMITappended; this one stops appending to user SQL altogether.The control was right, the mechanism couldn't deliver it
The toolbar is exactly the intended feature — a toggle on by default, count adjustable 1–100000. What sat underneath appended
LIMIT nto the user's statement, which means the guarantee depended on parsing SQL correctly. Every gap was either a broken query or a cap that silently didn't apply.Demonstrated against MySQL 8. The statement stops parsing:
Or the cap silently doesn't apply, which is worse — the toolbar goes on claiming it does:
Since
limitEnableddefaults totrue,SHOW TABLESin the editor was a syntax error out of the box.What other clients do
The split tracks who wrote the statement:
LIMITfor table browsing and pagination.FETCH_COUNT. The SQL goes to the server verbatim.Workbench is the exception that rewrites user SQL, and pays for it in the same failure modes listed above.
This codebase already has both call sites and already gets one right:
SchemaTreecomposesSELECT * FROM \db`.`t` LIMIT n` itself when browsing a table. That's unchanged. The executor-level rewrite was the misplaced half.The change
Rows are bounded while being read. The user's SQL reaches the server exactly as written; the executor stops keeping rows at the limit.
Two things follow that appending couldn't do:
LIMITat all —SHOWoutput, and the result sets a procedure returns.limitrows long is no longer reported as truncated. Truncation is now recorded when a row is actually withheld rather than inferred from the count, so the banner stops sending people to look for rows that don't exist.At the cap the loop stops accumulating but keeps draining — breaking out would abandon the rest of a multi-statement batch. The server produced those rows either way; what's bounded is what the app holds and returns.
has_limit_clause,strip_limit,find_limit_keywordandfind_keyword_offsetexisted only to support the rewriting and are gone with their tests — 187 lines of parsing that no longer has anything to parse.Tests
Ten integration tests. The six new ones all fail against the appending version — verified by stashing both source files:
64 mas-core + 16 mas-admin integration tests against live servers, 47 lib, 1819 frontend; clippy/fmt/dprint clean.
🤖 Generated with Claude Code