|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 |
| -from typing import List, Optional, Sequence |
| 3 | +from typing import List, Optional, Sequence, Tuple |
4 | 4 |
|
5 | 5 | from dbt_semantic_interfaces.references import SemanticModelReference
|
6 | 6 | from metricflow_semantics.assert_one_arg import assert_exactly_one_arg_set
|
7 |
| -from metricflow_semantics.instances import EntityInstance, InstanceSet |
| 7 | +from metricflow_semantics.instances import EntityInstance, InstanceSet, TimeDimensionInstance |
8 | 8 | from metricflow_semantics.mf_logging.lazy_formattable import LazyFormat
|
9 | 9 | from metricflow_semantics.specs.column_assoc import ColumnAssociation
|
10 | 10 | from metricflow_semantics.specs.dimension_spec import DimensionSpec
|
@@ -122,30 +122,39 @@ def column_association_for_dimension(
|
122 | 122 |
|
123 | 123 | return column_associations_to_return[0]
|
124 | 124 |
|
125 |
| - def column_association_for_time_dimension( |
126 |
| - self, |
127 |
| - time_dimension_spec: TimeDimensionSpec, |
128 |
| - ) -> ColumnAssociation: |
129 |
| - """Given the name of the time dimension, return the set of columns associated with it in the data set.""" |
| 125 | + def instances_for_time_dimensions( |
| 126 | + self, time_dimension_specs: Sequence[TimeDimensionSpec] |
| 127 | + ) -> Tuple[TimeDimensionInstance, ...]: |
| 128 | + """Return the instances associated with these specs in the data set.""" |
| 129 | + time_dimension_specs_set = set(time_dimension_specs) |
130 | 130 | matching_instances = 0
|
131 |
| - column_associations_to_return = None |
| 131 | + instances_to_return: Tuple[TimeDimensionInstance, ...] = () |
132 | 132 | for time_dimension_instance in self.instance_set.time_dimension_instances:
|
133 |
| - if time_dimension_instance.spec == time_dimension_spec: |
134 |
| - column_associations_to_return = time_dimension_instance.associated_columns |
| 133 | + if time_dimension_instance.spec in time_dimension_specs_set: |
| 134 | + instances_to_return += (time_dimension_instance,) |
135 | 135 | matching_instances += 1
|
136 | 136 |
|
137 |
| - if matching_instances > 1: |
| 137 | + if matching_instances != len(time_dimension_specs_set): |
138 | 138 | raise RuntimeError(
|
139 |
| - f"More than one time dimension instance with spec {time_dimension_spec} in " |
140 |
| - f"instance set: {self.instance_set}" |
| 139 | + f"Unexpected number of time dimension instances found matching specs.\nSpecs: {time_dimension_specs_set}\n" |
| 140 | + f"Instances: {instances_to_return}" |
141 | 141 | )
|
142 | 142 |
|
143 |
| - if not column_associations_to_return: |
| 143 | + return instances_to_return |
| 144 | + |
| 145 | + def instance_for_time_dimension(self, time_dimension_spec: TimeDimensionSpec) -> TimeDimensionInstance: |
| 146 | + """Given the name of the time dimension, return the instance associated with it in the data set.""" |
| 147 | + instances = self.instances_for_time_dimensions((time_dimension_spec,)) |
| 148 | + if not len(instances) == 1: |
144 | 149 | raise RuntimeError(
|
145 |
| - f"No time dimension instances with spec {time_dimension_spec} in instance set: {self.instance_set}" |
| 150 | + f"Unexpected number of time dimension instances found matching specs.\nSpecs: {time_dimension_spec}\n" |
| 151 | + f"Instances: {instances}" |
146 | 152 | )
|
| 153 | + return instances[0] |
147 | 154 |
|
148 |
| - return column_associations_to_return[0] |
| 155 | + def column_association_for_time_dimension(self, time_dimension_spec: TimeDimensionSpec) -> ColumnAssociation: |
| 156 | + """Given the name of the time dimension, return the set of columns associated with it in the data set.""" |
| 157 | + return self.instance_for_time_dimension(time_dimension_spec).associated_column |
149 | 158 |
|
150 | 159 | @property
|
151 | 160 | @override
|
|
0 commit comments