-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
9 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
potash
Author
Contributor
|
||
yield c.label("{}{}{}{}".format( | ||
prefix, c0.name, self.operator, c1.name)) | ||
|
||
# TODO: floordiv and truediv for py3 | ||
def __add__(self, other): | ||
|
@@ -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): | ||
|
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.