[NO REVIEW]Cosmos: append item id to hierarchical partition key ending in "/id"#49709
Open
xinlian12 wants to merge 1 commit into
Open
[NO REVIEW]Cosmos: append item id to hierarchical partition key ending in "/id"#49709xinlian12 wants to merge 1 commit into
xinlian12 wants to merge 1 commit into
Conversation
When a container's (hierarchical) partition key definition ends with "/id", the SDK now automatically appends the item's id to the partition key so callers can address an item using only the prefix of the partition key instead of repeating the id. Ported from .NET PR Azure/azure-cosmos-dotnet-v3#5600. Operations changed (id auto-appended): - Point operations: create/read/replace/upsert/delete/patch item - Bulk operations (id read from the item body for create/upsert) - readMany (by id + PartitionKey) Operations with limited support: - Transactional batch (and batch-in-bulk) throw when only the prefix of the partition key is provided, since a batch targets a single partition key and cannot carry a per-item id. Not affected: queries/read-feed, stored procedures, deleteAllItemsByPartitionKey, non-"/id"-terminated containers. Adds PartitionKeyHelper.isLastPartitionKeyPathId / ensureIdIsInPartitionKey(Internal) and wires them into RxDocumentClientImpl (point ops + batch + readMany) and BulkExecutorUtil. Includes unit tests (PartitionKeyHelperTest) and emulator integration tests (HierarchicalIdAsPartitionKeyTest). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds Cosmos DB Java SDK support for hierarchical partition keys whose last path is /id, by automatically appending the item id to the partition key for item-addressing operations. This mirrors the referenced .NET SDK behavior and reduces caller boilerplate when using prefix partition keys.
Changes:
- Added
PartitionKeyHelperutilities to detect/id-terminated hierarchical PK definitions and to augment PK values with the itemidwhen needed. - Wired the PK augmentation into point operations,
readMany, and bulk PK range resolution; enforced that transactional batch requires a fully-specified PK (includingid). - Added unit + emulator integration tests validating point ops, bulk,
readMany, and transactional batch behavior, and documented the feature in the changelog.
Show a summary per file
| File | Description |
|---|---|
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java | Ensures /id is appended for document point ops and readMany; enforces batch behavior. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/PartitionKeyHelper.java | Introduces helper methods to detect /id PK path and append item id to PK. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/TransactionalBulkExecutor.java | Updates batch PK range resolution call signature. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/BulkExecutorUtil.java | Completes /id-terminated PKs during bulk PK range id resolution (with lazy id retrieval). |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/batch/BulkExecutor.java | Passes effective serializer into bulk PK range id resolution. |
| sdk/cosmos/azure-cosmos/CHANGELOG.md | Documents the new /id hierarchical partition key support. |
| sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/implementation/PartitionKeyHelperTest.java | Adds unit tests for the new helper logic. |
| sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/HierarchicalIdAsPartitionKeyTest.java | Adds emulator tests covering point ops, readMany, bulk, and transactional batch behavior. |
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 2
- Review effort level: Low
Comment on lines
+185
to
+189
| if (PartitionKeyHelper.isLastPartitionKeyPathId(definition)) { | ||
| String itemId = itemIdSupplier == null ? null : itemIdSupplier.get(); | ||
| partitionKeyInternal = PartitionKeyHelper.ensureIdIsInPartitionKeyInternal( | ||
| definition, partitionKeyInternal, itemId); | ||
| } |
Comment on lines
830
to
835
| return BulkExecutorUtil.resolvePartitionKeyRangeId( | ||
| docClientWrapper, | ||
| container, | ||
| cosmosBatchBulkOperation.getPartitionKeyValue(), | ||
| (Supplier<String>) null, | ||
| null); |
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.
Description
When a Cosmos container's (hierarchical) partition key definition ends with
/id, the SDK now automatically appends the item'sidto the partition key. This lets callers address an item using only the prefix of the partition key instead of repeating theidvalue (e.g.readItem(id, new PartitionKeyBuilder().add(pk).build(), ...)instead of...add(pk).add(id).build()).This ports .NET PR Azure/azure-cosmos-dotnet-v3#5600 to the Java SDK.
Operations changed (id auto-appended when last partition key path is
/id)createItem,readItem,replaceItem,upsertItem,deleteItem,patchItem. The id is read from the item body for writes, or from the request path for read/delete/patch.executeBulkOperations) — the id is read from the item body for create/upsert, from the explicit operation id otherwise.readMany(byid+PartitionKey) — each identity's partition key is completed with its id.Operations with limited support
executeCosmosBatch) and batch-in-bulk require the fully specified partition key (including theid). A prefix-only partition key throwsIllegalArgumentException("itemId needs to be specified…"), because a batch targets a single partition key and cannot carry a per-item id. This matches the .NET behavior (batch effectively operates on a single document).Operations intentionally not affected
ResourceType.StoredProcedure).deleteAllItemsByPartitionKey(ResourceType.PartitionKey)./id, and explicitPartitionKey.NONE.Implementation
PartitionKeyHelper:isLastPartitionKeyPathId,ensureIdIsInPartitionKeyInternal,ensureIdIsInPartitionKey.RxDocumentClientImpl.addPartitionKeyInformation(point ops),addBatchHeaders(batch), andreadMany.BulkExecutorUtil(with the id lazily read from the item body via the effective item serializer, only for/id-terminated containers).How to verify
PartitionKeyHelperTest(13 cases, groupunit).HierarchicalIdAsPartitionKeyTest(groupemulator) covering point ops,readMany, bulk, and transactional batch (throw + success) with a prefix partition key on an["/pk","/id"]container.All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines