Skip to content

Commit

Permalink
WIP - enable object entity syntax in metric filters
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jun 5, 2024
1 parent ead98b2 commit ccd6665
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Sequence
from typing import Optional, Sequence, Union

from dbt_semantic_interfaces.call_parameter_sets import (
DimensionCallParameterSet,
Expand All @@ -12,6 +12,7 @@
METRIC_TIME_ELEMENT_NAME,
is_metric_time_name,
)
from dbt_semantic_interfaces.parsing.where_filter.where_filter_stubs import EntityStub
from dbt_semantic_interfaces.references import (
DimensionReference,
EntityReference,
Expand Down Expand Up @@ -106,7 +107,7 @@ def create_entity(entity_name: str, entity_path: Sequence[str] = ()) -> EntityCa
)

@staticmethod
def create_metric(metric_name: str, group_by: Sequence[str] = ()) -> MetricCallParameterSet:
def create_metric(metric_name: str, group_by: Sequence[Union[str, EntityStub]] = ()) -> MetricCallParameterSet:
"""Gets called by Jinja when rendering {{ Metric(...) }}."""
if not group_by:
raise ParseWhereFilterException(
Expand All @@ -115,5 +116,15 @@ def create_metric(metric_name: str, group_by: Sequence[str] = ()) -> MetricCallP
)
return MetricCallParameterSet(
metric_reference=MetricReference(element_name=metric_name),
group_by=tuple([LinkableElementReference(element_name=group_by_name) for group_by_name in group_by]),
group_by=tuple(
[
LinkableElementReference(
# TODO: add entity_links
group_by_item
if isinstance(group_by_item, str)
else group_by_item.element_name
)
for group_by_item in group_by
]
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,15 @@
from dbt_semantic_interfaces.parsing.where_filter.parameter_set_factory import (
ParameterSetFactory,
)
from dbt_semantic_interfaces.parsing.where_filter.where_filter_stubs import EntityStub
from dbt_semantic_interfaces.protocols.protocol_hint import ProtocolHint
from dbt_semantic_interfaces.protocols.query_interface import (
QueryInterfaceEntity,
QueryInterfaceEntityFactory,
QueryInterfaceMetric,
QueryInterfaceMetricFactory,
)


class EntityStub(ProtocolHint[QueryInterfaceEntity]):
"""An Entity implementation that just satisfies the protocol.
QueryInterfaceEntity currently has no methods and the parameter set is created in the factory.
So, there is nothing to do here.
"""

@override
def _implements_protocol(self) -> QueryInterfaceEntity:
return self


class MetricStub(ProtocolHint[QueryInterfaceMetric]):
"""A Metric implementation that just satisfies the protocol.
Expand Down Expand Up @@ -60,7 +48,7 @@ def __init__(self) -> None: # noqa
def create(self, entity_name: str, entity_path: Sequence[str] = ()) -> EntityStub:
"""Gets called by Jinja when rendering {{ Entity(...) }}."""
self.entity_call_parameter_sets.append(ParameterSetFactory.create_entity(entity_name, entity_path))
return EntityStub()
return EntityStub(element_name=entity_name, entity_links=entity_path)


class WhereFilterMetricFactory(ProtocolHint[QueryInterfaceMetricFactory]):
Expand Down
21 changes: 21 additions & 0 deletions dbt_semantic_interfaces/parsing/where_filter/where_filter_stubs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from dataclasses import dataclass
from typing import Sequence
from typing_extensions import override
from dbt_semantic_interfaces.protocols.protocol_hint import ProtocolHint
from dbt_semantic_interfaces.protocols.query_interface import QueryInterfaceEntity


@dataclass
class EntityStub(ProtocolHint[QueryInterfaceEntity]):
"""An Entity implementation that just satisfies the protocol.
QueryInterfaceEntity currently has no methods and the parameter set is created in the factory.
So, there is nothing to do here.
"""

element_name: str
entity_links: Sequence[str] = ()

@override
def _implements_protocol(self) -> QueryInterfaceEntity:
return self

0 comments on commit ccd6665

Please sign in to comment.