Fixed #34368 -- Made subquery raise NotSupportedError when referencing outer window expression.

Regression in f387d024fc.

Co-authored-by: Jannis Vajen <jvajen@gmail.com>
This commit is contained in:
Simon Charette 2023-02-26 13:43:00 +01:00 committed by Mariusz Felisiak
parent b15f162f25
commit c67ea79aa9
3 changed files with 25 additions and 1 deletions

View File

@ -857,6 +857,11 @@ class ResolvedOuterRef(F):
def resolve_expression(self, *args, **kwargs): def resolve_expression(self, *args, **kwargs):
col = super().resolve_expression(*args, **kwargs) col = super().resolve_expression(*args, **kwargs)
if col.contains_over_clause:
raise NotSupportedError(
f"Referencing outer query window expression is not supported: "
f"{self.name}."
)
# FIXME: Rename possibly_multivalued to multivalued and fix detection # FIXME: Rename possibly_multivalued to multivalued and fix detection
# for non-multivalued JOINs (e.g. foreign key fields). This should take # for non-multivalued JOINs (e.g. foreign key fields). This should take
# into account only many-to-many and one-to-many relationships. # into account only many-to-many and one-to-many relationships.

View File

@ -676,7 +676,7 @@ class SQLCompiler:
) )
) )
inner_query_compiler = inner_query.get_compiler( inner_query_compiler = inner_query.get_compiler(
self.using, elide_empty=self.elide_empty self.using, connection=self.connection, elide_empty=self.elide_empty
) )
inner_sql, inner_params = inner_query_compiler.as_sql( inner_sql, inner_params = inner_query_compiler.as_sql(
# The limits must be applied to the outer query to avoid pruning # The limits must be applied to the outer query to avoid pruning

View File

@ -1587,6 +1587,25 @@ class WindowUnsupportedTests(TestCase):
dense_rank=Window(expression=DenseRank()) dense_rank=Window(expression=DenseRank())
).get() ).get()
def test_filter_subquery(self):
qs = Employee.objects.annotate(
department_salary_rank=Window(
Rank(), partition_by="department", order_by="-salary"
)
)
msg = (
"Referencing outer query window expression is not supported: "
"department_salary_rank."
)
with self.assertRaisesMessage(NotSupportedError, msg):
qs.annotate(
employee_name=Subquery(
Employee.objects.filter(
age=OuterRef("department_salary_rank")
).values("name")[:1]
)
)
class NonQueryWindowTests(SimpleTestCase): class NonQueryWindowTests(SimpleTestCase):
def test_window_repr(self): def test_window_repr(self):