Opened 8 years ago

Closed 8 years ago

#26458 closed Cleanup/optimization (fixed)

The Avg aggregate should only automatically resolves its output field to FloatField on numeric sources

Reported by: Simon Charette Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Before #24649 was fixed it was simply not possible to use the Avg function on non-numeric sources. The *good enough* solution was to allow use of the output_field kwarg and document it should be used when averaging DurationField.

As DurationField uses different underlying datatype for its storage (bigint, interval, ...) averaging on such fields might require or not an explicit output_field and can be a source of confusion.

As the SQL AVG function only defaults to using floating value datatype as a return value when numeric input is used I suggest adjusting the Avg expression's output_field resolution to only use FloatField for default when its source field is an instance of one of Django's numerical field classes. This would lift the requirement of explicitly specifying an output_field when averaging DurationField on PostgreSQL.

Change History (4)

comment:2 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Tim Graham, 8 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Simon Charette <charettes@…>, 8 years ago

Resolution: fixed
Status: newclosed

In a6074e89:

Fixed #26458 -- Based Avg's default output_field resolution on its source field type.

Thanks Tim for the review and Josh for the input.

Note: See TracTickets for help on using tickets.
Back to Top