Skip to content

Commit

Permalink
Allow granularity name in where filter TimeDimension name
Browse files Browse the repository at this point in the history
This is needed to match behavior elsewhere where Jinja parsing happens
  • Loading branch information
courtneyholcomb committed Jul 9, 2024
1 parent 5e9dc58 commit 94f84de
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
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 94f84de

Please sign in to comment.