From b19a24d815bf02105f64f533ee5be66c29e24865 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Wed, 25 Sep 2024 12:52:41 -0700 Subject: [PATCH] Add empty manifest check for `SavedQueryDependencyResolver` (#1429) There seems to be issues with initializing `MetricflowQueryParser` with an empty manifest. This is related to recent time spine changes, but resolving that might take some time, so putting out this PR as a temporary solution. --- .../api/v0_1/saved_query_dependency_resolver.py | 14 ++++++++++++-- .../v0_1/test_saved_query_dependency_resolver.py | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/metricflow-semantics/metricflow_semantics/api/v0_1/saved_query_dependency_resolver.py b/metricflow-semantics/metricflow_semantics/api/v0_1/saved_query_dependency_resolver.py index 50893504cf..4367720272 100644 --- a/metricflow-semantics/metricflow_semantics/api/v0_1/saved_query_dependency_resolver.py +++ b/metricflow-semantics/metricflow_semantics/api/v0_1/saved_query_dependency_resolver.py @@ -2,7 +2,7 @@ import logging from dataclasses import dataclass -from typing import Tuple +from typing import Optional, Tuple from dbt_semantic_interfaces.protocols import SemanticManifest from dbt_semantic_interfaces.references import ( @@ -34,9 +34,19 @@ class SavedQueryDependencyResolver: def __init__(self, semantic_manifest: SemanticManifest) -> None: # noqa: D107 self._semantic_manifest = semantic_manifest - self._query_parser = MetricFlowQueryParser(SemanticManifestLookup(semantic_manifest)) + # TODO: There seems to be issues with an empty manifest, so doing this check for now. + self._query_parser: Optional[MetricFlowQueryParser] = None + if len(semantic_manifest.semantic_models) > 0: + self._query_parser = MetricFlowQueryParser(SemanticManifestLookup(semantic_manifest)) def _resolve_dependencies(self, saved_query_name: str) -> SavedQueryDependencySet: + if self._query_parser is None: + logger.warning( + "Trying to resolve the dependencies of a saved query when the provided semantic manifest does not " + "contain semantic models is unexpected. Returning an empty set." + ) + return SavedQueryDependencySet(()) + parse_result = self._query_parser.parse_and_validate_saved_query( saved_query_parameter=SavedQueryParameter(saved_query_name), where_filter=None, diff --git a/metricflow-semantics/tests_metricflow_semantics/api/v0_1/test_saved_query_dependency_resolver.py b/metricflow-semantics/tests_metricflow_semantics/api/v0_1/test_saved_query_dependency_resolver.py index d8514db27e..c4764ed930 100644 --- a/metricflow-semantics/tests_metricflow_semantics/api/v0_1/test_saved_query_dependency_resolver.py +++ b/metricflow-semantics/tests_metricflow_semantics/api/v0_1/test_saved_query_dependency_resolver.py @@ -1,6 +1,7 @@ from __future__ import annotations import pytest +from dbt_semantic_interfaces.implementations.project_configuration import PydanticProjectConfiguration from dbt_semantic_interfaces.implementations.semantic_manifest import ( PydanticSemanticManifest, ) @@ -23,3 +24,14 @@ def test_saved_query_dependency_resolver(resolver: SavedQueryDependencyResolver) SemanticModelReference(semantic_model_name="bookings_source"), SemanticModelReference(semantic_model_name="listings_latest"), ) + + +def test_empty_manifest() -> None: # noqa: D103 + """In case the manifest is empty, checks that the resolver can be created.""" + SavedQueryDependencyResolver( + PydanticSemanticManifest( + semantic_models=[], + metrics=[], + project_configuration=PydanticProjectConfiguration(), + ) + )