func.sum(), being a known generic function in sql/functions.py, by default will denote the return value as having the same SQL type as the value that was passed in. The BigInteger type object though doesn't do any python-side processing right now, its assumed DBAPIs can handle sending an int. In this case psycopg2 DBAPI isn't doing that for us. You can force it to int using a custom type:
class CoerceToInt(TypeDecorator): impl = BigInteger def process_result_value(self, value, dialect): if value is not None: value = int(value) return value func.sum(somefield, type_=CoerceToInt) Or you could just call int() on the result. For the date functions, SQLAlchemy doesn't know anything about those so you're getting what psycopg2 returns. If you think psycopg2 should do something different you can ask on their list. On Sep 26, 2011, at 7:04 AM, sector119 wrote: > Hello. > > Why func.sum(some_table.c.bigintfield) returns Decimal ? > documents_table.c.sum has BigInteger type (postgresql table field has > bigint type) > > the same issue I got with postgresql date_part func, when I extract > year or month I got Decimal result, not int.... > >>>> s = select([func.sum(documents_table.c.sum).label('payments_sum'), >>>> func.sum(documents_table.c.payments).label('payments_count')]) >>>> session.execute(s).fetchall() > 2011-09-26 13:48:41,543 INFO sqlalchemy.engine.base.Engine SELECT > sum(documents.sum) AS payments_sum, sum(documents.payments) AS > payments_count > FROM documents > 2011-09-26 13:48:41,543 INFO sqlalchemy.engine.base.Engine {} > [(Decimal('51788997139'), 8853396L)] > >>>> s = select([cast(func.sum(documents_table.c.sum), >>>> BigInteger).label('payments_sum'), >>>> func.sum(documents_table.c.payments).label('payments_count')]) >>>> session.execute(s).fetchall() > 2011-09-26 13:48:56,243 INFO sqlalchemy.engine.base.Engine SELECT > CAST(sum(documents.sum) AS BIGINT) AS payments_sum, > sum(documents.payments) AS payments_count > FROM documents > 2011-09-26 13:48:56,244 INFO sqlalchemy.engine.base.Engine {} > [(51788997139L, 8853396L)] > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalchemy@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.