Skip to content

fix(query): bound the rows read, instead of editing the user's SQL - #565

Merged
EVWorth merged 2 commits into
mainfrom
fix/query-row-limit-scope
Sep 8, 2026
Merged

EVWorth merged 2 commits into
mainfrom
fix/query-row-limit-scope

Conversation

@EVWorth

@EVWorth EVWorth commented Sep 8, 2026 •

Copy link
Copy Markdown
Owner

Closes #520. Rewritten after investigating how other clients do this — the first version of this PR narrowed which statements got a LIMIT appended; 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 n to 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:

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)
SELECT ... LOCK IN SHARE MODE  ERROR 1064
SELECT ... INTO @var           ERROR 1064

Or the cap silently doesn't apply, which is worse — the toolbar goes on claiming it does:

SELECT * FROM users -- note              appended LIMIT lands after the comment
                                         and is commented out → every row returned
SELECT * FROM (SELECT ... LIMIT 5) x     read as already-bounded → nothing added
TABLE / VALUES / WITH ... SELECT         not matched at all

Since limitEnabled defaults to true, SHOW TABLES in the editor was a syntax error out of the box.

What other clients do

The split tracks who wrote the statement:

  • Rewrite when the app composed it — Workbench, HeidiSQL, TablePlus, Sequel Ace append LIMIT for table browsing and pagination.
  • Cap the fetch when the user wrote it — DBeaver and DataGrip use driver max-rows and incremental fetch; psql uses 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: SchemaTree composes SELECT * 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:

  • The limit applies to statements whose syntax has no LIMIT at all — SHOW output, and the result sets a procedure returns.
  • A result exactly limit rows 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_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.

Tests

Ten integration tests. The six new ones all fail against the appending version — verified by stashing both source files:

a_locking_read_still_runs_under_a_row_limit ............ FAILED
a_trailing_comment_no_longer_swallows_the_limit ........ FAILED
a_limit_inside_a_subquery_does_not_count_as_the_outer_one  FAILED
show_output_is_bounded_too ............................. FAILED
a_result_exactly_the_limit_long_is_not_called_truncated  FAILED
the_statement_reaches_the_server_as_written ............ FAILED

64 mas-core + 16 mas-admin integration tests against live servers, 47 lib, 1819 frontend; clippy/fmt/dprint clean.

🤖 Generated with Claude Code

EVWorth and others added 2 commits September 7, 2026 21:26
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]>
@EVWorth EVWorth changed the title fix(query): apply the row limit only where a LIMIT means anything fix(query): bound the rows read, instead of editing the user's SQL Sep 8, 2026
@EVWorth
EVWorth merged commit 932ec3b into main Sep 8, 2026
10 checks passed
@EVWorth
EVWorth deleted the fix/query-row-limit-scope branch September 8, 2026 02:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P2][bug] The row limit is appended to EXPLAIN and SHOW, rewriting the statement it is meant to bound

1 participant