Skip to content

Commit

Permalink
WIP - custom grain for conversion metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Oct 18, 2024
1 parent c095335 commit 9f08b9f
Show file tree
Hide file tree
Showing 6 changed files with 836 additions and 10 deletions.
16 changes: 9 additions & 7 deletions metricflow/dataflow/builder/dataflow_plan_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,12 @@ def _build_aggregated_conversion_node(
unaggregated_base_measure_node = JoinOnEntitiesNode.create(
left_node=unaggregated_base_measure_node, join_targets=base_measure_recipe.join_targets
)
# TODO: write explanatory comment; or maybe refactor so this doesn't need to live here
filter_to_specs = base_required_linkable_specs.replace_custom_granularity_with_base_granularity()
filtered_unaggregated_base_node = FilterElementsNode.create(
parent_node=unaggregated_base_measure_node,
include_specs=group_specs_by_type(required_local_specs)
.merge(InstanceSpecSet.create_from_specs(base_required_linkable_specs.as_tuple))
.merge(InstanceSpecSet.create_from_specs(filter_to_specs.as_tuple))
.dedupe(),
)

Expand Down Expand Up @@ -1696,12 +1698,12 @@ def _build_aggregated_measure_from_measure_source_node(
non_additive_dimension_grain = self._semantic_model_lookup.get_defined_time_granularity(
TimeDimensionReference(non_additive_dimension_spec.name)
)
queried_time_dimension_spec: Optional[
TimeDimensionSpec
] = self._find_non_additive_dimension_in_linkable_specs(
agg_time_dimension=agg_time_dimension,
linkable_specs=queried_linkable_specs.as_tuple,
non_additive_dimension_spec=non_additive_dimension_spec,
queried_time_dimension_spec: Optional[TimeDimensionSpec] = (
self._find_non_additive_dimension_in_linkable_specs(
agg_time_dimension=agg_time_dimension,
linkable_specs=queried_linkable_specs.as_tuple,
non_additive_dimension_spec=non_additive_dimension_spec,
)
)
time_dimension_spec = TimeDimensionSpec(
# The NonAdditiveDimensionSpec name property is a plain element name
Expand Down
25 changes: 22 additions & 3 deletions tests_metricflow/query_rendering/test_custom_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,25 @@ def test_offset_metric_with_custom_granularity_filter_not_in_group_by( # noqa:
)


# Tests TODO:
# - join_to_timespine metric
# - join_to_timespine metric with filter on custom grain that's not in the group by (dun dun dun)
@pytest.mark.sql_engine_snapshot
def test_conversion_metric_with_custom_granularity( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
query_parser: MetricFlowQueryParser,
) -> None:
query_spec = query_parser.parse_and_validate_query(
metric_names=("visit_buy_conversion_rate_7days",),
group_by_names=("metric_time__martian_day",),
).query_spec

render_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)
Loading

0 comments on commit 9f08b9f

Please sign in to comment.