Skip to content

Commit

Permalink
syntax, when
Browse files Browse the repository at this point in the history
  • Loading branch information
potash committed Nov 17, 2016
1 parent db2f8ae commit 3f35a15
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions collate/collate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,23 @@ def to_sql_name(name):


class AggregateExpression(object):
def __init__(self, aggregates, operator):
def __init__(self, aggregates, operator, cast=None):
self.aggregates = aggregates
self.operator = operator
self.cast = cast if cast else ""

def get_columns(self, when=None, prefix=None):
if prefix is None:
prefix = ""

columns0 = self.aggregates[0].get_columns()
columns1 = self.aggregates[1].get_columns()
columns0 = self.aggregates[0].get_columns(when)
columns1 = self.aggregates[1].get_columns(when)

for c0, c1 in product(columns0, columns1):
c = ex.literal_column("({} {} {})".format(c0,self.operator,c1))\
.label("{}{}{}{}".format(prefix, c0.name, self.operator, c1.name))
yield c
c = ex.literal_column("({}{} {} {})".format(
c0, self.cast, self.operator, c1))

This comment has been minimized.

Copy link
@mbauman

mbauman Jan 5, 2017

Member

I found myself wanting arithmetic on Aggregates, so I was curious what you had done here. Am I correct in understanding that this is an "inner" arithmetic? That is, it runs per row? It's not clear to me that is the only choice — when I wanted to reach for arithmetic, I wanted the "outer" version — that is, it would effectively perform the arithmetic after the aggregation functions were run.

E.g., I want something like Aggregate("event == 'ems'", 'or') & Aggregate("event == 'booking'", 'or') to represent someone who has had both a EMS and booking event within a given time window.

This comment has been minimized.

Copy link
@potash

potash Jan 5, 2017

Author Contributor

No it's arithmetic on the level of aggregates, so what you want. Though I didn't overload the & or | operators, would be easy. Also I should change the naming to use "times", "per", "plus", "minus", "and", "or" instead of the operator symbol.

(By the way or isn't an aggregate function in postgres but bool_or is.)

yield c.label("{}{}{}{}".format(
prefix, c0.name, self.operator, c1.name))

# TODO: floordiv and truediv for py3
def __add__(self, other):
Expand All @@ -67,8 +69,7 @@ def __mul__(self, other):
return AggregateExpression([self, other], "*")

def __div__(self, other):
return AggregateExpression([self, other], "/")

return AggregateExpression([self, other], "/", "*1.0")


class Aggregate(AggregateExpression):
Expand Down

0 comments on commit 3f35a15

Please sign in to comment.