-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Bad Request Error on cross-partition query with an ORDER BY clause #41434
Copy link
Copy link
Open
Labels
ClientThis issue points to a problem in the data-plane of the library.This issue points to a problem in the data-plane of the library.CosmosService AttentionWorkflow: This issue is responsible by Azure service team.Workflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
Metadata
Metadata
Assignees
Labels
ClientThis issue points to a problem in the data-plane of the library.This issue points to a problem in the data-plane of the library.CosmosService AttentionWorkflow: This issue is responsible by Azure service team.Workflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.Issues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as thatThe issue doesn't require a change to the product in order to be resolved. Most issues start as that
Type
Projects
Status
No status
azure-cosmos4.9.03.11.3Describe the bug
When executing a cross-partition query with an
ORDER BYclause, the initial request for the first page of results succeeds and returns a continuation token. However, when a subsequent request is made with the exact same query and the provided token to fetch the next page, the Cosmos DB service returns an HTTP400 Bad Requestwith an "Invalid Continuation Token" error. This makes paginated, sorted queries across multiple partitions unusable.To Reproduce
Steps to reproduce the behavior:
/user_id).ORDER BYclause but does not filter on the partition key.SELECT * FROM c WHERE c.conversationStartTime >= @start AND c.conversationStartTime <= @end ORDER BY c.conversationStartTime DESCcontinuation_token=None) succeeds and returns a page of items and an opaque, non-JSON continuation token string.Expected behavior
The second request should succeed and return the next page of sorted results, along with a new continuation token (or
Noneif it's the last page).Screenshots
If applicable, add screenshots to help explain your problem.
error_order_by_logs.txt
Additional context
This issue has been thoroughly debugged, and the following has been confirmed:
Environment Stack:
4.0.62804.834.3.22875Token Integrity: Through extensive logging down to the raw HTTP request, we have confirmed that the full, unmodified continuation token string is being correctly placed in the
x-ms-continuationheader. The token is not being truncated or corrupted by the client application, the SDK, or any networking components.Token Format Analysis: We have verified that a query without the
ORDER BYclause works perfectly with pagination. This working query uses a structured JSON continuation token. The failingORDER BYquery uses a different, opaque string token format. Our application handles both formats correctly by Base64-encoding them, proving the client-side logic is sound.Critical Workaround Discovery: A successful workaround was found. If the query is modified to be a single-partition query by adding a
WHEREclause that filters on the partition key (e.g.,... AND c.user_id = @user_id), the pagination withORDER BYworks perfectly.This analysis isolates the bug specifically to the server-side processing of continuation tokens for cross-partition
ORDER BYqueries.