Query.apply_sum() does not work as expected from reading the documents

Issue #665 resolved
Former user created an issue

In reading the docs on Query.apply_sum(), the description was vague, and it did not explain it's behavior. This is best explained through examples.

Host.query().sum(Host.c.cpu_speed * Host.c.num_cpus)

In short, this is a goofy benchmark used in Smolt https://hosted.fedoraproject.org/projects/smolt/ which calculates a fairly useless but fun benchmark of the systems in the database. It generates the following SQL

SELECT sum(host.cpu_speed * host.num_cp_us) FROM host

Which is precisely the desired SQL. It was expected that apply_sum() could provide the same.

In testing it with apply_sum(), i received the following error:

Host.query().apply_sum(Host.c.num_cpus) Traceback (most recent call last): File "<console>", line 1, in <module> File "/usr/lib/python2.5/site-packages/sqlalchemy/orm/query.py", line 630, in getattr raise AttributeError(key) AttributeError: apply_sum

This leads me to two conclusions

A) apply_sum() is redundant and also broken, please remove, at least from the documentation. B) apply_sum() does something different, what is it?

At any rate, Query.sum() did work, and even worked with a "Criterion" although I'm not sure that's the right word at this point. I was pleasantly surprised, and this makes our code very very clean.

Comments (2)

  1. Former user Account Deleted
    • assigned issue to

    (original author: ged) Apply_sum should behave the same way as sum(), except that sum returns the result immediately while apply_sum returns a new query. If it doesn't it's a bug. Please post a (short) test case reproducing the problem. Please also specify which version of SQLalchemy you are using.

  2. Former user Account Deleted

    (original author: ged) Closing as it works here and there wasn't any answer to my request for more info. Feel free to reopen if you have more info on this.

  3. Log in to comment