Skip to content

Commit

Permalink
SQL rendering tests for custom granularity time dimensions requiring …
Browse files Browse the repository at this point in the history
…join (#1414)

SQL rendering tests for querying time dimensions that require a join
with custom granularity.
  • Loading branch information
courtneyholcomb authored Sep 24, 2024
1 parent 41e555f commit 9f6af7e
Show file tree
Hide file tree
Showing 16 changed files with 2,906 additions and 1 deletion.
1 change: 0 additions & 1 deletion metricflow/plan_conversion/dataflow_to_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,6 @@ def _get_time_spine_for_custom_granularity(self, custom_granularity: str) -> Tim
def _get_custom_granularity_column_name(self, custom_granularity_name: str) -> str:
time_spine_source = self._get_time_spine_for_custom_granularity(custom_granularity_name)
for custom_granularity in time_spine_source.custom_granularities:
print(custom_granularity)
if custom_granularity.name == custom_granularity_name:
return custom_granularity.column_name if custom_granularity.column_name else custom_granularity.name

Expand Down
29 changes: 29 additions & 0 deletions tests_metricflow/query_rendering/test_custom_granularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,32 @@ def test_no_metric_custom_granularity_metric_time( # noqa: D103
dataflow_plan_builder=dataflow_plan_builder,
query_spec=query_spec,
)


@pytest.mark.sql_engine_snapshot
def test_simple_metric_with_custom_granularity_and_join( # 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="ds",
time_granularity=ExpandedTimeGranularity(name="martian_day", base_granularity=TimeGranularity.DAY),
entity_links=(EntityReference("listing"),),
),
),
)

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,33 @@
-- Pass Only Elements: ['bookings', 'listing__ds__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'listing__ds__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_18.martian_day AS listing__ds__martian_day
, SUM(subq_17.bookings) AS bookings
FROM (
-- Join Standard Outputs
SELECT
DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS listing__ds__day
, subq_13.bookings AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'listing']
SELECT
listing_id AS listing
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_28000
) subq_13
LEFT OUTER JOIN
***************************.dim_listings_latest listings_latest_src_28000
ON
subq_13.listing = listings_latest_src_28000.listing_id
) subq_17
LEFT OUTER JOIN
***************************.mf_time_spine subq_18
ON
subq_17.listing__ds__day = subq_18.ds
GROUP BY
listing__ds__martian_day

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Pass Only Elements: ['bookings', 'listing__ds__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'listing__ds__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_18.martian_day AS listing__ds__martian_day
, SUM(subq_17.bookings) AS bookings
FROM (
-- Join Standard Outputs
SELECT
DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day
, subq_13.bookings AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'listing']
SELECT
listing_id AS listing
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_28000
) subq_13
LEFT OUTER JOIN
***************************.dim_listings_latest listings_latest_src_28000
ON
subq_13.listing = listings_latest_src_28000.listing_id
) subq_17
LEFT OUTER JOIN
***************************.mf_time_spine subq_18
ON
subq_17.listing__ds__day = subq_18.ds
GROUP BY
subq_18.martian_day

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- Pass Only Elements: ['bookings', 'listing__ds__day']
-- Join to Custom Granularity Dataset
-- Pass Only Elements: ['bookings', 'listing__ds__martian_day']
-- Aggregate Measures
-- Compute Metrics via Expressions
SELECT
subq_18.martian_day AS listing__ds__martian_day
, SUM(subq_17.bookings) AS bookings
FROM (
-- Join Standard Outputs
SELECT
DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day
, subq_13.bookings AS bookings
FROM (
-- Read Elements From Semantic Model 'bookings_source'
-- Metric Time Dimension 'ds'
-- Pass Only Elements: ['bookings', 'listing']
SELECT
listing_id AS listing
, 1 AS bookings
FROM ***************************.fct_bookings bookings_source_src_28000
) subq_13
LEFT OUTER JOIN
***************************.dim_listings_latest listings_latest_src_28000
ON
subq_13.listing = listings_latest_src_28000.listing_id
) subq_17
LEFT OUTER JOIN
***************************.mf_time_spine subq_18
ON
subq_17.listing__ds__day = subq_18.ds
GROUP BY
subq_18.martian_day
Loading

0 comments on commit 9f6af7e

Please sign in to comment.