fix(sql-editor): correct T-SQL/Azure SQL result paging#111
Merged
Conversation
Azure SQL was falling through to MySQL LIMIT/OFFSET, and SELECT … ORDER BY nested in a derived table is illegal on SQL Server — Next either failed or paged unordered. Use OFFSET/FETCH for azuresql and append to top-level ORDER BY. Co-authored-by: huy.phan9 <huyplb@users.noreply.github.com>
huyplb
marked this pull request as ready for review
July 27, 2026 00:42
Contributor
Author
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_c115f57e-7b47-403b-9667-da960078651f) |
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.
Bug and impact
Result paging for Azure SQL and many SQL Server SELECTs was broken:
LIMIT … OFFSET …wrap, which is invalid T-SQL. Page 0 fell back to an unwrapped query; Next fail-closed withPaging failed.ORDER BYwere nested asSELECT * FROM (… ORDER BY …) …, which SQL Server rejects in a derived table. Same fallback / broken Next path. Queries withoutORDER BYusedORDER BY (SELECT NULL), so pages could skip or duplicate rows.Root cause
wrapSqlForPageonly treatedsqlserver/mssqlas T-SQL (missingazuresql) and always wrapped into a derived table with a dummy outer order, instead of appendingOFFSET/FETCHwhen the statement already had a top-levelORDER BY.Fix
azuresqlas T-SQL (OFFSET … FETCH NEXT …).ORDER BYis present, appendOFFSET/FETCHto the original SQL so sort order is preserved and the statement is legal.ORDER BY (SELECT NULL)wrap.Validation
npx vitest run apps/web/src/backend/api/sql-page-wrap.test.ts apps/web/src/backend/api/sql-execute.test.ts— 21 passed.Note
Medium Risk
Changes generated SQL for live database paging in the SQL editor; mis-detection of ORDER BY could still produce wrong pages, but scope is limited and well-tested.
Overview
Fixes broken Next paging and invalid SQL for Azure SQL and SQL Server in the SQL editor by correcting how
wrapSqlForPagebuilds paged queries.Azure SQL is now treated like other T-SQL dialects (
OFFSET … ROWS FETCH NEXT …) instead of falling through to Postgres-styleLIMIT/OFFSET, which Azure rejects.For T-SQL, when the user query already has a top-level
ORDER BY, paging appendsOFFSET/FETCHto the original statement instead of wrapping it inSELECT * FROM (… ORDER BY …), which SQL Server disallows and which also preserved sort order incorrectly when only a dummy outerORDER BY (SELECT NULL)was used.A new
hasTopLevelOrderBylexer-style helper ignoresORDER BYinside subqueries, strings, comments, and bracketed identifiers so the append-vs-wrap decision stays safe. Queries without top-levelORDER BYstill use the derived-table + dummy order wrap. Unit tests cover Azure SQL, ORDER BY append paths, and detection edge cases.Reviewed by Cursor Bugbot for commit 91a654c. Bugbot is set up for automated code reviews on this repo. Configure here.