Skip to content

Commit

Permalink
Add SQL tests for simple metrics with sub-daily dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyholcomb committed Jul 30, 2024
1 parent 4e60281 commit cf47d11
Show file tree
Hide file tree
Showing 29 changed files with 12,318 additions and 0 deletions.
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,
)

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Join Standard Outputs
-- Pass Only Elements: ['bookings', 'listing__user__bio_added_ts__minute']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_22.user__bio_added_ts__minute AS listing__user__bio_added_ts__minute
, SUM(subq_15.bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'ds_partitioned__day', 'listing']
SELECT
DATETIME_TRUNC(ds_partitioned, day) AS ds_partitioned__day
, listing_id AS listing
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_28000
) subq_15
LEFT OUTER JOIN (
-- Join Standard Outputs
-- Pass Only Elements: ['user__ds_partitioned__day', 'user__bio_added_ts__minute', 'listing']
SELECT
DATETIME_TRUNC(users_ds_source_src_28000.ds_partitioned, day) AS user__ds_partitioned__day
, DATETIME_TRUNC(users_ds_source_src_28000.bio_added_ts, minute) AS user__bio_added_ts__minute
, listings_latest_src_28000.listing_id AS listing
FROM ***************************.dim_listings_latest listings_latest_src_28000
LEFT OUTER JOIN
***************************.dim_users users_ds_source_src_28000
ON
listings_latest_src_28000.user_id = users_ds_source_src_28000.user_id
) subq_22
ON
(
subq_15.listing = subq_22.listing
) AND (
subq_15.ds_partitioned__day = subq_22.user__ds_partitioned__day
)
GROUP BY
listing__user__bio_added_ts__minute

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
user__archived_at__hour
, SUM(new_users) AS new_users
FROM (
-- Read Elements From Semantic Model 'users_ds_source'
-- Metric Time Dimension 'created_at'
-- Pass Only Elements: ['new_users', 'user__archived_at__hour']
SELECT
DATETIME_TRUNC(archived_at, hour) AS user__archived_at__hour
, 1 AS new_users
FROM ***************************.dim_users users_ds_source_src_28000
) subq_6
GROUP BY
user__archived_at__hour

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Join Standard Outputs
-- Pass Only Elements: ['bookings', 'listing__user__bio_added_ts__minute']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_22.user__bio_added_ts__minute AS listing__user__bio_added_ts__minute
, SUM(subq_15.bookings) AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'ds_partitioned__day', 'listing']
SELECT
DATE_TRUNC('day', ds_partitioned) AS ds_partitioned__day
, listing_id AS listing
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_28000
) subq_15
LEFT OUTER JOIN (
-- Join Standard Outputs
-- Pass Only Elements: ['user__ds_partitioned__day', 'user__bio_added_ts__minute', 'listing']
SELECT
DATE_TRUNC('day', users_ds_source_src_28000.ds_partitioned) AS user__ds_partitioned__day
, DATE_TRUNC('minute', users_ds_source_src_28000.bio_added_ts) AS user__bio_added_ts__minute
, listings_latest_src_28000.listing_id AS listing
FROM ***************************.dim_listings_latest listings_latest_src_28000
LEFT OUTER JOIN
***************************.dim_users users_ds_source_src_28000
ON
listings_latest_src_28000.user_id = users_ds_source_src_28000.user_id
) subq_22
ON
(
subq_15.listing = subq_22.listing
) AND (
subq_15.ds_partitioned__day = subq_22.user__ds_partitioned__day
)
GROUP BY
subq_22.user__bio_added_ts__minute
Loading

0 comments on commit cf47d11

Please sign in to comment.