Code

Opened 6 years ago

Closed 6 years ago

#6821 closed (duplicate)

Aggregate support for QuerySets

Reported by: nicolaslara@… Owned by: nicolas
Component: Database layer (models, ORM) Version: queryset-refactor
Severity: Keywords: aggregation
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:

Description

Add aggregate support for QuerySets according to what have been discussed in http://groups.google.com/group/django-developers/browse_thread/thread/3d4039161f6d2f5e/302b088cc816c0a6?hl=en#302b088cc816c0a6

The proposed syntax includes the two QuerySet modifiers _annotate_ and _aggregate_ to augment the objects with aggregate information and retrieve aggregate information regarding the whole QuerySet respectively.

The notation for defining the fields and aggregate functions to use would be done in filter-like syntax

Purchase.objects.all().aggregate(quantity__sum='total_adquisitions', payment__avg='mean_pay') 

with the possibility of specifying aggregates in object form for non aliased aggregates (only the simple form is intended, for a more complex approach to object aggregates: http://code.djangoproject.com/ticket/6811 ). In this case a standard alias should be generated (i.e. quantity_sum, payment_avg)

Purchase.objects.all().aggregate(Sum('quantity'), Avg('payment'))

The GROUP BY is to be done internally. To specify the fields to be grouped it could be done with values()

Purchase.objects.all().values('identifier').aggregate(quantity__sum='num_adquisiitions') 

No way to specify that only the aggregated fields should appear in the results is intended because this could be done with the modifiers proposed in #5420.

Ordering should be specified using order_by standard syntax with the aliased name.

==Extra ==

As extra things that could be implemented are:

  • Custom aggregate creation (possibly as follows)
    >>> my_own_aggregate = Aggregate( sum('field_1') * 'field2', 'default_name')
    >>> queryset.aggregate(purchases__price__custom=my_own_aggregate('alias')) 
    
  • F syntax for refering to other fields in the model.

Please refer to the discussion thread for a more in depth description

Attachments (1)

aggregate.diff (4.2 KB) - added by Nicolas Lara <nicolaslara@…> 6 years ago.
aggregate modifier 0.1

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by Nicolas Lara <nicolaslara@…>

aggregate modifier 0.1

comment:1 Changed 6 years ago by Nicolas Lara <nicolaslara@…>

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to anonymous
  • Patch needs improvement set
  • Status changed from new to assigned

Added proof-of-concept patch that implements aggregate modifier. (No Joins/Aliases ...yet)

comment:2 Changed 6 years ago by Nicolas Lara <nicolaslara@…>

  • Owner changed from anonymous to nicolas
  • Status changed from assigned to new

comment:3 Changed 6 years ago by russellm

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #3566. You don't need to open a new ticket to work on aggregates - please continue discussions on the original ticket.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.