Skip to content

Commit

Permalink
Allow MetricsControl to aggregate on a column with an expression
Browse files Browse the repository at this point in the history
  • Loading branch information
michellethomas committed May 16, 2018
1 parent 2c5200a commit 4ea337c
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions superset/connectors/sqla/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,24 @@ def get_from_clause(self, template_processor=None, db_engine_spec=None):
return TextAsFrom(sa.text(from_sql), []).alias('expr_qry')
return self.get_sqla_table()

def adhoc_metric_to_sa(self, metric):
def adhoc_metric_to_sa(self, metric, cols):
"""
Turn an adhoc metric into a sqlalchemy column.
:param dict metric: Adhoc metric definition
:param dict cols: Columns for the current table
:returns: The metric defined as a sqlalchemy column
:rtype: sqlalchemy.sql.column
"""
expressionType = metric.get('expressionType')
if expressionType == utils.ADHOC_METRIC_EXPRESSION_TYPES['SIMPLE']:
sa_column = column(metric.get('column').get('column_name'))
column_name = metric.get('column').get('column_name')
sa_column = column(column_name)
table_column = cols.get(column_name)

if table_column:
sa_column = table_column.sqla_col

sa_metric = self.sqla_aggregations[metric.get('aggregate')](sa_column)
sa_metric = sa_metric.label(metric.get('label'))
return sa_metric
Expand Down Expand Up @@ -518,7 +532,7 @@ def get_sqla_query( # sqla
metrics_exprs = []
for m in metrics:
if utils.is_adhoc_metric(m):
metrics_exprs.append(self.adhoc_metric_to_sa(m))
metrics_exprs.append(self.adhoc_metric_to_sa(m, cols))
elif m in metrics_dict:
metrics_exprs.append(metrics_dict.get(m).sqla_col)
else:
Expand Down

0 comments on commit 4ea337c

Please sign in to comment.