123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406 |
- import datetime
- import math
- import re
- from decimal import Decimal
- from django.core.exceptions import FieldError
- from django.db import connection
- from django.db.models import (
- Avg,
- Case,
- Count,
- DateField,
- DateTimeField,
- DecimalField,
- DurationField,
- Exists,
- F,
- FloatField,
- IntegerField,
- Max,
- Min,
- OuterRef,
- Q,
- StdDev,
- Subquery,
- Sum,
- TimeField,
- Transform,
- Value,
- Variance,
- When,
- Window,
- )
- from django.db.models.expressions import Func, RawSQL
- from django.db.models.functions import (
- Cast,
- Coalesce,
- Greatest,
- Least,
- Lower,
- Mod,
- Now,
- Pi,
- TruncDate,
- TruncHour,
- )
- from django.test import TestCase
- from django.test.testcases import skipUnlessDBFeature
- from django.test.utils import Approximate, CaptureQueriesContext
- from django.utils import timezone
- from .models import Author, Book, Publisher, Store
- class NowUTC(Now):
- template = "CURRENT_TIMESTAMP"
- output_field = DateTimeField()
- def as_sql(self, compiler, connection, **extra_context):
- if connection.features.test_now_utc_template:
- extra_context["template"] = connection.features.test_now_utc_template
- return super().as_sql(compiler, connection, **extra_context)
- class AggregateTestCase(TestCase):
- @classmethod
- def setUpTestData(cls):
- cls.a1 = Author.objects.create(name="Adrian Holovaty", age=34)
- cls.a2 = Author.objects.create(name="Jacob Kaplan-Moss", age=35)
- cls.a3 = Author.objects.create(name="Brad Dayley", age=45)
- cls.a4 = Author.objects.create(name="James Bennett", age=29)
- cls.a5 = Author.objects.create(name="Jeffrey Forcier", age=37)
- cls.a6 = Author.objects.create(name="Paul Bissex", age=29)
- cls.a7 = Author.objects.create(name="Wesley J. Chun", age=25)
- cls.a8 = Author.objects.create(name="Peter Norvig", age=57)
- cls.a9 = Author.objects.create(name="Stuart Russell", age=46)
- cls.a1.friends.add(cls.a2, cls.a4)
- cls.a2.friends.add(cls.a1, cls.a7)
- cls.a4.friends.add(cls.a1)
- cls.a5.friends.add(cls.a6, cls.a7)
- cls.a6.friends.add(cls.a5, cls.a7)
- cls.a7.friends.add(cls.a2, cls.a5, cls.a6)
- cls.a8.friends.add(cls.a9)
- cls.a9.friends.add(cls.a8)
- cls.p1 = Publisher.objects.create(
- name="Apress", num_awards=3, duration=datetime.timedelta(days=1)
- )
- cls.p2 = Publisher.objects.create(
- name="Sams", num_awards=1, duration=datetime.timedelta(days=2)
- )
- cls.p3 = Publisher.objects.create(name="Prentice Hall", num_awards=7)
- cls.p4 = Publisher.objects.create(name="Morgan Kaufmann", num_awards=9)
- cls.p5 = Publisher.objects.create(name="Jonno's House of Books", num_awards=0)
- cls.b1 = Book.objects.create(
- isbn="159059725",
- name="The Definitive Guide to Django: Web Development Done Right",
- pages=447,
- rating=4.5,
- price=Decimal("30.00"),
- contact=cls.a1,
- publisher=cls.p1,
- pubdate=datetime.date(2007, 12, 6),
- )
- cls.b2 = Book.objects.create(
- isbn="067232959",
- name="Sams Teach Yourself Django in 24 Hours",
- pages=528,
- rating=3.0,
- price=Decimal("23.09"),
- contact=cls.a3,
- publisher=cls.p2,
- pubdate=datetime.date(2008, 3, 3),
- )
- cls.b3 = Book.objects.create(
- isbn="159059996",
- name="Practical Django Projects",
- pages=300,
- rating=4.0,
- price=Decimal("29.69"),
- contact=cls.a4,
- publisher=cls.p1,
- pubdate=datetime.date(2008, 6, 23),
- )
- cls.b4 = Book.objects.create(
- isbn="013235613",
- name="Python Web Development with Django",
- pages=350,
- rating=4.0,
- price=Decimal("29.69"),
- contact=cls.a5,
- publisher=cls.p3,
- pubdate=datetime.date(2008, 11, 3),
- )
- cls.b5 = Book.objects.create(
- isbn="013790395",
- name="Artificial Intelligence: A Modern Approach",
- pages=1132,
- rating=4.0,
- price=Decimal("82.80"),
- contact=cls.a8,
- publisher=cls.p3,
- pubdate=datetime.date(1995, 1, 15),
- )
- cls.b6 = Book.objects.create(
- isbn="155860191",
- name=(
- "Paradigms of Artificial Intelligence Programming: Case Studies in "
- "Common Lisp"
- ),
- pages=946,
- rating=5.0,
- price=Decimal("75.00"),
- contact=cls.a8,
- publisher=cls.p4,
- pubdate=datetime.date(1991, 10, 15),
- )
- cls.b1.authors.add(cls.a1, cls.a2)
- cls.b2.authors.add(cls.a3)
- cls.b3.authors.add(cls.a4)
- cls.b4.authors.add(cls.a5, cls.a6, cls.a7)
- cls.b5.authors.add(cls.a8, cls.a9)
- cls.b6.authors.add(cls.a8)
- s1 = Store.objects.create(
- name="Amazon.com",
- original_opening=datetime.datetime(1994, 4, 23, 9, 17, 42),
- friday_night_closing=datetime.time(23, 59, 59),
- )
- s2 = Store.objects.create(
- name="Books.com",
- original_opening=datetime.datetime(2001, 3, 15, 11, 23, 37),
- friday_night_closing=datetime.time(23, 59, 59),
- )
- s3 = Store.objects.create(
- name="Mamma and Pappa's Books",
- original_opening=datetime.datetime(1945, 4, 25, 16, 24, 14),
- friday_night_closing=datetime.time(21, 30),
- )
- s1.books.add(cls.b1, cls.b2, cls.b3, cls.b4, cls.b5, cls.b6)
- s2.books.add(cls.b1, cls.b3, cls.b5, cls.b6)
- s3.books.add(cls.b3, cls.b4, cls.b6)
- def test_empty_aggregate(self):
- self.assertEqual(Author.objects.aggregate(), {})
- def test_aggregate_in_order_by(self):
- msg = (
- "Using an aggregate in order_by() without also including it in "
- "annotate() is not allowed: Avg(F(book__rating)"
- )
- with self.assertRaisesMessage(FieldError, msg):
- Author.objects.values("age").order_by(Avg("book__rating"))
- def test_single_aggregate(self):
- vals = Author.objects.aggregate(Avg("age"))
- self.assertEqual(vals, {"age__avg": Approximate(37.4, places=1)})
- def test_multiple_aggregates(self):
- vals = Author.objects.aggregate(Sum("age"), Avg("age"))
- self.assertEqual(
- vals, {"age__sum": 337, "age__avg": Approximate(37.4, places=1)}
- )
- def test_filter_aggregate(self):
- vals = Author.objects.filter(age__gt=29).aggregate(Sum("age"))
- self.assertEqual(vals, {"age__sum": 254})
- def test_related_aggregate(self):
- vals = Author.objects.aggregate(Avg("friends__age"))
- self.assertEqual(vals, {"friends__age__avg": Approximate(34.07, places=2)})
- vals = Book.objects.filter(rating__lt=4.5).aggregate(Avg("authors__age"))
- self.assertEqual(vals, {"authors__age__avg": Approximate(38.2857, places=2)})
- vals = Author.objects.filter(name__contains="a").aggregate(Avg("book__rating"))
- self.assertEqual(vals, {"book__rating__avg": 4.0})
- vals = Book.objects.aggregate(Sum("publisher__num_awards"))
- self.assertEqual(vals, {"publisher__num_awards__sum": 30})
- vals = Publisher.objects.aggregate(Sum("book__price"))
- self.assertEqual(vals, {"book__price__sum": Decimal("270.27")})
- def test_aggregate_multi_join(self):
- vals = Store.objects.aggregate(Max("books__authors__age"))
- self.assertEqual(vals, {"books__authors__age__max": 57})
- vals = Author.objects.aggregate(Min("book__publisher__num_awards"))
- self.assertEqual(vals, {"book__publisher__num_awards__min": 1})
- def test_aggregate_alias(self):
- vals = Store.objects.filter(name="Amazon.com").aggregate(
- amazon_mean=Avg("books__rating")
- )
- self.assertEqual(vals, {"amazon_mean": Approximate(4.08, places=2)})
- def test_aggregate_transform(self):
- vals = Store.objects.aggregate(min_month=Min("original_opening__month"))
- self.assertEqual(vals, {"min_month": 3})
- def test_aggregate_join_transform(self):
- vals = Publisher.objects.aggregate(min_year=Min("book__pubdate__year"))
- self.assertEqual(vals, {"min_year": 1991})
- def test_annotate_basic(self):
- self.assertQuerySetEqual(
- Book.objects.annotate().order_by("pk"),
- [
- "The Definitive Guide to Django: Web Development Done Right",
- "Sams Teach Yourself Django in 24 Hours",
- "Practical Django Projects",
- "Python Web Development with Django",
- "Artificial Intelligence: A Modern Approach",
- "Paradigms of Artificial Intelligence Programming: Case Studies in "
- "Common Lisp",
- ],
- lambda b: b.name,
- )
- books = Book.objects.annotate(mean_age=Avg("authors__age"))
- b = books.get(pk=self.b1.pk)
- self.assertEqual(
- b.name, "The Definitive Guide to Django: Web Development Done Right"
- )
- self.assertEqual(b.mean_age, 34.5)
- def test_annotate_defer(self):
- qs = (
- Book.objects.annotate(page_sum=Sum("pages"))
- .defer("name")
- .filter(pk=self.b1.pk)
- )
- rows = [
- (
- self.b1.id,
- "159059725",
- 447,
- "The Definitive Guide to Django: Web Development Done Right",
- )
- ]
- self.assertQuerySetEqual(
- qs.order_by("pk"), rows, lambda r: (r.id, r.isbn, r.page_sum, r.name)
- )
- def test_annotate_defer_select_related(self):
- qs = (
- Book.objects.select_related("contact")
- .annotate(page_sum=Sum("pages"))
- .defer("name")
- .filter(pk=self.b1.pk)
- )
- rows = [
- (
- self.b1.id,
- "159059725",
- 447,
- "Adrian Holovaty",
- "The Definitive Guide to Django: Web Development Done Right",
- )
- ]
- self.assertQuerySetEqual(
- qs.order_by("pk"),
- rows,
- lambda r: (r.id, r.isbn, r.page_sum, r.contact.name, r.name),
- )
- def test_annotate_m2m(self):
- books = (
- Book.objects.filter(rating__lt=4.5)
- .annotate(Avg("authors__age"))
- .order_by("name")
- )
- self.assertQuerySetEqual(
- books,
- [
- ("Artificial Intelligence: A Modern Approach", 51.5),
- ("Practical Django Projects", 29.0),
- ("Python Web Development with Django", Approximate(30.3, places=1)),
- ("Sams Teach Yourself Django in 24 Hours", 45.0),
- ],
- lambda b: (b.name, b.authors__age__avg),
- )
- books = Book.objects.annotate(num_authors=Count("authors")).order_by("name")
- self.assertQuerySetEqual(
- books,
- [
- ("Artificial Intelligence: A Modern Approach", 2),
- (
- "Paradigms of Artificial Intelligence Programming: Case Studies in "
- "Common Lisp",
- 1,
- ),
- ("Practical Django Projects", 1),
- ("Python Web Development with Django", 3),
- ("Sams Teach Yourself Django in 24 Hours", 1),
- ("The Definitive Guide to Django: Web Development Done Right", 2),
- ],
- lambda b: (b.name, b.num_authors),
- )
- def test_backwards_m2m_annotate(self):
- authors = (
- Author.objects.filter(name__contains="a")
- .annotate(Avg("book__rating"))
- .order_by("name")
- )
- self.assertQuerySetEqual(
- authors,
- [
- ("Adrian Holovaty", 4.5),
- ("Brad Dayley", 3.0),
- ("Jacob Kaplan-Moss", 4.5),
- ("James Bennett", 4.0),
- ("Paul Bissex", 4.0),
- ("Stuart Russell", 4.0),
- ],
- lambda a: (a.name, a.book__rating__avg),
- )
- authors = Author.objects.annotate(num_books=Count("book")).order_by("name")
- self.assertQuerySetEqual(
- authors,
- [
- ("Adrian Holovaty", 1),
- ("Brad Dayley", 1),
- ("Jacob Kaplan-Moss", 1),
- ("James Bennett", 1),
- ("Jeffrey Forcier", 1),
- ("Paul Bissex", 1),
- ("Peter Norvig", 2),
- ("Stuart Russell", 1),
- ("Wesley J. Chun", 1),
- ],
- lambda a: (a.name, a.num_books),
- )
- def test_reverse_fkey_annotate(self):
- books = Book.objects.annotate(Sum("publisher__num_awards")).order_by("name")
- self.assertQuerySetEqual(
- books,
- [
- ("Artificial Intelligence: A Modern Approach", 7),
- (
- "Paradigms of Artificial Intelligence Programming: Case Studies in "
- "Common Lisp",
- 9,
- ),
- ("Practical Django Projects", 3),
- ("Python Web Development with Django", 7),
- ("Sams Teach Yourself Django in 24 Hours", 1),
- ("The Definitive Guide to Django: Web Development Done Right", 3),
- ],
- lambda b: (b.name, b.publisher__num_awards__sum),
- )
- publishers = Publisher.objects.annotate(Sum("book__price")).order_by("name")
- self.assertQuerySetEqual(
- publishers,
- [
- ("Apress", Decimal("59.69")),
- ("Jonno's House of Books", None),
- ("Morgan Kaufmann", Decimal("75.00")),
- ("Prentice Hall", Decimal("112.49")),
- ("Sams", Decimal("23.09")),
- ],
- lambda p: (p.name, p.book__price__sum),
- )
- def test_annotate_values(self):
- books = list(
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values()
- )
- self.assertEqual(
- books,
- [
- {
- "contact_id": self.a1.id,
- "id": self.b1.id,
- "isbn": "159059725",
- "mean_age": 34.5,
- "name": (
- "The Definitive Guide to Django: Web Development Done Right"
- ),
- "pages": 447,
- "price": Approximate(Decimal("30")),
- "pubdate": datetime.date(2007, 12, 6),
- "publisher_id": self.p1.id,
- "rating": 4.5,
- }
- ],
- )
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values("pk", "isbn", "mean_age")
- )
- self.assertEqual(
- list(books),
- [
- {
- "pk": self.b1.pk,
- "isbn": "159059725",
- "mean_age": 34.5,
- }
- ],
- )
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values("name")
- )
- self.assertEqual(
- list(books),
- [{"name": "The Definitive Guide to Django: Web Development Done Right"}],
- )
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .values()
- .annotate(mean_age=Avg("authors__age"))
- )
- self.assertEqual(
- list(books),
- [
- {
- "contact_id": self.a1.id,
- "id": self.b1.id,
- "isbn": "159059725",
- "mean_age": 34.5,
- "name": (
- "The Definitive Guide to Django: Web Development Done Right"
- ),
- "pages": 447,
- "price": Approximate(Decimal("30")),
- "pubdate": datetime.date(2007, 12, 6),
- "publisher_id": self.p1.id,
- "rating": 4.5,
- }
- ],
- )
- books = (
- Book.objects.values("rating")
- .annotate(n_authors=Count("authors__id"), mean_age=Avg("authors__age"))
- .order_by("rating")
- )
- self.assertEqual(
- list(books),
- [
- {
- "rating": 3.0,
- "n_authors": 1,
- "mean_age": 45.0,
- },
- {
- "rating": 4.0,
- "n_authors": 6,
- "mean_age": Approximate(37.16, places=1),
- },
- {
- "rating": 4.5,
- "n_authors": 2,
- "mean_age": 34.5,
- },
- {
- "rating": 5.0,
- "n_authors": 1,
- "mean_age": 57.0,
- },
- ],
- )
- authors = Author.objects.annotate(Avg("friends__age")).order_by("name")
- self.assertQuerySetEqual(
- authors,
- [
- ("Adrian Holovaty", 32.0),
- ("Brad Dayley", None),
- ("Jacob Kaplan-Moss", 29.5),
- ("James Bennett", 34.0),
- ("Jeffrey Forcier", 27.0),
- ("Paul Bissex", 31.0),
- ("Peter Norvig", 46.0),
- ("Stuart Russell", 57.0),
- ("Wesley J. Chun", Approximate(33.66, places=1)),
- ],
- lambda a: (a.name, a.friends__age__avg),
- )
- def test_count(self):
- vals = Book.objects.aggregate(Count("rating"))
- self.assertEqual(vals, {"rating__count": 6})
- def test_count_star(self):
- with self.assertNumQueries(1) as ctx:
- Book.objects.aggregate(n=Count("*"))
- sql = ctx.captured_queries[0]["sql"]
- self.assertIn("SELECT COUNT(*) ", sql)
- def test_count_distinct_expression(self):
- aggs = Book.objects.aggregate(
- distinct_ratings=Count(
- Case(When(pages__gt=300, then="rating")), distinct=True
- ),
- )
- self.assertEqual(aggs["distinct_ratings"], 4)
- def test_distinct_on_aggregate(self):
- for aggregate, expected_result in (
- (Avg, 4.125),
- (Count, 4),
- (Sum, 16.5),
- ):
- with self.subTest(aggregate=aggregate.__name__):
- books = Book.objects.aggregate(
- ratings=aggregate("rating", distinct=True)
- )
- self.assertEqual(books["ratings"], expected_result)
- def test_non_grouped_annotation_not_in_group_by(self):
- """
- An annotation not included in values() before an aggregate should be
- excluded from the group by clause.
- """
- qs = (
- Book.objects.annotate(xprice=F("price"))
- .filter(rating=4.0)
- .values("rating")
- .annotate(count=Count("publisher_id", distinct=True))
- .values("count", "rating")
- .order_by("count")
- )
- self.assertEqual(list(qs), [{"rating": 4.0, "count": 2}])
- def test_grouped_annotation_in_group_by(self):
- """
- An annotation included in values() before an aggregate should be
- included in the group by clause.
- """
- qs = (
- Book.objects.annotate(xprice=F("price"))
- .filter(rating=4.0)
- .values("rating", "xprice")
- .annotate(count=Count("publisher_id", distinct=True))
- .values("count", "rating")
- .order_by("count")
- )
- self.assertEqual(
- list(qs),
- [
- {"rating": 4.0, "count": 1},
- {"rating": 4.0, "count": 2},
- ],
- )
- def test_fkey_aggregate(self):
- explicit = list(Author.objects.annotate(Count("book__id")))
- implicit = list(Author.objects.annotate(Count("book")))
- self.assertCountEqual(explicit, implicit)
- def test_annotate_ordering(self):
- books = (
- Book.objects.values("rating")
- .annotate(oldest=Max("authors__age"))
- .order_by("oldest", "rating")
- )
- self.assertEqual(
- list(books),
- [
- {"rating": 4.5, "oldest": 35},
- {"rating": 3.0, "oldest": 45},
- {"rating": 4.0, "oldest": 57},
- {"rating": 5.0, "oldest": 57},
- ],
- )
- books = (
- Book.objects.values("rating")
- .annotate(oldest=Max("authors__age"))
- .order_by("-oldest", "-rating")
- )
- self.assertEqual(
- list(books),
- [
- {"rating": 5.0, "oldest": 57},
- {"rating": 4.0, "oldest": 57},
- {"rating": 3.0, "oldest": 45},
- {"rating": 4.5, "oldest": 35},
- ],
- )
- def test_aggregate_annotation(self):
- vals = Book.objects.annotate(num_authors=Count("authors__id")).aggregate(
- Avg("num_authors")
- )
- self.assertEqual(vals, {"num_authors__avg": Approximate(1.66, places=1)})
- def test_avg_duration_field(self):
- # Explicit `output_field`.
- self.assertEqual(
- Publisher.objects.aggregate(Avg("duration", output_field=DurationField())),
- {"duration__avg": datetime.timedelta(days=1, hours=12)},
- )
- # Implicit `output_field`.
- self.assertEqual(
- Publisher.objects.aggregate(Avg("duration")),
- {"duration__avg": datetime.timedelta(days=1, hours=12)},
- )
- def test_sum_duration_field(self):
- self.assertEqual(
- Publisher.objects.aggregate(Sum("duration", output_field=DurationField())),
- {"duration__sum": datetime.timedelta(days=3)},
- )
- def test_sum_distinct_aggregate(self):
- """
- Sum on a distinct() QuerySet should aggregate only the distinct items.
- """
- authors = Author.objects.filter(book__in=[self.b5, self.b6])
- self.assertEqual(authors.count(), 3)
- distinct_authors = authors.distinct()
- self.assertEqual(distinct_authors.count(), 2)
- # Selected author ages are 57 and 46
- age_sum = distinct_authors.aggregate(Sum("age"))
- self.assertEqual(age_sum["age__sum"], 103)
- def test_filtering(self):
- p = Publisher.objects.create(name="Expensive Publisher", num_awards=0)
- Book.objects.create(
- name="ExpensiveBook1",
- pages=1,
- isbn="111",
- rating=3.5,
- price=Decimal("1000"),
- publisher=p,
- contact_id=self.a1.id,
- pubdate=datetime.date(2008, 12, 1),
- )
- Book.objects.create(
- name="ExpensiveBook2",
- pages=1,
- isbn="222",
- rating=4.0,
- price=Decimal("1000"),
- publisher=p,
- contact_id=self.a1.id,
- pubdate=datetime.date(2008, 12, 2),
- )
- Book.objects.create(
- name="ExpensiveBook3",
- pages=1,
- isbn="333",
- rating=4.5,
- price=Decimal("35"),
- publisher=p,
- contact_id=self.a1.id,
- pubdate=datetime.date(2008, 12, 3),
- )
- publishers = (
- Publisher.objects.annotate(num_books=Count("book__id"))
- .filter(num_books__gt=1)
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers,
- ["Apress", "Prentice Hall", "Expensive Publisher"],
- lambda p: p.name,
- )
- publishers = Publisher.objects.filter(book__price__lt=Decimal("40.0")).order_by(
- "pk"
- )
- self.assertQuerySetEqual(
- publishers,
- [
- "Apress",
- "Apress",
- "Sams",
- "Prentice Hall",
- "Expensive Publisher",
- ],
- lambda p: p.name,
- )
- publishers = (
- Publisher.objects.annotate(num_books=Count("book__id"))
- .filter(num_books__gt=1, book__price__lt=Decimal("40.0"))
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers,
- ["Apress", "Prentice Hall", "Expensive Publisher"],
- lambda p: p.name,
- )
- publishers = (
- Publisher.objects.filter(book__price__lt=Decimal("40.0"))
- .annotate(num_books=Count("book__id"))
- .filter(num_books__gt=1)
- .order_by("pk")
- )
- self.assertQuerySetEqual(publishers, ["Apress"], lambda p: p.name)
- publishers = (
- Publisher.objects.annotate(num_books=Count("book"))
- .filter(num_books__range=[1, 3])
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers,
- [
- "Apress",
- "Sams",
- "Prentice Hall",
- "Morgan Kaufmann",
- "Expensive Publisher",
- ],
- lambda p: p.name,
- )
- publishers = (
- Publisher.objects.annotate(num_books=Count("book"))
- .filter(num_books__range=[1, 2])
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers,
- ["Apress", "Sams", "Prentice Hall", "Morgan Kaufmann"],
- lambda p: p.name,
- )
- publishers = (
- Publisher.objects.annotate(num_books=Count("book"))
- .filter(num_books__in=[1, 3])
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers,
- ["Sams", "Morgan Kaufmann", "Expensive Publisher"],
- lambda p: p.name,
- )
- publishers = Publisher.objects.annotate(num_books=Count("book")).filter(
- num_books__isnull=True
- )
- self.assertEqual(len(publishers), 0)
- def test_annotation(self):
- vals = Author.objects.filter(pk=self.a1.pk).aggregate(Count("friends__id"))
- self.assertEqual(vals, {"friends__id__count": 2})
- books = (
- Book.objects.annotate(num_authors=Count("authors__name"))
- .filter(num_authors__exact=2)
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- books,
- [
- "The Definitive Guide to Django: Web Development Done Right",
- "Artificial Intelligence: A Modern Approach",
- ],
- lambda b: b.name,
- )
- authors = (
- Author.objects.annotate(num_friends=Count("friends__id", distinct=True))
- .filter(num_friends=0)
- .order_by("pk")
- )
- self.assertQuerySetEqual(authors, ["Brad Dayley"], lambda a: a.name)
- publishers = (
- Publisher.objects.annotate(num_books=Count("book__id"))
- .filter(num_books__gt=1)
- .order_by("pk")
- )
- self.assertQuerySetEqual(
- publishers, ["Apress", "Prentice Hall"], lambda p: p.name
- )
- publishers = (
- Publisher.objects.filter(book__price__lt=Decimal("40.0"))
- .annotate(num_books=Count("book__id"))
- .filter(num_books__gt=1)
- )
- self.assertQuerySetEqual(publishers, ["Apress"], lambda p: p.name)
- books = Book.objects.annotate(num_authors=Count("authors__id")).filter(
- authors__name__contains="Norvig", num_authors__gt=1
- )
- self.assertQuerySetEqual(
- books, ["Artificial Intelligence: A Modern Approach"], lambda b: b.name
- )
- def test_more_aggregation(self):
- a = Author.objects.get(name__contains="Norvig")
- b = Book.objects.get(name__contains="Done Right")
- b.authors.add(a)
- b.save()
- vals = (
- Book.objects.annotate(num_authors=Count("authors__id"))
- .filter(authors__name__contains="Norvig", num_authors__gt=1)
- .aggregate(Avg("rating"))
- )
- self.assertEqual(vals, {"rating__avg": 4.25})
- def test_even_more_aggregate(self):
- publishers = (
- Publisher.objects.annotate(
- earliest_book=Min("book__pubdate"),
- )
- .exclude(earliest_book=None)
- .order_by("earliest_book")
- .values(
- "earliest_book",
- "num_awards",
- "id",
- "name",
- )
- )
- self.assertEqual(
- list(publishers),
- [
- {
- "earliest_book": datetime.date(1991, 10, 15),
- "num_awards": 9,
- "id": self.p4.id,
- "name": "Morgan Kaufmann",
- },
- {
- "earliest_book": datetime.date(1995, 1, 15),
- "num_awards": 7,
- "id": self.p3.id,
- "name": "Prentice Hall",
- },
- {
- "earliest_book": datetime.date(2007, 12, 6),
- "num_awards": 3,
- "id": self.p1.id,
- "name": "Apress",
- },
- {
- "earliest_book": datetime.date(2008, 3, 3),
- "num_awards": 1,
- "id": self.p2.id,
- "name": "Sams",
- },
- ],
- )
- vals = Store.objects.aggregate(
- Max("friday_night_closing"), Min("original_opening")
- )
- self.assertEqual(
- vals,
- {
- "friday_night_closing__max": datetime.time(23, 59, 59),
- "original_opening__min": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- )
- def test_annotate_values_list(self):
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values_list("pk", "isbn", "mean_age")
- )
- self.assertEqual(list(books), [(self.b1.id, "159059725", 34.5)])
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values_list("isbn")
- )
- self.assertEqual(list(books), [("159059725",)])
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values_list("mean_age")
- )
- self.assertEqual(list(books), [(34.5,)])
- books = (
- Book.objects.filter(pk=self.b1.pk)
- .annotate(mean_age=Avg("authors__age"))
- .values_list("mean_age", flat=True)
- )
- self.assertEqual(list(books), [34.5])
- books = (
- Book.objects.values_list("price")
- .annotate(count=Count("price"))
- .order_by("-count", "price")
- )
- self.assertEqual(
- list(books),
- [
- (Decimal("29.69"), 2),
- (Decimal("23.09"), 1),
- (Decimal("30"), 1),
- (Decimal("75"), 1),
- (Decimal("82.8"), 1),
- ],
- )
- def test_dates_with_aggregation(self):
- """
- .dates() returns a distinct set of dates when applied to a
- QuerySet with aggregation.
- Refs #18056. Previously, .dates() would return distinct (date_kind,
- aggregation) sets, in this case (year, num_authors), so 2008 would be
- returned twice because there are books from 2008 with a different
- number of authors.
- """
- dates = Book.objects.annotate(num_authors=Count("authors")).dates(
- "pubdate", "year"
- )
- self.assertSequenceEqual(
- dates,
- [
- datetime.date(1991, 1, 1),
- datetime.date(1995, 1, 1),
- datetime.date(2007, 1, 1),
- datetime.date(2008, 1, 1),
- ],
- )
- def test_values_aggregation(self):
- # Refs #20782
- max_rating = Book.objects.values("rating").aggregate(max_rating=Max("rating"))
- self.assertEqual(max_rating["max_rating"], 5)
- max_books_per_rating = (
- Book.objects.values("rating")
- .annotate(books_per_rating=Count("id"))
- .aggregate(Max("books_per_rating"))
- )
- self.assertEqual(max_books_per_rating, {"books_per_rating__max": 3})
- def test_ticket17424(self):
- """
- Doing exclude() on a foreign model after annotate() doesn't crash.
- """
- all_books = list(Book.objects.values_list("pk", flat=True).order_by("pk"))
- annotated_books = Book.objects.order_by("pk").annotate(one=Count("id"))
- # The value doesn't matter, we just need any negative
- # constraint on a related model that's a noop.
- excluded_books = annotated_books.exclude(publisher__name="__UNLIKELY_VALUE__")
- # Try to generate query tree
- str(excluded_books.query)
- self.assertQuerySetEqual(excluded_books, all_books, lambda x: x.pk)
- # Check internal state
- self.assertIsNone(annotated_books.query.alias_map["aggregation_book"].join_type)
- self.assertIsNone(excluded_books.query.alias_map["aggregation_book"].join_type)
- def test_ticket12886(self):
- """
- Aggregation over sliced queryset works correctly.
- """
- qs = Book.objects.order_by("-rating")[0:3]
- vals = qs.aggregate(average_top3_rating=Avg("rating"))["average_top3_rating"]
- self.assertAlmostEqual(vals, 4.5, places=2)
- def test_ticket11881(self):
- """
- Subqueries do not needlessly contain ORDER BY, SELECT FOR UPDATE or
- select_related() stuff.
- """
- qs = (
- Book.objects.select_for_update()
- .order_by("pk")
- .select_related("publisher")
- .annotate(max_pk=Max("pk"))
- )
- with CaptureQueriesContext(connection) as captured_queries:
- qs.aggregate(avg_pk=Avg("max_pk"))
- self.assertEqual(len(captured_queries), 1)
- qstr = captured_queries[0]["sql"].lower()
- self.assertNotIn("for update", qstr)
- forced_ordering = connection.ops.force_no_ordering()
- if forced_ordering:
- # If the backend needs to force an ordering we make sure it's
- # the only "ORDER BY" clause present in the query.
- self.assertEqual(
- re.findall(r"order by (\w+)", qstr),
- [", ".join(f[1][0] for f in forced_ordering).lower()],
- )
- else:
- self.assertNotIn("order by", qstr)
- self.assertEqual(qstr.count(" join "), 0)
- def test_decimal_max_digits_has_no_effect(self):
- Book.objects.all().delete()
- a1 = Author.objects.first()
- p1 = Publisher.objects.first()
- thedate = timezone.now()
- for i in range(10):
- Book.objects.create(
- isbn="abcde{}".format(i),
- name="none",
- pages=10,
- rating=4.0,
- price=9999.98,
- contact=a1,
- publisher=p1,
- pubdate=thedate,
- )
- book = Book.objects.aggregate(price_sum=Sum("price"))
- self.assertEqual(book["price_sum"], Decimal("99999.80"))
- def test_nonaggregate_aggregation_throws(self):
- with self.assertRaisesMessage(TypeError, "fail is not an aggregate expression"):
- Book.objects.aggregate(fail=F("price"))
- def test_nonfield_annotation(self):
- book = Book.objects.annotate(val=Max(Value(2))).first()
- self.assertEqual(book.val, 2)
- book = Book.objects.annotate(
- val=Max(Value(2), output_field=IntegerField())
- ).first()
- self.assertEqual(book.val, 2)
- book = Book.objects.annotate(val=Max(2, output_field=IntegerField())).first()
- self.assertEqual(book.val, 2)
- def test_annotation_expressions(self):
- authors = Author.objects.annotate(
- combined_ages=Sum(F("age") + F("friends__age"))
- ).order_by("name")
- authors2 = Author.objects.annotate(
- combined_ages=Sum("age") + Sum("friends__age")
- ).order_by("name")
- for qs in (authors, authors2):
- self.assertQuerySetEqual(
- qs,
- [
- ("Adrian Holovaty", 132),
- ("Brad Dayley", None),
- ("Jacob Kaplan-Moss", 129),
- ("James Bennett", 63),
- ("Jeffrey Forcier", 128),
- ("Paul Bissex", 120),
- ("Peter Norvig", 103),
- ("Stuart Russell", 103),
- ("Wesley J. Chun", 176),
- ],
- lambda a: (a.name, a.combined_ages),
- )
- def test_aggregation_expressions(self):
- a1 = Author.objects.aggregate(av_age=Sum("age") / Count("*"))
- a2 = Author.objects.aggregate(av_age=Sum("age") / Count("age"))
- a3 = Author.objects.aggregate(av_age=Avg("age"))
- self.assertEqual(a1, {"av_age": 37})
- self.assertEqual(a2, {"av_age": 37})
- self.assertEqual(a3, {"av_age": Approximate(37.4, places=1)})
- def test_avg_decimal_field(self):
- v = Book.objects.filter(rating=4).aggregate(avg_price=(Avg("price")))[
- "avg_price"
- ]
- self.assertIsInstance(v, Decimal)
- self.assertEqual(v, Approximate(Decimal("47.39"), places=2))
- def test_order_of_precedence(self):
- p1 = Book.objects.filter(rating=4).aggregate(avg_price=(Avg("price") + 2) * 3)
- self.assertEqual(p1, {"avg_price": Approximate(Decimal("148.18"), places=2)})
- p2 = Book.objects.filter(rating=4).aggregate(avg_price=Avg("price") + 2 * 3)
- self.assertEqual(p2, {"avg_price": Approximate(Decimal("53.39"), places=2)})
- def test_combine_different_types(self):
- msg = (
- "Cannot infer type of '+' expression involving these types: FloatField, "
- "DecimalField. You must set output_field."
- )
- qs = Book.objects.annotate(sums=Sum("rating") + Sum("pages") + Sum("price"))
- with self.assertRaisesMessage(FieldError, msg):
- qs.first()
- with self.assertRaisesMessage(FieldError, msg):
- qs.first()
- b1 = Book.objects.annotate(
- sums=Sum(F("rating") + F("pages") + F("price"), output_field=IntegerField())
- ).get(pk=self.b4.pk)
- self.assertEqual(b1.sums, 383)
- b2 = Book.objects.annotate(
- sums=Sum(F("rating") + F("pages") + F("price"), output_field=FloatField())
- ).get(pk=self.b4.pk)
- self.assertEqual(b2.sums, 383.69)
- b3 = Book.objects.annotate(
- sums=Sum(F("rating") + F("pages") + F("price"), output_field=DecimalField())
- ).get(pk=self.b4.pk)
- self.assertEqual(b3.sums, Approximate(Decimal("383.69"), places=2))
- def test_complex_aggregations_require_kwarg(self):
- with self.assertRaisesMessage(
- TypeError, "Complex annotations require an alias"
- ):
- Author.objects.annotate(Sum(F("age") + F("friends__age")))
- with self.assertRaisesMessage(TypeError, "Complex aggregates require an alias"):
- Author.objects.aggregate(Sum("age") / Count("age"))
- with self.assertRaisesMessage(TypeError, "Complex aggregates require an alias"):
- Author.objects.aggregate(Sum(1))
- def test_aggregate_over_complex_annotation(self):
- qs = Author.objects.annotate(combined_ages=Sum(F("age") + F("friends__age")))
- age = qs.aggregate(max_combined_age=Max("combined_ages"))
- self.assertEqual(age["max_combined_age"], 176)
- age = qs.aggregate(max_combined_age_doubled=Max("combined_ages") * 2)
- self.assertEqual(age["max_combined_age_doubled"], 176 * 2)
- age = qs.aggregate(
- max_combined_age_doubled=Max("combined_ages") + Max("combined_ages")
- )
- self.assertEqual(age["max_combined_age_doubled"], 176 * 2)
- age = qs.aggregate(
- max_combined_age_doubled=Max("combined_ages") + Max("combined_ages"),
- sum_combined_age=Sum("combined_ages"),
- )
- self.assertEqual(age["max_combined_age_doubled"], 176 * 2)
- self.assertEqual(age["sum_combined_age"], 954)
- age = qs.aggregate(
- max_combined_age_doubled=Max("combined_ages") + Max("combined_ages"),
- sum_combined_age_doubled=Sum("combined_ages") + Sum("combined_ages"),
- )
- self.assertEqual(age["max_combined_age_doubled"], 176 * 2)
- self.assertEqual(age["sum_combined_age_doubled"], 954 * 2)
- def test_values_annotation_with_expression(self):
- # ensure the F() is promoted to the group by clause
- qs = Author.objects.values("name").annotate(another_age=Sum("age") + F("age"))
- a = qs.get(name="Adrian Holovaty")
- self.assertEqual(a["another_age"], 68)
- qs = qs.annotate(friend_count=Count("friends"))
- a = qs.get(name="Adrian Holovaty")
- self.assertEqual(a["friend_count"], 2)
- qs = (
- qs.annotate(combined_age=Sum("age") + F("friends__age"))
- .filter(name="Adrian Holovaty")
- .order_by("-combined_age")
- )
- self.assertEqual(
- list(qs),
- [
- {
- "name": "Adrian Holovaty",
- "another_age": 68,
- "friend_count": 1,
- "combined_age": 69,
- },
- {
- "name": "Adrian Holovaty",
- "another_age": 68,
- "friend_count": 1,
- "combined_age": 63,
- },
- ],
- )
- vals = qs.values("name", "combined_age")
- self.assertEqual(
- list(vals),
- [
- {"name": "Adrian Holovaty", "combined_age": 69},
- {"name": "Adrian Holovaty", "combined_age": 63},
- ],
- )
- def test_annotate_values_aggregate(self):
- alias_age = (
- Author.objects.annotate(age_alias=F("age"))
- .values(
- "age_alias",
- )
- .aggregate(sum_age=Sum("age_alias"))
- )
- age = Author.objects.values("age").aggregate(sum_age=Sum("age"))
- self.assertEqual(alias_age["sum_age"], age["sum_age"])
- def test_annotate_over_annotate(self):
- author = (
- Author.objects.annotate(age_alias=F("age"))
- .annotate(sum_age=Sum("age_alias"))
- .get(name="Adrian Holovaty")
- )
- other_author = Author.objects.annotate(sum_age=Sum("age")).get(
- name="Adrian Holovaty"
- )
- self.assertEqual(author.sum_age, other_author.sum_age)
- def test_aggregate_over_aggregate(self):
- msg = "Cannot compute Avg('age_agg'): 'age_agg' is an aggregate"
- with self.assertRaisesMessage(FieldError, msg):
- Author.objects.aggregate(
- age_agg=Sum(F("age")),
- avg_age=Avg(F("age_agg")),
- )
- def test_annotated_aggregate_over_annotated_aggregate(self):
- with self.assertRaisesMessage(
- FieldError, "Cannot compute Sum('id__max'): 'id__max' is an aggregate"
- ):
- Book.objects.annotate(Max("id")).annotate(Sum("id__max"))
- class MyMax(Max):
- arity = None
- def as_sql(self, compiler, connection):
- self.set_source_expressions(self.get_source_expressions()[0:1])
- return super().as_sql(compiler, connection)
- with self.assertRaisesMessage(
- FieldError, "Cannot compute Max('id__max'): 'id__max' is an aggregate"
- ):
- Book.objects.annotate(Max("id")).annotate(my_max=MyMax("id__max", "price"))
- def test_multi_arg_aggregate(self):
- class MyMax(Max):
- output_field = DecimalField()
- arity = None
- def as_sql(self, compiler, connection):
- copy = self.copy()
- copy.set_source_expressions(copy.get_source_expressions()[0:1] + [None])
- return super(MyMax, copy).as_sql(compiler, connection)
- with self.assertRaisesMessage(TypeError, "Complex aggregates require an alias"):
- Book.objects.aggregate(MyMax("pages", "price"))
- with self.assertRaisesMessage(
- TypeError, "Complex annotations require an alias"
- ):
- Book.objects.annotate(MyMax("pages", "price"))
- Book.objects.aggregate(max_field=MyMax("pages", "price"))
- def test_add_implementation(self):
- class MySum(Sum):
- pass
- # test completely changing how the output is rendered
- def lower_case_function_override(self, compiler, connection):
- sql, params = compiler.compile(self.source_expressions[0])
- substitutions = {
- "function": self.function.lower(),
- "expressions": sql,
- "distinct": "",
- }
- substitutions.update(self.extra)
- return self.template % substitutions, params
- setattr(MySum, "as_" + connection.vendor, lower_case_function_override)
- qs = Book.objects.annotate(
- sums=MySum(
- F("rating") + F("pages") + F("price"), output_field=IntegerField()
- )
- )
- self.assertEqual(str(qs.query).count("sum("), 1)
- b1 = qs.get(pk=self.b4.pk)
- self.assertEqual(b1.sums, 383)
- # test changing the dict and delegating
- def lower_case_function_super(self, compiler, connection):
- self.extra["function"] = self.function.lower()
- return super(MySum, self).as_sql(compiler, connection)
- setattr(MySum, "as_" + connection.vendor, lower_case_function_super)
- qs = Book.objects.annotate(
- sums=MySum(
- F("rating") + F("pages") + F("price"), output_field=IntegerField()
- )
- )
- self.assertEqual(str(qs.query).count("sum("), 1)
- b1 = qs.get(pk=self.b4.pk)
- self.assertEqual(b1.sums, 383)
- # test overriding all parts of the template
- def be_evil(self, compiler, connection):
- substitutions = {"function": "MAX", "expressions": "2", "distinct": ""}
- substitutions.update(self.extra)
- return self.template % substitutions, ()
- setattr(MySum, "as_" + connection.vendor, be_evil)
- qs = Book.objects.annotate(
- sums=MySum(
- F("rating") + F("pages") + F("price"), output_field=IntegerField()
- )
- )
- self.assertEqual(str(qs.query).count("MAX("), 1)
- b1 = qs.get(pk=self.b4.pk)
- self.assertEqual(b1.sums, 2)
- def test_complex_values_aggregation(self):
- max_rating = Book.objects.values("rating").aggregate(
- double_max_rating=Max("rating") + Max("rating")
- )
- self.assertEqual(max_rating["double_max_rating"], 5 * 2)
- max_books_per_rating = (
- Book.objects.values("rating")
- .annotate(books_per_rating=Count("id") + 5)
- .aggregate(Max("books_per_rating"))
- )
- self.assertEqual(max_books_per_rating, {"books_per_rating__max": 3 + 5})
- def test_expression_on_aggregation(self):
- qs = (
- Publisher.objects.annotate(
- price_or_median=Greatest(
- Avg("book__rating", output_field=DecimalField()), Avg("book__price")
- )
- )
- .filter(price_or_median__gte=F("num_awards"))
- .order_by("num_awards")
- )
- self.assertQuerySetEqual(qs, [1, 3, 7, 9], lambda v: v.num_awards)
- qs2 = (
- Publisher.objects.annotate(
- rating_or_num_awards=Greatest(
- Avg("book__rating"), F("num_awards"), output_field=FloatField()
- )
- )
- .filter(rating_or_num_awards__gt=F("num_awards"))
- .order_by("num_awards")
- )
- self.assertQuerySetEqual(qs2, [1, 3], lambda v: v.num_awards)
- def test_arguments_must_be_expressions(self):
- msg = "QuerySet.aggregate() received non-expression(s): %s."
- with self.assertRaisesMessage(TypeError, msg % FloatField()):
- Book.objects.aggregate(FloatField())
- with self.assertRaisesMessage(TypeError, msg % True):
- Book.objects.aggregate(is_book=True)
- with self.assertRaisesMessage(
- TypeError, msg % ", ".join([str(FloatField()), "True"])
- ):
- Book.objects.aggregate(FloatField(), Avg("price"), is_book=True)
- def test_aggregation_subquery_annotation(self):
- """Subquery annotations are excluded from the GROUP BY if they are
- not explicitly grouped against."""
- latest_book_pubdate_qs = (
- Book.objects.filter(publisher=OuterRef("pk"))
- .order_by("-pubdate")
- .values("pubdate")[:1]
- )
- publisher_qs = Publisher.objects.annotate(
- latest_book_pubdate=Subquery(latest_book_pubdate_qs),
- ).annotate(count=Count("book"))
- with self.assertNumQueries(1) as ctx:
- list(publisher_qs)
- self.assertEqual(ctx[0]["sql"].count("SELECT"), 2)
- # The GROUP BY should not be by alias either.
- self.assertEqual(ctx[0]["sql"].lower().count("latest_book_pubdate"), 1)
- def test_aggregation_subquery_annotation_exists(self):
- latest_book_pubdate_qs = (
- Book.objects.filter(publisher=OuterRef("pk"))
- .order_by("-pubdate")
- .values("pubdate")[:1]
- )
- publisher_qs = Publisher.objects.annotate(
- latest_book_pubdate=Subquery(latest_book_pubdate_qs),
- count=Count("book"),
- )
- self.assertTrue(publisher_qs.exists())
- def test_aggregation_filter_exists(self):
- publishers_having_more_than_one_book_qs = (
- Book.objects.values("publisher")
- .annotate(cnt=Count("isbn"))
- .filter(cnt__gt=1)
- )
- query = publishers_having_more_than_one_book_qs.query.exists()
- _, _, group_by = query.get_compiler(connection=connection).pre_sql_setup()
- self.assertEqual(len(group_by), 1)
- def test_aggregation_exists_annotation(self):
- published_books = Book.objects.filter(publisher=OuterRef("pk"))
- publisher_qs = Publisher.objects.annotate(
- published_book=Exists(published_books),
- count=Count("book"),
- ).values_list("name", flat=True)
- self.assertCountEqual(
- list(publisher_qs),
- [
- "Apress",
- "Morgan Kaufmann",
- "Jonno's House of Books",
- "Prentice Hall",
- "Sams",
- ],
- )
- def test_aggregation_subquery_annotation_values(self):
- """
- Subquery annotations and external aliases are excluded from the GROUP
- BY if they are not selected.
- """
- books_qs = (
- Book.objects.annotate(
- first_author_the_same_age=Subquery(
- Author.objects.filter(
- age=OuterRef("contact__friends__age"),
- )
- .order_by("age")
- .values("id")[:1],
- )
- )
- .filter(
- publisher=self.p1,
- first_author_the_same_age__isnull=False,
- )
- .annotate(
- min_age=Min("contact__friends__age"),
- )
- .values("name", "min_age")
- .order_by("name")
- )
- self.assertEqual(
- list(books_qs),
- [
- {"name": "Practical Django Projects", "min_age": 34},
- {
- "name": (
- "The Definitive Guide to Django: Web Development Done Right"
- ),
- "min_age": 29,
- },
- ],
- )
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_aggregation_subquery_annotation_values_collision(self):
- books_rating_qs = Book.objects.filter(
- pk=OuterRef("book"),
- ).values("rating")
- publisher_qs = (
- Publisher.objects.filter(
- book__contact__age__gt=20,
- )
- .annotate(
- rating=Subquery(books_rating_qs),
- )
- .values("rating")
- .annotate(total_count=Count("*"))
- .order_by("rating")
- )
- self.assertEqual(
- list(publisher_qs),
- [
- {"rating": 3.0, "total_count": 1},
- {"rating": 4.0, "total_count": 3},
- {"rating": 4.5, "total_count": 1},
- {"rating": 5.0, "total_count": 1},
- ],
- )
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_aggregation_subquery_annotation_multivalued(self):
- """
- Subquery annotations must be included in the GROUP BY if they use
- potentially multivalued relations (contain the LOOKUP_SEP).
- """
- subquery_qs = Author.objects.filter(
- pk=OuterRef("pk"),
- book__name=OuterRef("book__name"),
- ).values("pk")
- author_qs = Author.objects.annotate(
- subquery_id=Subquery(subquery_qs),
- ).annotate(count=Count("book"))
- self.assertEqual(author_qs.count(), Author.objects.count())
- def test_aggregation_order_by_not_selected_annotation_values(self):
- result_asc = [
- self.b4.pk,
- self.b3.pk,
- self.b1.pk,
- self.b2.pk,
- self.b5.pk,
- self.b6.pk,
- ]
- result_desc = result_asc[::-1]
- tests = [
- ("min_related_age", result_asc),
- ("-min_related_age", result_desc),
- (F("min_related_age"), result_asc),
- (F("min_related_age").asc(), result_asc),
- (F("min_related_age").desc(), result_desc),
- ]
- for ordering, expected_result in tests:
- with self.subTest(ordering=ordering):
- books_qs = (
- Book.objects.annotate(
- min_age=Min("authors__age"),
- )
- .annotate(
- min_related_age=Coalesce("min_age", "contact__age"),
- )
- .order_by(ordering)
- .values_list("pk", flat=True)
- )
- self.assertEqual(list(books_qs), expected_result)
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_group_by_subquery_annotation(self):
- """
- Subquery annotations are included in the GROUP BY if they are
- grouped against.
- """
- long_books_count_qs = (
- Book.objects.filter(
- publisher=OuterRef("pk"),
- pages__gt=400,
- )
- .values("publisher")
- .annotate(count=Count("pk"))
- .values("count")
- )
- groups = [
- Subquery(long_books_count_qs),
- long_books_count_qs,
- long_books_count_qs.query,
- ]
- for group in groups:
- with self.subTest(group=group.__class__.__name__):
- long_books_count_breakdown = Publisher.objects.values_list(
- group,
- ).annotate(total=Count("*"))
- self.assertEqual(dict(long_books_count_breakdown), {None: 1, 1: 4})
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_group_by_exists_annotation(self):
- """
- Exists annotations are included in the GROUP BY if they are
- grouped against.
- """
- long_books_qs = Book.objects.filter(
- publisher=OuterRef("pk"),
- pages__gt=800,
- )
- has_long_books_breakdown = Publisher.objects.values_list(
- Exists(long_books_qs),
- ).annotate(total=Count("*"))
- self.assertEqual(dict(has_long_books_breakdown), {True: 2, False: 3})
- def test_group_by_nested_expression_with_params(self):
- books_qs = (
- Book.objects.annotate(greatest_pages=Greatest("pages", Value(600)))
- .values(
- "greatest_pages",
- )
- .annotate(
- min_pages=Min("pages"),
- least=Least("min_pages", "greatest_pages"),
- )
- .values_list("least", flat=True)
- )
- self.assertCountEqual(books_qs, [300, 946, 1132])
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_aggregation_subquery_annotation_related_field(self):
- publisher = Publisher.objects.create(name=self.a9.name, num_awards=2)
- book = Book.objects.create(
- isbn="159059999",
- name="Test book.",
- pages=819,
- rating=2.5,
- price=Decimal("14.44"),
- contact=self.a9,
- publisher=publisher,
- pubdate=datetime.date(2019, 12, 6),
- )
- book.authors.add(self.a5, self.a6, self.a7)
- books_qs = (
- Book.objects.annotate(
- contact_publisher=Subquery(
- Publisher.objects.filter(
- pk=OuterRef("publisher"),
- name=OuterRef("contact__name"),
- ).values("name")[:1],
- )
- )
- .filter(
- contact_publisher__isnull=False,
- )
- .annotate(count=Count("authors"))
- )
- with self.assertNumQueries(1) as ctx:
- self.assertSequenceEqual(books_qs, [book])
- if connection.features.allows_group_by_select_index:
- self.assertEqual(ctx[0]["sql"].count("SELECT"), 3)
- @skipUnlessDBFeature("supports_subqueries_in_group_by")
- def test_aggregation_nested_subquery_outerref(self):
- publisher_with_same_name = Publisher.objects.filter(
- id__in=Subquery(
- Publisher.objects.filter(
- name=OuterRef(OuterRef("publisher__name")),
- ).values("id"),
- ),
- ).values(publisher_count=Count("id"))[:1]
- books_breakdown = Book.objects.annotate(
- publisher_count=Subquery(publisher_with_same_name),
- authors_count=Count("authors"),
- ).values_list("publisher_count", flat=True)
- self.assertSequenceEqual(books_breakdown, [1] * 6)
- def test_aggregation_exists_multivalued_outeref(self):
- self.assertCountEqual(
- Publisher.objects.annotate(
- books_exists=Exists(
- Book.objects.filter(publisher=OuterRef("book__publisher"))
- ),
- books_count=Count("book"),
- ),
- Publisher.objects.all(),
- )
- def test_filter_in_subquery_or_aggregation(self):
- """
- Filtering against an aggregate requires the usage of the HAVING clause.
- If such a filter is unionized to a non-aggregate one the latter will
- also need to be moved to the HAVING clause and have its grouping
- columns used in the GROUP BY.
- When this is done with a subquery the specialized logic in charge of
- using outer reference columns to group should be used instead of the
- subquery itself as the latter might return multiple rows.
- """
- authors = Author.objects.annotate(
- Count("book"),
- ).filter(Q(book__count__gt=0) | Q(pk__in=Book.objects.values("authors")))
- self.assertCountEqual(authors, Author.objects.all())
- def test_aggregation_random_ordering(self):
- """Random() is not included in the GROUP BY when used for ordering."""
- authors = Author.objects.annotate(contact_count=Count("book")).order_by("?")
- self.assertQuerySetEqual(
- authors,
- [
- ("Adrian Holovaty", 1),
- ("Jacob Kaplan-Moss", 1),
- ("Brad Dayley", 1),
- ("James Bennett", 1),
- ("Jeffrey Forcier", 1),
- ("Paul Bissex", 1),
- ("Wesley J. Chun", 1),
- ("Stuart Russell", 1),
- ("Peter Norvig", 2),
- ],
- lambda a: (a.name, a.contact_count),
- ordered=False,
- )
- def test_order_by_aggregate_transform(self):
- class Mod100(Mod, Transform):
- def __init__(self, expr):
- super().__init__(expr, 100)
- sum_field = IntegerField()
- sum_field.register_lookup(Mod100, "mod100")
- publisher_pages = (
- Book.objects.values("publisher")
- .annotate(sum_pages=Sum("pages", output_field=sum_field))
- .order_by("sum_pages__mod100")
- )
- self.assertQuerySetEqual(
- publisher_pages,
- [
- {"publisher": self.p2.id, "sum_pages": 528},
- {"publisher": self.p4.id, "sum_pages": 946},
- {"publisher": self.p1.id, "sum_pages": 747},
- {"publisher": self.p3.id, "sum_pages": 1482},
- ],
- )
- def test_order_by_aggregate_default_alias(self):
- publisher_books = (
- Publisher.objects.values("book")
- .annotate(Count("book"))
- .order_by("book__count", "book__id")
- .values_list("book", flat=True)
- )
- self.assertQuerySetEqual(
- publisher_books,
- [
- None,
- self.b1.id,
- self.b2.id,
- self.b3.id,
- self.b4.id,
- self.b5.id,
- self.b6.id,
- ],
- )
- def test_empty_result_optimization(self):
- with self.assertNumQueries(0):
- self.assertEqual(
- Publisher.objects.none().aggregate(
- sum_awards=Sum("num_awards"),
- books_count=Count("book"),
- ),
- {
- "sum_awards": None,
- "books_count": 0,
- },
- )
- # Expression without empty_result_set_value forces queries to be
- # executed even if they would return an empty result set.
- raw_books_count = Func("book", function="COUNT")
- raw_books_count.contains_aggregate = True
- with self.assertNumQueries(1):
- self.assertEqual(
- Publisher.objects.none().aggregate(
- sum_awards=Sum("num_awards"),
- books_count=raw_books_count,
- ),
- {
- "sum_awards": None,
- "books_count": 0,
- },
- )
- def test_coalesced_empty_result_set(self):
- with self.assertNumQueries(0):
- self.assertEqual(
- Publisher.objects.none().aggregate(
- sum_awards=Coalesce(Sum("num_awards"), 0),
- )["sum_awards"],
- 0,
- )
- # Multiple expressions.
- with self.assertNumQueries(0):
- self.assertEqual(
- Publisher.objects.none().aggregate(
- sum_awards=Coalesce(Sum("num_awards"), None, 0),
- )["sum_awards"],
- 0,
- )
- # Nested coalesce.
- with self.assertNumQueries(0):
- self.assertEqual(
- Publisher.objects.none().aggregate(
- sum_awards=Coalesce(Coalesce(Sum("num_awards"), None), 0),
- )["sum_awards"],
- 0,
- )
- # Expression coalesce.
- with self.assertNumQueries(1):
- self.assertIsInstance(
- Store.objects.none().aggregate(
- latest_opening=Coalesce(
- Max("original_opening"),
- RawSQL("CURRENT_TIMESTAMP", []),
- ),
- )["latest_opening"],
- datetime.datetime,
- )
- def test_aggregation_default_unsupported_by_count(self):
- msg = "Count does not allow default."
- with self.assertRaisesMessage(TypeError, msg):
- Count("age", default=0)
- def test_aggregation_default_unset(self):
- for Aggregate in [Avg, Max, Min, StdDev, Sum, Variance]:
- with self.subTest(Aggregate):
- result = Author.objects.filter(age__gt=100).aggregate(
- value=Aggregate("age"),
- )
- self.assertIsNone(result["value"])
- def test_aggregation_default_zero(self):
- for Aggregate in [Avg, Max, Min, StdDev, Sum, Variance]:
- with self.subTest(Aggregate):
- result = Author.objects.filter(age__gt=100).aggregate(
- value=Aggregate("age", default=0),
- )
- self.assertEqual(result["value"], 0)
- def test_aggregation_default_integer(self):
- for Aggregate in [Avg, Max, Min, StdDev, Sum, Variance]:
- with self.subTest(Aggregate):
- result = Author.objects.filter(age__gt=100).aggregate(
- value=Aggregate("age", default=21),
- )
- self.assertEqual(result["value"], 21)
- def test_aggregation_default_expression(self):
- for Aggregate in [Avg, Max, Min, StdDev, Sum, Variance]:
- with self.subTest(Aggregate):
- result = Author.objects.filter(age__gt=100).aggregate(
- value=Aggregate("age", default=Value(5) * Value(7)),
- )
- self.assertEqual(result["value"], 35)
- def test_aggregation_default_group_by(self):
- qs = (
- Publisher.objects.values("name")
- .annotate(
- books=Count("book"),
- pages=Sum("book__pages", default=0),
- )
- .filter(books=0)
- )
- self.assertSequenceEqual(
- qs,
- [{"name": "Jonno's House of Books", "books": 0, "pages": 0}],
- )
- def test_aggregation_default_compound_expression(self):
- # Scale rating to a percentage; default to 50% if no books published.
- formula = Avg("book__rating", default=2.5) * 20.0
- queryset = Publisher.objects.annotate(rating=formula).order_by("name")
- self.assertSequenceEqual(
- queryset.values("name", "rating"),
- [
- {"name": "Apress", "rating": 85.0},
- {"name": "Jonno's House of Books", "rating": 50.0},
- {"name": "Morgan Kaufmann", "rating": 100.0},
- {"name": "Prentice Hall", "rating": 80.0},
- {"name": "Sams", "rating": 60.0},
- ],
- )
- def test_aggregation_default_using_time_from_python(self):
- expr = Min(
- "store__friday_night_closing",
- filter=~Q(store__name="Amazon.com"),
- default=datetime.time(17),
- )
- if connection.vendor == "mysql":
- # Workaround for #30224 for MySQL & MariaDB.
- expr.default = Cast(expr.default, TimeField())
- queryset = Book.objects.annotate(oldest_store_opening=expr).order_by("isbn")
- self.assertSequenceEqual(
- queryset.values("isbn", "oldest_store_opening"),
- [
- {"isbn": "013235613", "oldest_store_opening": datetime.time(21, 30)},
- {
- "isbn": "013790395",
- "oldest_store_opening": datetime.time(23, 59, 59),
- },
- {"isbn": "067232959", "oldest_store_opening": datetime.time(17)},
- {"isbn": "155860191", "oldest_store_opening": datetime.time(21, 30)},
- {
- "isbn": "159059725",
- "oldest_store_opening": datetime.time(23, 59, 59),
- },
- {"isbn": "159059996", "oldest_store_opening": datetime.time(21, 30)},
- ],
- )
- def test_aggregation_default_using_time_from_database(self):
- now = timezone.now().astimezone(datetime.timezone.utc)
- expr = Min(
- "store__friday_night_closing",
- filter=~Q(store__name="Amazon.com"),
- default=TruncHour(NowUTC(), output_field=TimeField()),
- )
- queryset = Book.objects.annotate(oldest_store_opening=expr).order_by("isbn")
- self.assertSequenceEqual(
- queryset.values("isbn", "oldest_store_opening"),
- [
- {"isbn": "013235613", "oldest_store_opening": datetime.time(21, 30)},
- {
- "isbn": "013790395",
- "oldest_store_opening": datetime.time(23, 59, 59),
- },
- {"isbn": "067232959", "oldest_store_opening": datetime.time(now.hour)},
- {"isbn": "155860191", "oldest_store_opening": datetime.time(21, 30)},
- {
- "isbn": "159059725",
- "oldest_store_opening": datetime.time(23, 59, 59),
- },
- {"isbn": "159059996", "oldest_store_opening": datetime.time(21, 30)},
- ],
- )
- def test_aggregation_default_using_date_from_python(self):
- expr = Min("book__pubdate", default=datetime.date(1970, 1, 1))
- if connection.vendor == "mysql":
- # Workaround for #30224 for MySQL & MariaDB.
- expr.default = Cast(expr.default, DateField())
- queryset = Publisher.objects.annotate(earliest_pubdate=expr).order_by("name")
- self.assertSequenceEqual(
- queryset.values("name", "earliest_pubdate"),
- [
- {"name": "Apress", "earliest_pubdate": datetime.date(2007, 12, 6)},
- {
- "name": "Jonno's House of Books",
- "earliest_pubdate": datetime.date(1970, 1, 1),
- },
- {
- "name": "Morgan Kaufmann",
- "earliest_pubdate": datetime.date(1991, 10, 15),
- },
- {
- "name": "Prentice Hall",
- "earliest_pubdate": datetime.date(1995, 1, 15),
- },
- {"name": "Sams", "earliest_pubdate": datetime.date(2008, 3, 3)},
- ],
- )
- def test_aggregation_default_using_date_from_database(self):
- now = timezone.now().astimezone(datetime.timezone.utc)
- expr = Min("book__pubdate", default=TruncDate(NowUTC()))
- queryset = Publisher.objects.annotate(earliest_pubdate=expr).order_by("name")
- self.assertSequenceEqual(
- queryset.values("name", "earliest_pubdate"),
- [
- {"name": "Apress", "earliest_pubdate": datetime.date(2007, 12, 6)},
- {"name": "Jonno's House of Books", "earliest_pubdate": now.date()},
- {
- "name": "Morgan Kaufmann",
- "earliest_pubdate": datetime.date(1991, 10, 15),
- },
- {
- "name": "Prentice Hall",
- "earliest_pubdate": datetime.date(1995, 1, 15),
- },
- {"name": "Sams", "earliest_pubdate": datetime.date(2008, 3, 3)},
- ],
- )
- def test_aggregation_default_using_datetime_from_python(self):
- expr = Min(
- "store__original_opening",
- filter=~Q(store__name="Amazon.com"),
- default=datetime.datetime(1970, 1, 1),
- )
- if connection.vendor == "mysql":
- # Workaround for #30224 for MySQL & MariaDB.
- expr.default = Cast(expr.default, DateTimeField())
- queryset = Book.objects.annotate(oldest_store_opening=expr).order_by("isbn")
- self.assertSequenceEqual(
- queryset.values("isbn", "oldest_store_opening"),
- [
- {
- "isbn": "013235613",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- {
- "isbn": "013790395",
- "oldest_store_opening": datetime.datetime(2001, 3, 15, 11, 23, 37),
- },
- {
- "isbn": "067232959",
- "oldest_store_opening": datetime.datetime(1970, 1, 1),
- },
- {
- "isbn": "155860191",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- {
- "isbn": "159059725",
- "oldest_store_opening": datetime.datetime(2001, 3, 15, 11, 23, 37),
- },
- {
- "isbn": "159059996",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- ],
- )
- def test_aggregation_default_using_datetime_from_database(self):
- now = timezone.now().astimezone(datetime.timezone.utc)
- expr = Min(
- "store__original_opening",
- filter=~Q(store__name="Amazon.com"),
- default=TruncHour(NowUTC(), output_field=DateTimeField()),
- )
- queryset = Book.objects.annotate(oldest_store_opening=expr).order_by("isbn")
- self.assertSequenceEqual(
- queryset.values("isbn", "oldest_store_opening"),
- [
- {
- "isbn": "013235613",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- {
- "isbn": "013790395",
- "oldest_store_opening": datetime.datetime(2001, 3, 15, 11, 23, 37),
- },
- {
- "isbn": "067232959",
- "oldest_store_opening": now.replace(
- minute=0, second=0, microsecond=0, tzinfo=None
- ),
- },
- {
- "isbn": "155860191",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- {
- "isbn": "159059725",
- "oldest_store_opening": datetime.datetime(2001, 3, 15, 11, 23, 37),
- },
- {
- "isbn": "159059996",
- "oldest_store_opening": datetime.datetime(1945, 4, 25, 16, 24, 14),
- },
- ],
- )
- def test_aggregation_default_using_duration_from_python(self):
- result = Publisher.objects.filter(num_awards__gt=3).aggregate(
- value=Sum("duration", default=datetime.timedelta(0)),
- )
- self.assertEqual(result["value"], datetime.timedelta(0))
- def test_aggregation_default_using_duration_from_database(self):
- result = Publisher.objects.filter(num_awards__gt=3).aggregate(
- value=Sum("duration", default=Now() - Now()),
- )
- self.assertEqual(result["value"], datetime.timedelta(0))
- def test_aggregation_default_using_decimal_from_python(self):
- result = Book.objects.filter(rating__lt=3.0).aggregate(
- value=Sum("price", default=Decimal("0.00")),
- )
- self.assertEqual(result["value"], Decimal("0.00"))
- def test_aggregation_default_using_decimal_from_database(self):
- result = Book.objects.filter(rating__lt=3.0).aggregate(
- value=Sum("price", default=Pi()),
- )
- self.assertAlmostEqual(result["value"], Decimal.from_float(math.pi), places=6)
- def test_aggregation_default_passed_another_aggregate(self):
- result = Book.objects.aggregate(
- value=Sum("price", filter=Q(rating__lt=3.0), default=Avg("pages") / 10.0),
- )
- self.assertAlmostEqual(result["value"], Decimal("61.72"), places=2)
- def test_aggregation_default_after_annotation(self):
- result = Publisher.objects.annotate(
- double_num_awards=F("num_awards") * 2,
- ).aggregate(value=Sum("double_num_awards", default=0))
- self.assertEqual(result["value"], 40)
- def test_aggregation_default_not_in_aggregate(self):
- result = Publisher.objects.annotate(
- avg_rating=Avg("book__rating", default=2.5),
- ).aggregate(Sum("num_awards"))
- self.assertEqual(result["num_awards__sum"], 20)
- def test_exists_none_with_aggregate(self):
- qs = Book.objects.annotate(
- count=Count("id"),
- exists=Exists(Author.objects.none()),
- )
- self.assertEqual(len(qs), 6)
- def test_alias_sql_injection(self):
- crafted_alias = """injected_name" from "aggregation_author"; --"""
- msg = (
- "Column aliases cannot contain whitespace characters, quotation marks, "
- "semicolons, or SQL comments."
- )
- with self.assertRaisesMessage(ValueError, msg):
- Author.objects.aggregate(**{crafted_alias: Avg("age")})
- def test_exists_extra_where_with_aggregate(self):
- qs = Book.objects.annotate(
- count=Count("id"),
- exists=Exists(Author.objects.extra(where=["1=0"])),
- )
- self.assertEqual(len(qs), 6)
- def test_multiple_aggregate_references(self):
- aggregates = Author.objects.aggregate(
- total_books=Count("book"),
- coalesced_total_books=Coalesce("total_books", 0),
- )
- self.assertEqual(
- aggregates,
- {
- "total_books": 10,
- "coalesced_total_books": 10,
- },
- )
- def test_group_by_reference_subquery(self):
- author_qs = (
- Author.objects.annotate(publisher_id=F("book__publisher"))
- .values("publisher_id")
- .annotate(cnt=Count("*"))
- .values("publisher_id")
- )
- qs = Publisher.objects.filter(pk__in=author_qs)
- self.assertCountEqual(qs, [self.p1, self.p2, self.p3, self.p4])
- def test_having_with_no_group_by(self):
- author_qs = (
- Author.objects.values(static_value=Value("static-value"))
- .annotate(sum=Sum("age"))
- .filter(sum__gte=0)
- .values_list("sum", flat=True)
- )
- self.assertEqual(list(author_qs), [337])
- def test_aggregate_arity(self):
- funcs_with_inherited_constructors = [Avg, Max, Min, Sum]
- msg = "takes exactly 1 argument (2 given)"
- for function in funcs_with_inherited_constructors:
- with (
- self.subTest(function=function),
- self.assertRaisesMessage(TypeError, msg),
- ):
- function(Value(1), Value(2))
- funcs_with_custom_constructors = [Count, StdDev, Variance]
- for function in funcs_with_custom_constructors:
- with self.subTest(function=function):
- # Extra arguments are rejected via the constructor.
- with self.assertRaises(TypeError):
- function(Value(1), True, Value(2))
- # If the constructor is skipped, the arity check runs.
- func_instance = function(Value(1), True)
- with self.assertRaisesMessage(TypeError, msg):
- super(function, func_instance).__init__(Value(1), Value(2))
- class AggregateAnnotationPruningTests(TestCase):
- @classmethod
- def setUpTestData(cls):
- cls.a1 = Author.objects.create(age=1)
- cls.a2 = Author.objects.create(age=2)
- cls.p1 = Publisher.objects.create(num_awards=1)
- cls.p2 = Publisher.objects.create(num_awards=0)
- cls.b1 = Book.objects.create(
- name="b1",
- publisher=cls.p1,
- pages=100,
- rating=4.5,
- price=10,
- contact=cls.a1,
- pubdate=datetime.date.today(),
- )
- cls.b1.authors.add(cls.a1)
- cls.b2 = Book.objects.create(
- name="b2",
- publisher=cls.p2,
- pages=1000,
- rating=3.2,
- price=50,
- contact=cls.a2,
- pubdate=datetime.date.today(),
- )
- cls.b2.authors.add(cls.a1, cls.a2)
- def test_unused_aliased_aggregate_pruned(self):
- with CaptureQueriesContext(connection) as ctx:
- cnt = Book.objects.alias(
- authors_count=Count("authors"),
- ).count()
- self.assertEqual(cnt, 2)
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
- self.assertNotIn("authors_count", sql)
- def test_unused_aliased_aggregate_and_annotation_reverse_fk(self):
- Book.objects.create(
- name="b3",
- publisher=self.p2,
- pages=1000,
- rating=4.2,
- price=50,
- contact=self.a2,
- pubdate=datetime.date.today(),
- )
- qs = Publisher.objects.annotate(
- total_pages=Sum("book__pages"),
- good_book=Case(
- When(book__rating__gt=4.0, then=Value(True)),
- default=Value(False),
- ),
- )
- self.assertEqual(qs.count(), 3)
- def test_unused_aliased_aggregate_and_annotation_reverse_fk_grouped(self):
- Book.objects.create(
- name="b3",
- publisher=self.p2,
- pages=1000,
- rating=4.2,
- price=50,
- contact=self.a2,
- pubdate=datetime.date.today(),
- )
- qs = (
- Publisher.objects.values("id", "name")
- .annotate(total_pages=Sum("book__pages"))
- .annotate(
- good_book=Case(
- When(book__rating__gt=4.0, then=Value(True)),
- default=Value(False),
- )
- )
- )
- self.assertEqual(qs.count(), 3)
- def test_non_aggregate_annotation_pruned(self):
- with CaptureQueriesContext(connection) as ctx:
- Book.objects.annotate(
- name_lower=Lower("name"),
- ).count()
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 1, "No subquery wrapping required")
- self.assertNotIn("name_lower", sql)
- def test_unreferenced_aggregate_annotation_pruned(self):
- with CaptureQueriesContext(connection) as ctx:
- cnt = Book.objects.annotate(
- authors_count=Count("authors"),
- ).count()
- self.assertEqual(cnt, 2)
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
- self.assertNotIn("authors_count", sql)
- def test_referenced_aggregate_annotation_kept(self):
- with CaptureQueriesContext(connection) as ctx:
- Book.objects.annotate(
- authors_count=Count("authors"),
- ).aggregate(Avg("authors_count"))
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
- self.assertEqual(sql.count("authors_count"), 2)
- def test_referenced_group_by_annotation_kept(self):
- queryset = Book.objects.values(pages_mod=Mod("pages", 10)).annotate(
- mod_count=Count("*")
- )
- self.assertEqual(queryset.count(), 1)
- def test_referenced_subquery_requires_wrapping(self):
- total_books_qs = (
- Author.book_set.through.objects.values("author")
- .filter(author=OuterRef("pk"))
- .annotate(total=Count("book"))
- )
- with self.assertNumQueries(1) as ctx:
- aggregate = (
- Author.objects.annotate(
- total_books=Subquery(total_books_qs.values("total"))
- )
- .values("pk", "total_books")
- .aggregate(
- sum_total_books=Sum("total_books"),
- )
- )
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
- self.assertEqual(aggregate, {"sum_total_books": 3})
- def test_referenced_composed_subquery_requires_wrapping(self):
- total_books_qs = (
- Author.book_set.through.objects.values("author")
- .filter(author=OuterRef("pk"))
- .annotate(total=Count("book"))
- )
- with self.assertNumQueries(1) as ctx:
- aggregate = (
- Author.objects.annotate(
- total_books=Subquery(total_books_qs.values("total")),
- total_books_ref=F("total_books") / 1,
- )
- .values("pk", "total_books_ref")
- .aggregate(
- sum_total_books=Sum("total_books_ref"),
- )
- )
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 3, "Subquery wrapping required")
- self.assertEqual(aggregate, {"sum_total_books": 3})
- @skipUnlessDBFeature("supports_over_clause")
- def test_referenced_window_requires_wrapping(self):
- total_books_qs = Book.objects.annotate(
- avg_publisher_pages=Coalesce(
- Window(Avg("pages"), partition_by=F("publisher")),
- 0.0,
- )
- )
- with self.assertNumQueries(1) as ctx:
- aggregate = total_books_qs.aggregate(
- sum_avg_publisher_pages=Sum("avg_publisher_pages"),
- books_count=Count("id"),
- )
- sql = ctx.captured_queries[0]["sql"].lower()
- self.assertEqual(sql.count("select"), 2, "Subquery wrapping required")
- self.assertEqual(
- aggregate,
- {"sum_avg_publisher_pages": 1100.0, "books_count": 2},
- )
- def test_aggregate_reference_lookup_rhs(self):
- aggregates = Author.objects.annotate(
- max_book_author=Max("book__authors"),
- ).aggregate(count=Count("id", filter=Q(id=F("max_book_author"))))
- self.assertEqual(aggregates, {"count": 1})
- def test_aggregate_reference_lookup_rhs_iter(self):
- aggregates = Author.objects.annotate(
- max_book_author=Max("book__authors"),
- ).aggregate(count=Count("id", filter=Q(id__in=[F("max_book_author"), 0])))
- self.assertEqual(aggregates, {"count": 1})
- @skipUnlessDBFeature("supports_select_union")
- def test_aggregate_combined_queries(self):
- # Combined queries could have members in their values select mask while
- # others have them in their annotation mask which makes annotation
- # pruning complex to implement hence why it's not implemented.
- qs = Author.objects.values(
- "age",
- other=Value(0),
- ).union(
- Book.objects.values(
- age=Value(0),
- other=Value(0),
- )
- )
- self.assertEqual(qs.count(), 3)
|