Repository navigation
fix(db): recognise count-less FETCH, FETCH ... WITH TIES, and preceding OFFSET in the query limiter - #1995
Merged
Conversation
…ng OFFSET in the query limiter (libredb#1930)
Contributor
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
11 of 21 tasks
cevheri
approved these changes
Oct 29, 2026
cevheri
added a commit
that referenced
this pull request
Oct 29, 2026
… reads (#2075) The FETCH probe comment listed SQL Server among the engines that accept every shape the probe reads. T-SQL takes FETCH only after OFFSET m {ROW|ROWS}, always with a count and only with ONLY. The comment now says which engine takes which part, and that a shape an engine rejects is kept as written so the engine reports its own error. docs/editor/query-optimization.md now describes the shapes the probe has read since #1995 (count-less FETCH, WITH TIES, a preceding OFFSET), and docs/providers/mssql.md no longer says the shared probes read only a ROWS ONLY tail. Behaviour is unchanged. Refs #1930
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.
Description
In
src/lib/db/utils/query-limiter.ts, theFETCHprobe inanalyzeUnderGrammarpreviously only matchedFETCH {FIRST|NEXT} <count> ROW[S] ONLY. As a result:FETCH {FIRST|NEXT} ROW[S] ONLYstatements were reported as unbounded (hasLimit: false), causingapplyQueryLimit(used bySqlBaseProviderandCouchbaseProvider) to appendLIMIT 500andOracleProvider,Db2Provider, andMSSQLProviderprepareQuerypaths to inject a secondFETCH FIRSTorTOPbound.FETCH {FIRST|NEXT} [n] ROW[S] WITH TIESstatements were reported as unbounded (hasLimit: false) and received a conflicting second bound.OFFSET m [ROW|ROWS]beforeFETCH ...(such asOFFSET 10 ROWS FETCH NEXT 20 ROWS ONLY) reportedhasOffset: falseandexistingOffset: undefined(appliedOffset: 0).Per @cevheri's scope on #1930:
FETCHprobe inanalyzeUnderGrammarto match\b(?:OFFSET\s+(${value})\s+(?:ROWS?\s+)?)?FETCH\s+(?:FIRST|NEXT)\s+(?:(${value})\s+)?ROWS?\s+(?:ONLY|WITH\s+TIES)\s*$:FETCH {FIRST|NEXT} ROW[S] {ONLY|WITH TIES}(omitted count) setshasLimit: trueandexistingLimit: 1.FETCH {FIRST|NEXT} <count> ROW[S] {ONLY|WITH TIES}setshasLimit: trueandexistingLimit: boundCount(count).OFFSET <m> [ROW|ROWS]setshasOffset: trueandexistingOffset: boundOffset(m)(reported asappliedOffset: mbyapplyQueryLimit).FETCHinside a subquery, string literal, or comment is not counted as the outer statement's bound).forceLimituntouched as requested.Proof: Reproduction Script (Before vs. After)
Before (
main@fb54531d)After (this PR)
Type of Change
Related Issue
Closes #1930
Changes Made
src/lib/db/utils/query-limiter.ts: UpdateanalyzeUnderGrammar'sFETCHprobe to recognise optional precedingOFFSET m [ROW|ROWS], omitted row counts (existingLimit = 1), andWITH TIESalongsideONLY.tests/unit/db/query-limiter.test.ts: Add unit tests covering each row of the issue table across[undefined, "postgres", "duckdb", "oracle", "db2", "mssql"], case and spacing variations, and negative cases (FETCHinside a subquery, string literal, line comment, or block comment) for bothanalyzeQueryandapplyQueryLimit.tests/integration/db/oracle-provider.test.ts: AddprepareQuerytests verifying that none of theFETCH/OFFSET ... FETCHvariants receive a secondFETCH FIRSTbound, whileFETCHinside a subquery, string literal, or comment still gets bounded.tests/integration/db/db2-provider.test.ts: AddprepareQuerytests verifying the same positive and negative cases for Db2.tests/integration/db/mssql-provider.test.ts: AddprepareQuerytests verifying that none of theFETCH/OFFSET ... FETCHvariants haveTOPspliced into them, whileFETCHinside a subquery, string literal, or comment still getsTOPinjected.Testing
I have tested this locally
I have added/updated tests
All existing tests pass
bun test tests/unit/db/query-limiter.test.ts tests/integration/db/oracle-provider.test.ts tests/integration/db/db2-provider.test.ts tests/integration/db/mssql-provider.test.ts: 1,062 pass, 0 fail (3,580 expect() calls), 100.00% line coverage onsrc/lib/db/utils/query-limiter.ts.Verified live against PostgreSQL (
SELECT * FROM users FETCH FIRST ROW ONLY;) in LibreDB Studio before and after the fix.Test Environment
main@fb54531d)Screenshots (if applicable)
Before (
SELECT * FROM users FETCH FIRST ROW ONLY;in Studio against real PostgreSQL)After (
SELECT * FROM users FETCH FIRST ROW ONLY;in Studio against real PostgreSQL)Checklist
bun run test:coverageandbun run coverage:check)src/lib/db/providers/, I updated the matchingdocs/providers/documentation andtests/integration/db/tests in the same PR (provider triad)AGENT_STATEMENT_LANGUAGErow, I kept its agent leg in step: the engine overlay undersrc/lib/agent/languages/<statement-language>/engines/, theAgent language packsubsection of its provider document, andtests/unit/lib/agent/engine-docs.test.tsAdditional Notes
forceLimitbehavior inapplyQueryLimitis intentionally left unchanged per maintainer scope on [BUG] Query limiter corrupts count-less FETCH FIRST ROW ONLY, FETCH ... WITH TIES, and forceLimit on FETCH clauses into invalid SQL #1930.