Skip to content

fix: cap the row count of LIMIT offset, count instead of its offset - #444

Open
serhiizghama wants to merge 2 commits into
bytebase:mainfrom
serhiizghama:fix/limit-offset-count-max-rows
Open

serhiizghama wants to merge 2 commits into
bytebase:mainfrom
serhiizghama:fix/limit-offset-count-max-rows

Conversation

@serhiizghama

Copy link
Copy Markdown

With max_rows set, a MySQL/MariaDB/SQLite query using the LIMIT offset, count form gets the wrong rows back. The limiter reads the first number after LIMIT as the row count, so on LIMIT 10, 5 it tightens the offset and leaves the count alone.

On SQLite with max_rows = 3, SELECT n FROM numbers ORDER BY n LIMIT 10, 5 came back as 5, 6, 7 instead of 11, 12, 13. It fails the other way too: LIMIT 2, 10 with max_rows = 5 looked like it was already under the cap (2 <= 5), so it wasn't rewritten at all and returned 10 rows.

findTopLevelLimit now also matches the optional , count part and treats the second operand as the row count. When it tightens the clause it writes the offset back as it was. A parameterized count (LIMIT 10, ?) takes the existing subquery-wrap path. Copilot flagged the same thing in its review of #400, but it didn't make it into #429.

I added unit cases and an SQLite integration test that fails on main. I couldn't run the MySQL/MariaDB containers locally, but they go through the same function.

@tianzhou
tianzhou requested a balanced review from Copilot September 29, 2026 13:06
@tianzhou

Copy link
Copy Markdown
Member

Please open an issue first

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The focused implementation correctly addresses both failure modes and includes appropriate regression coverage.

Review effort: Balanced
Findings: None

What changed in this PR

Fixes row limiting for MySQL/MariaDB/SQLite LIMIT offset, count queries.

Changes:

  • Caps the second LIMIT operand while preserving the offset.
  • Retains subquery wrapping for parameterized counts.
  • Adds unit and SQLite integration coverage.
File Description
src/​utils/​sql-row-limiter.ts Correctly parses and rewrites comma-form limits.
src/​utils/​__tests__/​sql-row-limiter.test.ts Tests literal, parameterized, and probe behavior.
src/​connectors/​__tests__/​sqlite.integration.test.ts Verifies returned rows, offsets, and truncation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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.

3 participants