Description
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 1000PostgreSQL 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
- Run
execute_querywithquery: "SELECT g AS id FROM generate_series(1,1500) g ORDER BY g -- note",limit: 1000,page: 1, thenpage: 2. - Both pages start at
1, and both reportpagination.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/OFFSETrewrite when the query ends inside a line comment, or put the clause on a new line after it (\nbeforeLIMIT), and - handle a trailing block comment and a
--inside a string literal correctly (a tokenizer that understands quotes, dollar quotes and comments, rather thansplit_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
aesslinger commentedon Nov 27, 2026
Follow-up from the Tabularis side, in case it saves some work.
Still present on
main.src/utils/pagination.rsis byte-identical onmainandv1.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 commentScanRules { hash_comments, line_comment_requires_space, backslash_escapes }, the per-dialect switches. For PostgreSQL:hash_comments: false,line_comment_requires_space: false, andbackslash_escapesonly forE'...'strings (or whenstandard_conforming_stringsis off, which the host reads withSHOW 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:
SELECT g FROM t ORDER BY g -- exportSELECT g FROM t ORDER BY g -- export\nLIMIT 1001 OFFSET 1000SELECT g FROM t ORDER BY g\n-- exportSELECT g FROM t ORDER BY g\n-- export\nLIMIT 1001 OFFSET 1000SELECT g FROM t ORDER BY g /* export */SELECT g FROM t ORDER BY g /* export */\nLIMIT 1001 OFFSET 1000SELECT '--' AS a FROM tSELECT '--' 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_semicolonindrivers/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_offsetin this plugin has the same root cause as the append:strip_limit_offset,extract_user_limitandextract_user_offsetall tokenize withsplit_whitespace, so a trailing-- commentmakesLIMIT 10not the last tokens and the user's own limit is not found. The same scanner should drive all three, not only the append.\nbeforeLIMIT) 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_moreloop described in the report is read fromhandlers/query.rs, not reproduced end to end.fix: close review findings on comment-aware pagination (#204)
fix: paginate queries that end in a comment (#204) (#208)
chore: release 1.0.0-rc.15 (#213)