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

Support annotations and joins in F() #1761

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

henadzit
Copy link
Contributor

@henadzit henadzit commented Nov 8, 2024

Description

This PR:

  • Adds support for referencing relationship fields in F(), e.g. F("author__name") that automatically introduces joins to the query
  • Allows referencing annotations in F(), e.g. IntFields.annotate(intnum_plus_1=F("intnum") + 1).annotate(intnum_plus_2=F("intnum_plus_1") + 1)

In order to achieve that, the following implementation details changed

  • The behavior of Expression.resolve was unified to always return a ResolveResult. Previously it could have been either a tuple or dictionary.
  • F was changed to be an Expression instead of pypika's Field. This allowed to move the F's resolution code inside F.resolve and simplify the resolution code in general because F can be treated as other Expressions.
  • CombinedExpression was introduced to handle arithmetic operations on F(), e.g. F("a") + 1. It was inspired by Django's CombinedExpression and replaces pypika's arithmetic. CombinedExpression is a child of Expression, so it can be resolved without knowing the exact type of the object as opposed to pypika's ArithmeticExpression that was used previosly.

As part of this PR, I had to change how custom functions can be defined. It changed from

from tortoise.expressions import F
from pypika.terms import Function

class JsonSet(Function):
    def __init__(self, field: F, expression: str, value: Any):
        super().__init__("JSON_SET", field, expression, value)

to

from tortoise.functions import Function
from pypika.terms import Function as PupikaFunction

# it is a tortoise Function now, not Pypika's Function
class PypikaJsonSet(Function):
    def __init__(self, field: F, expression: str, value: Any):
        super().__init__("JSON_SET", field, expression, value)
    
    database_func = PypikaJsonSet

Effectively now custom functions are supposed to be a tortoise Function, not a Pypika's Function. The change had to be done because the Pypika's Function is no longer compatible with F and, frankly, having the interface that accepts both Pypika's functions and tortoise functions is a bit messy and makes the code quite convoluted.

Motivation and Context

This introduces functionality supported by Django but also there is a clear demand for these features in the tortoise community. The following issues should be fixed by this PR

How Has This Been Tested?

  • Tested on a sample web app
  • Added tests

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

coveralls commented Nov 8, 2024

Pull Request Test Coverage Report for Build 11742066623

Details

  • 216 of 219 (98.63%) changed or added relevant lines in 5 files are covered.
  • 2 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.09%) to 89.162%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/backends/base/executor.py 13 14 92.86%
tortoise/expressions.py 139 141 98.58%
Files with Coverage Reduction New Missed Lines %
tortoise/queryset.py 1 94.47%
tortoise/backends/base/executor.py 1 91.35%
Totals Coverage Status
Change from base Build 11660093686: 0.09%
Covered Lines: 6084
Relevant Lines: 6709

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants