diff --git a/metricflow/test/fixtures/semantic_manifest_yamls/extended_date_manifest/metrics/bookings_monthly_cumulative.yaml b/metricflow/test/fixtures/semantic_manifest_yamls/extended_date_manifest/metrics/bookings_monthly_cumulative.yaml new file mode 100644 index 0000000000..43463b8dc8 --- /dev/null +++ b/metricflow/test/fixtures/semantic_manifest_yamls/extended_date_manifest/metrics/bookings_monthly_cumulative.yaml @@ -0,0 +1,7 @@ +metric: + name: trailing_3_months_bookings + description: Prior 3 months' bookings. Tests a cumulative metric with non-day granularity. + type: cumulative + type_params: + measure: bookings_monthly + window: 3 month diff --git a/metricflow/test/integration/test_cases/itest_cumulative_metric.yaml b/metricflow/test/integration/test_cases/itest_cumulative_metric.yaml index a86a9ea838..521af2bb87 100644 --- a/metricflow/test/integration/test_cases/itest_cumulative_metric.yaml +++ b/metricflow/test/integration/test_cases/itest_cumulative_metric.yaml @@ -347,3 +347,40 @@ integration_test: OR {{ render_time_constraint("a.ds", "2020-01-04", "2020-01-04") }} 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 diff --git a/metricflow/test/query_rendering/test_cumulative_metric_rendering.py b/metricflow/test/query_rendering/test_cumulative_metric_rendering.py index f2bf7d50f4..38a59242fe 100644 --- a/metricflow/test/query_rendering/test_cumulative_metric_rendering.py +++ b/metricflow/test/query_rendering/test_cumulative_metric_rendering.py @@ -23,6 +23,7 @@ from metricflow.test.fixtures.model_fixtures import ConsistentIdObjectRepository from metricflow.test.fixtures.setup_fixtures import MetricFlowTestSessionState from metricflow.test.query_rendering.compare_rendered_query import convert_and_check +from metricflow.test.time.metric_time_dimension import MTD_SPEC_MONTH @pytest.mark.sql_engine_snapshot @@ -264,3 +265,33 @@ def test_cumulative_metric_grain_to_date( sql_client=sql_client, node=dataflow_plan.sink_output_nodes[0].parent_node, ) + + +@pytest.mark.sql_engine_snapshot +def test_cumulative_metric_month( + request: FixtureRequest, + mf_test_session_state: MetricFlowTestSessionState, + extended_date_dataflow_plan_builder: DataflowPlanBuilder, + extended_date_dataflow_to_sql_converter: DataflowToSqlQueryPlanConverter, + consistent_id_object_repository: ConsistentIdObjectRepository, + sql_client: SqlClient, +) -> None: + """Tests rendering a query for a cumulative metric based on a monthly time dimension.""" + dataflow_plan = extended_date_dataflow_plan_builder.build_plan( + MetricFlowQuerySpec( + metric_specs=(MetricSpec(element_name="trailing_3_months_bookings"),), + dimension_specs=(), + time_dimension_specs=(MTD_SPEC_MONTH,), + time_range_constraint=TimeRangeConstraint( + start_time=as_datetime("2020-03-05"), end_time=as_datetime("2021-01-04") + ), + ) + ) + + convert_and_check( + request=request, + mf_test_session_state=mf_test_session_state, + dataflow_to_sql_converter=extended_date_dataflow_to_sql_converter, + sql_client=sql_client, + node=dataflow_plan.sink_output_nodes[0].parent_node, + ) diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..ca19294f53 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC(subq_4.ds, month) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + metric_time__month + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, month) AS monthly_ds__month + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, quarter) AS monthly_ds__quarter + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, year) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, month) AS booking__monthly_ds__month + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, quarter) AS booking__monthly_ds__quarter + , DATE_TRUNC(bookings_monthly_source_src_10026.ds, year) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > DATE_SUB(CAST(subq_3.metric_time__month AS DATETIME), INTERVAL 3 month) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..e01ff82d55 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC(ds, month) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + metric_time__month +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC(ds, month) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC(ds, month) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > DATE_SUB(CAST(subq_12.metric_time__month AS DATETIME), INTERVAL 3 month) + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..e42f8a0b2b --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > DATEADD(month, -3, subq_3.metric_time__month) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..213d9532f5 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > DATEADD(month, -3, subq_12.metric_time__month) + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + subq_12.metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..52349f4577 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > subq_3.metric_time__month - INTERVAL 3 month + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..73a16ae4f0 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > subq_12.metric_time__month - INTERVAL 3 month + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + subq_12.metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..7aed70d1a0 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > subq_3.metric_time__month - MAKE_INTERVAL(months => 3) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..db96d10539 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > subq_12.metric_time__month - MAKE_INTERVAL(months => 3) + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + subq_12.metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..e42f8a0b2b --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > DATEADD(month, -3, subq_3.metric_time__month) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..213d9532f5 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > DATEADD(month, -3, subq_12.metric_time__month) + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + subq_12.metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..e42f8a0b2b --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN '2019-12-05' AND '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > DATEADD(month, -3, subq_3.metric_time__month) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..213d9532f5 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN '2020-03-05' AND '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN '2019-12-05' AND '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > DATEADD(month, -3, subq_12.metric_time__month) + ) +WHERE subq_12.metric_time__month BETWEEN '2020-03-05' AND '2021-01-04' +GROUP BY + subq_12.metric_time__month diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0.sql new file mode 100644 index 0000000000..ab2bd6d3d7 --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0.sql @@ -0,0 +1,136 @@ +-- Compute Metrics via Expressions +SELECT + subq_8.metric_time__month + , subq_8.bookings_monthly AS trailing_3_months_bookings +FROM ( + -- Aggregate Measures + SELECT + subq_7.metric_time__month + , SUM(subq_7.bookings_monthly) AS bookings_monthly + FROM ( + -- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_6.metric_time__month + , subq_6.bookings_monthly + FROM ( + -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] + SELECT + subq_5.metric_time__month + , subq_5.bookings_monthly + FROM ( + -- Join Self Over Time Range + SELECT + subq_3.metric_time__month AS metric_time__month + , subq_2.monthly_ds__month AS monthly_ds__month + , subq_2.monthly_ds__quarter AS monthly_ds__quarter + , subq_2.monthly_ds__year AS monthly_ds__year + , subq_2.monthly_ds__extract_year AS monthly_ds__extract_year + , subq_2.monthly_ds__extract_quarter AS monthly_ds__extract_quarter + , subq_2.monthly_ds__extract_month AS monthly_ds__extract_month + , subq_2.booking__monthly_ds__month AS booking__monthly_ds__month + , subq_2.booking__monthly_ds__quarter AS booking__monthly_ds__quarter + , subq_2.booking__monthly_ds__year AS booking__monthly_ds__year + , subq_2.booking__monthly_ds__extract_year AS booking__monthly_ds__extract_year + , subq_2.booking__monthly_ds__extract_quarter AS booking__monthly_ds__extract_quarter + , subq_2.booking__monthly_ds__extract_month AS booking__monthly_ds__extract_month + , subq_2.metric_time__quarter AS metric_time__quarter + , subq_2.metric_time__year AS metric_time__year + , subq_2.metric_time__extract_year AS metric_time__extract_year + , subq_2.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_2.metric_time__extract_month AS metric_time__extract_month + , subq_2.listing AS listing + , subq_2.booking__listing AS booking__listing + , subq_2.bookings_monthly AS bookings_monthly + FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', subq_4.ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_4 + WHERE subq_4.ds BETWEEN timestamp '2020-03-05' AND timestamp '2021-01-04' + GROUP BY + DATE_TRUNC('month', subq_4.ds) + ) subq_3 + INNER JOIN ( + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + subq_1.monthly_ds__month + , subq_1.monthly_ds__quarter + , subq_1.monthly_ds__year + , subq_1.monthly_ds__extract_year + , subq_1.monthly_ds__extract_quarter + , subq_1.monthly_ds__extract_month + , subq_1.booking__monthly_ds__month + , subq_1.booking__monthly_ds__quarter + , subq_1.booking__monthly_ds__year + , subq_1.booking__monthly_ds__extract_year + , subq_1.booking__monthly_ds__extract_quarter + , subq_1.booking__monthly_ds__extract_month + , subq_1.metric_time__month + , subq_1.metric_time__quarter + , subq_1.metric_time__year + , subq_1.metric_time__extract_year + , subq_1.metric_time__extract_quarter + , subq_1.metric_time__extract_month + , subq_1.listing + , subq_1.booking__listing + , subq_1.bookings_monthly + FROM ( + -- Metric Time Dimension 'monthly_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.listing + , subq_0.booking__listing + , subq_0.bookings_monthly + FROM ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + SELECT + bookings_monthly_source_src_10026.bookings_monthly + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS monthly_ds__extract_month + , DATE_TRUNC('month', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__month + , DATE_TRUNC('quarter', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__quarter + , DATE_TRUNC('year', bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__year + , EXTRACT(year FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_year + , EXTRACT(quarter FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_quarter + , EXTRACT(month FROM bookings_monthly_source_src_10026.ds) AS booking__monthly_ds__extract_month + , bookings_monthly_source_src_10026.listing_id AS listing + , bookings_monthly_source_src_10026.listing_id AS booking__listing + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + ) subq_0 + ) subq_1 + WHERE subq_1.metric_time__month BETWEEN timestamp '2019-12-05' AND timestamp '2021-01-04' + ) subq_2 + ON + ( + subq_2.metric_time__month <= subq_3.metric_time__month + ) AND ( + subq_2.metric_time__month > DATE_ADD('month', -3, subq_3.metric_time__month) + ) + ) subq_5 + ) subq_6 + WHERE subq_6.metric_time__month BETWEEN timestamp '2020-03-05' AND timestamp '2021-01-04' + ) subq_7 + GROUP BY + subq_7.metric_time__month +) subq_8 diff --git a/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0_optimized.sql b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0_optimized.sql new file mode 100644 index 0000000000..546e55075c --- /dev/null +++ b/metricflow/test/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_metric_month__plan0_optimized.sql @@ -0,0 +1,36 @@ +-- Join Self Over Time Range +-- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] +-- Constrain Time Range to [2020-03-05T00:00:00, 2021-01-04T00:00:00] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_12.metric_time__month AS metric_time__month + , SUM(subq_11.bookings_monthly) AS trailing_3_months_bookings +FROM ( + -- Time Spine + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + FROM ***************************.mf_time_spine subq_13 + WHERE ds BETWEEN timestamp '2020-03-05' AND timestamp '2021-01-04' + GROUP BY + DATE_TRUNC('month', ds) +) subq_12 +INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_monthly_source' + -- Metric Time Dimension 'monthly_ds' + -- Constrain Time Range to [2019-12-05T00:00:00, 2021-01-04T00:00:00] + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , bookings_monthly + FROM ***************************.fct_bookings_extended_monthly bookings_monthly_source_src_10026 + WHERE DATE_TRUNC('month', ds) BETWEEN timestamp '2019-12-05' AND timestamp '2021-01-04' +) subq_11 +ON + ( + subq_11.metric_time__month <= subq_12.metric_time__month + ) AND ( + subq_11.metric_time__month > DATE_ADD('month', -3, subq_12.metric_time__month) + ) +WHERE subq_12.metric_time__month BETWEEN timestamp '2020-03-05' AND timestamp '2021-01-04' +GROUP BY + subq_12.metric_time__month