AnyOfFilter shouldn't impose distinct
by default unless joining on another table
#61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context
Currently,
AnyOfFilter
, since it inherits fromMultipleChoiceFilter
, setsdistinct=True
by default on queries. This is for a good reason: when filtering on a field that may traverse relationships, rows may get duplicated as result.Issues
More often that not,
AnyOfFilter
is used to filter on the table own fields, in which casedistinct
is not necessary as rows won't duplicate by definition (they all have different primary keys 😅). It is unclear whether PostgreSQL can figure the same thing out on its own (I've done a simple test locally against a table and it seems it doesn't, at least for the case I tested). In case it can't, there are possible performance gains of not using distinct when filtering own table fields.Solution
distinct
becomes a three-valued attribute forAnyOfFilter
:True
: uses distinct()False
: doesn't use distinct()None
(the default): infer based on thefield_name
: if it contains a navigation (identified by the presence of a__
), use distinct.Note: lookups are also double underscored and will become "false positives" (triggering distinct when not needed), but this matches the current behavior (which triggers distinct all the time) and is not a regression.