Skip to content

Commit

Permalink
Add test for bug with get_min_queryable_time_granularity (#1482)
Browse files Browse the repository at this point in the history
This PR adds a test case to illustrate a bug with
`MetricLookup.get_min_queryable_time_granularity()`. A fix for this test
case is provided in a later PR.
  • Loading branch information
plypaul authored Oct 29, 2024
1 parent 4d7c806 commit aa68e85
Show file tree
Hide file tree
Showing 21 changed files with 479 additions and 424 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
metric:
name: monthly_bookings_to_daily_bookings
description: Ratio of daily bookings that are included in the monthly bookings total as a derived metric.
type: ratio
type_params:
numerator:
name: bookings
denominator:
name: bookings_monthly
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
---
semantic_model:
name: bookings_monthly_source
description: bookings_monthly_source
name: monthly_bookings_source
description: >
Contains bookings by month. Temporarily renamed from `bookings_monthly_source` to trigger edge case for
skipped tests in `test_metric_lookup.py`.
node_relation:
schema_name: $source_schema
alias: fct_bookings_extended_monthly

defaults:
agg_time_dimension: monthly_ds
agg_time_dimension: ds

measures:
- name: bookings_monthly
agg: sum
create_metric: true

primary_entity: booking
primary_entity: booking_monthly

dimensions:
- name: monthly_ds
expr: ds
- name: ds
type: time
type_params:
time_granularity: month
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
AMBIGUOUS_RESOLUTION_MANIFEST_ANCHOR,
)
from metricflow_semantics.test_helpers.semantic_manifest_yamls.cyclic_join_manifest import CYCLIC_JOIN_MANIFEST_ANCHOR
from metricflow_semantics.test_helpers.semantic_manifest_yamls.extended_date_manifest import (
EXTENDED_DATE_MANIFEST_ANCHOR,
)
from metricflow_semantics.test_helpers.semantic_manifest_yamls.multi_hop_join_manifest import (
MULTI_HOP_JOIN_MANIFEST_ANCHOR,
)
Expand Down Expand Up @@ -67,6 +70,12 @@ def simple_multi_hop_join_manifest(template_mapping: Dict[str, str]) -> Pydantic
return load_semantic_manifest(SIMPLE_MULTI_HOP_JOIN_MANIFEST_ANCHOR.directory, template_mapping)


@pytest.fixture(scope="session")
def extended_date_manifest(template_mapping: Dict[str, str]) -> PydanticSemanticManifest:
"""Manifest used for many tests."""
return load_semantic_manifest(EXTENDED_DATE_MANIFEST_ANCHOR.directory, template_mapping)


@pytest.fixture(scope="session")
def simple_multi_hop_join_manifest_lookup( # noqa: D103
simple_multi_hop_join_manifest: PydanticSemanticManifest,
Expand Down Expand Up @@ -129,3 +138,10 @@ def column_association_resolver( # noqa: D103
simple_semantic_manifest_lookup: SemanticManifestLookup,
) -> ColumnAssociationResolver:
return DunderColumnAssociationResolver(simple_semantic_manifest_lookup)


@pytest.fixture(scope="session")
def extended_date_semantic_manifest_lookup( # noqa: D103
extended_date_manifest: PydanticSemanticManifest,
) -> SemanticManifestLookup:
return SemanticManifestLookup(extended_date_manifest)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import annotations

import logging

import pytest
from dbt_semantic_interfaces.references import MetricReference
from dbt_semantic_interfaces.type_enums import TimeGranularity
from metricflow_semantics.model.semantic_manifest_lookup import SemanticManifestLookup

logger = logging.getLogger(__name__)


@pytest.mark.skip("get_min_queryable_time_granularity has a bug with agg. time dimensions at different grains.")
def test_min_queryable_time_granularity_for_different_agg_time_grains( # noqa: D103
extended_date_semantic_manifest_lookup: SemanticManifestLookup,
) -> None:
metric_lookup = extended_date_semantic_manifest_lookup.metric_lookup

min_queryable_grain = metric_lookup.get_min_queryable_time_granularity(
MetricReference("monthly_bookings_to_daily_bookings")
)

# Since `monthly_bookings_to_daily_bookings` is based on metrics with DAY and MONTH aggregation time grains,
# the minimum queryable grain should be MONTH.
assert min_queryable_grain == TimeGranularity.MONTH
Original file line number Diff line number Diff line change
Expand Up @@ -348,42 +348,43 @@ integration_test:
GROUP BY a.ds
ORDER BY a.ds
---
integration_test:
name: cumulative_metric_month_granularity
description: Query a cumulative metric that uses non-day granularity.
model: EXTENDED_DATE_MODEL
metrics: ["trailing_3_months_bookings"]
group_bys: ["metric_time__month"]
order_bys: ["metric_time__month"]
time_constraint: ["2020-03-05", "2021-01-04"]
check_query: |
SELECT
subq_3.metric_time__month
, SUM(subq_2.bookings_monthly) AS trailing_3_months_bookings
FROM (
SELECT
{{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
FROM {{ source_schema }}.mf_time_spine
WHERE {{ render_time_constraint("ds", "2020-03-01", "2021-01-31") }}
GROUP BY
{{ render_date_trunc("ds", TimeGranularity.MONTH) }}
) subq_3
INNER JOIN (
SELECT
{{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
, bookings_monthly
FROM {{ source_schema }}.fct_bookings_extended_monthly
WHERE {{ render_time_constraint(render_date_trunc("ds", TimeGranularity.MONTH), "2019-12-01", "2021-01-31") }}
) subq_2
ON (
subq_2.metric_time__month <= subq_3.metric_time__month
) AND (
subq_2.metric_time__month > {{ render_date_sub("subq_3", "metric_time__month", 3, TimeGranularity.MONTH) }}
)
WHERE {{ render_time_constraint("subq_3.metric_time__month", "2020-03-01", "2021-01-31") }}
GROUP BY
subq_3.metric_time__month
ORDER BY subq_3.metric_time__month
# TODO: Test is currently broken.
#integration_test:
# name: cumulative_metric_month_granularity
# description: Query a cumulative metric that uses non-day granularity.
# model: EXTENDED_DATE_MODEL
# metrics: ["trailing_3_months_bookings"]
# group_bys: ["metric_time__month"]
# order_bys: ["metric_time__month"]
# time_constraint: ["2020-03-05", "2021-01-04"]
# check_query: |
# SELECT
# subq_3.metric_time__month
# , SUM(subq_2.bookings_monthly) AS trailing_3_months_bookings
# FROM (
# SELECT
# {{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
# FROM {{ source_schema }}.mf_time_spine
# WHERE {{ render_time_constraint("ds", "2020-03-01", "2021-01-31") }}
# GROUP BY
# {{ render_date_trunc("ds", TimeGranularity.MONTH) }}
# ) subq_3
# INNER JOIN (
# SELECT
# {{ render_date_trunc("ds", TimeGranularity.MONTH) }} AS metric_time__month
# , bookings_monthly
# FROM {{ source_schema }}.fct_bookings_extended_monthly
# WHERE {{ render_time_constraint(render_date_trunc("ds", TimeGranularity.MONTH), "2019-12-01", "2021-01-31") }}
# ) subq_2
# ON (
# subq_2.metric_time__month <= subq_3.metric_time__month
# ) AND (
# subq_2.metric_time__month > {{ render_date_sub("subq_3", "metric_time__month", 3, TimeGranularity.MONTH) }}
# )
# WHERE {{ render_time_constraint("subq_3.metric_time__month", "2020-03-01", "2021-01-31") }}
# GROUP BY
# subq_3.metric_time__month
# ORDER BY subq_3.metric_time__month
---
integration_test:
name: cumulative_metric_with_agg_time_dimension
Expand Down
23 changes: 12 additions & 11 deletions tests_metricflow/integration/test_cases/itest_dimensions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,18 @@ integration_test:
GROUP BY
v.ds
---
integration_test:
name: query_non_default_time_dimension_without_granularity
description: Query just a time dimension, no granularity specified. Should assume default granularity for dimension.
model: EXTENDED_DATE_MODEL
group_bys: [ "booking__monthly_ds"]
check_query: |
SELECT
ds AS booking__monthly_ds__month
FROM {{ source_schema }}.fct_bookings_extended_monthly
GROUP BY
ds
# TODO: Test is currently broken.
#integration_test:
# name: query_non_default_time_dimension_without_granularity
# description: Query just a time dimension, no granularity specified. Should assume default granularity for dimension.
# model: EXTENDED_DATE_MODEL
# group_bys: [ "booking__monthly_ds"]
# check_query: |
# SELECT
# ds AS booking__monthly_ds__month
# FROM {{ source_schema }}.fct_bookings_extended_monthly
# GROUP BY
# ds
---
integration_test:
name: query_dimension_with_constraint_from_diff_source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def test_cumulative_metric_grain_to_date(
)


@pytest.mark.skip("Test is currently broken")
@pytest.mark.sql_engine_snapshot
def test_cumulative_metric_month(
request: FixtureRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ FROM (
, EXTRACT(year FROM subq_2.metric_time__month) AS metric_time__extract_year
, EXTRACT(quarter FROM subq_2.metric_time__month) AS metric_time__extract_quarter
, EXTRACT(month FROM subq_2.metric_time__month) AS metric_time__extract_month
, subq_1.monthly_ds__month AS monthly_ds__month
, subq_1.monthly_ds__quarter AS monthly_ds__quarter
, subq_1.monthly_ds__year AS monthly_ds__year
, subq_1.monthly_ds__extract_year AS monthly_ds__extract_year
, subq_1.monthly_ds__extract_quarter AS monthly_ds__extract_quarter
, subq_1.monthly_ds__extract_month AS monthly_ds__extract_month
, subq_1.booking__monthly_ds__month AS booking__monthly_ds__month
, subq_1.booking__monthly_ds__quarter AS booking__monthly_ds__quarter
, subq_1.booking__monthly_ds__year AS booking__monthly_ds__year
, subq_1.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year
, subq_1.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter
, subq_1.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month
, subq_1.ds__month AS ds__month
, subq_1.ds__quarter AS ds__quarter
, subq_1.ds__year AS ds__year
, subq_1.ds__extract_year AS ds__extract_year
, subq_1.ds__extract_quarter AS ds__extract_quarter
, subq_1.ds__extract_month AS ds__extract_month
, subq_1.booking_monthly__ds__month AS booking_monthly__ds__month
, subq_1.booking_monthly__ds__quarter AS booking_monthly__ds__quarter
, subq_1.booking_monthly__ds__year AS booking_monthly__ds__year
, subq_1.booking_monthly__ds__extract_year AS booking_monthly__ds__extract_year
, subq_1.booking_monthly__ds__extract_quarter AS booking_monthly__ds__extract_quarter
, subq_1.booking_monthly__ds__extract_month AS booking_monthly__ds__extract_month
, subq_1.listing AS listing
, subq_1.booking__listing AS booking__listing
, subq_1.booking_monthly__listing AS booking_monthly__listing
, subq_1.bookings_monthly AS bookings_monthly
FROM (
-- Time Spine
Expand All @@ -50,48 +50,48 @@ FROM (
metric_time__month
) subq_2
INNER JOIN (
-- Metric Time Dimension 'monthly_ds'
-- Metric Time Dimension 'ds'
SELECT
subq_0.monthly_ds__month
, subq_0.monthly_ds__quarter
, subq_0.monthly_ds__year
, subq_0.monthly_ds__extract_year
, subq_0.monthly_ds__extract_quarter
, subq_0.monthly_ds__extract_month
, subq_0.booking__monthly_ds__month
, subq_0.booking__monthly_ds__quarter
, subq_0.booking__monthly_ds__year
, subq_0.booking__monthly_ds__extract_year
, subq_0.booking__monthly_ds__extract_quarter
, subq_0.booking__monthly_ds__extract_month
, subq_0.monthly_ds__month AS metric_time__month
, subq_0.monthly_ds__quarter AS metric_time__quarter
, subq_0.monthly_ds__year AS metric_time__year
, subq_0.monthly_ds__extract_year AS metric_time__extract_year
, subq_0.monthly_ds__extract_quarter AS metric_time__extract_quarter
, subq_0.monthly_ds__extract_month AS metric_time__extract_month
subq_0.ds__month
, subq_0.ds__quarter
, subq_0.ds__year
, subq_0.ds__extract_year
, subq_0.ds__extract_quarter
, subq_0.ds__extract_month
, subq_0.booking_monthly__ds__month
, subq_0.booking_monthly__ds__quarter
, subq_0.booking_monthly__ds__year
, subq_0.booking_monthly__ds__extract_year
, subq_0.booking_monthly__ds__extract_quarter
, subq_0.booking_monthly__ds__extract_month
, subq_0.ds__month AS metric_time__month
, subq_0.ds__quarter AS metric_time__quarter
, subq_0.ds__year AS metric_time__year
, subq_0.ds__extract_year AS metric_time__extract_year
, subq_0.ds__extract_quarter AS metric_time__extract_quarter
, subq_0.ds__extract_month AS metric_time__extract_month
, subq_0.listing
, subq_0.booking__listing
, subq_0.booking_monthly__listing
, subq_0.bookings_monthly
FROM (
-- Read Elements From Semantic Model 'bookings_monthly_source'
-- Read Elements From Semantic Model 'monthly_bookings_source'
SELECT
bookings_monthly_source_src_16000.bookings_monthly
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, month) AS monthly_ds__month
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, quarter) AS monthly_ds__quarter
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, year) AS monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_16000.ds) AS monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_16000.ds) AS monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_16000.ds) AS monthly_ds__extract_month
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, month) AS booking__monthly_ds__month
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, quarter) AS booking__monthly_ds__quarter
, DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, year) AS booking__monthly_ds__year
, EXTRACT(year FROM bookings_monthly_source_src_16000.ds) AS booking__monthly_ds__extract_year
, EXTRACT(quarter FROM bookings_monthly_source_src_16000.ds) AS booking__monthly_ds__extract_quarter
, EXTRACT(month FROM bookings_monthly_source_src_16000.ds) AS booking__monthly_ds__extract_month
, bookings_monthly_source_src_16000.listing_id AS listing
, bookings_monthly_source_src_16000.listing_id AS booking__listing
FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_16000
monthly_bookings_source_src_16000.bookings_monthly
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, month) AS ds__month
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, quarter) AS ds__quarter
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, year) AS ds__year
, EXTRACT(year FROM monthly_bookings_source_src_16000.ds) AS ds__extract_year
, EXTRACT(quarter FROM monthly_bookings_source_src_16000.ds) AS ds__extract_quarter
, EXTRACT(month FROM monthly_bookings_source_src_16000.ds) AS ds__extract_month
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, month) AS booking_monthly__ds__month
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, quarter) AS booking_monthly__ds__quarter
, DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, year) AS booking_monthly__ds__year
, EXTRACT(year FROM monthly_bookings_source_src_16000.ds) AS booking_monthly__ds__extract_year
, EXTRACT(quarter FROM monthly_bookings_source_src_16000.ds) AS booking_monthly__ds__extract_quarter
, EXTRACT(month FROM monthly_bookings_source_src_16000.ds) AS booking_monthly__ds__extract_month
, monthly_bookings_source_src_16000.listing_id AS listing
, monthly_bookings_source_src_16000.listing_id AS booking_monthly__listing
FROM ***************************.fct_bookings_extended_monthly monthly_bookings_source_src_16000
) subq_0
) subq_1
ON
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ FROM (
-- Compute Metrics via Expressions
SELECT
subq_10.metric_time__month AS metric_time__month
, SUM(bookings_monthly_source_src_16000.bookings_monthly) AS bookings_last_month
, SUM(monthly_bookings_source_src_16000.bookings_monthly) AS bookings_last_month
FROM (
-- Time Spine
SELECT
Expand All @@ -19,9 +19,9 @@ FROM (
metric_time__month
) subq_10
INNER JOIN
***************************.fct_bookings_extended_monthly bookings_monthly_source_src_16000
***************************.fct_bookings_extended_monthly monthly_bookings_source_src_16000
ON
DATE_SUB(CAST(subq_10.metric_time__month AS DATETIME), INTERVAL 1 month) = DATETIME_TRUNC(bookings_monthly_source_src_16000.ds, month)
DATE_SUB(CAST(subq_10.metric_time__month AS DATETIME), INTERVAL 1 month) = DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, month)
GROUP BY
metric_time__month
) subq_15
Loading

0 comments on commit aa68e85

Please sign in to comment.