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 simple metrics with sub-daily dimensions #1360

Merged
merged 3 commits into from
Jul 30, 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 @@ -7,6 +7,9 @@ semantic_model:
schema_name: $source_schema
alias: dim_users

defaults:
agg_time_dimension: created_at

dimensions:
- name: ds
type: time
Expand Down Expand Up @@ -45,3 +48,9 @@ semantic_model:
- name: user
type: primary
expr: user_id

measures:
- name: new_users
expr: "1"
agg: SUM
create_metric: true
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
'user__listing__user__views_times_booking_value',
'user__listings',
'user__lux_listings',
'user__new_users',
'user__popular_listing_bookings_per_booker',
'user__regional_starting_balance_ratios',
'user__revenue',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'max_booking_value',
'median_booking_value',
'min_booking_value',
'new_users',
'referred_bookings',
'smallest_listing',
'total_account_balance_first_day',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ Model Join-Path Entity Links
('listings_latest',) ("('user',)", "('user',)") largest_listing ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") lux_listings ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") new_users ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") popular_listing_bookings_per_booker ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") regional_starting_balance_ratios ['JOINED', 'METRIC']
('listings_latest',) ("('user',)", "('user',)") revenue ['JOINED', 'METRIC']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
'company__user__company__largest_listing',
'company__user__company__listings',
'company__user__company__lux_listings',
'company__user__company__new_users',
'company__user__company__popular_listing_bookings_per_booker',
'company__user__company__regional_starting_balance_ratios',
'company__user__company__revenue',
Expand Down Expand Up @@ -559,6 +560,7 @@
'user__listing__user__views_times_booking_value',
'user__listings',
'user__lux_listings',
'user__new_users',
'user__popular_listing_bookings_per_booker',
'user__regional_starting_balance_ratios',
'user__revenue',
Expand Down
50 changes: 50 additions & 0 deletions tests_metricflow/integration/test_cases/itest_metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2215,3 +2215,53 @@ integration_test:
GROUP BY {{ render_date_trunc("ds", TimeGranularity.DAY) }}
) a ON b.ds = a.ds
WHERE {{ render_between_time_constraint("b.ds", "2020-01-03", "2020-01-03") }}
---
integration_test:
name: simple_metric_with_sub_daily_dimension
description: Tests querying a metric with a sub-daily dimension
model: SIMPLE_MODEL
metrics: ["new_users"]
group_bys: ["user__last_profile_edit_ts__millisecond"]
check_query: |
SELECT
{{ render_date_trunc("last_profile_edit_ts", TimeGranularity.MILLISECOND) }} AS user__last_profile_edit_ts__millisecond
, SUM(1) AS new_users
FROM {{ source_schema }}.dim_users
GROUP BY {{ render_date_trunc("last_profile_edit_ts", TimeGranularity.MILLISECOND) }}
---
integration_test:
name: simple_metric_with_joined_sub_daily_dimension
description: Tests querying a metric with a sub-daily dimension that requires a join
model: SIMPLE_MODEL
metrics: ["bookings"]
group_bys: ["user__last_login_ts__minute"]
check_query: |
SELECT
subq_8.user__last_login_ts__minute AS listing__user__last_login_ts__minute
, SUM(subq_2.bookings) AS bookings
FROM (
SELECT
{{ render_date_trunc("ds_partitioned", TimeGranularity.DAY) }} AS ds_partitioned__day
, listing_id AS listing
, 1 AS bookings
FROM {{ source_schema }}.fct_bookings
) subq_2
LEFT OUTER JOIN (
SELECT
{{ render_date_trunc("u.ds_partitioned", TimeGranularity.DAY) }} AS user__ds_partitioned__day
, {{ render_date_trunc("u.last_login_ts", TimeGranularity.MINUTE) }} AS user__last_login_ts__minute
, l.listing_id AS listing
FROM {{ source_schema }}.dim_listings_latest l
LEFT OUTER JOIN
{{ source_schema }}.dim_users u
ON
l.user_id = u.user_id
) subq_8
ON
(
subq_2.listing = subq_8.listing
) AND (
subq_2.ds_partitioned__day = subq_8.user__ds_partitioned__day
)
GROUP BY
subq_8.user__last_login_ts__minute
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,64 @@ def test_sub_daily_dimension( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_simple_metric_with_sub_daily_dimension( # 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("new_users"),),
time_dimension_specs=(
TimeDimensionSpec(
element_name="archived_at",
time_granularity=TimeGranularity.HOUR,
entity_links=(EntityReference("user"),),
),
),
)

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_simple_metric_with_joined_sub_daily_dimension( # 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("bookings"),),
time_dimension_specs=(
TimeDimensionSpec(
element_name="bio_added_ts",
time_granularity=TimeGranularity.MINUTE,
entity_links=(
EntityReference("listing"),
EntityReference("user"),
),
),
),
)

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_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 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_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 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_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 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_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -753,7 +753,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -813,7 +813,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -697,7 +697,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down Expand Up @@ -801,7 +801,7 @@
<!-- node_id = NodeId(id_str='ss_28011') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28008 sql_expr=1), -->
<!-- column_alias='visits', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<!-- distinct = False -->
<MetricTimeDimensionTransformNode>
<!-- description = "Metric Time Dimension 'ds'" -->
<!-- node_id = NodeId(id_str='sma_28014') -->
<!-- node_id = NodeId(id_str='sma_28015') -->
<!-- aggregation_time_dimension = 'ds' -->
<ReadSqlSourceNode>
<!-- description = "Read From SemanticModelDataSet('views_source')" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,7 @@
<!-- node_id = NodeId(id_str='ss_28010') -->
<!-- col0 = -->
<!-- SqlSelectColumn( -->
<!-- expr=SqlStringExpression(node_id=str_28006 sql_expr=1), -->
<!-- expr=SqlStringExpression(node_id=str_28007 sql_expr=1), -->
<!-- column_alias='views', -->
<!-- ) -->
<!-- col1 = -->
Expand Down
Loading
Loading