spring-attic / spring-cloud-gcp

Integration for Google Cloud Platform APIs with Spring
Apache License 2.0
704 stars 694 forks source link

IndexOutOfBoundsException thrown when Spanner returns NULL as a query result #2577

Closed TanyaOhotnik closed 3 years ago

TanyaOhotnik commented 3 years ago

Here is my repository, example simplified to show the issue:

import org.springframework.cloud.gcp.data.spanner.repository.query.Query;
...
@Repository
public interface CarRepository extends SpannerRepository<Car, UUID> {

  @Query("SELECT MODEL FROM CAR WHERE CAR_ID = @carId")
  Optional<String> findModelByCarId(UUID carId);
}

When I am searching by id which does not exist, I am receiving an exception:

java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
    at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
    at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
    at java.base/java.util.Objects.checkIndex(Objects.java:372)
    at java.base/java.util.ArrayList.get(ArrayList.java:459)
    at org.springframework.cloud.gcp.data.spanner.repository.query.AbstractSpannerQuery.convertToSimpleReturnType(AbstractSpannerQuery.java:83)
    at org.springframework.cloud.gcp.data.spanner.repository.query.AbstractSpannerQuery.execute(AbstractSpannerQuery.java:67)
    at org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQuery.execute(SqlSpannerQuery.java:69)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor$QueryMethodInvoker.invoke(QueryExecutorMethodInterceptor.java:195)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:130)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    at com.sun.proxy.$Proxy102.findModelByCarId(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)

At the same time, the query works as expected when the entity available in DB, and the result is not null. I expect that issue can be related to conversion to a struct in org.springframework.cloud.gcp.data.spanner.repository.query.SqlSpannerQuery#executeReadSql.

Lib: Hoxton.SR8, specifically org.springframework.cloud:spring-cloud-gcp-starter-data-spanner:1.2.5.RELEASE

dmitry-s commented 3 years ago

@TanyaOhotnik thanks for reporting, I will look into this.