Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: recursive metric definitions #32228

Merged
merged 1 commit into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions superset/jinja_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
import dateutil
from flask import current_app, g, has_request_context, request
from flask_babel import gettext as _
from jinja2 import DebugUndefined, Environment
from jinja2 import DebugUndefined, Environment, nodes
from jinja2.nodes import Call, Node
from jinja2.sandbox import SandboxedEnvironment
from sqlalchemy.engine.interfaces import Dialect
from sqlalchemy.sql.expression import bindparam
Expand Down Expand Up @@ -888,6 +889,26 @@ def get_dataset_id_from_context(metric_key: str) -> int:
raise SupersetTemplateException(exc_message)


def has_metric_macro(template_string: str, env: Environment) -> bool:
"""
Checks if a template string contains a metric macro.

>>> has_metric_macro("{{ metric('my_metric') }}")
True

"""
ast = env.parse(template_string)

def visit_node(node: Node) -> bool:
return (
isinstance(node, Call)
and isinstance(node.node, nodes.Name)
and node.node.name == "metric"
) or any(visit_node(child) for child in node.iter_child_nodes())

return visit_node(ast)


def metric_macro(metric_key: str, dataset_id: Optional[int] = None) -> str:
"""
Given a metric key, returns its syntax.
Expand All @@ -908,16 +929,32 @@ def metric_macro(metric_key: str, dataset_id: Optional[int] = None) -> str:
dataset = DatasetDAO.find_by_id(dataset_id)
if not dataset:
raise DatasetNotFoundError(f"Dataset ID {dataset_id} not found.")

metrics: dict[str, str] = {
metric.metric_name: metric.expression for metric in dataset.metrics
}
dataset_name = dataset.table_name
if metric := metrics.get(metric_key):
return metric
raise SupersetTemplateException(
_(
"Metric ``%(metric_name)s`` not found in %(dataset_name)s.",
metric_name=metric_key,
dataset_name=dataset_name,
if metric_key not in metrics:
raise SupersetTemplateException(
_(
"Metric ``%(metric_name)s`` not found in %(dataset_name)s.",
metric_name=metric_key,
dataset_name=dataset.table_name,
)
)
)

definition = metrics[metric_key]

env = SandboxedEnvironment(undefined=DebugUndefined)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@betodealmeida is this SandboxedEnvironment intentional?

context = {"metric": partial(safe_proxy, metric_macro)}
while has_metric_macro(definition, env):
old_definition = definition
template = env.from_string(definition)
try:
definition = template.render(context)
except RecursionError as ex:
raise SupersetTemplateException("Cyclic metric macro detected") from ex

if definition == old_definition:
break
betodealmeida marked this conversation as resolved.
Show resolved Hide resolved

return definition
93 changes: 93 additions & 0 deletions tests/unit_tests/jinja_context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,99 @@ def test_metric_macro_with_dataset_id(mocker: MockerFixture) -> None:
mock_get_form_data.assert_not_called()


def test_metric_macro_recursive(mocker: MockerFixture) -> None:
"""
Test the ``metric_macro`` when the definition is recursive.
"""
mock_g = mocker.patch("superset.jinja_context.g")
mock_g.form_data = {"datasource": {"id": 1}}
DatasetDAO = mocker.patch("superset.daos.dataset.DatasetDAO") # noqa: N806
DatasetDAO.find_by_id.return_value = SqlaTable(
table_name="test_dataset",
metrics=[
SqlMetric(metric_name="a", expression="COUNT(*)"),
SqlMetric(metric_name="b", expression="{{ metric('a') }}"),
SqlMetric(metric_name="c", expression="{{ metric('b') }}"),
],
database=Database(database_name="my_database", sqlalchemy_uri="sqlite://"),
schema="my_schema",
sql=None,
)
assert metric_macro("c", 1) == "COUNT(*)"


def test_metric_macro_recursive_compound(mocker: MockerFixture) -> None:
"""
Test the ``metric_macro`` when the definition is compound.
"""
mock_g = mocker.patch("superset.jinja_context.g")
mock_g.form_data = {"datasource": {"id": 1}}
DatasetDAO = mocker.patch("superset.daos.dataset.DatasetDAO") # noqa: N806
DatasetDAO.find_by_id.return_value = SqlaTable(
table_name="test_dataset",
metrics=[
SqlMetric(metric_name="a", expression="SUM(*)"),
SqlMetric(metric_name="b", expression="COUNT(*)"),
SqlMetric(
metric_name="c",
expression="{{ metric('a') }} / {{ metric('b') }}",
),
],
database=Database(database_name="my_database", sqlalchemy_uri="sqlite://"),
schema="my_schema",
sql=None,
)
assert metric_macro("c", 1) == "SUM(*) / COUNT(*)"


def test_metric_macro_recursive_cyclic(mocker: MockerFixture) -> None:
"""
Test the ``metric_macro`` when the definition is cyclic.

In this case it should stop, and not go into an infinite loop.
"""
mock_g = mocker.patch("superset.jinja_context.g")
mock_g.form_data = {"datasource": {"id": 1}}
DatasetDAO = mocker.patch("superset.daos.dataset.DatasetDAO") # noqa: N806
DatasetDAO.find_by_id.return_value = SqlaTable(
table_name="test_dataset",
metrics=[
SqlMetric(metric_name="a", expression="{{ metric('c') }}"),
SqlMetric(metric_name="b", expression="{{ metric('a') }}"),
SqlMetric(metric_name="c", expression="{{ metric('b') }}"),
],
database=Database(database_name="my_database", sqlalchemy_uri="sqlite://"),
schema="my_schema",
sql=None,
)
with pytest.raises(SupersetTemplateException) as excinfo:
metric_macro("c", 1)
assert str(excinfo.value) == "Cyclic metric macro detected"


def test_metric_macro_recursive_infinite(mocker: MockerFixture) -> None:
"""
Test the ``metric_macro`` when the definition is cyclic.

In this case it should stop, and not go into an infinite loop.
"""
mock_g = mocker.patch("superset.jinja_context.g")
mock_g.form_data = {"datasource": {"id": 1}}
DatasetDAO = mocker.patch("superset.daos.dataset.DatasetDAO") # noqa: N806
DatasetDAO.find_by_id.return_value = SqlaTable(
table_name="test_dataset",
metrics=[
SqlMetric(metric_name="a", expression="{{ metric('a') }}"),
],
database=Database(database_name="my_database", sqlalchemy_uri="sqlite://"),
schema="my_schema",
sql=None,
)
with pytest.raises(SupersetTemplateException) as excinfo:
metric_macro("a", 1)
assert str(excinfo.value) == "Cyclic metric macro detected"


def test_metric_macro_with_dataset_id_invalid_key(mocker: MockerFixture) -> None:
"""
Test the ``metric_macro`` when passing a dataset ID and an invalid key.
Expand Down
Loading