Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jun 1, 2022
1 parent 2fff8e8 commit ed43b79
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/docs/installation/sql-templating.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Once you have the ID you can query it as if it were a table:
SELECT * FROM {{ dataset(42) }} LIMIT 10
```

IF you want to select the metric definitions as well, in addition to the columns, you need to pass an additional keyword argument:
If you want to select the metric definitions as well, in addition to the columns, you need to pass an additional keyword argument:

```
SELECT * FROM {{ dataset(42, include_metrics=True) }} LIMIT 10
Expand All @@ -295,5 +295,5 @@ SELECT * FROM {{ dataset(42, include_metrics=True) }} LIMIT 10
Since metrics are aggregations, the resulting SQL expression will be grouped by all non-metric columns. You can specify a subset of columns to group by instead:

```
SELECT * FROM {{ dataset(42, include_metrics=True, groupby=["ds", "category"]) }} LIMIT 10
SELECT * FROM {{ dataset(42, include_metrics=True, columns=["ds", "category"]) }} LIMIT 10
```
5 changes: 2 additions & 3 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ def get_template_processor(
def dataset_macro(
dataset_id: int,
include_metrics: bool = False,
groupby: Optional[List[str]] = None,
columns: Optional[List[str]] = None,
) -> str:
"""
Given a dataset ID, return the SQL that represents it.
Expand All @@ -624,14 +624,13 @@ def dataset_macro(
if not dataset:
raise DatasetNotFoundError(f"Dataset {dataset_id} not found!")

columns = [column.column_name for column in dataset.columns]
columns = columns or [column.column_name for column in dataset.columns]
metrics = [metric.metric_name for metric in dataset.metrics]
query_obj = {
"is_timeseries": False,
"filter": [],
"metrics": metrics if include_metrics else None,
"columns": columns,
"groupby": groupby,
}
sqla_query = dataset.get_query_str_extended(query_obj)
sql = sqla_query.sql
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/jinja_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_dataset_macro(mocker: MockFixture, app_context: None) -> None:
)

assert (
dataset_macro(1, include_metrics=True, groupby=["ds"])
dataset_macro(1, include_metrics=True, columns=["ds"])
== """(SELECT ds AS ds,
COUNT(*) AS cnt
FROM my_schema.old_dataset
Expand Down

0 comments on commit ed43b79

Please sign in to comment.