Skip to content

Commit

Permalink
Add support for SORTOP clause for AGGREGATEs.
Browse files Browse the repository at this point in the history
 * pyrseas/dbobject/function.py (Aggregate.create): Reorganize all
   optional clauses and add SORTOP.  (ProcDict.query): Retrieve
   aggsortop.  (ProcDict._from_catalog): Remove sortop if not used.
 * tests/dbobject/test_function.py: Add test for SORTOP.
  • Loading branch information
jmafc committed Jan 6, 2012
1 parent a65b812 commit 38f8bbf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pyrseas/dbobject/function.py
Expand Up @@ -151,16 +151,19 @@ def create(self):
:return: SQL statements
"""
stmts = []
ffunc = cond = ''
opt_clauses = []
if hasattr(self, 'finalfunc'):
ffname = self.finalfunc[:self.finalfunc.index('(')]
ffunc = ",\n FINALFUNC = %s" % (ffname)
opt_clauses.append("FINALFUNC = %s" % ffname)
if hasattr(self, 'initcond'):
cond = ",\n INITCOND = '%s'" % (self.initcond)
opt_clauses.append("INITCOND = '%s'" % self.initcond)
if hasattr(self, 'sortop'):
opt_clauses.append("SORTOP = %s" % self.sortop)
stmts.append("CREATE AGGREGATE %s(%s) (\n SFUNC = %s,"
"\n STYPE = %s%s%s)" % (
self.qualname(),
self.arguments, self.sfunc, self.stype, ffunc, cond))
self.arguments, self.sfunc, self.stype,
opt_clauses and ',\n ' or '', ',\n '.join(opt_clauses)))
if hasattr(self, 'description'):
stmts.append(self.comment())
return stmts
Expand All @@ -181,7 +184,7 @@ class ProcDict(DbObjectDict):
aggtransfn::regprocedure AS sfunc,
aggtranstype::regtype AS stype,
aggfinalfn::regprocedure AS finalfunc,
agginitval AS initcond,
agginitval AS initcond, aggsortop::regoper AS sortop,
obj_description(p.oid, 'pg_proc') AS description,
prorows::integer AS rows
FROM pg_proc p
Expand All @@ -203,6 +206,8 @@ def _from_catalog(self):
del proc.cost
if proc.finalfunc == '-':
del proc.finalfunc
if proc.sortop == '0':
del proc.sortop
self[(sch, prc, arg)] = Aggregate(**proc.__dict__)
else:
self[(sch, prc, arg)] = Function(**proc.__dict__)
Expand Down
11 changes: 11 additions & 0 deletions tests/dbobject/test_function.py
Expand Up @@ -330,6 +330,17 @@ def test_map_aggregate_init_final(self):
self.assertEqual(dbmap['schema public']['aggregate a1(integer)'],
expmap)

def test_map_aggregate_sortop(self):
"Map an aggregate with a SORTOP"
self.db.execute(CREATE_STMT2)
expmap = {'sfunc': 'f1(integer,integer)', 'stype': 'integer',
'sortop': 'pg_catalog.>'}
dbmap = self.db.execute_and_map(
"CREATE AGGREGATE a1 (integer) (SFUNC = f1, STYPE = integer, "
"SORTOP = >)")
self.assertEqual(dbmap['schema public']['aggregate a1(integer)'],
expmap)


class AggregateToSqlTestCase(PyrseasTestCase):
"""Test SQL generation from input aggregates"""
Expand Down

0 comments on commit 38f8bbf

Please sign in to comment.