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 #8509: Support doc blocks in nested semantic model YAML #8709

Merged
merged 4 commits into from
Sep 26, 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230926-001527.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Support doc blocks in nested semantic model YAML
time: 2023-09-26T00:15:27.328363+01:00
custom:
Author: aranke
Issue: "8509"
29 changes: 29 additions & 0 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,16 @@
config.project_name,
)
_process_docs_for_metrics(ctx, metric)
for semantic_model in self.manifest.semantic_models.values():
if semantic_model.created_at < self.started_at:
continue

Check warning on line 1177 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1177

Added line #L1177 was not covered by tests
ctx = generate_runtime_docs_context(
config,
semantic_model,
self.manifest,
config.project_name,
)
_process_docs_for_semantic_model(ctx, semantic_model)

# Loops through all nodes and exposures, for each element in
# 'sources' array finds the source node and updates the
Expand Down Expand Up @@ -1406,6 +1416,25 @@
metric.description = get_rendered(metric.description, context)


def _process_docs_for_semantic_model(
context: Dict[str, Any], semantic_model: SemanticModel
) -> None:
if semantic_model.description:
semantic_model.description = get_rendered(semantic_model.description, context)

Check warning on line 1423 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1423

Added line #L1423 was not covered by tests

for dimension in semantic_model.dimensions:
if dimension.description:
dimension.description = get_rendered(dimension.description, context)

Check warning on line 1427 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1427

Added line #L1427 was not covered by tests

for measure in semantic_model.measures:
if measure.description:
measure.description = get_rendered(measure.description, context)

Check warning on line 1431 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1431

Added line #L1431 was not covered by tests

for entity in semantic_model.entities:
if entity.description:
entity.description = get_rendered(entity.description, context)

Check warning on line 1435 in core/dbt/parser/manifest.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/parser/manifest.py#L1435

Added line #L1435 was not covered by tests


def _process_refs(
manifest: Manifest, current_project: str, node, dependencies: Optional[Mapping[str, Project]]
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/parser/schema_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _is_norender_key(self, keypath: Keypath) -> bool:

if (
len(keypath) >= 3
and keypath[0] == "columns"
and keypath[0] in ("columns", "dimensions", "measures", "entities")
and keypath[2] in ("tests", "description")
):
return True
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/semantic_models/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@
agg_time_dimension: created_at
"""

semantic_model_descriptions = """
{% docs semantic_model_description %} foo {% enddocs %}
{% docs dimension_description %} bar {% enddocs %}
{% docs measure_description %} baz {% enddocs %}
{% docs entity_description %} qux {% enddocs %}
"""

semantic_model_people_yml_with_docs = """
version: 2

semantic_models:
- name: semantic_people
model: ref('people')
description: "{{ doc('semantic_model_description') }}"
dimensions:
- name: favorite_color
type: categorical
description: "{{ doc('dimension_description') }}"
- name: created_at
type: TIME
type_params:
time_granularity: day
measures:
- name: years_tenure
agg: SUM
expr: tenure
description: "{{ doc('measure_description') }}"
- name: people
agg: count
expr: id
entities:
- name: id
description: "{{ doc('entity_description') }}"
type: primary
defaults:
agg_time_dimension: created_at
"""

enabled_semantic_model_people_yml = """
version: 2

Expand Down
25 changes: 23 additions & 2 deletions tests/functional/semantic_models/test_semantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from dbt.contracts.graph.manifest import Manifest
from dbt.exceptions import CompilationError
from dbt.tests.util import run_dbt


from tests.functional.semantic_models.fixtures import (
models_people_sql,
simple_metricflow_time_spine_sql,
semantic_model_people_yml,
models_people_metrics_yml,
semantic_model_people_yml_with_docs,
semantic_model_descriptions,
)


Expand All @@ -36,6 +36,27 @@ def test_depends_on(self, project):
)


class TestSemanticModelNestedDocs:
@pytest.fixture(scope="class")
def models(self):
return {
"people.sql": models_people_sql,
"metricflow_time_spine.sql": simple_metricflow_time_spine_sql,
"semantic_models.yml": semantic_model_people_yml_with_docs,
"people_metrics.yml": models_people_metrics_yml,
"docs.md": semantic_model_descriptions,
}

def test_depends_on(self, project):
manifest = run_dbt(["parse"])
node = manifest.semantic_models["semantic_model.test.semantic_people"]

assert node.description == "foo"
assert node.dimensions[0].description == "bar"
assert node.measures[0].description == "baz"
assert node.entities[0].description == "qux"


class TestSemanticModelUnknownModel:
@pytest.fixture(scope="class")
def models(self):
Expand Down
Loading