Skip to content

Commit f436a46

Browse files
committed
Give up on add all occurrence counts in one query for now
1 parent 8b69bb8 commit f436a46

File tree

2 files changed

+16
-45
lines changed

2 files changed

+16
-45
lines changed

ami/main/models.py

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,46 +2165,16 @@ def with_children(self):
21652165
def with_occurrence_counts(self) -> models.QuerySet:
21662166
"""
21672167
Count the number of occurrences for a taxon and all occurrences of the taxon's children.
2168-
"""
2169-
qs = self.get_queryset()
21702168
2171-
# Subquery to get all child taxa IDs
2172-
import json
2169+
@TODO Try a recursive CTE in a raw SQL query,
2170+
or count the occurrences in a separate query and attach them to the Taxon objects.
2171+
"""
21732172

2174-
child_taxa = qs.filter(
2175-
parents_json__contains=models.Value(
2176-
json.dumps([{"id": models.OuterRef("pk")}]), output_field=models.JSONField()
2177-
)
2178-
).values("pk")
2179-
2180-
# Debugging: Log the SQL for child_taxa
2181-
logger.info(f"SQL for child_taxa: {child_taxa.query}")
2182-
2183-
# Debugging: Count and log the number of child taxa
2184-
child_count = child_taxa.count()
2185-
logger.info(f"Number of child taxa found: {child_count}")
2186-
2187-
# Debugging: Log a sample of child taxa
2188-
sample_children = list(child_taxa[:5])
2189-
logger.info(f"Sample of child taxa: {sample_children}")
2190-
2191-
# Annotate with occurrence counts
2192-
qs = qs.annotate(
2193-
occurrences_count_bulk=models.Count(
2194-
"occurrences",
2195-
filter=models.Q(
2196-
models.Q(occurrences__determination_id=models.F("pk"))
2197-
| models.Q(occurrences__determination_id__in=models.Subquery(child_taxa))
2198-
),
2199-
distinct=True,
2200-
)
2173+
raise NotImplementedError(
2174+
"Occurrence counts can not be calculated in a subquery with the current JSONField schema. "
2175+
"Fetch them per taxon."
22012176
)
22022177

2203-
# Debugging: Log the final SQL query
2204-
logger.info(f"Final SQL query: {qs.query}")
2205-
2206-
return qs
2207-
22082178

22092179
class TaxonParent(pydantic.BaseModel):
22102180
"""

ami/main/tests.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,16 @@ def _count_occurrences_recursive(taxon):
569569
def test_recursive_occurrence_count_from_manager(self):
570570
from ami.main.models import Taxon
571571

572-
taxa_with_counts = Taxon.objects.with_occurrence_counts()
573-
for taxon in taxa_with_counts:
574-
occurrence_count_total = taxon.occurrences_count_recursive()
575-
self.assertEqual(occurrence_count_total, taxon.occurrences_count)
576-
577-
for taxon in taxa_with_counts:
578-
occurrence_count_direct = taxon.occurrences.count()
579-
occurrence_count_total = taxon.occurrences_count_recursive()
580-
self.assertEqual(occurrence_count_total, occurrence_count_direct)
572+
with self.assertRaises(NotImplementedError):
573+
taxa_with_counts = Taxon.objects.with_occurrence_counts()
574+
for taxon in taxa_with_counts:
575+
occurrence_count_total = taxon.occurrences_count_recursive()
576+
self.assertEqual(occurrence_count_total, taxon.occurrences_count)
577+
578+
for taxon in taxa_with_counts:
579+
occurrence_count_direct = taxon.occurrences.count()
580+
occurrence_count_total = taxon.occurrences_count_recursive()
581+
self.assertEqual(occurrence_count_total, occurrence_count_direct)
581582

582583

583584
class TestIdentification(APITestCase):

0 commit comments

Comments
 (0)