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 Annotated Query select * with no where but with a sort. (#40083) #40165

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/spring/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 bug with annotated queries that have no where clause but do have a sort - See [PR 40165](https://github.com/Azure/azure-sdk-for-java/pull/40165).

#### Other Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public SqlQuerySpec generateSortedQuery(SqlQuerySpec querySpec, Sort sort) {
matcher.find();
int beginIndex = matcher.start(0) + 6;
String tableName = querySpec.getQueryText().substring(beginIndex);
tableName = tableName.substring(0, tableName.indexOf(" "));
if (tableName.indexOf(" ") != -1) {
tableName = tableName.substring(0, tableName.indexOf(" "));
}

String querySort = AbstractQueryGenerator.generateQuerySort(sort, tableName);
String queryText = querySpec.getQueryText() + " " + querySort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,24 @@ public void testAnnotatedQueryWithArrayContainsAndSort() {
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);
}

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

final List<Address> resultsAsc = addressRepository.annotatedFindAllWithSort(Sort.by(Sort.Direction.ASC, "postalCode"));
assertAddressOrder(resultsAsc, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION2, Address.TEST_ADDRESS1_PARTITION1);
}

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

final List<Address> resultsAsc = addressRepository.annotatedFindAllWithSort(Sort.by(Sort.Direction.DESC, "postalCode"));
assertAddressOrder(resultsAsc, Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS1_PARTITION2, Address.TEST_ADDRESS2_PARTITION1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ public void testAnnotatedQueryWithNewLinesInQueryString2() {
.verifyComplete();
}

@Test
public void testAnnotatedFindAllWithSortAsc() {
final Flux<Role> roleAscFlux = repository.annotatedFindAllWithSort(Sort.by(Sort.Direction.ASC, "id"));
StepVerifier.create(roleAscFlux)
.expectNext(TEST_ROLE_1)
.expectNext(TEST_ROLE_2)
.expectNext(TEST_ROLE_3)
.expectNext(TEST_ROLE_4)
.verifyComplete();
}

@Test
public void testAnnotatedFindAllWithSortDesc() {
final Flux<Role> roleAscFlux = repository.annotatedFindAllWithSort(Sort.by(Sort.Direction.DESC, "id"));
StepVerifier.create(roleAscFlux)
.expectNext(TEST_ROLE_4)
.expectNext(TEST_ROLE_3)
.expectNext(TEST_ROLE_2)
.expectNext(TEST_ROLE_1)
.verifyComplete();
}

@Test
public void testAnnotatedQueryWithMultipleLevels() {
List<String> levels = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,7 @@ public interface AddressRepository extends CosmosRepository<Address, String> {

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

@Query(value = "SELECT * FROM C")
List<Address> annotatedFindAllWithSort(Sort sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public interface ReactiveRoleRepository extends ReactiveCosmosRepository<Role, S

@Query(value = "select * \n from c \n where c.name = @name \n")
Flux<Role> annotatedFindRoleByNameWithSort2(@Param("name") String name, Sort sort);

@Query(value = "select * from c")
Flux<Role> annotatedFindAllWithSort(Sort sort);
}
Loading