From 91862219fc6e0781293ec86a7db9b8aa117c6181 Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Sat, 27 Jul 2024 00:07:55 -0700 Subject: [PATCH] Remove bad granularity assumptions --- .../metricflow_semantics/specs/time_dimension_spec.py | 7 ++++++- metricflow/dataflow/builder/node_evaluator.py | 11 +++++++---- metricflow/dataset/convert_semantic_model.py | 4 ++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py b/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py index 4f7a844d5d..4212bfef01 100644 --- a/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py +++ b/metricflow-semantics/metricflow_semantics/specs/time_dimension_spec.py @@ -101,7 +101,12 @@ def without_first_entity_link(self) -> TimeDimensionSpec: # noqa: D102 @property def without_entity_links(self) -> TimeDimensionSpec: # noqa: D102 - return TimeDimensionSpec.from_name(self.element_name) + return TimeDimensionSpec( + element_name=self.element_name, + time_granularity=self.time_granularity, + date_part=self.date_part, + entity_links=(), + ) @staticmethod def from_name(name: str) -> TimeDimensionSpec: # noqa: D102 diff --git a/metricflow/dataflow/builder/node_evaluator.py b/metricflow/dataflow/builder/node_evaluator.py index d22533be75..0c6715c3b9 100644 --- a/metricflow/dataflow/builder/node_evaluator.py +++ b/metricflow/dataflow/builder/node_evaluator.py @@ -200,14 +200,19 @@ def _find_joinable_candidate_nodes_that_can_satisfy_linkable_specs( candidates_for_join: List[JoinLinkableInstancesRecipe] = [] left_node_spec_set = left_node_instance_set.spec_set for right_node in self._nodes_available_for_joins: + data_set_in_right_node: SqlDataSet = self._node_data_set_resolver.get_output_data_set(right_node) + linkable_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.linkable_specs + # If right node is time spine source node, use cross join. if right_node in self._time_spine_nodes: - needed_metric_time_specs = group_specs_by_type(needed_linkable_specs).metric_time_specs + satisfiable_metric_time_specs = set(needed_linkable_specs).intersection( + set(linkable_specs_in_right_node) + ) candidates_for_join.append( JoinLinkableInstancesRecipe( node_to_join=right_node, join_on_entity=None, - satisfiable_linkable_specs=list(needed_metric_time_specs), + satisfiable_linkable_specs=list(satisfiable_metric_time_specs), join_on_partition_dimensions=(), join_on_partition_time_dimensions=(), join_type=SqlJoinType.CROSS_JOIN, @@ -215,8 +220,6 @@ def _find_joinable_candidate_nodes_that_can_satisfy_linkable_specs( ) continue - data_set_in_right_node: SqlDataSet = self._node_data_set_resolver.get_output_data_set(right_node) - linkable_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.linkable_specs entity_specs_in_right_node = data_set_in_right_node.instance_set.spec_set.entity_specs # For each unlinked entity in the data set, create a candidate for joining. diff --git a/metricflow/dataset/convert_semantic_model.py b/metricflow/dataset/convert_semantic_model.py index 198bdbce87..3304aefce3 100644 --- a/metricflow/dataset/convert_semantic_model.py +++ b/metricflow/dataset/convert_semantic_model.py @@ -104,7 +104,7 @@ def _create_time_dimension_instance( self, element_name: str, entity_links: Tuple[EntityReference, ...], - time_granularity: TimeGranularity = DEFAULT_TIME_GRANULARITY, + time_granularity: TimeGranularity, date_part: Optional[DatePart] = None, semantic_model_name: Optional[str] = None, ) -> TimeDimensionInstance: @@ -332,7 +332,7 @@ def _build_time_dimension_instances_and_columns( ) -> Tuple[List[TimeDimensionInstance], List[SqlSelectColumn]]: time_dimension_instances: List[TimeDimensionInstance] = [] select_columns: List[SqlSelectColumn] = [] - # Add time dimensions with a smaller granularity for ease in query resolution + # Add time dimensions with a larger granularity for ease in query resolution for time_granularity in TimeGranularity: if time_granularity.to_int() > defined_time_granularity.to_int(): time_dimension_instance = self._create_time_dimension_instance(