Skip to content

[Query] Fix: SELECT VALUE ... GROUP BY throws ClassCastException#48507

Open
aayush3011 wants to merge 2 commits intoAzure:mainfrom
aayush3011:users/akataria/bugFix
Open

[Query] Fix: SELECT VALUE ... GROUP BY throws ClassCastException#48507
aayush3011 wants to merge 2 commits intoAzure:mainfrom
aayush3011:users/akataria/bugFix

Conversation

@aayush3011
Copy link
Member

@aayush3011 aayush3011 commented Mar 21, 2026

Description

Any SELECT VALUE <expression> ... GROUP BY query crashes with ClassCastException: 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:

  1. RewrittenGroupByProjection.getPayload() unconditionally cast the payload to ObjectNode, but SELECT VALUE queries produce ArrayNode payloads from the backend.

  2. GroupingTable hardcoded an empty aggregate operators list instead of passing the actual operators from the query plan. This caused SELECT VALUE aggregate queries to skip proper aggregation and return raw internal structures like {"item": 5} instead of the scalar 5.

  3. SingleGroupAggregator.addValues() only accepted Document, but SELECT VALUE payloads can be non-ObjectNode types.

Changes

File Change
GroupByDocumentQueryExecutionContext.java getPayload() handles non-ObjectNode payloads and unwraps single-element arrays. Added aggregates parameter to createAsync().
GroupingTable.java Accepts and passes aggregate operators from the query plan instead of empty list.
PipelinedDocumentQueryExecutionContext.java Passes queryInfo.getAggregates() to createAsync().
SingleGroupAggregator.java addValues() parameter widened from Document to Object.
GroupByQueryTests.java 3 new e2e tests: SELECT VALUE COUNT, SELECT VALUE SUM, SELECT VALUE <field> with GROUP BY.

Testing

  • 3 new e2e tests pass across Session, ConsistentPrefix, and Eventual consistency levels

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. This means that previously merged commits do not appear in the history of the PR. For more information on cleaning up the commits in your PR, see this page.

Testing Guidelines

  • Pull request includes test coverage for the included changes.

@aayush3011 aayush3011 requested review from a team and kirankumarkolli as code owners March 21, 2026 21:00
Copilot AI review requested due to automatic review settings March 21, 2026 21:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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-ObjectNode payload shapes (including SELECT VALUE).
  • Propagates aggregate operators from the query plan into the GROUP BY aggregation pipeline.
  • Adds new e2e tests covering SELECT VALUE + GROUP BY for COUNT, 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.

- 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>
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