Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give enums singular names #1138

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions metricflow/engine/metricflow_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from metricflow.mf_logging.pretty_print import mf_pformat
from metricflow.model.semantic_manifest_lookup import SemanticManifestLookup
from metricflow.model.semantics.linkable_element_properties import (
LinkableElementProperties,
LinkableElementProperty,
)
from metricflow.model.semantics.linkable_spec_resolver import LinkableDimension
from metricflow.model.semantics.semantic_model_lookup import SemanticModelLookup
Expand Down Expand Up @@ -228,7 +228,7 @@ def explain(

@abstractmethod
def simple_dimensions_for_metrics(
self, metric_names: List[str], without_any_property: Sequence[LinkableElementProperties]
self, metric_names: List[str], without_any_property: Sequence[LinkableElementProperty]
) -> List[Dimension]:
"""Retrieves a list of all common dimensions for metric_names.

Expand Down Expand Up @@ -546,10 +546,10 @@ def get_measures_for_metrics(self, metric_names: List[str]) -> List[Measure]: #
def simple_dimensions_for_metrics( # noqa: D102
self,
metric_names: List[str],
without_any_property: Sequence[LinkableElementProperties] = (
LinkableElementProperties.ENTITY,
LinkableElementProperties.DERIVED_TIME_GRANULARITY,
LinkableElementProperties.LOCAL_LINKED,
without_any_property: Sequence[LinkableElementProperty] = (
LinkableElementProperty.ENTITY,
LinkableElementProperty.DERIVED_TIME_GRANULARITY,
LinkableElementProperty.LOCAL_LINKED,
),
) -> List[Dimension]:
path_key_to_linkable_dimensions = (
Expand All @@ -570,10 +570,10 @@ def simple_dimensions_for_metrics( # noqa: D102
if linkable_dimension.date_part is not None:
continue

if LinkableElementProperties.METRIC_TIME in linkable_dimension.properties:
if LinkableElementProperty.METRIC_TIME in linkable_dimension.properties:
metric_time_name = DataSet.metric_time_dimension_name()
assert linkable_dimension.element_name == metric_time_name, (
f"{linkable_dimension} has the {LinkableElementProperties.METRIC_TIME}, but the name does not"
f"{linkable_dimension} has the {LinkableElementProperty.METRIC_TIME}, but the name does not"
f"match."
)

Expand Down Expand Up @@ -623,7 +623,7 @@ def entities_for_metrics(self, metric_names: List[str]) -> List[Entity]: # noqa
metric_references=[MetricReference(element_name=mname) for mname in metric_names],
with_any_property=frozenset(
{
LinkableElementProperties.ENTITY,
LinkableElementProperty.ENTITY,
}
),
)
Expand Down
18 changes: 9 additions & 9 deletions metricflow/model/semantics/linkable_element_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import FrozenSet


class LinkableElementProperties(Enum):
class LinkableElementProperty(Enum):
"""The properties associated with a valid linkable element.

Local means an element that is defined within the same semantic model as the measure. This definition is used
Expand All @@ -29,15 +29,15 @@ class LinkableElementProperties(Enum):
METRIC = "metric"

@staticmethod
def all_properties() -> FrozenSet[LinkableElementProperties]: # noqa: D102
def all_properties() -> FrozenSet[LinkableElementProperty]: # noqa: D102
return frozenset(
{
LinkableElementProperties.LOCAL,
LinkableElementProperties.LOCAL_LINKED,
LinkableElementProperties.JOINED,
LinkableElementProperties.MULTI_HOP,
LinkableElementProperties.DERIVED_TIME_GRANULARITY,
LinkableElementProperties.METRIC_TIME,
LinkableElementProperties.METRIC,
LinkableElementProperty.LOCAL,
LinkableElementProperty.LOCAL_LINKED,
LinkableElementProperty.JOINED,
LinkableElementProperty.MULTI_HOP,
LinkableElementProperty.DERIVED_TIME_GRANULARITY,
LinkableElementProperty.METRIC_TIME,
LinkableElementProperty.METRIC,
}
)
68 changes: 33 additions & 35 deletions metricflow/model/semantics/linkable_spec_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from metricflow.dataset.dataset import DataSet
from metricflow.errors.errors import UnknownMetricLinkingError
from metricflow.mf_logging.pretty_print import mf_pformat
from metricflow.model.semantics.linkable_element_properties import LinkableElementProperties
from metricflow.model.semantics.linkable_element_properties import LinkableElementProperty
from metricflow.model.semantics.semantic_model_join_evaluator import SemanticModelJoinEvaluator
from metricflow.protocols.semantics import SemanticModelAccessor
from metricflow.specs.specs import (
Expand Down Expand Up @@ -59,7 +59,7 @@ class LinkableDimension:
element_name: str
entity_links: Tuple[EntityReference, ...]
join_path: Tuple[SemanticModelJoinPathElement, ...]
properties: FrozenSet[LinkableElementProperties]
properties: FrozenSet[LinkableElementProperty]
time_granularity: Optional[TimeGranularity]
date_part: Optional[DatePart]

Expand All @@ -84,7 +84,7 @@ class LinkableEntity:
# The semantic model where this entity was defined.
semantic_model_origin: SemanticModelReference
element_name: str
properties: FrozenSet[LinkableElementProperties]
properties: FrozenSet[LinkableElementProperty]
entity_links: Tuple[EntityReference, ...]
join_path: Tuple[SemanticModelJoinPathElement, ...]

Expand All @@ -105,7 +105,7 @@ class LinkableMetric:
join_by_semantic_model: SemanticModelReference
# TODO: Enable joining by dimension
entity_links: Tuple[EntityReference, ...]
properties: FrozenSet[LinkableElementProperties]
properties: FrozenSet[LinkableElementProperty]
join_path: Tuple[SemanticModelJoinPathElement, ...]

@property
Expand Down Expand Up @@ -242,9 +242,9 @@ def intersection_by_path_key(linkable_element_sets: Sequence[LinkableElementSet]

def filter(
self,
with_any_of: FrozenSet[LinkableElementProperties],
without_any_of: FrozenSet[LinkableElementProperties] = frozenset(),
without_all_of: FrozenSet[LinkableElementProperties] = frozenset(),
with_any_of: FrozenSet[LinkableElementProperty],
without_any_of: FrozenSet[LinkableElementProperty] = frozenset(),
without_all_of: FrozenSet[LinkableElementProperty] = frozenset(),
) -> LinkableElementSet:
"""Filter elements in the set.

Expand Down Expand Up @@ -376,7 +376,7 @@ def _generate_linkable_time_dimensions(
dimension: Dimension,
entity_links: Tuple[EntityReference, ...],
join_path: Sequence[SemanticModelJoinPathElement],
with_properties: FrozenSet[LinkableElementProperties],
with_properties: FrozenSet[LinkableElementProperty],
) -> Sequence[LinkableDimension]:
"""Generates different versions of the given dimension, but at other valid time granularities."""
linkable_dimensions = []
Expand All @@ -389,7 +389,7 @@ def _generate_linkable_time_dimensions(
continue
properties = set(with_properties)
if time_granularity != defined_time_granularity:
properties.add(LinkableElementProperties.DERIVED_TIME_GRANULARITY)
properties.add(LinkableElementProperty.DERIVED_TIME_GRANULARITY)

linkable_dimensions.append(
LinkableDimension(
Expand Down Expand Up @@ -500,12 +500,12 @@ def __init__(
if metric.type is MetricType.CUMULATIVE:
linkable_sets_for_measure.append(
self._get_linkable_element_set_for_measure(measure).filter(
with_any_of=LinkableElementProperties.all_properties(),
with_any_of=LinkableElementProperty.all_properties(),
# Use filter() here becasue `without_all_of` param is only available on that method.
without_all_of=frozenset(
{
LinkableElementProperties.METRIC_TIME,
LinkableElementProperties.DERIVED_TIME_GRANULARITY,
LinkableElementProperty.METRIC_TIME,
LinkableElementProperty.DERIVED_TIME_GRANULARITY,
}
),
)
Expand Down Expand Up @@ -562,7 +562,7 @@ def __init__(
),
),
join_by_semantic_model=semantic_model.reference,
properties=frozenset({LinkableElementProperties.METRIC}),
properties=frozenset({LinkableElementProperty.METRIC}),
),
)
for metric in joinable_metrics
Expand Down Expand Up @@ -601,7 +601,7 @@ def _get_joinable_metrics_for_semantic_model(self, semantic_model: SemanticModel
element_name=metric_ref.element_name,
join_by_semantic_model=semantic_model.reference,
entity_links=(entity_link,),
properties=frozenset({LinkableElementProperties.METRIC}),
properties=frozenset({LinkableElementProperty.METRIC}),
join_path=(),
)
)
Expand All @@ -625,7 +625,7 @@ def _get_elements_in_semantic_model(self, semantic_model: SemanticModel) -> Link
element_name=entity.reference.element_name,
entity_links=(),
join_path=(),
properties=frozenset({LinkableElementProperties.LOCAL, LinkableElementProperties.ENTITY}),
properties=frozenset({LinkableElementProperty.LOCAL, LinkableElementProperty.ENTITY}),
)
)
for entity_link in self._semantic_model_lookup.entity_links_for_local_elements(semantic_model):
Expand All @@ -638,12 +638,12 @@ def _get_elements_in_semantic_model(self, semantic_model: SemanticModel) -> Link
element_name=entity.reference.element_name,
entity_links=(entity_link,),
join_path=(),
properties=frozenset({LinkableElementProperties.LOCAL, LinkableElementProperties.ENTITY}),
properties=frozenset({LinkableElementProperty.LOCAL, LinkableElementProperty.ENTITY}),
)
)

for entity_link in self._semantic_model_lookup.entity_links_for_local_elements(semantic_model):
dimension_properties = frozenset({LinkableElementProperties.LOCAL})
dimension_properties = frozenset({LinkableElementProperty.LOCAL})
for dimension in semantic_model.dimensions:
dimension_type = dimension.type
if dimension_type is DimensionType.CATEGORICAL:
Expand Down Expand Up @@ -776,12 +776,12 @@ def _get_metric_time_elements(self, measure_reference: Optional[MeasureReference
# Anything that's not at the base time granularity of the measure's aggregation time dimension
# should be considered derived.
properties=(
frozenset({LinkableElementProperties.METRIC_TIME})
frozenset({LinkableElementProperty.METRIC_TIME})
if time_granularity is defined_granularity and date_part is None
else frozenset(
{
LinkableElementProperties.METRIC_TIME,
LinkableElementProperties.DERIVED_TIME_GRANULARITY,
LinkableElementProperty.METRIC_TIME,
LinkableElementProperty.DERIVED_TIME_GRANULARITY,
}
)
),
Expand Down Expand Up @@ -823,7 +823,7 @@ def _get_joined_elements(self, measure_semantic_model: SemanticModel) -> Linkabl
[
self.create_linkable_element_set_from_join_path(
join_path=join_path,
with_properties=frozenset({LinkableElementProperties.JOINED}),
with_properties=frozenset({LinkableElementProperty.JOINED}),
)
for join_path in join_paths
]
Expand All @@ -850,9 +850,7 @@ def _get_joined_elements(self, measure_semantic_model: SemanticModel) -> Linkabl
+ tuple(
self.create_linkable_element_set_from_join_path(
join_path=new_join_path,
with_properties=frozenset(
{LinkableElementProperties.JOINED, LinkableElementProperties.MULTI_HOP}
),
with_properties=frozenset({LinkableElementProperty.JOINED, LinkableElementProperty.MULTI_HOP}),
)
for new_join_path in new_join_paths
)
Expand All @@ -864,8 +862,8 @@ def _get_joined_elements(self, measure_semantic_model: SemanticModel) -> Linkabl
def _get_linkable_element_set_for_measure(
self,
measure_reference: MeasureReference,
with_any_of: FrozenSet[LinkableElementProperties] = LinkableElementProperties.all_properties(),
without_any_of: FrozenSet[LinkableElementProperties] = frozenset(),
with_any_of: FrozenSet[LinkableElementProperty] = LinkableElementProperty.all_properties(),
without_any_of: FrozenSet[LinkableElementProperty] = frozenset(),
) -> LinkableElementSet:
"""See get_linkable_element_set_for_measure()."""
measure_semantic_model = self._get_semantic_model_for_measure(measure_reference)
Expand All @@ -890,8 +888,8 @@ def _get_linkable_element_set_for_measure(
def get_linkable_element_set_for_measure(
self,
measure_reference: MeasureReference,
with_any_of: FrozenSet[LinkableElementProperties],
without_any_of: FrozenSet[LinkableElementProperties],
with_any_of: FrozenSet[LinkableElementProperty],
without_any_of: FrozenSet[LinkableElementProperty],
) -> LinkableElementSet:
"""Get the valid linkable elements for the given measure."""
return self._get_linkable_element_set_for_measure(
Expand All @@ -902,8 +900,8 @@ def get_linkable_element_set_for_measure(

def get_linkable_elements_for_distinct_values_query(
self,
with_any_of: FrozenSet[LinkableElementProperties],
without_any_of: FrozenSet[LinkableElementProperties],
with_any_of: FrozenSet[LinkableElementProperty],
without_any_of: FrozenSet[LinkableElementProperty],
) -> LinkableElementSet:
"""Returns queryable items for a distinct group-by-item values query.

Expand All @@ -914,8 +912,8 @@ def get_linkable_elements_for_distinct_values_query(
def get_linkable_elements_for_metrics(
self,
metric_references: Sequence[MetricReference],
with_any_of: FrozenSet[LinkableElementProperties] = LinkableElementProperties.all_properties(),
without_any_of: FrozenSet[LinkableElementProperties] = frozenset(),
with_any_of: FrozenSet[LinkableElementProperty] = LinkableElementProperty.all_properties(),
without_any_of: FrozenSet[LinkableElementProperty] = frozenset(),
) -> LinkableElementSet:
"""Gets the valid linkable elements that are common to all requested metrics."""
linkable_element_sets = []
Expand Down Expand Up @@ -983,7 +981,7 @@ def _find_next_possible_paths(
def create_linkable_element_set_from_join_path(
self,
join_path: SemanticModelJoinPath,
with_properties: FrozenSet[LinkableElementProperties],
with_properties: FrozenSet[LinkableElementProperty],
) -> LinkableElementSet:
"""Given the current path, generate the respective linkable elements from the last semantic model in the path."""
entity_links = tuple(x.join_on_entity for x in join_path.path_elements)
Expand Down Expand Up @@ -1031,7 +1029,7 @@ def create_linkable_element_set_from_join_path(
element_name=entity.reference.element_name,
entity_links=entity_links,
join_path=join_path.path_elements,
properties=with_properties.union({LinkableElementProperties.ENTITY}),
properties=with_properties.union({LinkableElementProperty.ENTITY}),
)
)

Expand All @@ -1041,7 +1039,7 @@ def create_linkable_element_set_from_join_path(
entity_links=entity_links,
join_path=join_path.path_elements,
join_by_semantic_model=semantic_model.reference,
properties=with_properties.union({LinkableElementProperties.METRIC}),
properties=with_properties.union({LinkableElementProperty.METRIC}),
)
for metric in self._joinable_metrics_for_semantic_models.get(join_path.last_semantic_model_reference, set())
]
Expand Down
Loading
Loading