Skip to content

Commit

Permalink
Test rendering for conversion metric filter (#1195)
Browse files Browse the repository at this point in the history
### Description
Rendering test for a conversion metric filter.
  • Loading branch information
courtneyholcomb authored May 10, 2024
1 parent e6ae851 commit 3be09af
Show file tree
Hide file tree
Showing 27 changed files with 6,923 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests_metricflow/query_rendering/test_metric_filter_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,29 @@ def test_group_by_has_local_entity_prefix( # noqa: D103
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)


@pytest.mark.sql_engine_snapshot
def test_filter_with_conversion_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
sql_client: SqlClient,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
query_parser: MetricFlowQueryParser,
) -> None:
query_spec = query_parser.parse_and_validate_query(
metric_names=("listings",),
where_constraint=PydanticWhereFilter(
where_sql_template="{{ Metric('visit_buy_conversion_rate', ['user']) }} > 2",
),
)
dataflow_plan = dataflow_plan_builder.build_plan(query_spec)

convert_and_check(
request=request,
mf_test_configuration=mf_test_configuration,
dataflow_to_sql_converter=dataflow_to_sql_converter,
sql_client=sql_client,
node=dataflow_plan.sink_output_nodes[0].parent_node,
)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
-- Constrain Output with WHERE
-- Pass Only Elements: ['listings',]
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
SUM(listings) AS listings
FROM (
-- Join Standard Outputs
-- Pass Only Elements: ['listings', 'user__visit_buy_conversion_rate']
SELECT
CAST(subq_57.buys AS FLOAT64) / CAST(NULLIF(subq_57.visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate
, subq_42.listings AS listings
FROM (
-- Read Elements From Semantic Model 'listings_latest'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['listings', 'user']
SELECT
user_id AS user
, 1 AS listings
FROM ***************************.dim_listings_latest listings_latest_src_28000
) subq_42
LEFT OUTER JOIN (
-- Combine Aggregated Outputs
SELECT
COALESCE(subq_46.user, subq_56.user) AS user
, MAX(subq_46.visits) AS visits
, MAX(subq_56.buys) AS buys
FROM (
-- Aggregate Measures
SELECT
subq_45.user
, SUM(visits) AS visits
FROM (
-- Read Elements From Semantic Model 'visits_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['visits', 'user']
SELECT
user_id AS user
, 1 AS visits
FROM ***************************.fct_visits visits_source_src_28000
) subq_45
GROUP BY
user
) subq_46
FULL OUTER JOIN (
-- Find conversions for user within the range of INF
-- Pass Only Elements: ['buys', 'user']
-- Aggregate Measures
SELECT
subq_53.user
, SUM(buys) AS buys
FROM (
-- Dedupe the fanout with mf_internal_uuid in the conversion data set
SELECT DISTINCT
first_value(subq_49.visits) OVER (
PARTITION BY
subq_52.user
, subq_52.ds__day
, subq_52.mf_internal_uuid
ORDER BY subq_49.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS visits
, first_value(subq_49.ds__day) OVER (
PARTITION BY
subq_52.user
, subq_52.ds__day
, subq_52.mf_internal_uuid
ORDER BY subq_49.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS ds__day
, first_value(subq_49.user) OVER (
PARTITION BY
subq_52.user
, subq_52.ds__day
, subq_52.mf_internal_uuid
ORDER BY subq_49.ds__day DESC
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS user
, subq_52.mf_internal_uuid AS mf_internal_uuid
, subq_52.buys AS buys
FROM (
-- Read Elements From Semantic Model 'visits_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['visits', 'ds__day', 'user']
SELECT
DATE_TRUNC(ds, day) AS ds__day
, user_id AS user
, 1 AS visits
FROM ***************************.fct_visits visits_source_src_28000
) subq_49
INNER JOIN (
-- Read Elements From Semantic Model 'buys_source'
-- Metric Time Dimension 'ds'
-- Add column with generated UUID
SELECT
DATE_TRUNC(ds, day) AS ds__day
, user_id AS user
, 1 AS buys
, GENERATE_UUID() AS mf_internal_uuid
FROM ***************************.fct_buys buys_source_src_28000
) subq_52
ON
(
subq_49.user = subq_52.user
) AND (
(subq_49.ds__day <= subq_52.ds__day)
)
) subq_53
GROUP BY
user
) subq_56
ON
subq_46.user = subq_56.user
GROUP BY
user
) subq_57
ON
subq_42.user = subq_57.user
) subq_61
WHERE user__visit_buy_conversion_rate > 2
Loading

0 comments on commit 3be09af

Please sign in to comment.