#32152: Groupby after aggregation and subquery produces subtly wrong results
-------------------------------------+-------------------------------------
Reporter: Christian Klus | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Christian Klus):
Here's the test case I used (not sure if it's in the right location). It
succeeds on 3.0.6 and fails on subsequent releases.
{{{
diff --git a/tests/aggregation_regress/tests.py
b/tests/aggregation_regress/tests.py
index 7604335257..dac995e1fc 100644
--- a/tests/aggregation_regress/tests.py
+++ b/tests/aggregation_regress/tests.py
@@ -8,9 +8,10 @@ from django.contrib.contenttypes.models import
ContentType
from django.core.exceptions import FieldError
from django.db import connection
from django.db.models import (
- Aggregate, Avg, Case, Count, DecimalField, F, IntegerField, Max, Q,
StdDev,
- Sum, Value, Variance, When,
+ Aggregate, Avg, Case, Count, DecimalField, F, IntegerField, Max,
OuterRef,
+ Q, StdDev, Subquery, Sum, Value, Variance, When
)
+from django.db.models.functions import TruncYear
from django.test import TestCase, skipUnlessAnyDBFeature,
skipUnlessDBFeature
from django.test.utils import Approximate
@@ -102,6 +103,21 @@ class AggregationTests(TestCase):
s2.books.add(cls.b1, cls.b3, cls.b5, cls.b6)
s3.books.add(cls.b3, cls.b4, cls.b6)
+ def test_groupby_after_aggregation_and_subquery(self):
+ self.assertEqual(
+ Book.objects.all().annotate(
+ pub_year=TruncYear('pubdate')
+ ).order_by().values('pub_year').annotate(
+ total_pages=Sum('pages'),
+ top_rating=Subquery(
+ Book.objects.filter(
+ pubdate__year=OuterRef('pub_year')
+ ).order_by('rating').values('rating')[:1]
+ )
+ ).values('pub_year', 'total_pages', 'top_rating').count(),
+ 4
+ )
+
def assertObjectAttrs(self, obj, **kwargs):
for attr, value in kwargs.items():
self.assertEqual(getattr(obj, attr), value)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32152#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/065.338031cfaddc06c454f0fe6bd13d5ec9%40djangoproject.com.