Skip to content

Add pagination support for composite-key queries on NoSQL drivers#408

Open
carllerche wants to merge 7 commits intomainfrom
claude/add-pagination-assertions-eqf91
Open

Add pagination support for composite-key queries on NoSQL drivers#408
carllerche wants to merge 7 commits intomainfrom
claude/add-pagination-assertions-eqf91

Conversation

@carllerche
Copy link
Member

Summary

This PR adds support for pagination, sorting, and limiting on composite-key models (partition + local key) for NoSQL drivers like DynamoDB. Previously, order_by(), limit(), and paginate() were only supported on SQL databases. These operations now work across all drivers by passing pagination parameters through the query execution pipeline.

Key Changes

  • New test suite: Added composite_key_pagination.rs with comprehensive tests for:

    • Paginating composite-key models with .paginate(), .next(), and .prev()
    • Sorting with .order_by() in both ascending and descending directions
    • Limiting results with .limit()
    • Tests run on all drivers (including DynamoDB) without SQL requirement
  • Query planning: Modified statement.rs to extract pagination parameters (limit, sort direction, cursor) from query statements and pass them to QueryPk operations via a new extract_query_pk_pagination() method

  • MIR and execution layers: Updated mir/query_pk.rs and exec/query_pk.rs to carry pagination fields (limit, order, cursor) through the compilation pipeline

  • Driver operation: Extended driver/operation/query_pk.rs with three new optional fields:

    • limit: Maximum number of items to return
    • order: Sort direction (Asc/Desc) for composite keys
    • cursor: Serialized key for resuming paginated queries
  • DynamoDB implementation: Updated query_pk.rs in the DynamoDB driver to apply pagination parameters:

    • Uses .limit() for result count limiting
    • Uses .scan_index_forward() to control sort direction
    • Uses .set_exclusive_start_key() for cursor-based pagination
  • Removed assertion: Deleted the assertion in lower.rs that prevented ordering and limiting on non-SQL drivers, enabling these features for all backends

  • Enhanced SQL tests: Updated one_model_sort_limit.rs with operation verification to ensure SQL queries include proper ORDER BY and LIMIT clauses

Implementation Details

The pagination parameters flow through the compilation pipeline: statement parsing → query planning → MIR → execution → driver operation. This allows NoSQL drivers to implement pagination natively using their query APIs while maintaining the same high-level API for users across all database backends.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL

Add limit, scan_index_forward, and exclusive_start_key fields to the
QueryPk driver operation, enabling DynamoDB-native pagination. The engine
planner now extracts order_by/limit/offset from query statements and maps
them to these fields for NoSQL drivers, removing the previous panics that
blocked ordering and limit on KV paths. The DynamoDB driver passes these
parameters through to the AWS SDK Query API.

Add integration tests asserting the correct driver ops are dispatched with
pagination attributes set when querying partitioned composite keys with
limit and order_by.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
Resolve merge conflict in DynamoDB query_pk.rs by combining the
secondary index branching logic from main with the pagination
parameters (limit, scan_index_forward, exclusive_start_key) from
this branch. Also fix &Db -> &mut Db API changes in pagination
driver-ops tests and add missing pagination fields to the new
secondary index QueryPk initialization in plan/statement.rs.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
Combine main's refactored try_fan_out closure API with pagination
fields (limit, scan_index_forward, exclusive_start_key) from this
branch.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
- scan_index_forward: Option<bool> → order: Option<stmt::Direction>
- exclusive_start_key: Option<stmt::Value> → cursor: Option<stmt::Value>

The K/V engine interface should use generic terminology rather than
DynamoDB-specific names. `order` uses the existing `stmt::Direction`
type (Asc/Desc) instead of a bool, and `cursor` is a standard
pagination term replacing `exclusive_start_key`.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
Instead of a standalone test file, add QuerySql structure assertions
directly to the existing sort_asc, paginate, and limit tests in
one_model_sort_limit.rs. This verifies that order_by and limit clauses
flow through to the SQL statements without duplicating test coverage.

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
The existing pagination tests in one_model_sort_limit.rs all require(sql),
meaning DynamoDB never exercises pagination, limit, or ordering. Add a new
composite_key_pagination test module with four tests that use a composite
partition+local key model (no requires gate), so they run on every driver
including DynamoDB:

- paginate_composite_key: descending pagination with next/prev navigation
- paginate_composite_key_asc: ascending pagination walking all pages
- limit_composite_key: limit with and without ordering
- sort_composite_key: ascending and descending sort verification

Each test asserts the correct driver operation type (QuerySql for SQL,
QueryPk for KV drivers).

https://claude.ai/code/session_019tLeKCnD9izwJjJXYQWygL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants