diff --git a/panoramix/forms.py b/panoramix/forms.py index 9190f510e..428eecdbf 100644 --- a/panoramix/forms.py +++ b/panoramix/forms.py @@ -137,6 +137,7 @@ class FormFactory(object): 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', @@ -264,8 +265,9 @@ class FormFactory(object): # 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( diff --git a/panoramix/models.py b/panoramix/models.py index fbf8d56fb..336e55f75 100644 --- a/panoramix/models.py +++ b/panoramix/models.py @@ -435,6 +435,7 @@ class SqlaTable(Model, Queryable, AuditMixinNullable): 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'): @@ -449,7 +450,10 @@ class SqlaTable(Model, Queryable, AuditMixinNullable): 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) diff --git a/panoramix/viz.py b/panoramix/viz.py index 4b918d8ff..709517440 100644 --- a/panoramix/viz.py +++ b/panoramix/viz.py @@ -159,7 +159,8 @@ class BaseViz(object): # 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,