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

Return duplicate data when querying via Query Methods. #2799

Closed
junghoon-vans opened this issue Dec 11, 2023 · 1 comment
Closed

Return duplicate data when querying via Query Methods. #2799

junghoon-vans opened this issue Dec 11, 2023 · 1 comment
Assignees
Labels
type: bug A general bug

Comments

@junghoon-vans
Copy link
Contributor

junghoon-vans commented Dec 11, 2023

In the current implementation, there is an issue with returning the same data twice when querying with Query Methods.

Problem

For example, here's the test code I created.
The expected result seems to be that eddard and jon should be returned.

@Test
void shouldReturnEntitiesWithoutDuplicates() {
        
    Person eddard = new Person("eddard", "stark");
    eddard.setAlive(true);
   
    Person robb = new Person("robb", "stark");
    robb.setAlive(false);
    
    Person jon = new Person("jon", "snow");
    jon.setAlive(true);
    
    repo.saveAll(Arrays.asList(eddard, robb, jon));
    
    List<Person> result = repo.findByFirstnameAndLastnameOrAliveIsTrue("eddard", "stark");
    assertThat(result).hasSize(2);
    assertThat(result).contains(eddard, jon);
}

However, the actual result is that two identical eddards and one jon are returned.

Actual Result

Cause of the problem

The fix is simple. Just make sure to remove duplicate values before running the query.

private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows, String keyspace, Class<T> type) {

    // ...
    
    RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {

	List<byte[]> allKeys = new ArrayList<>();
	if (!criteria.getSismember().isEmpty()) {
		allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember())));
	}

	if (!criteria.getOrSismember().isEmpty()) {
		allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember())));
	}

Looking at the code in RedisQueryEngine, there is no logic to remove duplicate data that exists in sismember and orSismember. so if the same entity fits multiple conditions, it may be returned as a duplicate.

Additional Context

Note that if we modify the logic, we'll need to do the same for RedisQueryEngine#count.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 11, 2023
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Dec 11, 2023
@mp911de mp911de self-assigned this Dec 11, 2023
mp911de added a commit that referenced this issue Dec 11, 2023
We now avoid duplicate keys if a key is found in two indices.

Closes #2799
mp911de added a commit that referenced this issue Dec 11, 2023
We now avoid duplicate keys if a key is found in two indices.

Closes #2799
@mp911de mp911de added this to the 3.1.7 (2023.0.7) milestone Dec 11, 2023
@mp911de
Copy link
Member

mp911de commented Dec 11, 2023

That's fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants