Skip to content

Commit 60ef4c8

Browse files
Fix query with sort and limit (Azure#18339)
* add failing test demonstrating issue with generated query * do not assume query ends in whitespace when appending offset
1 parent f0d2574 commit 60ef4c8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ public static void afterClassCleanup() {
134134
cosmosTemplate.deleteContainer(personInfo.getContainerName());
135135
}
136136

137+
private void insertPerson(Person person) {
138+
cosmosTemplate.insert(person,
139+
new PartitionKey(personInfo.getPartitionKeyFieldValue(person)));
140+
}
141+
137142
@Test(expected = CosmosAccessException.class)
138143
public void testInsertDuplicateId() {
139144
cosmosTemplate.insert(Person.class.getSimpleName(), TEST_PERSON,
@@ -419,6 +424,33 @@ public void testPaginationQuery() {
419424
assertThat(responseDiagnosticsTestUtils.getCosmosResponseStatistics().getRequestCharge()).isGreaterThan(0);
420425
}
421426

427+
@Test
428+
public void testFindWithSortAndLimit() {
429+
final Person testPerson4 = new Person("id_4", "fred", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
430+
final Person testPerson5 = new Person("id_5", "barney", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
431+
final Person testPerson6 = new Person("id_6", "george", NEW_LAST_NAME, HOBBIES, ADDRESSES, AGE);
432+
433+
insertPerson(testPerson4);
434+
insertPerson(testPerson5);
435+
insertPerson(testPerson6);
436+
437+
final Criteria criteria = Criteria.getInstance(CriteriaType.IS_EQUAL, "lastName",
438+
Collections.singletonList(NEW_LAST_NAME), Part.IgnoreCaseType.ALWAYS);
439+
final CosmosQuery query = new CosmosQuery(criteria);
440+
query.with(Sort.by(Sort.Direction.ASC, "firstName"));
441+
442+
final List<Person> result = TestUtils.toList(cosmosTemplate.find(query, Person.class, containerName));
443+
assertThat(result.size()).isEqualTo(3);
444+
assertThat(result.get(0).getFirstName()).isEqualTo("barney");
445+
assertThat(result.get(1).getFirstName()).isEqualTo("fred");
446+
assertThat(result.get(2).getFirstName()).isEqualTo("george");
447+
448+
query.setLimit(1);
449+
final List<Person> resultWithLimit = TestUtils.toList(cosmosTemplate.find(query, Person.class, containerName));
450+
assertThat(resultWithLimit.size()).isEqualTo(1);
451+
assertThat(resultWithLimit.get(0).getFirstName()).isEqualTo("barney");
452+
}
453+
422454
@Test
423455
public void testFindAllWithPageableAndSort() {
424456
cosmosTemplate.insert(TEST_PERSON_2,

sdk/cosmos/azure-spring-data-cosmos/src/main/java/com/azure/spring/data/cosmos/core/generator/AbstractQueryGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ protected SqlQuerySpec generateCosmosQuery(@NonNull CosmosQuery query,
259259

260260
if (query.getLimit() > 0) {
261261
queryString = new StringBuilder(queryString)
262-
.append("OFFSET 0 LIMIT ")
262+
.append(" OFFSET 0 LIMIT ")
263263
.append(query.getLimit()).toString();
264264
}
265265

0 commit comments

Comments
 (0)