Skip to content

Persistence: DynamoDB (atomic) sort the children index by entity id - #6006

Merged
flyrain merged 3 commits into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-gsi1-by-id
Nov 11, 2026
Merged

flyrain merged 3 commits into
apache:mainfrom
ZephyrYWZhou:dynamodb-atomic-gsi1-by-id

Conversation

@ZephyrYWZhou

Copy link
Copy Markdown
Contributor

Summary

The DynamoDB (atomic) backend keeps a children index, gsi1, for listing a parent's children. Its sort key was the entity name. Polaris pages listings by entity id instead: JdbcBasePersistenceImpl orders children by id, and the shared EntityIdToken resumes after the last id returned. With a name sort key, gsi1 could not resume from an EntityIdToken without reading every child and sorting in memory.

This PR makes gsi1sk the entity id, zero-padded to 19 digits so that string order matches numeric order. The upcoming listing PR can then page with one Query (gsi1sk > pad(lastId), limited to the page size) and reuse EntityIdToken as is.

Nothing reads gsi1 yet and the backend cannot be selected, so no stored data is affected.

Changes

  • DynamoDbLayout: add childrenGsiSk(long id) for the padded id. Rename byNameGsiPk to childrenGsiPk, and update the comments that called gsi1 a by-name index. By-name lookups go through the name sentinel, not gsi1.
  • EntityItem: write childrenGsiSk(id) into gsi1sk instead of the name.
  • DynamoDbSchema: comment and helper-name updates only. gsi1sk stays a string attribute, so the table and index definitions do not change.

Testing

  • DynamoDbLayoutTest: padding (0, 42, Long.MAX_VALUE) and numeric sort order.
  • EntityItemTest: expects the padded id in gsi1sk.
  • DynamoDbSchemaIT: siblings whose name order and id order differ come back from gsi1 in id order. Ids 30 and 200 also check the padding.
  • ./gradlew :polaris-persistence-dynamodb-atomic:spotlessCheck checkstyleMain checkstyleTest checkstyleIntTest compileIntTestJava test javadoc passes locally. DynamoDbSchemaIT needs Docker and runs in CI.

Checklist

@github-project-automation github-project-automation Bot moved this to PRs In Progress in Basic Kanban Board Nov 6, 2026
@ZephyrYWZhou
ZephyrYWZhou marked this pull request as ready for review November 7, 2026 23:17
flyrain
flyrain previously approved these changes Nov 8, 2026

@flyrain flyrain 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.

LGTM. Thanks for the fix, @ZephyrYWZhou !

@github-project-automation github-project-automation Bot moved this from PRs In Progress to Ready to merge in Basic Kanban Board Nov 8, 2026

@vigneshio vigneshio 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.

Thanks @ZephyrYWZhou. Left a note on the padding format.

* relational-jdbc} does). Entity ids are non-negative.
*/
public static String childrenGsiSk(long id) {
return String.format("%019d", id);

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.

Since this is a DynamoDB key, String.format without Locale.ROOT picks up the JVM's default digits. On a non-Latin locale it would store a different gsi1sk than the one the listing query looks for. Let's use Locale.ROOT, same as the other padded ids in the tree.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ahhh Good catch, thanks @vigneshio. Switched to String.format(Locale.ROOT, "%019d", id) - same as the padded version numbers in LocalIcebergCatalog.

@orca-review-bot orca-review-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good. This is a small, focused change that lines gsi1 up with id-based paging so the listing PR can reuse EntityIdToken. The rename is complete. The table and GSI definitions don't change because gsi1sk stays a string. Realm isolation is unchanged since the realm stays in gsi1pk. The IT picks ids 30/200 so that name order, id order, and unpadded order all differ, which is a nice touch. One optional nit inline about negative ids. No CHANGELOG needed since the backend isn't selectable yet.

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

* numeric order. This lists children in id order, the order Polaris pages by (as {@code
* relational-jdbc} does). Entity ids are non-negative.
*/
public static String childrenGsiSk(long id) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A negative id would sort wrong here. %019d turns -42 into "-000000000000000042", which lands before every positive id, so gsi1sk > pad(lastId) paging would quietly skip or repeat rows. That can't happen today because JDBC's IdGenerator masks ids to non-negative. But this module hasn't settled its id source yet, and the javadoc's "ids are non-negative" isn't enforced. Can we throw IllegalArgumentException for id < 0 so a bad id fails loudly instead?

🤖 AI-generated review comment — agents make mistakes, so please verify before acting on it.

@ZephyrYWZhou

Copy link
Copy Markdown
Contributor Author

Thanks for the review and approval @flyrain @vigneshio! Could @flyrain help with the merge of this fixing PR please? Thanks!

@flyrain
flyrain merged commit 02ba4b6 into apache:main Nov 11, 2026
26 checks passed
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in Basic Kanban Board Nov 11, 2026
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.

DynamoDB (atomic): sort the children index (gsi1) by entity id to match id-based paging

4 participants