| 550 | 550 | Perform a COUNT() query using the current filter constraints. |
| 551 | 551 | """ |
| 552 | 552 | obj = self.clone() |
| 553 | # Strip unused annotations for count queries | |
| 554 | obj._strip_unused_annotations_for_count() | |
| 553 | 555 | obj.add_annotation(Count("*"), alias="__count", is_summary=True) |
| 554 | 556 | return obj.get_aggregation(using, ["__count"])["__count"] |
| 555 | 557 | |
| 558 | def _strip_unused_annotations_for_count(self): | |
| 559 | """ | |
| 560 | Strip annotations that are not referenced by filters, other annotations, or ordering. | |
| 561 | This optimizes count queries by removing unnecessary computations. | |
| 562 | """ | |
| 563 | if not self.annotations: | |
| 564 | return | |
| 565 | ||
| 566 | # Get all annotation aliases | |
| 567 | annotation_aliases = set(self.annotations.keys()) | |
| 568 | ||
| 569 | # Check which annotations are referenced by filters | |
| 570 | referenced_annotations = set() | |
| 571 | if self.where: | |
| 572 | referenced_annotations.update(self._get_annotations_in_where(self.where)) | |
| 573 | ||
| 574 | # Check which annotations are referenced by ordering | |
| 575 | if self.order_by: | |
| 576 | for order_expr in self.order_by: | |
| 577 | if hasattr(order_expr, 'name') and order_expr.name in annotation_aliases: | |
| 578 | referenced_annotations.add(order_expr.name) | |
| 579 | ||
| 580 | # Check which annotations are referenced by other annotations | |
| 581 | for alias, annotation in self.annotations.items(): | |
| 582 | if hasattr(annotation, 'get_source_expressions'): | |
| 583 | for source_expr in annotation.get_source_expressions(): | |
| 584 | if hasattr(source_expr, 'name') and source_expr.name in annotation_aliases: | |
| 585 | referenced_annotations.add(source_expr.name) | |
| 586 | ||
| 587 | # Remove unused annotations | |
| 588 | unused_annotations = annotation_aliases - referenced_annotations | |
| 589 | for alias in unused_annotations: | |
| 590 | del self.annotations[alias] | |
| 591 | if alias in self.annotation_select: | |
| 592 | del self.annotation_select[alias] | |
| 593 | ||
| 594 | def _get_annotations_in_where(self, node): | |
| 595 | """ | |
| 596 | Recursively find all annotation references in a where node. | |
| 597 | """ | |
| 598 | annotations = set() | |
| 599 | if hasattr(node, 'children'): | |
| 600 | for child in node.children: | |
| 601 | annotations.update(self._get_annotations_in_where(child)) | |
| 602 | if hasattr(node, 'lhs'): | |
| 603 | if hasattr(node.lhs, 'target') and hasattr(node.lhs.target, 'annotation'): | |
| 604 | annotations.add(node.lhs.target.annotation) | |
| 605 | elif hasattr(node.lhs, 'name') and node.lhs.name in self.annotations: | |
| 606 | annotations.add(node.lhs.name) | |
| 607 | return annotations | |
| 608 | ||
| 556 | 609 | def has_filters(self): |
| 557 | 610 | return self.where |
| 558 | 611 |
| Test Name | Status |
|---|---|
test_unreferenced_aggregate_annotation_pruned (aggregation.tests.AggregateAnnotationPruningTests) | Fail |
Subquery annotations must be included in the GROUP BY if they use | Fail |
test_non_aggregate_annotation_pruned (aggregation.tests.AggregateAnnotationPruningTests) | Pass |
test_unused_aliased_aggregate_pruned (aggregation.tests.AggregateAnnotationPruningTests) | Pass |
test_referenced_aggregate_annotation_kept (aggregation.tests.AggregateAnnotationPruningTests) | Pass |
test_add_implementation (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_alias (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_in_order_by (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_join_transform (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_multi_join (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_over_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_over_complex_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_aggregate_transform (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_after_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_compound_expression (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_expression (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_group_by (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_integer (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_not_in_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_passed_another_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_unset (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_unsupported_by_count (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_date_from_database (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_date_from_python (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_datetime_from_database (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_datetime_from_python (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_decimal_from_database (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_decimal_from_python (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_duration_from_database (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_duration_from_python (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_time_from_database (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_using_time_from_python (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_default_zero (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_exists_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_exists_multivalued_outeref (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_expressions (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_filter_exists (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_nested_subquery_outerref (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_order_by_not_selected_annotation_values (aggregation.tests.AggregateTestCase) | Pass |
Random() is not included in the GROUP BY when used for ordering. | Pass |
Subquery annotations are excluded from the GROUP BY if they are | Pass |
test_aggregation_subquery_annotation_exists (aggregation.tests.AggregateTestCase) | Pass |
test_aggregation_subquery_annotation_related_field (aggregation.tests.AggregateTestCase) | Pass |
Subquery annotations and external aliases are excluded from the GROUP | Pass |
test_aggregation_subquery_annotation_values_collision (aggregation.tests.AggregateTestCase) | Pass |
test_alias_sql_injection (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_basic (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_defer (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_defer_select_related (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_m2m (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_ordering (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_over_annotate (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_values (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_values_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_annotate_values_list (aggregation.tests.AggregateTestCase) | Pass |
test_annotated_aggregate_over_annotated_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_annotation_expressions (aggregation.tests.AggregateTestCase) | Pass |
test_arguments_must_be_expressions (aggregation.tests.AggregateTestCase) | Pass |
test_avg_decimal_field (aggregation.tests.AggregateTestCase) | Pass |
test_avg_duration_field (aggregation.tests.AggregateTestCase) | Pass |
test_backwards_m2m_annotate (aggregation.tests.AggregateTestCase) | Pass |
test_coalesced_empty_result_set (aggregation.tests.AggregateTestCase) | Pass |
test_combine_different_types (aggregation.tests.AggregateTestCase) | Pass |
test_complex_aggregations_require_kwarg (aggregation.tests.AggregateTestCase) | Pass |
test_complex_values_aggregation (aggregation.tests.AggregateTestCase) | Pass |
test_count (aggregation.tests.AggregateTestCase) | Pass |
test_count_distinct_expression (aggregation.tests.AggregateTestCase) | Pass |
test_count_star (aggregation.tests.AggregateTestCase) | Pass |
.dates() returns a distinct set of dates when applied to a | Pass |
test_decimal_max_digits_has_no_effect (aggregation.tests.AggregateTestCase) | Pass |
test_distinct_on_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_empty_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_empty_result_optimization (aggregation.tests.AggregateTestCase) | Pass |
test_even_more_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_exists_extra_where_with_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_exists_none_with_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_expression_on_aggregation (aggregation.tests.AggregateTestCase) | Pass |
test_filter_aggregate (aggregation.tests.AggregateTestCase) | Pass |
Filtering against an aggregate requires the usage of the HAVING clause. | Pass |
test_filtering (aggregation.tests.AggregateTestCase) | Pass |
test_fkey_aggregate (aggregation.tests.AggregateTestCase) | Pass |
Exists annotations are included in the GROUP BY if they are | Pass |
Subquery annotations are included in the GROUP BY if they are | Pass |
An annotation included in values() before an aggregate should be | Pass |
test_more_aggregation (aggregation.tests.AggregateTestCase) | Pass |
test_multi_arg_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_multiple_aggregates (aggregation.tests.AggregateTestCase) | Pass |
An annotation not included in values() before an aggregate should be | Pass |
test_nonaggregate_aggregation_throws (aggregation.tests.AggregateTestCase) | Pass |
test_nonfield_annotation (aggregation.tests.AggregateTestCase) | Pass |
test_order_of_precedence (aggregation.tests.AggregateTestCase) | Pass |
test_related_aggregate (aggregation.tests.AggregateTestCase) | Pass |
test_reverse_fkey_annotate (aggregation.tests.AggregateTestCase) | Pass |
test_single_aggregate (aggregation.tests.AggregateTestCase) | Pass |
Sum on a distinct() QuerySet should aggregate only the distinct items. | Pass |
test_sum_duration_field (aggregation.tests.AggregateTestCase) | Pass |
Subqueries do not needlessly contain ORDER BY, SELECT FOR UPDATE or | Pass |
Aggregation over sliced queryset works correctly. | Pass |
Doing exclude() on a foreign model after annotate() doesn't crash. | Pass |
test_values_aggregation (aggregation.tests.AggregateTestCase) | Pass |
test_values_annotation_with_expression (aggregation.tests.AggregateTestCase) | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
