You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/05/26 11:33:33 UTC
[GitHub] [superset] villebro commented on a diff in pull request #15241: feat: query datasets from SQL Lab
villebro commented on code in PR #15241:
URL: https://github.com/apache/superset/pull/15241#discussion_r882570402
##########
superset/jinja_context.py:
##########
@@ -602,3 +604,35 @@ def get_template_processor(
else:
template_processor = NoOpTemplateProcessor
return template_processor(database=database, table=table, query=query, **kwargs)
+
+
+def dataset_macro(
+ dataset_id: int,
+ include_metrics: bool = False,
+ groupby: Optional[List[str]] = None,
+) -> str:
+ """
+ Given a dataset ID, return the SQL that represents it.
+
+ The generated SQL includes all columns (including computed) by default. Optionally
+ the user can also request metrics to be included, and columns to group by.
+ """
+ # pylint: disable=import-outside-toplevel
+ from superset.datasets.dao import DatasetDAO
+
+ dataset = DatasetDAO.find_by_id(dataset_id)
+ if not dataset:
+ raise DatasetNotFoundError(f"Dataset {dataset_id} not found!")
+
+ columns = [column.column_name for column in dataset.columns]
+ metrics = [metric.metric_name for metric in dataset.metrics]
+ query_obj = {
+ "is_timeseries": False,
Review Comment:
nit: as we're planning on phasing `is_timeseries` out soon, this should be ok to remove:
```suggestion
```
##########
superset/jinja_context.py:
##########
@@ -602,3 +604,35 @@ def get_template_processor(
else:
template_processor = NoOpTemplateProcessor
return template_processor(database=database, table=table, query=query, **kwargs)
+
+
+def dataset_macro(
+ dataset_id: int,
+ include_metrics: bool = False,
+ groupby: Optional[List[str]] = None,
+) -> str:
+ """
+ Given a dataset ID, return the SQL that represents it.
+
+ The generated SQL includes all columns (including computed) by default. Optionally
+ the user can also request metrics to be included, and columns to group by.
+ """
+ # pylint: disable=import-outside-toplevel
+ from superset.datasets.dao import DatasetDAO
+
+ dataset = DatasetDAO.find_by_id(dataset_id)
+ if not dataset:
+ raise DatasetNotFoundError(f"Dataset {dataset_id} not found!")
+
+ columns = [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,
Review Comment:
Since `groupby` is deprecated in `QueryObject`, maybe we could just do this:
```suggestion
"columns": groupby or columns,
```
I believe it has the same effect
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org