Skip to content

Commit

Permalink
Adding custom HAVING clause
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Nov 24, 2015
1 parent de52449 commit 3cc5db5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion panoramix/forms.py
Expand Up @@ -137,6 +137,7 @@ def __init__(self, viz):
default=default_metric,
choices=datasource.metrics_combo),
'where': TextField('Custom WHERE clause', default=''),
'having': TextField('Custom HAVING clause', default=''),
'compare_lag': TextField('Comparison Period Lag',
description="Based on granularity, number of time periods to compare against"),
'compare_suffix': TextField('Comparison suffix',
Expand Down Expand Up @@ -264,8 +265,9 @@ class QueryForm(OmgWtForm):

# datasource type specific form elements
if datasource.__class__.__name__ == 'SqlaTable':
QueryForm.field_order += ['where']
QueryForm.field_order += ['where', 'having']
setattr(QueryForm, 'where', px_form_fields['where'])
setattr(QueryForm, 'having', px_form_fields['having'])

if 'granularity' in viz.form_fields:
setattr(
Expand Down
4 changes: 4 additions & 0 deletions panoramix/models.py
Expand Up @@ -435,6 +435,7 @@ def query(
if inner_to_dttm:
inner_time_filter[1] = timestamp <= inner_to_dttm.isoformat()
where_clause_and = []
having_clause_and = []
for col, op, eq in filter:
col_obj = cols[col]
if op in ('in', 'not in'):
Expand All @@ -449,7 +450,10 @@ def query(
where_clause_and.append(cond)
if extras and 'where' in extras:
where_clause_and += [text(extras['where'])]
if extras and 'having' in extras:
having_clause_and += [text(extras['having'])]
qry = qry.where(and_(*(time_filter + where_clause_and)))
qry = qry.having(and_(*having_clause_and))
qry = qry.order_by(desc(main_metric_expr))
qry = qry.limit(row_limit)

Expand Down
3 changes: 2 additions & 1 deletion panoramix/viz.py
Expand Up @@ -159,7 +159,8 @@ def query_obj(self):
# extras are used to query elements specific to a datasource type
# for instance the extra where clause that applies only to Tables
extras = {
'where': form_data.get("where", '')
'where': form_data.get("where", ''),
'having': form_data.get("having", ''),
}
d = {
'granularity': granularity,
Expand Down

0 comments on commit 3cc5db5

Please sign in to comment.