Skip to content

Bug: pagination appends LIMIT/OFFSET inside a trailing -- comment, so every page returns the same rows #204

Done

Description

@aesslinger

Plugin version

v1.0.0-rc.14 (utils/pagination.rs as of that tag)

Tabularis app version

0.33.1 (seen while reviewing TabularisDB/tabularis#1625)

Operating system

macOS (the behavior is in SQL text handling, not platform specific)

What happened?

build_paginated_query appends LIMIT n OFFSET m to the end of the query text, but it is not comment-aware. strip_limit_offset only trims trailing whitespace and ; and tokenizes with split_whitespace, so a query that ends in a -- line comment gets the clause appended inside the comment:

SELECT g AS id FROM generate_series(1,1500) g ORDER BY g -- export LIMIT 1001 OFFSET 1000

PostgreSQL ignores everything after --, so no pagination happens. The statement returns every row from the start, on every page.

Checked against a real PostgreSQL (the plugin's pagination text, run with psql):

Query Rows returned First row
... ORDER BY g LIMIT 1001 OFFSET 1000 500 1001
... ORDER BY g -- export LIMIT 1001 OFFSET 1000 1500 1

The handler then takes the first page_size rows and, because more than page_size came back, reports has_more: true. So every page returns the same first rows, and a caller that follows has_more never reaches the end. Tabularis's "Export query" does exactly that, so an export of a query with a trailing comment repeats the first 1000 rows until it is cancelled.

I did not drive the plugin's JSON-RPC directly. The SQL behavior above is checked against PostgreSQL, and the has_more consequence is read from handlers/query.rs; it is worth confirming with a test.

Steps to reproduce

  1. Run execute_query with query: "SELECT g AS id FROM generate_series(1,1500) g ORDER BY g -- note", limit: 1000, page: 1, then page: 2.
  2. Both pages start at 1, and both report pagination.has_more = true.

Error message, if any

None. The query succeeds, which is why the result is wrong without any error.

Suggested fix

Make appending the paging clause comment-safe. Either:

  • skip the LIMIT/OFFSET rewrite when the query ends inside a line comment, or put the clause on a new line after it (\n before LIMIT), and
  • handle a trailing block comment and a -- inside a string literal correctly (a tokenizer that understands quotes, dollar quotes and comments, rather than split_whitespace).

Also add tests for a trailing -- comment, a trailing /* comment */, and a -- inside a string literal.

Related

TabularisDB/tabularis#1625 (CodeRabbit finding on export.rs). The host loop trusts has_more; a defensive guard on the host side is hard to do safely, because a table with many identical rows legitimately returns identical pages.

Activity

  1. added
    bug
    on Nov 24, 2026
  2. aesslinger commented on Nov 27, 2026

    @aesslinger
    CollaboratorAuthor

    Follow-up from the Tabularis side, in case it saves some work.

    Still present on main. src/utils/pagination.rs is byte-identical on main and v1.0.0-rc.14 (same blob, 145340c7), so a new release would not change this on its own.

    Tabularis already solved the same problem for its own drivers. The host has a comment- and quote-aware pagination scanner (introduced in TabularisDB/tabularis#389, with its string rules reworked in #1653 for v0.34.0). It is used by the host's MySQL and SQLite drivers, not by this plugin, since this plugin paginates inside its own process. The relevant code in the Tabularis repo is in src-tauri/src/drivers/common/query.rs:

    • build_paginated_query_for(query, page_size, page, ScanRules)
    • trailing_comment_start(sql, rules), a small state machine that walks the SQL and tracks single quotes, double quotes, -- line comments and /* */ block comments, so it knows whether the end of the query is inside a comment
    • ScanRules { hash_comments, line_comment_requires_space, backslash_escapes }, the per-dialect switches. For PostgreSQL: hash_comments: false, line_comment_requires_space: false, and backslash_escapes only for E'...' strings (or when standard_conforming_strings is off, which the host reads with SHOW standard_conforming_strings)

    I ran it with PostgreSQL rules on the cases from this issue. It places the page clause on a new line after the comment instead of on the same line:

    Query Result
    SELECT g FROM t ORDER BY g -- export SELECT g FROM t ORDER BY g -- export\nLIMIT 1001 OFFSET 1000
    SELECT g FROM t ORDER BY g\n-- export SELECT g FROM t ORDER BY g\n-- export\nLIMIT 1001 OFFSET 1000
    SELECT g FROM t ORDER BY g /* export */ SELECT g FROM t ORDER BY g /* export */\nLIMIT 1001 OFFSET 1000
    SELECT '--' AS a FROM t SELECT '--' AS a FROM t LIMIT 1001 OFFSET 1000 (the -- is inside a string, so it is left alone)

    The same code also has a test for a trailing line comment (test_build_paginated_query_preserves_trailing_line_comment_without_semicolon in drivers/common/tests.rs) and for a trailing # comment, which are good starting cases for this plugin's tests.

    Two details that matter for the fix here, since strip_limit_offset in this plugin has the same root cause as the append:

    • strip_limit_offset, extract_user_limit and extract_user_offset all tokenize with split_whitespace, so a trailing -- comment makes LIMIT 10 not the last tokens and the user's own limit is not found. The same scanner should drive all three, not only the append.
    • The minimal fix is to put the clause on a new line (\n before LIMIT) when the query ends inside a line comment, but a quote- and comment-aware scan is needed to know that, because a -- inside a string literal or a block comment is not a line comment.

    Everything above about the host scanner is from reading and running it. I still have not driven this plugin's JSON-RPC directly, so the has_more loop described in the report is read from handlers/query.rs, not reproduced end to end.

  3. self-assigned this
    on Nov 28, 2026
  4. added a commit that references this issue on Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions