diff --git a/superset/models.py b/superset/models.py index 882634680a27b..cc43b00948a55 100644 --- a/superset/models.py +++ b/superset/models.py @@ -1019,7 +1019,9 @@ def query( # sqla 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 diff --git a/superset/views.py b/superset/views.py index 990524903e233..5dc9f570e248e 100755 --- a/superset/views.py +++ b/superset/views.py @@ -2185,10 +2185,17 @@ def sqllab_viz(self): 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()),