1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fixed #35586 -- Added support for set-returning database functions.

Aggregation optimization didn't account for not referenced set-returning annotations on Postgres.

Co-authored-by: Simon Charette <charette.s@gmail.com>
This commit is contained in:
Devin Cox
2024-08-09 13:56:56 -07:00
committed by Sarah Boyce
parent 228128618b
commit e03083917d
6 changed files with 54 additions and 0 deletions

View File

@@ -1,7 +1,9 @@
import datetime
from decimal import Decimal
from unittest import skipUnless
from django.core.exceptions import FieldDoesNotExist, FieldError
from django.db import connection
from django.db.models import (
BooleanField,
Case,
@@ -15,6 +17,7 @@ from django.db.models import (
FloatField,
Func,
IntegerField,
JSONField,
Max,
OuterRef,
Q,
@@ -43,6 +46,7 @@ from .models import (
Company,
DepartmentStore,
Employee,
JsonModel,
Publisher,
Store,
Ticket,
@@ -1167,6 +1171,23 @@ class NonAggregateAnnotationTestCase(TestCase):
with self.assertRaisesMessage(ValueError, msg):
Book.objects.annotate(**{crafted_alias: Value(1)})
@skipUnless(connection.vendor == "postgresql", "PostgreSQL tests")
@skipUnlessDBFeature("supports_json_field")
def test_set_returning_functions(self):
class JSONBPathQuery(Func):
function = "jsonb_path_query"
output_field = JSONField()
set_returning = True
test_model = JsonModel.objects.create(
data={"key": [{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]}, id=1
)
qs = JsonModel.objects.annotate(
table_element=JSONBPathQuery("data", Value("$.key[*]"))
).filter(pk=test_model.pk)
self.assertEqual(qs.count(), len(qs))
class AliasTests(TestCase):
@classmethod