Skip to content

Commit

Permalink
not mutating time_dimension_factory.time_dimension_specs in WhereFilt…
Browse files Browse the repository at this point in the history
…erDimension
  • Loading branch information
DevonFulcher committed Sep 21, 2023
1 parent 6470b08 commit d8640fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
4 changes: 0 additions & 4 deletions metricflow/protocols/query_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def grain(self, _grain: str) -> QueryInterfaceDimension:
"""The time granularity."""
raise NotImplementedError

def alias(self, _alias: str) -> QueryInterfaceDimension:
"""Renaming the column."""
raise NotImplementedError

def descending(self, _is_descending: bool) -> QueryInterfaceDimension:
"""Set the sort order for order-by."""

Expand Down
34 changes: 12 additions & 22 deletions metricflow/specs/where_filter_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,37 @@ def __init__( # noqa
entity_path: Sequence[str],
call_parameter_sets: FilterCallParameterSets,
column_association_resolver: ColumnAssociationResolver,
time_dimension_specs: List[TimeDimensionSpec],
) -> None:
self._dimension_spec_resolver = DimensionSpecResolver(call_parameter_sets)
self.name = name
self.spec = self._dimension_spec_resolver.resolve_dimension_spec(name, entity_path)
self._column_association_resolver = column_association_resolver
self.entity_path = entity_path
self.time_granularity: Optional[TimeGranularity] = None
self._time_dimension_specs = time_dimension_specs
self._name = name
self._entity_path = entity_path
self.dimension_spec = self._dimension_spec_resolver.resolve_dimension_spec(name, entity_path)
self.time_dimension_spec: Optional[TimeDimensionSpec] = None

def grain(self, time_granularity_name: str) -> QueryInterfaceDimension:
"""The time granularity."""
self.time_granularity = TimeGranularity(time_granularity_name)
self.spec = self._dimension_spec_resolver.resolve_time_dimension_spec(
self.name, self.time_granularity, self.entity_path
self.time_dimension_spec = self._dimension_spec_resolver.resolve_time_dimension_spec(
self._name, TimeGranularity(time_granularity_name), self._entity_path
)
self._time_dimension_specs.append(self.spec)
return self

def date_part(self, _date_part: str) -> QueryInterfaceDimension:
"""The date_part requested to extract."""
raise NotImplementedError

def alias(self, _alias: str) -> QueryInterfaceDimension:
"""Renaming the column."""
raise NotImplementedError
raise InvalidQuerySyntax("date_part isn't currently supported in the where parameter and filter spec")

def descending(self, _is_descending: bool) -> QueryInterfaceDimension:
"""Set the sort order for order-by."""
raise InvalidQuerySyntax(
"Can't set descending in the where clause. Try setting descending in the order_by clause instead"
)
raise InvalidQuerySyntax("descending is invalid in the where parameter and filter spec")

def __str__(self) -> str:
"""Returns the column name.
Important in the Jinja sandbox.
"""
return self._column_association_resolver.resolve_spec(self.spec).column_name
return self._column_association_resolver.resolve_spec(
self.time_dimension_spec or self.dimension_spec
).column_name


class WhereFilterDimensionFactory(ProtocolHint[QueryInterfaceDimensionFactory]):
Expand All @@ -87,17 +79,15 @@ def __init__( # noqa
self,
call_parameter_sets: FilterCallParameterSets,
column_association_resolver: ColumnAssociationResolver,
time_dimension_specs: List[TimeDimensionSpec],
):
self._call_parameter_sets = call_parameter_sets
self._column_association_resolver = column_association_resolver
self._time_dimension_specs = time_dimension_specs
self.created: List[WhereFilterDimension] = []

def create(self, name: str, entity_path: Sequence[str] = ()) -> WhereFilterDimension:
"""Create a WhereFilterDimension."""
dimension = WhereFilterDimension(
name, entity_path, self._call_parameter_sets, self._column_association_resolver, self._time_dimension_specs
name, entity_path, self._call_parameter_sets, self._column_association_resolver
)
self.created.append(dimension)
return dimension
12 changes: 6 additions & 6 deletions metricflow/specs/where_filter_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dbt_semantic_interfaces.protocols.where_filter import WhereFilter

from metricflow.specs.column_assoc import ColumnAssociationResolver
from metricflow.specs.specs import LinkableSpecSet, TimeDimensionSpec, WhereFilterSpec
from metricflow.specs.specs import LinkableSpecSet, WhereFilterSpec
from metricflow.specs.where_filter_dimension import WhereFilterDimensionFactory
from metricflow.specs.where_filter_entity import WhereFilterEntityFactory
from metricflow.specs.where_filter_time_dimension import WhereFilterTimeDimensionFactory
Expand Down Expand Up @@ -35,9 +35,7 @@ def create_from_where_filter(self, where_filter: WhereFilter) -> WhereFilterSpec
call_parameter_sets = where_filter.call_parameter_sets

time_dimension_factory = WhereFilterTimeDimensionFactory(call_parameter_sets, self._column_association_resolver)
dimension_factory = WhereFilterDimensionFactory(
call_parameter_sets, self._column_association_resolver, time_dimension_factory.time_dimension_specs
)
dimension_factory = WhereFilterDimensionFactory(call_parameter_sets, self._column_association_resolver)
entity_factory = WhereFilterEntityFactory(call_parameter_sets, self._column_association_resolver)
try:
rendered_sql_template = jinja2.Template(
Expand All @@ -56,8 +54,10 @@ def create_from_where_filter(self, where_filter: WhereFilter) -> WhereFilterSpec

dimension_specs = []
for dimension in dimension_factory.created:
if not dimension.time_granularity:
dimension_specs.append(dimension.spec)
if dimension.time_dimension_spec:
time_dimension_factory.time_dimension_specs.append(dimension.time_dimension_spec)
else:
dimension_specs.append(dimension.dimension_spec)

return WhereFilterSpec(
where_sql=rendered_sql_template,
Expand Down
11 changes: 0 additions & 11 deletions metricflow/test/specs/test_where_filter_dimension.py

This file was deleted.

0 comments on commit d8640fc

Please sign in to comment.