-
Notifications
You must be signed in to change notification settings - Fork 451
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
Change tag search condition from or
to and
#6752
Conversation
1642098
to
52a6aff
Compare
return select(tt.torrent.infohash for tt in self.instance.TorrentTag | ||
if self._show_condition(tt) and tt.tag.name in tags).fetch() | ||
|
||
# first, get all torrents that contains any tag from `tags` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: The result of a query that returns torrents with any tags may be several orders larger than the result of a query that returns torrents with all tags. Retrieving all these objects into memory may be very slow.
suggestion: It is better to select torrents with all tags directly in the SQL query.
SQL does not have a direct way to express the FORALL universal quantifier. To express the necessary condition, it is necessary to use a double negation: "select all torrents for which does not exist a tag from the list of required tags that is not associated with this torrent". It will be something like:
# torrents that contains all necessary tags
query_results = select(
torrent for torrent in self.instance.Torrent
if not exists(
tag for tag in self.instance.Tag
if tag.name in tags and not exists(
torrent_tag for torrent_tag in self.instance.TorrentTag
if torrent_tag.torrent == torrent
and torrent_tag.tag == tag
and self._show_condition(torrent_tag)
)
)
)
Queries with double not exists
may be hard to read and understand, but they are the proper way to express the FORALL quantifier in SQL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kozlovsky wow, looks like black magic 👏
Thanks. Implemented.
52a6aff
to
fe45ea8
Compare
Kudos, SonarCloud Quality Gate passed! |
retest this please |
1 similar comment
retest this please |
This PR is related to #6708 and introduces a bit different logic for tag search than it was before.
As @kozlovsky suggested earlier, we should return 'intersection' of search results instead of 'union' in the case that more than one tag is specified in the search query.
This test that should clarify what I mean: