Skip to content

Commit

Permalink
Fix #9534: Add NodeRelation to SavedQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
aranke committed Mar 22, 2024
1 parent c6c0c79 commit 72acfb8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
4 changes: 4 additions & 0 deletions core/dbt/artifacts/resources/v1/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import time

from dataclasses import dataclass, field

from dbt_semantic_interfaces.implementations.semantic_model import NodeRelation

from dbt.artifacts.resources.base import GraphResource
from dbt.artifacts.resources.v1.components import DependsOn, RefArgs
from dbt.artifacts.resources.v1.semantic_layer_components import (
Expand Down Expand Up @@ -73,6 +76,7 @@ class SavedQueryConfig(BaseConfig):
class SavedQueryMandatory(GraphResource):
query_params: QueryParams
exports: List[Export]
node_relation: Optional[NodeRelation]


@dataclass
Expand Down
11 changes: 3 additions & 8 deletions core/dbt/artifacts/resources/v1/semantic_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import time

from dataclasses import dataclass, field

from dbt_semantic_interfaces.implementations.semantic_model import NodeRelation

from dbt.artifacts.resources.base import GraphResource
from dbt.artifacts.resources.v1.components import DependsOn, RefArgs
from dbt_common.contracts.config.base import BaseConfig, CompareBehavior, MergeBehavior
Expand Down Expand Up @@ -37,14 +40,6 @@ class Defaults(dbtClassMixin):
agg_time_dimension: Optional[str] = None


@dataclass
class NodeRelation(dbtClassMixin):
alias: str
schema_name: str # TODO: Could this be called simply "schema" so we could reuse StateRelation?
database: Optional[str] = None
relation_name: Optional[str] = None


# ====================================
# Dimension objects
# Dimension protocols: https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/dbt_semantic_interfaces/protocols/dimension.py
Expand Down
26 changes: 18 additions & 8 deletions core/dbt/parser/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,6 @@ def load(self) -> Manifest:
self.process_unit_tests(self.root_project.project_name)
self.process_docs(self.root_project)
self.process_metrics(self.root_project)
self.process_saved_queries(self.root_project)
self.check_valid_group_config()
self.check_valid_access_property()

Expand Down Expand Up @@ -1104,6 +1103,11 @@ def process_refs(self, current_project: str, dependencies: Optional[Mapping[str,
continue
_process_refs(self.manifest, current_project, semantic_model, dependencies)
self.update_semantic_model(semantic_model)
for saved_query in self.manifest.saved_queries.values():
if saved_query.created_at < self.started_at:
continue
_process_refs(self.manifest, current_project, saved_query, dependencies)
self.update_saved_query(saved_query, current_project)

# Takes references in 'metrics' array of nodes and exposures, finds the target
# node, and updates 'depends_on.nodes' with the unique id
Expand All @@ -1123,14 +1127,20 @@ def process_metrics(self, config: RuntimeConfig):
continue
_process_metrics_for_node(self.manifest, current_project, exposure)

def process_saved_queries(self, config: RuntimeConfig):
def update_saved_query(self, saved_query: SavedQuery, current_project: str) -> None:
"""Processes SavedQuery nodes to populate their `depends_on`."""
current_project = config.project_name
for saved_query in self.manifest.saved_queries.values():
# TODO:
# 1. process `where` of SavedQuery for `depends_on`s
# 2. process `group_by` of SavedQuery for `depends_on``
_process_metrics_for_node(self.manifest, current_project, saved_query)
# TODO:
# 1. process `where` of SavedQuery for `depends_on`s
# 2. process `group_by` of SavedQuery for `depends_on``
_process_metrics_for_node(self.manifest, current_project, saved_query)
if saved_query.depends_on_nodes[0]:
refd_node = self.manifest.nodes[saved_query.depends_on_nodes[0]]
saved_query.node_relation = NodeRelation(
relation_name=refd_node.relation_name,
alias=refd_node.alias,
schema_name=refd_node.schema,
database=refd_node.database,
)

def update_semantic_model(self, semantic_model) -> None:
# This has to be done at the end of parsing because the referenced model
Expand Down

0 comments on commit 72acfb8

Please sign in to comment.