lucid icon indicating copy to clipboard operation
lucid copied to clipboard

Fix paginate count query when used with distinct

Open ptrpiero opened this issue 3 years ago • 1 comments

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

  • [x] 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.

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

ptrpiero avatar Jun 08 '22 15:06 ptrpiero

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[bot] avatar Aug 11 '22 20:08 stale[bot]

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..

ptrpiero avatar Aug 29 '22 21:08 ptrpiero

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 avatar Aug 30 '22 09:08 RomainLanz

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 avatar Sep 01 '22 04:09 thetutlage

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

thetutlage avatar Sep 09 '22 17:09 thetutlage