Skip to content

Respect SQL row limits during pagination - #211

Open
Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 2 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/sql-pagination-row-limits
Open

Sylvester Kaczmarek (sylvesterkaczmarek) wants to merge 2 commits into
microsoft:mainfrom
sylvesterkaczmarek:fix/sql-pagination-row-limits

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek Sylvester Kaczmarek (sylvesterkaczmarek) commented Nov 6, 2026 •

Copy link
Copy Markdown

Fixes #210.

Summary

Respect integer-literal TOP and OFFSET ... FETCH row limits across the complete SQL result in both synchronous and asynchronous clients. Stop requesting pages as soon as the requested number of valid rows is available, trimming an oversized first or final page. Offset handling remains server-side, so the client does not skip rows twice.

Use one shared, conservative limit reader for the supported SELECT subset. It ignores quoted values/identifiers and comments, and leaves percentage limits, WITH TIES, expressions, parameters and ambiguous nested comments to the server. The query text and existing validation are unchanged. Unbounded queries retain pagination and its existing cycle/error guards. TOP 0 retains the initial request and returns no rows.

Validation

On Python 3.12:

  • 62 new regression cases passed, including sync/async parity, first-page stopping, multi-page trimming, both nextLink spellings, zero/short results, literal/comment protection and existing cycle handling.
  • The initial bounded-query regression subset fails 14 cases against the original implementation.
  • Full unit suite: 2,540 passed, plus 32 passing subtests.
  • Black and Ruff passed on the new test file; changed modules compile and the diff check passes.
  • Ruff comparison of the three modified production modules reports the same 480 existing findings before and after, with zero introduced findings. This is not a claim that the repository is globally lint-clean.
  • Independent source review identified a nested-comment ambiguity, which is covered by additional regressions and corrected by declining to infer a cap for those inputs; follow-up review found no remaining incorrect-cap case within the supported subset.

HTTP transport and response pages are mocked. No credentials, live Dataverse tenant or integration endpoint was used, and no public method signature or dependency declaration changes.

Pagination follow-up

Parenthesized TOP counts can be followed immediately by a projection, while unparenthesized counts still require a token separator. Added adjacent bracketed and unquoted projection cases, percentage and WITH TIES exclusions, and both synchronous and asynchronous stopping tests.

Merged main 5d6f358 in signed commit 0e55710. Python 3.12: the focused row-limit suite passed all 71 tests. Black and Ruff checks for the test file passed, as did the diff check. The earlier full-suite result is from the previous revision; a full rerun and hosted CI remain outstanding.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The parser fails to recognize valid parenthesized TOP limits adjacent to projections, causing sync and async clients to follow nextLink and return more rows than requested.

1 open issue
What changed in this PR

Addresses #210 by enforcing SQL row limits across paginated results in both synchronous and asynchronous Dataverse clients.

Changes:

  • Adds a shared reader for integer-literal TOP and OFFSET ... FETCH limits, leaving unsupported or ambiguous forms to the server.
  • Stops pagination when the limit is reached and trims excess rows without applying offsets twice.
  • Adds regression coverage for sync/async behavior, parsing safeguards, and pagination edge cases.
File Description
tests/​unit/​data/​test_sql_row_limits.py Adds shared parsing and sync/async pagination regressions.
src/​PowerPlatform/​Dataverse/​data/​_odata.py Enforces row limits during synchronous SQL pagination.
src/​PowerPlatform/​Dataverse/​data/​_odata_base.py Adds shared SQL row-limit detection.
src/​PowerPlatform/​Dataverse/​aio/​data/​_async_odata.py Applies equivalent limits to asynchronous SQL pagination.

Review effort: Balanced

re.DOTALL,
)
_SQL_TOP_LIMIT_RE = re.compile(
r"^\s*SELECT\s+(?:DISTINCT\s+|ALL\s+)?TOP\b\s*(?:\(\s*([0-9]+)\s*\)|([0-9]+))(?=\s)",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 0e55710. Parenthesized integer TOP now accepts an adjacent projection; the unparenthesized form still requires whitespace. Both clients are covered, including the PERCENT and WITH TIES exclusions. All 71 focused tests, Black and Ruff passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants