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

Resolve OuterRef in CTE Subquery #19

Merged
merged 2 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions django_cte/expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import django
from django.db.models import Subquery


class CTESubqueryResolver(object):

def __init__(self, annotation):
self.annotation = annotation

def resolve_expression(self, *args, **kw):
# source: django.db.models.expressions.Subquery.resolve_expression
# --- begin copied code (lightly adapted) --- #

# Need to recursively resolve these.
def resolve_all(child):
if hasattr(child, 'children'):
[resolve_all(_child) for _child in child.children]
if hasattr(child, 'rhs'):
child.rhs = resolve(child.rhs)

def resolve(child):
if hasattr(child, 'resolve_expression'):
resolved = child.resolve_expression(*args, **kw)
# Add table alias to the parent query's aliases to prevent
# quoting.
if hasattr(resolved, 'alias') and \
resolved.alias != resolved.target.model._meta.db_table:
get_query(clone).external_aliases.add(resolved.alias)
return resolved
return child

# --- end copied code --- #

if django.VERSION < (3, 0):
def get_query(clone):
return clone.queryset.query
else:
def get_query(clone):
return clone.query

# NOTE this uses the old (pre-Django 3) way of resolving.
# Should a different technique should be used on Django 3+?
clone = self.annotation.resolve_expression(*args, **kw)
if isinstance(self.annotation, Subquery):
for cte in get_query(clone)._with_ctes:
resolve_all(cte.query.where)
for key, value in cte.query.annotations.items():
if isinstance(value, Subquery):
cte.query.annotations[key] = resolve(value)
return clone
6 changes: 6 additions & 0 deletions django_cte/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
SQLUpdateCompiler,
)

from .expressions import CTESubqueryResolver


class CTEQuery(Query):
"""A Query which processes SQL compilation through the CTE compiler"""
Expand Down Expand Up @@ -42,6 +44,10 @@ def get_compiler(self, using=None, connection=None):
klass = COMPILER_TYPES.get(self.__class__, CTEQueryCompiler)
return klass(self, connection, using)

def add_annotation(self, annotation, *args, **kw):
annotation = CTESubqueryResolver(annotation)
super(CTEQuery, self).add_annotation(annotation, *args, **kw)

def __chain(self, _name, klass=None, *args, **kwargs):
klass = QUERY_TYPES.get(klass, self.__class__)
clone = getattr(super(CTEQuery, self), _name)(klass, *args, **kwargs)
Expand Down
51 changes: 49 additions & 2 deletions tests/test_cte.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from unittest import SkipTest

from django.db.models import IntegerField, TextField
from django.db.models.aggregates import Count, Sum
from django.db.models.expressions import Exists, F, OuterRef, Subquery, Value
from django.db.models.aggregates import Count, Max, Min, Sum
from django.db.models.expressions import (
Exists, ExpressionWrapper, F, OuterRef, Subquery, Value,
)
from django.db.models.functions import Concat
from django.test import TestCase

Expand Down Expand Up @@ -323,3 +325,48 @@ def test_delete_cte_query(self):
('earth', 33),
('proxima centauri', 2000),
])

def test_outerref_in_cte_query(self):
# This query is meant to return the difference between min and max
# order of each region, through a subquery
min_and_max = With(
Order.objects
.filter(region=OuterRef("pk"))
.values('region') # This is to force group by region_id
.annotate(
amount_min=Min("amount"),
amount_max=Max("amount"),
)
.values('amount_min', 'amount_max')
)
regions = (
Region.objects
.annotate(
difference=Subquery(
min_and_max.queryset().with_cte(min_and_max).annotate(
difference=ExpressionWrapper(
F('amount_max') - F('amount_min'),
output_field=int_field,
),
).values('difference')[:1],
output_field=IntegerField()
)
)
.order_by("name")
)
print(regions.query)

data = [(r.name, r.difference) for r in regions]
self.assertEqual(data, [
("bernard's star", None),
('deimos', None),
('earth', 3),
('mars', 2),
('mercury', 2),
('moon', 2),
('phobos', None),
('proxima centauri', 0),
('proxima centauri b', 2),
('sun', 0),
('venus', 3)
])