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

Fix paginate count query when used with distinct #852

Merged
merged 2 commits into from
Sep 9, 2022

Conversation

ptrpiero
Copy link
Contributor

@ptrpiero ptrpiero commented Jun 8, 2022

fix(src/database/querybuilder/database.ts): move the query within a subquery in paginate total count

The main query has been mooved inside a subquery to keep its original syntax so that paginate() can
always count exact number of records fetch within it

fix #844

Proposed changes

The main problem with paginate is that is always counting total records with COUNT(*) followed by the original query. This is not a good way to do it, as for example the query could count records using DISINCT some_field. In this PR the count query is generalized since the actual user query, is put inside a subquery saving its original syntax, and counting total records with COUNT(*) on top of it.

Types of changes

What types of changes does your code introduce?

Put an x in the boxes that apply

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

  • I have read the CONTRIBUTING doc
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

…ubquery in paginate total count

The main query has been mooved inside a subquery to keep its original syntax so that paginate() can
always count exact number of records fetch within it

fix adonisjs#844
@stale
Copy link

stale bot commented Aug 11, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Abandoned Dropped and not into consideration label Aug 11, 2022
@ptrpiero
Copy link
Contributor Author

Hey @thetutlage any news on this pull request? Why is it in state abandoned? It is actually solving a real issue, I would expect it is merged into develop..

@RomainLanz
Copy link
Member

RomainLanz commented Aug 30, 2022

Hey @ptrpiero! 👋🏻

It was moved to Abandoned by stale since there was no activity in this PR for the last 30 days.

Don't worry about it. It is a good way for us to be pinged for staling issues/pr. 😄
I am sure Harminder will get back to you when he has the time.

@RomainLanz RomainLanz requested a review from thetutlage August 30, 2022 09:13
@RomainLanz RomainLanz added Status: Review Needed Review from the core team is required before moving forward and removed Status: Abandoned Dropped and not into consideration labels Aug 30, 2022
@thetutlage
Copy link
Member

Sorry for the late reply. The PR somehow got skipped.

The sub-query approach looks like to me. I don't see this having any specific issues. Also, the performance is identical with and without subquery (atleast on PostgreSQL).

Can you please add a test that uses distinct on a column that has duplicate values?

Right now, the test you have added has distinct(users.id), so with or without distinct the number of returns rows will be the same. How about adding duplicate names in the users table and then paginating on the distinct one's?

@thetutlage thetutlage merged commit a315cb1 into adonisjs:develop Sep 9, 2022
@thetutlage
Copy link
Member

Thanks @ptrpiero for taking out the time to create this PR. Highly appreciated 👍

@ptrpiero ptrpiero deleted the fix/issue-844 branch September 12, 2022 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Review Needed Review from the core team is required before moving forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Paginate is not counting total results correctly when used with distinct
4 participants