Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for metrics that require time spine with sub-daily granularity #1361

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,55 @@ semantic_model:
expr: "1"
agg: SUM
create_metric: true
- name: archived_users
expr: "1"
agg: SUM
create_metric: true
agg_time_dimension: archived_at
---
metric:
name: subdaily_cumulative_window_metric
description: cumulative window metric with a sub-daily agg time dim
type: cumulative
type_params:
measure: archived_users
cumulative_type_params:
window: 3 hours
---
metric:
name: subdaily_cumulative_grain_to_date_metric
description: cumulative grain to date metric with a sub-daily agg time dim
type: cumulative
type_params:
measure: archived_users
cumulative_type_params:
grain_to_date: hour
---
metric:
name: subdaily_offset_window_metric
description: offset window metric with a sub-daily agg time dim
type: derived
type_params:
expr: archived_users
metrics:
- name: archived_users
offset_window: 1 hour
---
metric:
name: subdaily_offset_grain_to_date_metric
description: offset grain to date metric with a sub-daily agg time dim
type: derived
type_params:
expr: archived_users
metrics:
- name: archived_users
offset_to_grain: hour
---
metric:
name: subdaily_join_to_time_spine_metric
description: simple metric with sub-daily agg time dim that joins to time spine
type: simple
type_params:
measure:
name: archived_users
join_to_timespine: true
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
'user__archived_at__quarter',
'user__archived_at__week',
'user__archived_at__year',
'user__archived_users',
'user__bio_added_ts__day',
'user__bio_added_ts__extract_day',
'user__bio_added_ts__extract_day',
Expand Down Expand Up @@ -456,6 +457,7 @@
'user__revenue_instance__user__revenue',
'user__revenue_instance__user__revenue_all_time',
'user__smallest_listing',
'user__subdaily_join_to_time_spine_metric',
'user__total_account_balance_first_day',
'user__verification__user__identity_verifications',
'user__view__user__views',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'account_balance',
'approximate_continuous_booking_value_p99',
'approximate_discrete_booking_value_p99',
'archived_users',
'average_booking_value',
'bookers',
'booking_payments',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('user',)", "('revenue_instance', 'user')") revenue ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('revenue_instance', 'user')") revenue_all_time ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") active_listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") archived_users ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") current_account_balance_by_user ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") identity_verifications ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") largest_listing ['JOINED', 'METRIC']
Expand All @@ -188,6 +189,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('user',)", "('user',)") revenue ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") revenue_all_time ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") smallest_listing ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") subdaily_join_to_time_spine_metric ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") total_account_balance_first_day ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") views ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") visit_buy_conversion_rate ['JOINED', 'METRIC']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
'company__listing__user__company__views_times_booking_value',
'company__user',
'company__user__company__active_listings',
'company__user__company__archived_users',
'company__user__company__current_account_balance_by_user',
'company__user__company__identity_verifications',
'company__user__company__largest_listing',
Expand All @@ -99,6 +100,7 @@
'company__user__company__revenue',
'company__user__company__revenue_all_time',
'company__user__company__smallest_listing',
'company__user__company__subdaily_join_to_time_spine_metric',
'company__user__company__total_account_balance_first_day',
'company__user__company__views',
'company__user__company__visit_buy_conversion_rate',
Expand Down Expand Up @@ -452,6 +454,7 @@
'user__archived_at__extract_quarter',
'user__archived_at__extract_year',
'user__archived_at__hour',
'user__archived_users',
'user__bio_added_ts__extract_day',
'user__bio_added_ts__extract_dow',
'user__bio_added_ts__extract_doy',
Expand Down Expand Up @@ -568,6 +571,7 @@
'user__revenue_instance__user__revenue',
'user__revenue_instance__user__revenue_all_time',
'user__smallest_listing',
'user__subdaily_join_to_time_spine_metric',
'user__total_account_balance_first_day',
'user__verification__user__identity_verifications',
'user__view__user__views',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,43 @@ integration_test:
, metric_time__year
, t2mr
) subq_9
---
integration_test:
name: subdaily_cumulative_window_metric
description: test a subdaily cumulative window metric
model: SIMPLE_MODEL
metrics: ["subdaily_cumulative_window_metric"]
group_bys: ["metric_time__hour"]
check_query: |
SELECT
subq_3.ts AS metric_time__hour
, SUM(subq_1.archived_users) AS subdaily_cumulative_window_metric
FROM {{ source_schema }}.mf_time_spine_hour subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, 1 AS archived_users
FROM {{ source_schema }}.dim_users
) subq_1
ON (subq_1.metric_time__hour <= subq_3.ts) AND (subq_1.metric_time__hour > {{ render_date_sub("subq_3", "Ts", 3, TimeGranularity.HOUR) }})
GROUP BY subq_3.ts
---
integration_test:
name: subdaily_cumulative_grain_to_date_metric
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oooo, I need to make sure we can do grain to date with custom granularities...... duly noted.

description: test a subdaily cumulative grain_to_date metric
model: SIMPLE_MODEL
metrics: ["subdaily_cumulative_grain_to_date_metric"]
group_bys: ["metric_time__hour"]
check_query: |
SELECT
subq_3.ts AS metric_time__hour
, SUM(subq_1.archived_users) AS subdaily_cumulative_grain_to_date_metric
FROM {{ source_schema }}.mf_time_spine_hour subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, 1 AS archived_users
FROM {{ source_schema }}.dim_users
) subq_1
ON (subq_1.metric_time__hour <= subq_3.ts) AND (subq_1.metric_time__hour >= {{ render_date_trunc("subq_3.ts", TimeGranularity.HOUR) }})
GROUP BY subq_3.ts
65 changes: 65 additions & 0 deletions tests_metricflow/integration/test_cases/itest_metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2265,3 +2265,68 @@ integration_test:
)
GROUP BY
subq_8.user__last_login_ts__minute
---
integration_test:
name: subdaily_offset_window_metric
description: test a subdaily offset window metric
model: SIMPLE_MODEL
metrics: ["subdaily_offset_window_metric"]
group_bys: ["metric_time__hour"]
check_query: |
SELECT
subq_3.ts AS metric_time__hour
, SUM(subq_1.archived_users) AS subdaily_offset_window_metric
FROM {{ source_schema }}.mf_time_spine_hour subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, 1 AS archived_users
FROM {{ source_schema }}.dim_users
) subq_1
ON {{ render_date_sub("subq_3", "ts", 1, TimeGranularity.HOUR) }} = subq_1.metric_time__hour
GROUP BY subq_3.ts
---
integration_test:
name: subdaily_offset_grain_to_date_metric
description: test a subdaily offset to grain metric
model: SIMPLE_MODEL
metrics: ["subdaily_offset_grain_to_date_metric"]
group_bys: ["metric_time__hour"]
check_query: |
SELECT
subq_3.ts AS metric_time__hour
, SUM(subq_1.archived_users) AS subdaily_offset_grain_to_date_metric
FROM {{ source_schema }}.mf_time_spine_hour subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, 1 AS archived_users
FROM {{ source_schema }}.dim_users
) subq_1
ON {{ render_date_trunc("subq_3.ts", TimeGranularity.HOUR) }} = subq_1.metric_time__hour
GROUP BY subq_3.ts
---
integration_test:
name: subdaily_join_to_time_spine_metric
description: test a subdaily join to time spine metric
model: SIMPLE_MODEL
metrics: ["subdaily_join_to_time_spine_metric"]
group_bys: ["metric_time__hour"]
check_query: |
SELECT
subq_5.ts AS metric_time__hour
, subq_3.archived_users AS subdaily_join_to_time_spine_metric
FROM {{ source_schema }}.mf_time_spine_hour subq_5
LEFT OUTER JOIN (
SELECT
metric_time__hour
, SUM(archived_users) AS archived_users
FROM (
SELECT
{{ render_date_trunc("archived_at", TimeGranularity.HOUR) }} AS metric_time__hour
, 1 AS archived_users
FROM {{ source_schema }}.dim_users
) subq_2
GROUP BY metric_time__hour
) subq_3
ON subq_5.ts = subq_3.metric_time__hour
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,118 @@ def test_simple_metric_with_joined_sub_daily_dimension( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_cumulative_window_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("subdaily_cumulative_window_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_cumulative_grain_to_date_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("subdaily_cumulative_grain_to_date_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_offset_window_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("subdaily_offset_window_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_offset_to_grain_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("subdaily_offset_grain_to_date_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
)

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,
)


@pytest.mark.sql_engine_snapshot
def test_subdaily_join_to_time_spine_metric( # noqa: D103
request: FixtureRequest,
mf_test_configuration: MetricFlowTestConfiguration,
dataflow_plan_builder: DataflowPlanBuilder,
dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter,
sql_client: SqlClient,
) -> None:
query_spec = MetricFlowQuerySpec(
metric_specs=(MetricSpec("subdaily_join_to_time_spine_metric"),),
time_dimension_specs=(DataSet.metric_time_dimension_spec(time_granularity=TimeGranularity.HOUR),),
)

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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28009 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -694,7 +694,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28009 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28009 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -860,7 +860,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28009 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Loading
Loading