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

fix(dbt): derived metrics in dbt cloud #199

Merged
merged 1 commit into from
Apr 3, 2023
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
4 changes: 1 addition & 3 deletions src/preset_cli/cli/superset/sync/dbt/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ def get_metric_expression(metric_name: str, metrics: Dict[str, MetricSchema]) ->
# dbt >= 1.3
type_ = metric["calculation_method"]
sql = metric["expression"]
expression = "derived"
else:
# dbt < 1.3
type_ = metric["type"]
sql = metric["sql"]
expression = "expression"

if metric.get("filters"):
sql = apply_filters(sql, metric["filters"])
Expand All @@ -54,7 +52,7 @@ def get_metric_expression(metric_name: str, metrics: Dict[str, MetricSchema]) ->
if type_ == "count_distinct":
return f"COUNT(DISTINCT {sql})"

if type_ == expression:
if type_ in {"expression", "derived"}:
statement = sqlparse.parse(sql)[0]
tokens = statement.tokens[:]
while tokens:
Expand Down
21 changes: 21 additions & 0 deletions tests/cli/superset/sync/dbt/metrics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,22 @@ def test_get_metric_expression() -> None:
"sql": "user_id",
},
),
"load_fill_by_weight": metric_schema.load(
{
"depends_on": [
"metric.breakthrough_dw.load_weight_lbs",
"metric.breakthrough_dw.load_weight_capacity_lbs",
],
"description": "The Load Fill by Weight",
"filters": [],
"label": "Load Fill by Weight",
"meta": {},
"name": "load_fill_by_weight",
"sql": "load_weight_lbs / load_weight_capacity_lbs",
"type": "derived",
"unique_id": "metric.breakthrough_dw.load_fill_by_weight",
},
),
}
assert get_metric_expression("one", metrics) == (
"COUNT(CASE WHEN is_paying is true AND lifetime_value >= 100 AND "
Expand All @@ -64,6 +80,11 @@ def test_get_metric_expression() -> None:
"- COUNT(DISTINCT user_id)"
)

assert (
get_metric_expression("load_fill_by_weight", metrics)
== "load_weight_lbs / load_weight_capacity_lbs"
)

with pytest.raises(Exception) as excinfo:
get_metric_expression("four", metrics)
assert str(excinfo.value) == (
Expand Down