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

findFirst Derived Query Language Not Implemented #2859

Open
crisadoicinf opened this issue May 7, 2024 · 0 comments
Open

findFirst Derived Query Language Not Implemented #2859

crisadoicinf opened this issue May 7, 2024 · 0 comments
Labels
priority: p2 type: bug Something isn't working

Comments

@crisadoicinf
Copy link

Describe the bug
The FirestoreReactiveRepository interface doesn't properly implement the findFirst method like Derived Query Methods. Firestore supports limiting the queried results, so a limit is expected in the translated query sent to Firestore using findFirst or findTop syntax.

Here is a simplified example.

import com.google.cloud.spring.data.firestore.Document;
import com.google.cloud.firestore.annotation.DocumentId;
import com.google.cloud.spring.data.firestore.FirestoreReactiveRepository;

@Document
public class MyEntity{
    @DocumentId
    private String id;
    private String group;
    private String name;
    private Long time;
}

public interface MyEntityRepository extends FirestoreReactiveRepository<MyEntity> {

    Mono<MyEntity> findFirstByGroupAndNameOrderByTimeDesc(String group, String name);

}

The implementation of MyEntityRepository is provided by SimpleFirestoreReactiveRepository.java and executed by FirestoreTemplate.java. The following is the built query sent to the method com.google.cloud.spring.data.firestore.FirestoreTemplate:execute when calling findFirstByGroupAndNameOrderByTimeDesc("group1","name1")

where {
  composite_filter {
    op: AND
    filters {
      field_filter {
        field {
          field_path: "group"
        }
        op: EQUAL
        value {
          string_value: "group1"
        }
      }
    }
    filters {
      field_filter {
        field {
          field_path: "name"
        }
        op: EQUAL
        value {
          string_value: "name1"
        }
      }
    }
  }
}
order_by {
  field {
    field_path: "time"
  }
  direction: DESCENDING
}

Notice, there is no limit in the query. A workaround for this is explicitly pass a Pageable object to the query as follow;

public interface MyEntityRepository extends FirestoreReactiveRepository<MyEntity> {

    Mono<MyEntity> findByGroupAndNameOrderByTimeDesc(String group, String name, Pageable page);

}

This time, calling findByGroupAndNameOrderByTimeDesc("group1","name1", PageRequest.of(0, 1)) results in the following query which includes a limit:

where {
  composite_filter {
    op: AND
    filters {
      field_filter {
        field {
          field_path: "group"
        }
        op: EQUAL
        value {
          string_value: "group1"
        }
      }
    }
    filters {
      field_filter {
        field {
          field_path: "name"
        }
        op: EQUAL
        value {
          string_value: "name1"
        }
      }
    }
  }
}
order_by {
  field {
    field_path: "time"
  }
  direction: DESCENDING
}
limit {
  value: 1
}

Although there is a workaround for this issue, the significant problem lies in the fact that the SimpleFirestoreReactiveRepository does not throw any unsupported exception; instead, it executes the query while completely ignoring the limit part. This oversight could result in a large number of reads in Firestore and consequently lead to a substantial bill.

Here is an excerpt of the pom file.

<dependencyManagement>
    <dependencies>
        <dependency>
	    <groupId>com.google.cloud</groupId>
	    <artifactId>spring-cloud-gcp-dependencies</artifactId>
	    <version>5.1.2</version>
	    <type>pom</type>
	    <scope>import</scope>
	</dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-data-firestore</artifactId>
    </dependency>
</dependencies>
@mpeddada1 mpeddada1 added type: bug Something isn't working priority: p2 labels May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants