Skip to content

Commit

Permalink
Allow passing granularity in where filter TimeDimension name (#1316)
Browse files Browse the repository at this point in the history
Allow users to use dunder syntax to pass granularity in where filter
`TimeDimension`. Ex:
`{{ TimeDimension('metric_time__month') }} > 2020-01-01`
This behavior is currently implemented everywhere else that similar
Jinja parsing happens, making this one site inconsistent. Fix that to
avoid user confusion.
  • Loading branch information
courtneyholcomb authored Jul 9, 2024
1 parent 7c5a2c0 commit 3fd75e8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# dbt Cloud depends on metricflow-semantics (dependency set in dbt-mantle), so DSI must always point to a production version here.
dbt-semantic-interfaces>=0.6.1, <2.0.0
dbt-semantic-interfaces>=0.6.5, <2.0.0
graphviz>=0.18.2, <0.21
python-dateutil>=2.9.0, <2.10.0
rapidfuzz>=3.0, <4.0
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def create(
)
structured_name = DunderedNameFormatter.parse_name(time_dimension_name.lower())

grain_parsed_from_name = structured_name.time_granularity
grain_from_param = TimeGranularity(time_granularity_name) if time_granularity_name else None
if grain_parsed_from_name and grain_from_param and grain_parsed_from_name != grain_from_param:
raise InvalidQuerySyntax(
f"Received different granularities in `time_dimension_name` parameter ('{time_dimension_name}') "
f"and `time_granularity_name` parameter ('{time_granularity_name}')."
)

TimeGranularity(time_granularity_name.lower()) if time_granularity_name else None
return WhereFilterTimeDimension(
column_association_resolver=self._column_association_resolver,
resolved_spec_lookup=self._resolved_spec_lookup,
Expand All @@ -99,6 +108,6 @@ def create(
element_name=structured_name.element_name,
entity_links=tuple(EntityReference(entity_link_name.lower()) for entity_link_name in entity_path)
+ structured_name.entity_links,
time_grain=TimeGranularity(time_granularity_name.lower()) if time_granularity_name else None,
time_grain=grain_from_param or grain_parsed_from_name,
date_part=DatePart(date_part_name.lower()) if date_part_name else None,
)
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,70 @@ def test_time_dimension_in_filter( # noqa: D103
)


def test_time_dimension_with_grain_in_name( # noqa: D103
column_association_resolver: ColumnAssociationResolver,
) -> None:
where_filter_specs = WhereSpecFactory(
column_association_resolver=column_association_resolver,
spec_resolution_lookup=create_spec_lookup(
call_parameter_set=TimeDimensionCallParameterSet(
entity_path=(EntityReference("listing"),),
time_dimension_reference=TimeDimensionReference("created_at"),
time_granularity=TimeGranularity.MONTH,
),
resolved_spec=TimeDimensionSpec(
element_name="created_at",
entity_links=(EntityReference("listing"),),
time_granularity=TimeGranularity.MONTH,
),
resolved_linkable_element_set=LinkableElementSet(
path_key_to_linkable_dimensions={
ElementPathKey(
element_name="created_at",
element_type=LinkableElementType.TIME_DIMENSION,
entity_links=(EntityReference("listing"),),
time_granularity=TimeGranularity.MONTH,
date_part=None,
): (
LinkableDimension(
defined_in_semantic_model=SemanticModelReference("listings_source"),
dimension_type=DimensionType.CATEGORICAL,
element_name="created_at",
entity_links=(EntityReference("listing"),),
join_path=SemanticModelJoinPath(
left_semantic_model_reference=SemanticModelReference("bookings_source"),
),
properties=frozenset(),
time_granularity=TimeGranularity.MONTH,
date_part=None,
),
)
}
),
),
).create_from_where_filter_intersection(
filter_location=EXAMPLE_FILTER_LOCATION,
filter_intersection=create_where_filter_intersection(
"{{ TimeDimension('listing__created_at__month') }} = '2020-01-01'"
),
)
assert len(where_filter_specs) == 1
where_filter_spec = where_filter_specs[0]
assert where_filter_spec.where_sql == "listing__created_at__month = '2020-01-01'"
assert LinkableSpecSet.create_from_specs(where_filter_spec.linkable_specs) == LinkableSpecSet(
dimension_specs=(),
time_dimension_specs=(
TimeDimensionSpec(
element_name="created_at",
entity_links=(EntityReference(element_name="listing"),),
time_granularity=TimeGranularity.MONTH,
),
),
entity_specs=(),
group_by_metric_specs=(),
)


def test_date_part_in_filter( # noqa: D103
column_association_resolver: ColumnAssociationResolver,
) -> None:
Expand Down

0 comments on commit 3fd75e8

Please sign in to comment.