Azure / Azure/azure-sdk-for-java

[BUG] Query with aggregates and GROUP BY works Azure Data Studio but generates an exception with spring-data-cosmos

Open
#43,912 6 comments 0 reactions 1 assignee Assigned to @jeet1995 View on GitHub
bug Client Cosmos cosmos-java-ecosystem-br-planning customer-reported needs-team-attention Service Attention
Dominant language
Java
Stars
2.6k
Forks
2.2k
Avg merge
2d 8h
Merged PRs (30d)
178

Description

**Describe the bug**
The following query works in Azure Data Studio:

```
SELECT c.scs, c.bbid, CONCAT(c.scs, "-", c.microservice) AS scsMicroservice, COUNT(1), SUM(c.totalCharacters) FROM c WHERE c.scs != null GROUP BY c.scs, c.bbid, CONCAT(c.scs, "-", c.microservice)
```

But when used with `@Query()` on a method in a `CosmosRepository` it generates an exception

***Exception or Stack Trace***
```
java.lang.IllegalArgumentException: Entity is null.
at org.springframework.util.Assert.notNull(Assert.java:172)
at com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter.readInternal(MappingCosmosConverter.java:94)
at com.azure.spring.data.cosmos.core.convert.MappingCosmosConverter.read(MappingCosmosConverter.java:79)
at com.azure.spring.data.cosmos.core.CosmosTemplate.toDomainObject(CosmosTemplate.java:1303)
at com.azure.spring.data.cosmos.core.CosmosTemplate.emitOnLoadEventAndConvertToDomainObject(CosmosTemplate.java:1299)
at com.azure.spring.data.cosmos.core.CosmosTemplate.lambda$runQuery$35(CosmosTemplate.java:1157)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.FluxFlatMap$FlatMapMain.drainLoop(FluxFlatMap.java:713)
at reactor.core.publisher.FluxFlatMap$FlatMapMain.drain(FluxFlatMap.java:589)
at reactor.core.publisher.FluxFlatMap$FlatMapInner.onSubscribe(FluxFlatMap.java:956)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:201)
at reactor.core.publisher.FluxIterable.subscribe(FluxIterable.java:83)
at reactor.core.publisher.Flux.subscribe(Flux.java:8642)
at reactor.core.publisher.FluxFlatMap$FlatMapMain.onNext(FluxFlatMap.java:427)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.runAsync(FluxPublishOn.java:440)
at reactor.core.publisher.FluxPublishOn$PublishOnSubscriber.run(FluxPublishOn.java:527)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:84)
at reactor.core.scheduler.WorkerTask.call(WorkerTask.java:37)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:840)
Suppressed: java.lang.Exception: #block terminated with an error
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:100)
at reactor.core.publisher.Mono.block(Mono.java:1742)
at com.azure.spring.data.cosmos.core.CosmosTemplate.runQuery(CosmosTemplate.java:1159)
at com.azure.spring.data.cosmos.repository.support.StringBasedCosmosQuery.execute(StringBasedCosmosQuery.java:108)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:170)
at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:158)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164)
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
```

**To Reproduce**
Steps to reproduce the behavior:

1. Create a `ComsosRepository` with an `@Query()` method.
2. Call the method.

***Code Snippet***
```
@Query(value =
"SELECT c.scs, c.bbid, CONCAT(c.scs, \"-\", c.microservice) AS scsMicroservice, COUNT(1), SUM(c.totalCharacters) FROM c WHERE c.scs != null GROUP BY c.scs, c.bbid, CONCAT(c.scs, \"-\", c.microservice)"
)
List getStatistics();
```

**Expected behavior**
The query does not cause an excpetion.

**Setup (please complete the following information):**
- Library/Libraries: com.azure:azure-spring-data-cosmos:5.14.0
- Java version: 17
- Frameworks: Spring Boot

**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [ X ] Bug Description Added
- [ X ] Repro Steps Added
- [ X ] Setup information Added

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.