fix(parser): preserve T-SQL OFFSET/FETCH across parse→generate roundtrip (CR-010)#16
Merged
Merged
Conversation
…p (CR-010) The T-SQL parser failed to consume the trailing ROWS keyword after 'OFFSET <expr>'. As a result, when re-parsing T-SQL like ORDER BY col OFFSET 2 ROWS FETCH NEXT 5 ROWS ONLY the subsequent match_token(Fetch) saw ROWS on the stream, returned false, and the FETCH clause was silently dropped. The generator already emits OFFSET/FETCH correctly when fetch_first is set, so transpile worked but parse->generate roundtrips lost the page-size limit. Fix: after parsing the OFFSET expression, consume the optional ROWS/ROW keyword (ANSI SQL:2008 / T-SQL form). Tests (tests/test_dialects.rs): - tsql_offset_fetch_roundtrip_preserved - tsql_offset_fetch_with_subquery_order_roundtrip - tsql_offset_only_no_fetch_roundtrip - pg_limit_offset_transpile_then_tsql_roundtrip (end-to-end ABAC scenario) Refs: PSQ-2420
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.
Summary
Fixes CR-010 / PSQ-2420: T-SQL
FETCH NEXT m ROWS ONLYwas silently dropped during parse → generate roundtrips, causing data over-exposure in the Secure Query Gateway whenever an ABAC policy was active on aLIMIT/OFFSETquery.Root cause
The defect is in the parser, not the generator. After parsing
OFFSET <expr>, the parser did not consume the trailingROWS/ROWkeyword. So for input like:```
ORDER BY col OFFSET 2 ROWS FETCH NEXT 5 ROWS ONLY
```
the next `match_token(Fetch)` saw `ROWS` and returned `false`, leaving `fetch_first = None`. The generator already emits `OFFSET n ROWS FETCH NEXT m ROWS ONLY` correctly for T-SQL/Fabric when `fetch_first` is set, which is why direct transpile worked but re-parsing the transpiled SQL did not.
Fix
Two-line change in `src/parser/sql_parser.rs`: after parsing the OFFSET expression, consume the optional `ROWS`/`ROW` keyword (ANSI SQL:2008 / T-SQL form).
Tests
Added 4 tests in `tests/test_dialects.rs`:
All previously passing tests continue to pass (zero regressions across the full suite).
Validation
Refs: PSQ-2420