[Query] Fix: SELECT VALUE ... GROUP BY throws ClassCastException#48507
Open
aayush3011 wants to merge 2 commits intoAzure:mainfrom
Open
[Query] Fix: SELECT VALUE ... GROUP BY throws ClassCastException#48507aayush3011 wants to merge 2 commits intoAzure:mainfrom
SELECT VALUE ... GROUP BY throws ClassCastException#48507aayush3011 wants to merge 2 commits intoAzure:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes Cosmos DB Java SDK query pipeline handling for SELECT VALUE ... GROUP BY queries that currently crash (and/or return incorrectly shaped results), aligning behavior with Data Explorer and the .NET SDK.
Changes:
- Adjusts GROUP BY projection payload handling to support non-
ObjectNodepayload shapes (includingSELECT VALUE). - Propagates aggregate operators from the query plan into the GROUP BY aggregation pipeline.
- Adds new e2e tests covering
SELECT VALUE+GROUP BYforCOUNT,SUM, and scalar field projection.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/GroupByDocumentQueryExecutionContext.java | Updates GROUP BY projection payload extraction to avoid invalid casts and support SELECT VALUE payload shapes. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/GroupingTable.java | Threads aggregate operators into the grouping table so aggregation is performed correctly. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/PipelinedDocumentQueryExecutionContext.java | Passes queryInfo.getAggregates() into the GROUP BY execution context. |
| sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/SingleGroupAggregator.java | Widens addValues signature to accept non-Document payloads (needed for SELECT VALUE). |
| sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/GroupByQueryTests.java | Adds e2e coverage for SELECT VALUE + GROUP BY scenarios. |
...rc/main/java/com/azure/cosmos/implementation/query/GroupByDocumentQueryExecutionContext.java
Outdated
Show resolved
Hide resolved
.../azure-cosmos/src/main/java/com/azure/cosmos/implementation/query/SingleGroupAggregator.java
Show resolved
Hide resolved
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/GroupByQueryTests.java
Show resolved
Hide resolved
sdk/cosmos/azure-cosmos-tests/src/test/java/com/azure/cosmos/rx/GroupByQueryTests.java
Show resolved
Hide resolved
- getPayload(): Always unwrap single-element arrays before type-checking, handling scalar SELECT VALUE payloads (TextNode/NumericNode) correctly - SelectListAggregateValues.addValues(): Add instanceof guard with descriptive IllegalArgumentException instead of raw ClassCastException - COUNT test: Compare per-city count multisets instead of just totals - SUM test: Compare per-city sum multisets instead of just totals Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Any
SELECT VALUE <expression> ... GROUP BYquery crashes withClassCastException: ArrayNode cannot be cast to ObjectNode. This has been broken since GROUP BY was added in v4.1.0. The same queries work in Data Explorer and the .NET SDK.Root Cause
Three issues in the GROUP BY query pipeline:
RewrittenGroupByProjection.getPayload()unconditionally cast the payload toObjectNode, butSELECT VALUEqueries produceArrayNodepayloads from the backend.GroupingTablehardcoded an empty aggregate operators list instead of passing the actual operators from the query plan. This causedSELECT VALUEaggregate queries to skip proper aggregation and return raw internal structures like{"item": 5}instead of the scalar5.SingleGroupAggregator.addValues()only acceptedDocument, butSELECT VALUEpayloads can be non-ObjectNodetypes.Changes
GroupByDocumentQueryExecutionContext.javagetPayload()handles non-ObjectNode payloads and unwraps single-element arrays. Addedaggregatesparameter tocreateAsync().GroupingTable.javaPipelinedDocumentQueryExecutionContext.javaqueryInfo.getAggregates()tocreateAsync().SingleGroupAggregator.javaaddValues()parameter widened fromDocumenttoObject.GroupByQueryTests.javaSELECT VALUE COUNT,SELECT VALUE SUM,SELECT VALUE <field>with GROUP BY.Testing
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines