[sqllab] Fix sql expression bug with count distinct metrics (#1805)

* Return error message when metrics are not valid

* Fix bug with count distinct expression

* Fixed codeclimate issue
This commit is contained in:
vera-liu 2016-12-14 16:09:22 -08:00 committed by GitHub
parent 84a3b55912
commit 638f27c2df
2 changed files with 14 additions and 5 deletions

View File

@ -1019,7 +1019,9 @@ class SqlaTable(Model, Queryable, AuditMixinNullable, ImportMixin):
raise Exception(_(
"Datetime column not provided as part table configuration "
"and is required by this type of chart"))
for m in metrics:
if m not in metrics_dict:
raise Exception(_("Metric '{}' is not valid".format(m)))
metrics_exprs = [metrics_dict.get(m).sqla_col for m in metrics]
timeseries_limit_metric = metrics_dict.get(timeseries_limit_metric)
timeseries_limit_metric_expr = None

View File

@ -2185,10 +2185,17 @@ class Superset(BaseSupersetView):
dims.append(col)
agg = config.get('agg')
if agg:
metrics.append(models.SqlMetric(
metric_name="{agg}__{column_name}".format(**locals()),
expression="{agg}({column_name})".format(**locals()),
))
if agg == 'count_distinct':
metrics.append(models.SqlMetric(
metric_name="{agg}__{column_name}".format(**locals()),
expression="COUNT(DISTINCT {column_name})"
.format(**locals()),
))
else:
metrics.append(models.SqlMetric(
metric_name="{agg}__{column_name}".format(**locals()),
expression="{agg}({column_name})".format(**locals()),
))
if not metrics:
metrics.append(models.SqlMetric(
metric_name="count".format(**locals()),