Skip to content

[NO REVIEW]Cosmos: append item id to hierarchical partition key ending in "/id"#49709

Open
xinlian12 wants to merge 1 commit into
Azure:mainfrom
xinlian12:xinlian12/hpk-id-as-partition-key
Open

[NO REVIEW]Cosmos: append item id to hierarchical partition key ending in "/id"#49709
xinlian12 wants to merge 1 commit into
Azure:mainfrom
xinlian12:xinlian12/hpk-id-as-partition-key

Conversation

@xinlian12

Copy link
Copy Markdown
Member

Description

When a Cosmos container's (hierarchical) partition key definition ends with /id, the SDK now automatically appends the item's id to the partition key. This lets callers address an item using only the prefix of the partition key instead of repeating the id value (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)

  • Point operationscreateItem, readItem, replaceItem, upsertItem, deleteItem, patchItem. The id is read from the item body for writes, or from the request path for read/delete/patch.
  • Bulk operations (executeBulkOperations) — the id is read from the item body for create/upsert, from the explicit operation id otherwise.
  • readMany (by id + PartitionKey) — each identity's partition key is completed with its id.

Operations with limited support

  • Transactional batch (executeCosmosBatch) and batch-in-bulk require the fully specified partition key (including the id). A prefix-only partition key throws IllegalArgumentException("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

  • Queries / read-feed / change feed (these use the hierarchical partition key prefix — the intended prefix functionality).
  • Stored procedures (ResourceType.StoredProcedure).
  • deleteAllItemsByPartitionKey (ResourceType.PartitionKey).
  • Containers whose partition key does not end with /id, and explicit PartitionKey.NONE.

Implementation

  • New helpers in PartitionKeyHelper: isLastPartitionKeyPathId, ensureIdIsInPartitionKeyInternal, ensureIdIsInPartitionKey.
  • Wired into RxDocumentClientImpl.addPartitionKeyInformation (point ops), addBatchHeaders (batch), and readMany.
  • Wired into BulkExecutorUtil (with the id lazily read from the item body via the effective item serializer, only for /id-terminated containers).

How to verify

  • Unit tests: PartitionKeyHelperTest (13 cases, group unit).
  • Emulator integration tests: HierarchicalIdAsPartitionKeyTest (group emulator) covering point ops, readMany, bulk, and transactional batch (throw + success) with a prefix partition key on an ["/pk","/id"] container.
  • Validated end-to-end against a live Cosmos account during development.

All SDK Contribution checklist:

  • The pull request does not introduce [breaking changes]
  • CHANGELOG is updated for new features, bug fixes or other significant changes.
  • I have read the contribution guidelines.

General Guidelines and Best Practices

  • Title of the pull request is clear and informative.
  • There are a small number of commits, each of which have an informative message.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

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>
Copilot AI review requested due to automatic review settings July 2, 2026 17:49
@xinlian12 xinlian12 requested review from a team and kirankumarkolli as code owners July 2, 2026 17:49
@github-actions github-actions Bot added the Cosmos label Jul 2, 2026
@xinlian12 xinlian12 changed the title Cosmos: append item id to hierarchical partition key ending in "/id" [NO REVIEW]Cosmos: append item id to hierarchical partition key ending in "/id" Jul 2, 2026

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.

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 PartitionKeyHelper utilities to detect /id-terminated hierarchical PK definitions and to augment PK values with the item id when needed.
  • Wired the PK augmentation into point operations, readMany, and bulk PK range resolution; enforced that transactional batch requires a fully-specified PK (including id).
  • 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants