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

API: Do count distinct since left join duplicates results #19208

Merged
merged 7 commits into from
Jul 24, 2023

Conversation

joselcvarela
Copy link
Member

Motivation

In some scenarios, filter_count returns the result of the amount of relationships instead of just the amount records on current collection.
This happens because the results are duplicated since a left join is being used.

Solution

In order to solve this, we can just do a countDistinct using the primary key of the current collection.
That will remove the duplicated results and filter_count will have proper value

Fixes #18608

@changeset-bot
Copy link

changeset-bot bot commented Jul 19, 2023

🦋 Changeset detected

Latest commit: fbaf31e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@directus/api Patch
directus Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@paescuj
Copy link
Member

paescuj commented Jul 20, 2023

LGTM so far, but would be great to have this covered in a test.

@paescuj paescuj requested a review from licitdev July 20, 2023 10:26
Copy link
Member

@paescuj paescuj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work, thank you both! ❤️

@paescuj paescuj merged commit 3a54f0d into directus:main Jul 24, 2023
7 checks passed
@github-actions github-actions bot added this to the Next Release milestone Jul 24, 2023
sethkaufee pushed a commit to sethkaufee/directus that referenced this pull request Jul 24, 2023
…#19208)

* Do count distinct since `left join` duplicates results

* Add changeset

* Correctly access count property

* Update .changeset/angry-suits-compete.md

* Optimize for simple filters

* Add tests

* Workaround broken 'as' for countDistinct in SQLite3

---------

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
Co-authored-by: ian <licitdev@gmail.com>
br-rafaelbarros pushed a commit to personal-forks/directus-source that referenced this pull request Nov 7, 2023
…#19208)

* Do count distinct since `left join` duplicates results

* Add changeset

* Correctly access count property

* Update .changeset/angry-suits-compete.md

* Optimize for simple filters

* Add tests

* Workaround broken 'as' for countDistinct in SQLite3

---------

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
Co-authored-by: ian <licitdev@gmail.com>
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

meta=filter_count returns incorrect value when filtering on related.
3 participants