|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | +from functools import cached_property |
| 5 | +from typing import Optional, Set |
| 6 | + |
| 7 | +from dbt_semantic_interfaces.enum_extension import assert_values_exhausted |
| 8 | +from dbt_semantic_interfaces.type_enums import DatePart, TimeGranularity |
| 9 | + |
| 10 | +from metricflow_semantics.collection_helpers.mf_type_aliases import AnyLengthTuple |
| 11 | +from metricflow_semantics.experimental.dataclass_helpers import fast_frozen_dataclass |
| 12 | +from metricflow_semantics.experimental.metricflow_exception import MetricflowInternalError |
| 13 | +from metricflow_semantics.experimental.ordered_set import FrozenOrderedSet, MutableOrderedSet, OrderedSet |
| 14 | +from metricflow_semantics.experimental.semantic_graph.attribute_resolution.attribute_recipe_step import ( |
| 15 | + AttributeRecipeStep, |
| 16 | +) |
| 17 | +from metricflow_semantics.experimental.semantic_graph.model_id import SemanticModelId |
| 18 | +from metricflow_semantics.mf_logging.lazy_formattable import LazyFormat |
| 19 | +from metricflow_semantics.model.linkable_element_property import LinkableElementProperty |
| 20 | +from metricflow_semantics.model.semantics.linkable_element import LinkableElementType |
| 21 | +from metricflow_semantics.time.granularity import ExpandedTimeGranularity |
| 22 | + |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | + |
| 26 | +IndexedDunderName = AnyLengthTuple[str] |
| 27 | + |
| 28 | + |
| 29 | +@fast_frozen_dataclass() |
| 30 | +class AttributeRecipe: |
| 31 | + """The recipe for computing an attribute by following a path in the semantic graph.""" |
| 32 | + |
| 33 | + indexed_dunder_name: IndexedDunderName = () |
| 34 | + joined_model_ids: AnyLengthTuple[SemanticModelId] = () |
| 35 | + element_properties: FrozenOrderedSet[LinkableElementProperty] = FrozenOrderedSet() |
| 36 | + entity_link_names: AnyLengthTuple[str] = () |
| 37 | + |
| 38 | + element_type: Optional[LinkableElementType] = None |
| 39 | + # Maps to the time grain set for a time dimension in a semantic model |
| 40 | + source_time_grain: Optional[TimeGranularity] = None |
| 41 | + # Maps to the attribute's time grain or date part. |
| 42 | + recipe_time_grain: Optional[ExpandedTimeGranularity] = None |
| 43 | + recipe_date_part: Optional[DatePart] = None |
| 44 | + |
| 45 | + @cached_property |
| 46 | + def dunder_name_elements_set(self) -> Set[str]: |
| 47 | + """The elements of a dunder name as a set for fast repeated-element checks.""" |
| 48 | + return set(self.indexed_dunder_name) |
| 49 | + |
| 50 | + @cached_property |
| 51 | + def joined_model_id_set(self) -> Set[SemanticModelId]: |
| 52 | + """The joined semantic model IDs as a set for fast repeated-model checks.""" |
| 53 | + return set(self.joined_model_ids) |
| 54 | + |
| 55 | + @staticmethod |
| 56 | + def create(initial_step: AttributeRecipeStep) -> AttributeRecipe: # noqa: D102 |
| 57 | + dunder_name_elements: AnyLengthTuple[str] = () |
| 58 | + if initial_step.add_dunder_name_element is not None: |
| 59 | + dunder_name_elements = (initial_step.add_dunder_name_element,) |
| 60 | + entity_link_names: AnyLengthTuple[str] = () |
| 61 | + if initial_step.add_entity_link is not None: |
| 62 | + entity_link_names = (initial_step.add_entity_link,) |
| 63 | + |
| 64 | + models_in_join: AnyLengthTuple[SemanticModelId] = () |
| 65 | + |
| 66 | + add_model_join = initial_step.add_model_join |
| 67 | + if add_model_join is not None: |
| 68 | + models_in_join = models_in_join + (add_model_join,) |
| 69 | + |
| 70 | + return AttributeRecipe( |
| 71 | + indexed_dunder_name=dunder_name_elements, |
| 72 | + joined_model_ids=models_in_join, |
| 73 | + element_properties=FrozenOrderedSet(initial_step.add_properties or ()), |
| 74 | + element_type=initial_step.set_element_type, |
| 75 | + entity_link_names=entity_link_names, |
| 76 | + source_time_grain=initial_step.set_source_time_grain, |
| 77 | + recipe_time_grain=initial_step.set_time_grain_access, |
| 78 | + recipe_date_part=initial_step.set_date_part_access, |
| 79 | + ) |
| 80 | + |
| 81 | + @cached_property |
| 82 | + def last_model_id(self) -> Optional[SemanticModelId]: |
| 83 | + """The last model ID that was added to the join.""" |
| 84 | + if self.joined_model_ids: |
| 85 | + return None |
| 86 | + |
| 87 | + return tuple(self.joined_model_ids)[-1] |
| 88 | + |
| 89 | + def append_step(self, recipe_step: AttributeRecipeStep) -> AttributeRecipe: |
| 90 | + """Add a step to the end of the recipe.""" |
| 91 | + dundered_name_elements = self.indexed_dunder_name |
| 92 | + if recipe_step.add_dunder_name_element is not None: |
| 93 | + dundered_name_elements = dundered_name_elements + (recipe_step.add_dunder_name_element,) |
| 94 | + entity_link_names = self.entity_link_names |
| 95 | + if recipe_step.add_entity_link is not None: |
| 96 | + entity_link_names = entity_link_names + (recipe_step.add_entity_link,) |
| 97 | + |
| 98 | + models_in_join = self.joined_model_ids |
| 99 | + join_model = recipe_step.add_model_join |
| 100 | + |
| 101 | + if join_model is not None: |
| 102 | + models_in_join = models_in_join + (join_model,) |
| 103 | + |
| 104 | + return AttributeRecipe( |
| 105 | + indexed_dunder_name=dundered_name_elements, |
| 106 | + joined_model_ids=models_in_join, |
| 107 | + element_properties=self.element_properties.union(recipe_step.add_properties) |
| 108 | + if recipe_step.add_properties is not None |
| 109 | + else self.element_properties, |
| 110 | + element_type=recipe_step.set_element_type or self.element_type, |
| 111 | + entity_link_names=entity_link_names, |
| 112 | + source_time_grain=recipe_step.set_source_time_grain or self.source_time_grain, |
| 113 | + recipe_time_grain=recipe_step.set_time_grain_access or self.recipe_time_grain, |
| 114 | + recipe_date_part=recipe_step.set_date_part_access or self.recipe_date_part, |
| 115 | + ) |
| 116 | + |
| 117 | + def push_step(self, recipe_step: AttributeRecipeStep) -> AttributeRecipe: |
| 118 | + """Add a step to the beginning of the recipe.""" |
| 119 | + dundered_name_elements = self.indexed_dunder_name |
| 120 | + if recipe_step.add_dunder_name_element is not None: |
| 121 | + dundered_name_elements = (recipe_step.add_dunder_name_element,) + dundered_name_elements |
| 122 | + entity_link_names = self.entity_link_names |
| 123 | + if recipe_step.add_entity_link is not None: |
| 124 | + entity_link_names = (recipe_step.add_entity_link,) + entity_link_names |
| 125 | + models_in_join = self.joined_model_ids |
| 126 | + add_model_join = recipe_step.add_model_join |
| 127 | + if add_model_join is not None: |
| 128 | + if len(models_in_join) == 0: |
| 129 | + models_in_join = (add_model_join,) |
| 130 | + else: |
| 131 | + models_in_join = (add_model_join,) + models_in_join |
| 132 | + |
| 133 | + return AttributeRecipe( |
| 134 | + indexed_dunder_name=dundered_name_elements, |
| 135 | + joined_model_ids=models_in_join, |
| 136 | + element_properties=FrozenOrderedSet(recipe_step.add_properties).union(self.element_properties) |
| 137 | + if recipe_step.add_properties is not None |
| 138 | + else self.element_properties, |
| 139 | + element_type=self.element_type or recipe_step.set_element_type, |
| 140 | + entity_link_names=entity_link_names, |
| 141 | + source_time_grain=self.source_time_grain or recipe_step.set_source_time_grain, |
| 142 | + recipe_time_grain=self.recipe_time_grain or recipe_step.set_time_grain_access, |
| 143 | + recipe_date_part=self.recipe_date_part or recipe_step.set_date_part_access, |
| 144 | + ) |
| 145 | + |
| 146 | + def push_steps(self, *updates: AttributeRecipeStep) -> AttributeRecipe: |
| 147 | + """See `push_step`.""" |
| 148 | + result = self |
| 149 | + for update in updates: |
| 150 | + result = result.push_step(update) |
| 151 | + return result |
| 152 | + |
| 153 | + def resolve_complete_properties(self) -> OrderedSet[LinkableElementProperty]: |
| 154 | + """Resolve the complete set of `LinkableElementProperty` for this recipe. |
| 155 | +
|
| 156 | + While many properties were set by recipe steps during traversal, some need to be resolved at the end as it |
| 157 | + is easier / faster to determine at the end. |
| 158 | + """ |
| 159 | + element_type = self.element_type |
| 160 | + |
| 161 | + if element_type is None: |
| 162 | + raise ValueError(LazyFormat("Recipe is missing the element type", recipe=self)) |
| 163 | + |
| 164 | + properties = MutableOrderedSet(self.element_properties) |
| 165 | + |
| 166 | + model_ids = self.joined_model_ids |
| 167 | + model_id_count = len(model_ids) |
| 168 | + if model_id_count == 0: |
| 169 | + if LinkableElementProperty.METRIC_TIME not in properties: |
| 170 | + raise ValueError(LazyFormat("Recipe is missing context on accessed semantic models", recipe=self)) |
| 171 | + elif model_id_count == 1: |
| 172 | + if element_type is not LinkableElementType.METRIC and LinkableElementProperty.METRIC_TIME not in properties: |
| 173 | + properties.add(LinkableElementProperty.LOCAL) |
| 174 | + elif model_id_count == 2: |
| 175 | + properties.add(LinkableElementProperty.JOINED) |
| 176 | + elif model_id_count >= 3: |
| 177 | + properties.update( |
| 178 | + ( |
| 179 | + LinkableElementProperty.JOINED, |
| 180 | + LinkableElementProperty.MULTI_HOP, |
| 181 | + ) |
| 182 | + ) |
| 183 | + else: |
| 184 | + raise MetricflowInternalError( |
| 185 | + LazyFormat("Reached unhandled case", model_id_count=model_id_count, recipe=self) |
| 186 | + ) |
| 187 | + |
| 188 | + # Add `DERIVED_TIME_GRANULARITY` if the grain is different from the element's grain. |
| 189 | + source_time_grain = self.source_time_grain |
| 190 | + recipe_time_grain = self.recipe_time_grain |
| 191 | + if source_time_grain is not None: |
| 192 | + if recipe_time_grain is None and self.recipe_date_part is None: |
| 193 | + raise ValueError( |
| 194 | + LazyFormat( |
| 195 | + "Recipe has a source time-grain, but no recipe time-grain or recipe date-part", recipe=self |
| 196 | + ) |
| 197 | + ) |
| 198 | + if recipe_time_grain is not None and source_time_grain is not recipe_time_grain.base_granularity: |
| 199 | + properties.add(LinkableElementProperty.DERIVED_TIME_GRANULARITY) |
| 200 | + |
| 201 | + return properties |
| 202 | + |
| 203 | + def resolve_element_name(self) -> Optional[str]: |
| 204 | + """Resolve the element name. |
| 205 | +
|
| 206 | + Currently, the recipe stores the dunder-name elements (e.g. ["metric_time", "day"]), but not the element name. |
| 207 | + Since the position of the element name in the list depends on the type of element, this method helps to resolve |
| 208 | + that. |
| 209 | + """ |
| 210 | + element_type = self.element_type |
| 211 | + |
| 212 | + # Incomplete recipe. |
| 213 | + if element_type is None: |
| 214 | + return None |
| 215 | + |
| 216 | + dunder_name_elements = self.indexed_dunder_name |
| 217 | + dunder_name_element_count = len(dunder_name_elements) |
| 218 | + |
| 219 | + # Incomplete recipe. |
| 220 | + if dunder_name_element_count == 0: |
| 221 | + return None |
| 222 | + |
| 223 | + if element_type is LinkableElementType.TIME_DIMENSION: |
| 224 | + # e.g. ['metric_time'] |
| 225 | + if dunder_name_element_count == 1: |
| 226 | + return dunder_name_elements[-1] |
| 227 | + # e.g. ['metric_time', 'day'] |
| 228 | + else: |
| 229 | + return dunder_name_elements[-2] |
| 230 | + elif ( |
| 231 | + element_type is LinkableElementType.ENTITY |
| 232 | + or element_type is LinkableElementType.DIMENSION |
| 233 | + or element_type is LinkableElementType.TIME_DIMENSION |
| 234 | + or element_type is LinkableElementType.METRIC |
| 235 | + ): |
| 236 | + return dunder_name_elements[-1] |
| 237 | + else: |
| 238 | + assert_values_exhausted(element_type) |
0 commit comments