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

[FEATURE REQ] spring-data-cosmos paging without incurring cost of count query for custom query using paginationQuery method. #23484

Closed
2 tasks
anath-onetrust opened this issue Aug 11, 2021 · 14 comments
Assignees
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@anath-onetrust
Copy link
Contributor

anath-onetrust commented Aug 11, 2021

Is your feature request related to a problem? Please describe.
I want to page through a large result set using continuation tokens and the azure-spring-data-cosmos library method which will accept my query with return argument. However, CosmosTemplate repository methods(paginationQuery) always issue a count query to populate the total elements in the page response, and when the query is complex and the result set is large, the count query can be very expensive.

Describe the solution you'd like
I would like to be able to use CosmosPageRequest to fetch a page of data with a continuation token without issuing a count query to populate the total elements. There is a private slice method in CosmosTemplate which is called from paginationQuery method after executing count query , would like a wrapper public method of that so that it can call directly to avoid count query .Existing private method private Slice sliceQuery(SqlQuerySpec querySpec,Pageable pageable, Sort sort,Class returnType, String containerName)
Describe alternatives you've considered
Considered extending CosmosTemplate, but too many fields and methods are defined with private access modifiers.

Additional context
Add any other context or screenshots about the feature request here.

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Description Added
  • Expected solution specified
@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Aug 11, 2021
@joshfree joshfree added azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. labels Aug 11, 2021
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Aug 11, 2021
@joshfree
Copy link
Member

@backwind1233 @yiliuTo could you please take a look?

@backwind1233
Copy link
Contributor

Hi, @kushagraThapar could you please take a look?

@kushagraThapar
Copy link
Member

@anath-onetrust - I see that sliceQuery in CosmosTemplate is actually a public API. So that can be used if needed.
Also, this code snippet shows how to use slice query.

SliceQueriesUserRepository - https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-spring-data-cosmos/src/samples/java/com/azure/spring/data/cosmos/SliceQueriesUserRepository.java

@anath-onetrust
Copy link
Contributor Author

anath-onetrust commented Aug 18, 2021

@kushagraThapar Yes there is a public sliceQuery method in CosmosTemplate but that does not meet our requirement .We need to pass a return argument in sliceQuery which is not supported by public method but private method supports it .Sharing both method signature for reference .

private Slice sliceQuery(SqlQuerySpec querySpec,
Pageable pageable, Sort sort,
Class returnType, String containerName)

public Slice sliceQuery(CosmosQuery query, Class domainType, String containerName)

@kushagraThapar
Copy link
Member

@anath-onetrust - in the public API, domainType is the actual return type. So you should be able to use it.
See this for example -
final Slice<Person> slice = cosmosTemplate.sliceQuery(query, Person.class, containerName);
https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/core/CosmosTemplateIT.java#L597

@anath-onetrust
Copy link
Contributor Author

@kushagraThapar I saw the API but here we have an issue to use existing public API since the API only supports CosmosQuery in query parameter but our requirement is to pass SqlQuerySpec in API call which is not supported .Is there any way to achieve this.

public Slice sliceQuery(CosmosQuery query, Class domainType, String containerName)

@anath-onetrust
Copy link
Contributor Author

@kushagraThapar Could you please update us we are waiting for it .

@kushagraThapar
Copy link
Member

@anath-onetrust while we try to implement this in spring / or investigate the possibility of it, can you use cosmos java SDK client and query directly using java SDK instead of spring ?
For example, you can get the cosmosClient bean through applicationContext in spring data SDK, and then using that cosmosClient bean, you can directly query through cosmos java SDK using SqlQuerySpec

@anath-onetrust
Copy link
Contributor Author

@kushagraThapar what if we add a new public method in CosmosTemplate that internally call private sliceQuery method something like .

public Slice runSliceQuery(SqlQuerySpec querySpec, Pageable pageable, Class<?> domainType, Class returnType) {
final String containerName = getContainerName(domainType);
return sliceQuery(querySpec, pageable, pageable.getSort(), returnType, containerName);
}

I believe this is a straight forward change if you accept this I can raise a PR for same .
Actually here we are depending on requestContinuation for pagination so not sure if we use cosmosClient we will get it or not .

@kushagraThapar
Copy link
Member

@anath-onetrust - That seems like a possible solution for the time being. We can try it out and see how it works for you guys. Please raise a PR and I will look into it. Thank you!

anath-onetrust added a commit to anath-onetrust/azure-sdk-for-java that referenced this issue Oct 5, 2021
…nt query for custom query using paginationQuery method. Azure#23484
@anath-onetrust
Copy link
Contributor Author

@kushagraThapar I have raised a PR please let me know if I have missed anything .
#24540

@kushagraThapar
Copy link
Member

@anath-onetrust - Thanks for raising the PR, it looks good to me.
However my concern is that it might not support complete pagination APIs, like getTotalCount / getTotalPages / next / previous, etc.

Please read this github issue, where we are having the discussion on how to support pagination on spring data cosmos SDK - #24262

@johnmannix
Copy link
Contributor

johnmannix commented Oct 6, 2021

For issue #24262 it is worth pointing out that you can add a custom method to the repository returning Slice<T> and it will automatically return results that support pagination via continuation token without issuing a count query, and without having to call custom methods on the cosmosTemplate.

Even if you are planning to adjust the way the main pageable implementation works, it makes sense to include the runSliceQuery api for parity with runPageQuery, and that would be enough to unblock us.

kushagraThapar pushed a commit that referenced this issue Oct 7, 2021
…nt query for custom query using paginationQuery method. #23484 (#24540)
@kushagraThapar
Copy link
Member

@johnmannix @anath-onetrust - this has been done and will be released today. Thank you again for your contribution as always :)
Appreciate it!

@github-actions github-actions bot locked and limited conversation to collaborators Apr 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
azure-spring All azure-spring related issues azure-spring-cosmos Spring cosmos related issues. Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

5 participants