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(optimizer): Avoid extra queries for prefetches with existing prefetch hints #582

Merged
merged 4 commits into from
Jul 14, 2024

Conversation

bellini666
Copy link
Member

@bellini666 bellini666 commented Jul 13, 2024

Fix #559

Summary by Sourcery

This pull request addresses a bug where prefetching with existing prefetch hints resulted in extra database queries. It includes a new test to verify the fix and updates the optimizer to skip unnecessary optimizations in such cases.

  • Bug Fixes:
    • Fixed an issue where prefetching with existing prefetch hints caused extra database queries.
  • Enhancements:
    • Updated the optimizer to skip optimization when a field has prefetch_related, preventing extra unused queries.
  • Tests:
    • Added a test to ensure that prefetch hints with fields having the same name as the model do not cause extra database queries.

@bellini666 bellini666 self-assigned this Jul 13, 2024
Copy link
Contributor

sourcery-ai bot commented Jul 13, 2024

Reviewer's Guide by Sourcery

This pull request addresses issue #559 by adding a new test to ensure that prefetch hints with fields having the same name as the model do not cause extra database queries. The optimizer was modified to skip optimization when prefetch_related is present in the field's store, preventing extra unused queries. Additionally, the assert_num_queries utility was updated to handle async cases more gracefully.

File-Level Changes

Files Changes
tests/test_optimizer.py
strawberry_django/optimizer.py
Added a new test to ensure prefetch hints with fields having the same name as the model do not cause extra DB queries and modified the optimizer to skip optimization when prefetch_related is present in the field's store.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @bellini666 - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟡 Testing: 5 issues found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

strawberry_django/optimizer.py Show resolved Hide resolved
strawberry_django/optimizer.py Outdated Show resolved Hide resolved
strawberry_django/optimizer.py Outdated Show resolved Hide resolved
tests/test_optimizer.py Show resolved Hide resolved
tests/test_optimizer.py Outdated Show resolved Hide resolved
tests/test_optimizer.py Show resolved Hide resolved
tests/test_optimizer.py Show resolved Hide resolved
tests/test_optimizer.py Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Jul 13, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.

Project coverage is 88.79%. Comparing base (c7c50e9) to head (867e918).

Files Patch % Lines
strawberry_django/optimizer.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #582      +/-   ##
==========================================
- Coverage   88.90%   88.79%   -0.11%     
==========================================
  Files          41       41              
  Lines        3595     3598       +3     
==========================================
- Hits         3196     3195       -1     
- Misses        399      403       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bellini666 bellini666 force-pushed the fix_prefetch_extra_queries branch from b996269 to f422a46 Compare July 13, 2024 18:08
@bellini666 bellini666 requested a review from patrick91 July 13, 2024 18:26
bellini666 and others added 3 commits July 14, 2024 10:05
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@bellini666 bellini666 force-pushed the fix_prefetch_extra_queries branch 2 times, most recently from c1ff7ee to eedbdcb Compare July 14, 2024 13:08
@bellini666 bellini666 merged commit 862e64e into main Jul 14, 2024
22 checks passed
@bellini666 bellini666 deleted the fix_prefetch_extra_queries branch July 14, 2024 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

prefetch_related and filtering in custom resolver
3 participants