sadpandajoe commented on code in PR #35662:
URL: https://github.com/apache/superset/pull/35662#discussion_r3940499879


##########
docs/admin_docs/configuration/sql-templating.mdx:
##########
@@ -556,14 +556,23 @@ WHERE
 
 It's possible to query physical and virtual datasets using the `dataset` 
macro. This is useful if you've defined computed columns and metrics on your 
datasets, and want to reuse the definition in adhoc SQL Lab queries.
 
-To use the macro, first you need to find the ID of the dataset. This can be 
done by going to the view showing all the datasets, hovering over the dataset 
you're interested in, and looking at its URL. For example, if the URL for a 
dataset is 
https://superset.example.org/explore/?dataset_type=table&dataset_id=42 its ID 
is 42.
+To use the macro, you can reference the dataset either by its numeric ID or by 
its name.
+
+- By ID: you need to find the dataset's ID. This can be done by going to the 
view showing all the datasets, hovering over the dataset you're interested in, 
and looking at its URL. For example, if the URL for a dataset is 
https://superset.example.org/explore/?dataset_type=table&dataset_id=42 its ID 
is 42.
+- By name: just pass the dataset’s exact name

Review Comment:
   A duplicate dataset name raises the ambiguity error, but this says an exact 
name is sufficient and only shows the unqualified call. Could the docs show how 
to pass `schema`, `catalog`, or `database_id` to select one?



##########
superset/jinja_context.py:
##########
@@ -1101,28 +1104,91 @@ def get_template_processor(
 
 
 def dataset_macro(
-    dataset_id: int,
+    dataset_id: Union[int, str],
     include_metrics: bool = False,
     columns: list[str] | None = None,
     from_dttm: datetime | None = None,
     to_dttm: datetime | None = None,
+    schema: str | None = None,
+    catalog: str | None = None,
+    database_id: Union[int, str] | None = None,
+    alias: str | None = None,
 ) -> str:
     """
-    Given a dataset ID, return the SQL that represents it.
+    Given a dataset ID or name, return the SQL that represents it.
+
+    If ``dataset_id`` is an integer, it is treated as the unique dataset ID and
+    the optional ``schema``, ``catalog`` and ``database_id`` parameters are
+    ignored.
+
+    If ``dataset_id`` is a string, it is treated as a dataset name. The 
optional
+    ``schema``, ``catalog`` and ``database_id`` parameters are used to narrow
+    down the search when provided. If multiple datasets match the provided
+    criteria, an error is raised because the dataset name is ambiguous.
 
     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.
 
-    The from_dttm and to_dttm parameters are filled in from filter values in 
explore
-    views, and we take them to make those properties available to jinja 
templates in
-    the underlying dataset.
+    The ``from_dttm`` and ``to_dttm`` parameters are filled in from filter 
values in
+    explore views, and we take them to make those properties available to jinja
+    templates in the underlying dataset.
+
+    The ``alias`` parameter allows the user to specify an explicit alias for 
the
+    returned subquery.
     """
     # pylint: disable=import-outside-toplevel
+    from sqlalchemy.orm.exc import MultipleResultsFound
+
     from superset.daos.dataset import DatasetDAO
 
-    dataset = DatasetDAO.find_by_id(dataset_id)
+    filters = {
+        key: value
+        for key, value in {
+            "database_id": database_id,
+            "catalog": catalog,
+            "schema": schema,
+        }.items()
+        if value is not None
+    }
+
+    if isinstance(dataset_id, str):
+        try:
+            dataset = DatasetDAO.get_table_by_catalog_schema_and_name(
+                table_name=dataset_id,
+                **cast(
+                    dict[str, Any],
+                    filters,
+                ),
+            )

Review Comment:
   Agreed—the name path now calls an unconfigured DAO mock, so the new test 
fails and the required unit suite stays red. Could the fixture configure that 
lookup to return the dataset?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to