Hello all, Since even before I saw Alex Gaynor's presentation "I hate the Django ORM" (the one with the `Sum(F("end_time") - F("start_time"))` query), the problem of complex aggregates and expressions has vexed me. So, I figured I would try to solve it.
Originally, I started this trying to pursue a solution to ticket #14030, but after I took a couple of lousy shots at it, it dawned on me that the ticket would be better resolved as a result of solving the more general case. I realized that aggregates were just a special case of expressions, and that the best solution was going to take a refactoring of Aggregate into ExpressionNode. I have uploaded my branch; it can be found here: https://github.com/NateBragg/django/tree/14030 Since this is a non-trivial change, I was hoping to open the topic for debate here, and get some feedback before proposing my solution for inclusion. Some particular points of note: * I tried to preserve as much interface as possible; I didn't know how much was considered to be more public, so generally I tried to add rather than subtract. However, I did remove a couple things - if you see something missing that shouldn't be, let me know. * Currently, I'm getting the entire test suite passed on sqlite, oracle, mysql, postgres, and postgis. I was unable to test on oracle spatial - any help with that would be appreciated. * When fields are combined, they are coerced to a common type; IntegerFields are coerced to FloatFields, which are coerced into DecimalFields as needed. Any other kinds of combinations must be of the same types. Also, when coerced to a DecimalField, the precision is limited by the original DecimalField. If this is not correct, or other coercions should be added, I'd like to correct that. * When joins are required, they tend to be LEFT OUTER; I'd like some feedback on this, as I'm not 100% sure its always the best behavior. * As the aggregates are a little more complicated, on trivial cases there is a minor reduction in performance; using djangobench, I measured somewhere between a 3% and 8% increase in runtime. * I don't have enough tests - mostly for a lack of creativity. What kind of composed aggregates and expressions would you like to see? I'll add tests for them. Thank you all, and sorry for the above being a short book. -- Nate Bragg -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.