Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing bug with ARRAY_CONTAINS in annotated queries. #31179

Merged
merged 10 commits into from
Sep 28, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void testAnnotatedQueryWithPageableSort() {
}

@Test
public void testAnnotatedQueryWithMultipleCities() {
public void testAnnotatedQueryWithMultipleCitiesAndSort() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2);
addressRepository.saveAll(addresses);

Expand All @@ -280,4 +280,39 @@ public void testAnnotatedQueryWithMultipleCities() {
final List<Address> resultsAsc2 = addressRepository.annotatedFindByCityIn(cities2, Sort.by(Sort.Direction.ASC, "postalCode"));
assertAddressOrder(resultsAsc2, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2, Address.TEST_ADDRESS1_PARTITION1);
}

trande4884 marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testAnnotatedQueryWithArrayContains() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2);
addressRepository.saveAll(addresses);

List<String> cities = new ArrayList<>();
cities.add(TestConstants.CITY);
final List<Address> resultsAsc = addressRepository.annotatedFindByCities(cities);
assertAddressOrder(resultsAsc, Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);

List<String> cities2 = new ArrayList<>();
cities2.add(TestConstants.CITY);
cities2.add(TestConstants.CITY_0);
final List<Address> resultsAsc2 = addressRepository.annotatedFindByCities(cities2);
assertAddressOrder(resultsAsc2, Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2);

}

@Test
public void testAnnotatedQueryWithArrayContainsAndSort() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2);
addressRepository.saveAll(addresses);

List<String> cities = new ArrayList<>();
cities.add(TestConstants.CITY);
final List<Address> resultsAsc = addressRepository.annotatedFindByCitiesWithSort(cities, Sort.by(Sort.Direction.ASC, "postalCode"));
assertAddressOrder(resultsAsc, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION1);

List<String> cities2 = new ArrayList<>();
cities2.add(TestConstants.CITY);
cities2.add(TestConstants.CITY_0);
final List<Address> resultsAsc2 = addressRepository.annotatedFindByCitiesWithSort(cities2, Sort.by(Sort.Direction.ASC, "postalCode"));
assertAddressOrder(resultsAsc2, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2, Address.TEST_ADDRESS1_PARTITION1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@ public interface AddressRepository extends CosmosRepository<Address, String> {

@Query(value = "select * from a where a.city IN (@cities)")
List<Address> annotatedFindByCityIn(@Param("cities") List<String> cities, Sort sort);

@Query(value = "SELECT * FROM a WHERE ARRAY_CONTAINS(@cities, a.city) ")
List<Address> annotatedFindByCities(@Param("cities") List<String> cities);

@Query(value = "SELECT * FROM a WHERE ARRAY_CONTAINS(@cities, a.city) ")
List<Address> annotatedFindByCitiesWithSort(@Param("cities") List<String> cities, Sort sort);
}
1 change: 1 addition & 0 deletions sdk/cosmos/azure-spring-data-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Breaking Changes

#### Bugs Fixed
* Fixing ARRAY_CONTAINS annotated query bug introduced by fixing to IN annotated queries. - See [PR 31179](https://github.com/Azure/azure-sdk-for-java/pull/31179)

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,29 @@ public Object execute(final Object[] parameters) {
String expandedQuery = query;
List<SqlParameter> sqlParameters = new ArrayList<>();
for (int paramIndex = 0; paramIndex < parameters.length; paramIndex++) {
Parameter queryParam = getQueryMethod().getParameters().getParameter(paramIndex);
if (parameters[paramIndex] instanceof Collection) {
List<String> expandParam = ((Collection<?>) parameters[paramIndex]).stream()
.map(Object::toString).collect(Collectors.toList());
List<String> expandedParamKeys = new ArrayList<>();
for (int arrayIndex = 0; arrayIndex < expandParam.size(); arrayIndex++) {
String paramName = "@" + queryParam.getName().orElse("") + arrayIndex;
expandedParamKeys.add(paramName);
sqlParameters.add(new SqlParameter(paramName, toCosmosDbValue(expandParam.get(arrayIndex))));
}
expandedQuery = expandedQuery.replaceAll("@" + queryParam.getName().orElse(""), String.join(",", expandedParamKeys));
} else {
if (!Pageable.class.isAssignableFrom(queryParam.getType())
&& !Sort.class.isAssignableFrom(queryParam.getType())) {
sqlParameters.add(new SqlParameter("@" + queryParam.getName().orElse(""), toCosmosDbValue(parameters[paramIndex])));
Parameter queryParam = getQueryMethod().getParameters().getParameter(paramIndex);
if (!queryParam.getName().isEmpty()) {
String inParamCheck = "ARRAY_CONTAINS(@" + queryParam.getName().get();
if (parameters[paramIndex] instanceof Collection
&& expandedQuery.toString().toUpperCase().replaceAll("\\s", "")
trande4884 marked this conversation as resolved.
Show resolved Hide resolved
.indexOf(inParamCheck.toUpperCase()) == -1) {
List<String> expandParam = ((Collection<?>) parameters[paramIndex]).stream()
.map(Object::toString).collect(Collectors.toList());
List<String> expandedParamKeys = new ArrayList<>();
for (int arrayIndex = 0; arrayIndex < expandParam.size(); arrayIndex++) {
String paramName = "@" + queryParam.getName().orElse("") + arrayIndex;
expandedParamKeys.add(paramName);
sqlParameters.add(new SqlParameter(paramName, toCosmosDbValue(expandParam.get(arrayIndex))));
}
expandedQuery = expandedQuery.replaceAll("@" + queryParam.getName().orElse(""), String.join(",", expandedParamKeys));
} else {
if (!Pageable.class.isAssignableFrom(queryParam.getType())
&& !Sort.class.isAssignableFrom(queryParam.getType())) {
sqlParameters.add(new SqlParameter("@" + queryParam.getName().orElse(""), toCosmosDbValue(parameters[paramIndex])));
}
}
}
}
}

SqlQuerySpec querySpec = new SqlQuerySpec(expandedQuery, sqlParameters);
if (isPageQuery()) {
Expand Down