Make keyset cursor pagination NULL-aware

Closed
SA
5mo ago
+854-23

Cursor.SQLFragment emitted a seek predicate with no NULL handling, so once a page boundary landed on a NULL order value the WHERE matched no rows and pagination terminated early, silently dropping the remaining rows.

Rewrite SQLFragment to emit explicit NULLS placement and a null-aware, type-agnostic seek predicate, mirroring the direction and NULLS placement on the Head/Tail inversion so backward paging stays an exact inverse. For NOT NULL columns it reduces to the previous predicate.

Add white-box pagination tests that evaluate the generated SQL with three-valued logic and walk the whole set forward and backward across the NULL boundary.


Summary by cubic

Makes keyset cursor pagination NULL-aware so pages don’t truncate when the sort column has NULLs. Adds explicit NULLS placement and a null-aware seek; backward paging mirrors forward exactly.

Tests +785 -0

  • Add Postgres-backed e2e walk over adversarial datasets (NULL blocks, ties) to confirm forward/backward paging reconstruct full order.
  • Lock exact SQL and named args across direction/position/key cases; @cursor_field_value appears only for non-NULL seeks.
  • Add unit tests for CursorKey round-trip and invalid formats, OrderDirection text marshaling, and Page trimming/HasNext/HasPrev for Head/Tail.

Service +67 -21

  • Rewrite Cursor.SQLFragment via a template that sets ASC/DESC, NULLS FIRST/LAST, and id tiebreak; Tail flips both for symmetry.
  • Use a null-aware, type-agnostic seek; bind @cursor_field_value only when non-NULL.

Other +2 -2

  • Run the new e2e suite in CI and Makefile by including ./e2e/page/....