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

Add meta to SemanticModels #8754

Merged
merged 3 commits into from
Oct 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
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230929-170945.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Add meta attribute to SemanticModels
time: 2023-09-29T17:09:45.0354-05:00
custom:
Author: emmyoop
Issue: "8511"
4 changes: 4 additions & 0 deletions core/dbt/contracts/graph/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@ class SemanticModelConfig(BaseConfig):
default=None,
metadata=CompareBehavior.Exclude.meta(),
)
meta: Dict[str, Any] = field(
default_factory=dict,
metadata=MergeBehavior.Update.meta(),
)


@dataclass
Expand Down
1 change: 1 addition & 0 deletions core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,7 @@ class SemanticModel(GraphNode):
refs: List[RefArgs] = field(default_factory=list)
created_at: float = field(default_factory=lambda: time.time())
config: SemanticModelConfig = field(default_factory=SemanticModelConfig)
meta: Dict[str, Any] = field(default_factory=dict)
unrendered_config: Dict[str, Any] = field(default_factory=dict)
primary_entity: Optional[str] = None
group: Optional[str] = None
Expand Down
1 change: 1 addition & 0 deletions core/dbt/contracts/graph/unparsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@ class UnparsedSemanticModel(dbtClassMixin):
name: str
model: str # looks like "ref(...)"
config: Dict[str, Any] = field(default_factory=dict)
meta: Dict[str, Any] = field(default_factory=dict)
description: Optional[str] = None
label: Optional[str] = None
defaults: Optional[Defaults] = None
Expand Down
7 changes: 7 additions & 0 deletions core/dbt/parser/schema_yaml_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ def _generate_semantic_model_config(
base=False,
patch_config_dict=precedence_configs,
)

return config

def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
Expand Down Expand Up @@ -595,8 +596,14 @@ def parse_semantic_model(self, unparsed: UnparsedSemanticModel):
config=config,
unrendered_config=unrendered_config,
group=config.group,
meta=unparsed.meta,
)

# If we have meta in the config, copy to node level, for backwards
# compatibility with earlier node-only config.
if "meta" in config and config["meta"]:
parsed.meta = config["meta"]
Comment on lines +602 to +605
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we have to support the legacy way for a new node?

Copy link
Member Author

@emmyoop emmyoop Oct 3, 2023

Choose a reason for hiding this comment

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

@QMalcolm specifically because of this comment in the original issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

I've pinged @jtcohen6 for clarity on this. I read that comment to mean we've left the top level meta attribute on existing nodes to support backwards compatibility, but I now see that it can also be read to mean we need to add it to all new nodes for backwards compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

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

@emmyoop @QMalcolm I'm happy with whatever's easier here.

Strictly, we only need it for existing node types — so for all new node types (including semantic models), we only need to support it nested within config.


ctx = generate_parse_semantic_models(
parsed,
self.root_project,
Expand Down
12 changes: 12 additions & 0 deletions schemas/dbt/manifest/v11.json
Original file line number Diff line number Diff line change
Expand Up @@ -5497,6 +5497,12 @@
}
],
"default": null
},
"meta": {
"type": "object",
"propertyNames": {
"type": "string"
}
}
},
"additionalProperties": true
Expand Down Expand Up @@ -5635,6 +5641,12 @@
"config": {
"$ref": "#/$defs/SemanticModelConfig"
},
"meta": {
"type": "object",
"propertyNames": {
"type": "string"
}
},
"unrendered_config": {
"type": "object",
"propertyNames": {
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/semantic_models/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@
config:
enabled: true
group: some_group
meta:
my_meta: 'testing'
my_other_meta: 'testing more'
dimensions:
- name: favorite_color
type: categorical
Expand Down Expand Up @@ -185,6 +188,7 @@
agg_time_dimension: created_at
"""


schema_yml = """models:
- name: fct_revenue
description: This is the model fct_revenue. It should be able to use doc blocks
Expand Down
23 changes: 23 additions & 0 deletions tests/functional/semantic_models/test_semantic_model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,26 @@ def test_project_plus_yaml_level(self, project):
).config

assert isinstance(config_test_table, SemanticModelConfig)


# test setting meta attributes in semantic model config
class TestMetaConfig:
@pytest.fixture(scope="class")
def models(self):
return {
"people.sql": models_people_sql,
"metricflow_time_spine.sql": metricflow_time_spine_sql,
"semantic_models.yml": enabled_semantic_model_people_yml,
"people_metrics.yml": models_people_metrics_yml,
"groups.yml": groups_yml,
}

def test_meta_config(self, project):
run_dbt(["parse"])
manifest = get_manifest(project.project_root)
sm_id = "semantic_model.test.semantic_people"
assert sm_id in manifest.semantic_models
sm_node = manifest.semantic_models[sm_id]
meta_expected = {"my_meta": "testing", "my_other_meta": "testing more"}
assert sm_node.meta == meta_expected
assert sm_node.config.meta == meta_expected