Skip to content

Commit

Permalink
[sqllab] Fix sql expression bug with count distinct metrics (#1805)
Browse files Browse the repository at this point in the history
* Return error message when metrics are not valid

* Fix bug with count distinct expression

* Fixed codeclimate issue
  • Loading branch information
vera-liu committed Dec 15, 2016
1 parent 84a3b55 commit 638f27c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
4 changes: 3 additions & 1 deletion superset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down

0 comments on commit 638f27c

Please sign in to comment.