diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql index a897a1e20..6f47e7a57 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) AS metric_time__day , MAX(subq_21.visits) AS visits - , MAX(subq_32.buys) AS buys + , MAX(subq_31.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day'] @@ -22,12 +32,12 @@ FROM ( metric_time__day , SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte ) subq_18 WHERE metric_time__day = '2020-01-01' GROUP BY @@ -43,50 +53,49 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_23.user + , subq_22.user , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte + ) subq_22 WHERE metric_time__day = '2020-01-01' - ) subq_25 + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -97,19 +106,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( - (subq_25.metric_time__day <= subq_28.metric_time__day) + (subq_24.metric_time__day <= subq_27.metric_time__day) ) - ) subq_29 + ) subq_28 GROUP BY metric_time__day - ) subq_32 + ) subq_31 ON - subq_21.metric_time__day = subq_32.metric_time__day + subq_21.metric_time__day = subq_31.metric_time__day GROUP BY - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) +) subq_32 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql index f6f89ff4c..f398ad587 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_categorical_filter__plan0_optimized.sql @@ -5,17 +5,28 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) AS metric_time__day + , COALESCE(subq_21.visit__referrer_id, subq_31.visit__referrer_id) AS visit__referrer_id , MAX(subq_21.visits) AS visits - , MAX(subq_32.buys) AS buys + , MAX(subq_31.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] @@ -25,13 +36,13 @@ FROM ( , visit__referrer_id , SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + metric_time__day + , sma_0_cte.user + , visit__referrer_id + , visits + FROM sma_0_cte sma_0_cte ) subq_18 WHERE visit__referrer_id = 'ref_id_01' GROUP BY @@ -49,60 +60,59 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_24.visit__referrer_id) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_23.user + , subq_22.user , visit__referrer_id , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + metric_time__day + , sma_0_cte.user + , visit__referrer_id + , visits + FROM sma_0_cte sma_0_cte + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' - ) subq_25 + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -113,25 +123,25 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( - (subq_25.metric_time__day <= subq_28.metric_time__day) + (subq_24.metric_time__day <= subq_27.metric_time__day) ) - ) subq_29 + ) subq_28 GROUP BY metric_time__day , visit__referrer_id - ) subq_32 + ) subq_31 ON ( - subq_21.visit__referrer_id = subq_32.visit__referrer_id + subq_21.visit__referrer_id = subq_31.visit__referrer_id ) AND ( - subq_21.metric_time__day = subq_32.metric_time__day + subq_21.metric_time__day = subq_31.metric_time__day ) GROUP BY - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) - , COALESCE(subq_21.visit__referrer_id, subq_32.visit__referrer_id) -) subq_33 + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) + , COALESCE(subq_21.visit__referrer_id, subq_31.visit__referrer_id) +) subq_32 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_different_time_dimension_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_different_time_dimension_grains__plan0_optimized.sql index 44664050d..8b70260ff 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_different_time_dimension_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_different_time_dimension_grains__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys_month) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_with_monthly_conversion -FROM ( +WITH sma_0_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', ds) AS metric_time__month + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys_month) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_with_monthly_conversion +FROM ( + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_0_cte sma_0_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 1 month @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_0_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__month - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__month DESC + subq_23.user + , subq_23.metric_time__month + , subq_23.mf_internal_uuid + ORDER BY sma_0_cte.metric_time__month DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__month) OVER ( + , FIRST_VALUE(sma_0_cte.metric_time__month) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__month - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__month DESC + subq_23.user + , subq_23.metric_time__month + , subq_23.mf_internal_uuid + ORDER BY sma_0_cte.metric_time__month DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__month - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_0_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__month - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__month DESC + subq_23.user + , subq_23.metric_time__month + , subq_23.mf_internal_uuid + ORDER BY sma_0_cte.metric_time__month DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys_month AS buys_month - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__month', 'user'] - SELECT - DATE_TRUNC('month', ds) AS metric_time__month - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys_month AS buys_month + FROM sma_0_cte sma_0_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds_month' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys_month , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_0_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__month <= subq_24.metric_time__month + sma_0_cte.metric_time__month <= subq_23.metric_time__month ) AND ( - subq_21.metric_time__month > subq_24.metric_time__month - INTERVAL 1 month + sma_0_cte.metric_time__month > subq_23.metric_time__month - INTERVAL 1 month ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql index c9a022201..43919c3d2 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter__plan0_optimized.sql @@ -6,8 +6,18 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - CAST(MAX(subq_32.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_21.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate + CAST(MAX(subq_31.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_21.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits',] @@ -15,12 +25,12 @@ FROM ( SELECT SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte ) subq_18 WHERE metric_time__day = '2020-01-01' ) subq_21 @@ -33,50 +43,49 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_23.user + , subq_22.user , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte + ) subq_22 WHERE metric_time__day = '2020-01-01' - ) subq_25 + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -87,12 +96,12 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( - (subq_25.metric_time__day <= subq_28.metric_time__day) + (subq_24.metric_time__day <= subq_27.metric_time__day) ) - ) subq_29 -) subq_32 + ) subq_28 +) subq_31 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql index 6d88593af..cc33b2b28 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_filter_not_in_group_by__plan0_optimized.sql @@ -6,8 +6,19 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - COALESCE(MAX(subq_32.buys), 0) AS visit_buy_conversions + COALESCE(MAX(subq_31.buys), 0) AS visit_buy_conversions FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits',] @@ -15,12 +26,13 @@ FROM ( SELECT SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + metric_time__day + , sma_0_cte.user + , visit__referrer_id + , visits + FROM sma_0_cte sma_0_cte ) subq_18 WHERE visit__referrer_id = 'ref_id_01' ) subq_21 @@ -33,60 +45,59 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_24.visit__referrer_id) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_23.user + , subq_22.user , visit__referrer_id , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + metric_time__day + , sma_0_cte.user + , visit__referrer_id + , visits + FROM sma_0_cte sma_0_cte + ) subq_22 WHERE visit__referrer_id = 'ref_id_01' - ) subq_25 + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -97,16 +108,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( ( - subq_25.metric_time__day <= subq_28.metric_time__day + subq_24.metric_time__day <= subq_27.metric_time__day ) AND ( - subq_25.metric_time__day > subq_28.metric_time__day - INTERVAL 7 day + subq_24.metric_time__day > subq_27.metric_time__day - INTERVAL 7 day ) ) - ) subq_29 -) subq_32 + ) subq_28 +) subq_31 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql index cd85a3b7f..bcff047b5 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_time_constraint__plan0_optimized.sql @@ -5,15 +5,28 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH ctr_1_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_24.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id , MAX(subq_24.visits) AS visits - , MAX(subq_36.buys) AS buys + , MAX(subq_34.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id'] @@ -22,14 +35,13 @@ FROM ( visit__referrer_id , SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Read From CTE For node_id=ctr_1 SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + metric_time__day + , ctr_1_cte.user + , visit__referrer_id + , visits + FROM ctr_1_cte ctr_1_cte ) subq_21 WHERE visit__referrer_id = 'ref_id_01' GROUP BY @@ -45,62 +57,59 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_29.visits) OVER ( + FIRST_VALUE(subq_27.visits) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_29.metric_time__day) OVER ( + , FIRST_VALUE(subq_27.metric_time__day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_29.user) OVER ( + , FIRST_VALUE(subq_27.user) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_32.mf_internal_uuid AS mf_internal_uuid - , subq_32.buys AS buys + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_27.user + , subq_25.user , visit__referrer_id , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Read From CTE For node_id=ctr_1 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + metric_time__day + , ctr_1_cte.user + , visit__referrer_id + , visits + FROM ctr_1_cte ctr_1_cte + ) subq_25 WHERE visit__referrer_id = 'ref_id_01' - ) subq_29 + ) subq_27 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -111,19 +120,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_32 + ) subq_30 ON ( - subq_29.user = subq_32.user + subq_27.user = subq_30.user ) AND ( - (subq_29.metric_time__day <= subq_32.metric_time__day) + (subq_27.metric_time__day <= subq_30.metric_time__day) ) - ) subq_33 + ) subq_31 GROUP BY visit__referrer_id - ) subq_36 + ) subq_34 ON - subq_24.visit__referrer_id = subq_36.visit__referrer_id + subq_24.visit__referrer_id = subq_34.visit__referrer_id GROUP BY - COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) -) subq_37 + COALESCE(subq_24.visit__referrer_id, subq_34.visit__referrer_id) +) subq_35 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql index f818227f4..35c5b1730 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) AS metric_time__day + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) AS metric_time__day , MAX(subq_21.visits) AS visits - , MAX(subq_32.buys) AS buys + , MAX(subq_31.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day'] @@ -22,12 +32,12 @@ FROM ( metric_time__day , SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte ) subq_18 WHERE metric_time__day = '2020-01-01' GROUP BY @@ -43,50 +53,49 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_23.user + , subq_22.user , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + metric_time__day + , sma_0_cte.user + , visits + FROM sma_0_cte sma_0_cte + ) subq_22 WHERE metric_time__day = '2020-01-01' - ) subq_25 + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -97,23 +106,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( ( - subq_25.metric_time__day <= subq_28.metric_time__day + subq_24.metric_time__day <= subq_27.metric_time__day ) AND ( - subq_25.metric_time__day > subq_28.metric_time__day - INTERVAL 7 day + subq_24.metric_time__day > subq_27.metric_time__day - INTERVAL 7 day ) ) - ) subq_29 + ) subq_28 GROUP BY metric_time__day - ) subq_32 + ) subq_31 ON - subq_21.metric_time__day = subq_32.metric_time__day + subq_21.metric_time__day = subq_31.metric_time__day GROUP BY - COALESCE(subq_21.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_21.metric_time__day, subq_31.metric_time__day) +) subq_32 diff --git a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql index 7abd4226a..f7244ceb1 100644 --- a/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metric_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_window_and_time_constraint__plan0_optimized.sql @@ -5,17 +5,30 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH ctr_1_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 + WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) AS metric_time__day - , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_24.metric_time__day, subq_34.metric_time__day) AS metric_time__day + , COALESCE(subq_24.visit__referrer_id, subq_34.visit__referrer_id) AS visit__referrer_id , MAX(subq_24.visits) AS visits - , MAX(subq_36.buys) AS buys + , MAX(subq_34.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] @@ -25,15 +38,13 @@ FROM ( , visit__referrer_id , SUM(visits) AS visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Read From CTE For node_id=ctr_1 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' + metric_time__day + , ctr_1_cte.user + , visit__referrer_id + , visits + FROM ctr_1_cte ctr_1_cte ) subq_21 WHERE visit__referrer_id = 'ref_id_01' GROUP BY @@ -51,62 +62,59 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_29.visits) OVER ( + FIRST_VALUE(subq_27.visits) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_29.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_27.visit__referrer_id) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_29.metric_time__day) OVER ( + , FIRST_VALUE(subq_27.metric_time__day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_29.user) OVER ( + , FIRST_VALUE(subq_27.user) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_30.user + , subq_30.metric_time__day + , subq_30.mf_internal_uuid + ORDER BY subq_27.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_32.mf_internal_uuid AS mf_internal_uuid - , subq_32.buys AS buys + , subq_30.mf_internal_uuid AS mf_internal_uuid + , subq_30.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_27.user + , subq_25.user , visit__referrer_id , visits FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-02T00:00:00] + -- Read From CTE For node_id=ctr_1 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-02' - ) subq_27 + metric_time__day + , ctr_1_cte.user + , visit__referrer_id + , visits + FROM ctr_1_cte ctr_1_cte + ) subq_25 WHERE visit__referrer_id = 'ref_id_01' - ) subq_29 + ) subq_27 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,29 +125,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_32 + ) subq_30 ON ( - subq_29.user = subq_32.user + subq_27.user = subq_30.user ) AND ( ( - subq_29.metric_time__day <= subq_32.metric_time__day + subq_27.metric_time__day <= subq_30.metric_time__day ) AND ( - subq_29.metric_time__day > subq_32.metric_time__day - INTERVAL 7 day + subq_27.metric_time__day > subq_30.metric_time__day - INTERVAL 7 day ) ) - ) subq_33 + ) subq_31 GROUP BY metric_time__day , visit__referrer_id - ) subq_36 + ) subq_34 ON ( - subq_24.visit__referrer_id = subq_36.visit__referrer_id + subq_24.visit__referrer_id = subq_34.visit__referrer_id ) AND ( - subq_24.metric_time__day = subq_36.metric_time__day + subq_24.metric_time__day = subq_34.metric_time__day ) GROUP BY - COALESCE(subq_24.metric_time__day, subq_36.metric_time__day) - , COALESCE(subq_24.visit__referrer_id, subq_36.visit__referrer_id) -) subq_37 + COALESCE(subq_24.metric_time__day, subq_34.metric_time__day) + , COALESCE(subq_24.visit__referrer_id, subq_34.visit__referrer_id) +) subq_35 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity__plan0_optimized.sql index ba175e642..c7867e473 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity__plan0_optimized.sql @@ -3,34 +3,38 @@ test_filename: test_custom_granularity.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__martian_day + metric_time__martian_day AS metric_time__martian_day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__martian_day, subq_32.metric_time__martian_day) AS metric_time__martian_day + COALESCE(subq_21.metric_time__martian_day, subq_31.metric_time__martian_day) AS metric_time__martian_day , MAX(subq_21.visits) AS visits - , MAX(subq_32.buys) AS buys + , MAX(subq_31.buys) AS buys FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset -- Pass Only Elements: ['visits', 'metric_time__martian_day'] -- Aggregate Measures SELECT subq_18.martian_day AS metric_time__martian_day - , SUM(subq_17.visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + , SUM(sma_0_cte.visits) AS visits + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN ***************************.mf_time_spine subq_18 ON - subq_17.ds__day = subq_18.ds + sma_0_cte.metric_time__day = subq_18.ds GROUP BY subq_18.martian_day ) subq_21 @@ -44,62 +48,55 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_25.visits) OVER ( + FIRST_VALUE(subq_24.visits) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_25.metric_time__martian_day) OVER ( + , FIRST_VALUE(subq_24.metric_time__martian_day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__martian_day - , FIRST_VALUE(subq_25.metric_time__day) OVER ( + , FIRST_VALUE(subq_24.metric_time__day) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_25.user) OVER ( + , FIRST_VALUE(subq_24.user) OVER ( PARTITION BY - subq_28.user - , subq_28.metric_time__day - , subq_28.mf_internal_uuid - ORDER BY subq_25.metric_time__day DESC + subq_27.user + , subq_27.metric_time__day + , subq_27.mf_internal_uuid + ORDER BY subq_24.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_28.mf_internal_uuid AS mf_internal_uuid - , subq_28.buys AS buys + , subq_27.mf_internal_uuid AS mf_internal_uuid + , subq_27.buys AS buys FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset -- Pass Only Elements: ['visits', 'metric_time__day', 'metric_time__martian_day', 'user'] SELECT - subq_23.martian_day AS metric_time__martian_day - , subq_22.ds__day AS metric_time__day - , subq_22.user AS user - , subq_22.visits AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_22 + subq_22.martian_day AS metric_time__martian_day + , sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.user AS user + , sma_0_cte.visits AS visits + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.mf_time_spine subq_23 + ***************************.mf_time_spine subq_22 ON - subq_22.ds__day = subq_23.ds - ) subq_25 + sma_0_cte.metric_time__day = subq_22.ds + ) subq_24 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -110,23 +107,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_28 + ) subq_27 ON ( - subq_25.user = subq_28.user + subq_24.user = subq_27.user ) AND ( ( - subq_25.metric_time__day <= subq_28.metric_time__day + subq_24.metric_time__day <= subq_27.metric_time__day ) AND ( - subq_25.metric_time__day > subq_28.metric_time__day - INTERVAL 7 day + subq_24.metric_time__day > subq_27.metric_time__day - INTERVAL 7 day ) ) - ) subq_29 + ) subq_28 GROUP BY metric_time__martian_day - ) subq_32 + ) subq_31 ON - subq_21.metric_time__martian_day = subq_32.metric_time__martian_day + subq_21.metric_time__martian_day = subq_31.metric_time__martian_day GROUP BY - COALESCE(subq_21.metric_time__martian_day, subq_32.metric_time__martian_day) -) subq_33 + COALESCE(subq_21.metric_time__martian_day, subq_31.metric_time__martian_day) +) subq_32 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter__plan0_optimized.sql index 088cf85f8..601e08e42 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter__plan0_optimized.sql @@ -3,15 +3,25 @@ test_filename: test_custom_granularity.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__martian_day + metric_time__martian_day AS metric_time__martian_day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__martian_day, subq_36.metric_time__martian_day) AS metric_time__martian_day + COALESCE(subq_24.metric_time__martian_day, subq_35.metric_time__martian_day) AS metric_time__martian_day , MAX(subq_24.visits) AS visits - , MAX(subq_36.buys) AS buys + , MAX(subq_35.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__martian_day'] @@ -20,22 +30,16 @@ FROM ( metric_time__martian_day , SUM(visits) AS visits FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset SELECT - subq_19.visits AS visits + sma_0_cte.visits AS visits , subq_20.martian_day AS metric_time__martian_day - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_19 + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN ***************************.mf_time_spine subq_20 ON - subq_19.ds__day = subq_20.ds + sma_0_cte.metric_time__day = subq_20.ds ) subq_21 WHERE metric_time__martian_day = '2020-01-01' GROUP BY @@ -51,71 +55,64 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_29.visits) OVER ( + FIRST_VALUE(subq_28.visits) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_29.metric_time__martian_day) OVER ( + , FIRST_VALUE(subq_28.metric_time__martian_day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__martian_day - , FIRST_VALUE(subq_29.metric_time__day) OVER ( + , FIRST_VALUE(subq_28.metric_time__day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_29.user) OVER ( + , FIRST_VALUE(subq_28.user) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_32.mf_internal_uuid AS mf_internal_uuid - , subq_32.buys AS buys + , subq_31.mf_internal_uuid AS mf_internal_uuid + , subq_31.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day', 'metric_time__martian_day', 'user'] SELECT metric_time__martian_day , metric_time__day - , subq_27.user + , subq_26.user , visits FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset SELECT - subq_25.ds__day AS metric_time__day - , subq_25.user AS user - , subq_25.visits AS visits - , subq_26.martian_day AS metric_time__martian_day - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_25 + sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.user AS user + , sma_0_cte.visits AS visits + , subq_25.martian_day AS metric_time__martian_day + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.mf_time_spine subq_26 + ***************************.mf_time_spine subq_25 ON - subq_25.ds__day = subq_26.ds - ) subq_27 + sma_0_cte.metric_time__day = subq_25.ds + ) subq_26 WHERE metric_time__martian_day = '2020-01-01' - ) subq_29 + ) subq_28 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -126,23 +123,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_32 + ) subq_31 ON ( - subq_29.user = subq_32.user + subq_28.user = subq_31.user ) AND ( ( - subq_29.metric_time__day <= subq_32.metric_time__day + subq_28.metric_time__day <= subq_31.metric_time__day ) AND ( - subq_29.metric_time__day > subq_32.metric_time__day - INTERVAL 7 day + subq_28.metric_time__day > subq_31.metric_time__day - INTERVAL 7 day ) ) - ) subq_33 + ) subq_32 GROUP BY metric_time__martian_day - ) subq_36 + ) subq_35 ON - subq_24.metric_time__martian_day = subq_36.metric_time__martian_day + subq_24.metric_time__martian_day = subq_35.metric_time__martian_day GROUP BY - COALESCE(subq_24.metric_time__martian_day, subq_36.metric_time__martian_day) -) subq_37 + COALESCE(subq_24.metric_time__martian_day, subq_35.metric_time__martian_day) +) subq_36 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index f99e5bafd..0ad8093c9 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_conversion_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -4,8 +4,18 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - CAST(MAX(subq_36.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_24.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days + CAST(MAX(subq_35.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_24.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits',] @@ -13,22 +23,16 @@ FROM ( SELECT SUM(visits) AS visits FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset SELECT - subq_19.visits AS visits + sma_0_cte.visits AS visits , subq_20.martian_day AS metric_time__martian_day - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_19 + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN ***************************.mf_time_spine subq_20 ON - subq_19.ds__day = subq_20.ds + sma_0_cte.metric_time__day = subq_20.ds ) subq_21 WHERE metric_time__martian_day = '2020-01-01' ) subq_24 @@ -41,71 +45,64 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_29.visits) OVER ( + FIRST_VALUE(subq_28.visits) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_29.metric_time__martian_day) OVER ( + , FIRST_VALUE(subq_28.metric_time__martian_day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__martian_day - , FIRST_VALUE(subq_29.metric_time__day) OVER ( + , FIRST_VALUE(subq_28.metric_time__day) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_29.user) OVER ( + , FIRST_VALUE(subq_28.user) OVER ( PARTITION BY - subq_32.user - , subq_32.metric_time__day - , subq_32.mf_internal_uuid - ORDER BY subq_29.metric_time__day DESC + subq_31.user + , subq_31.metric_time__day + , subq_31.mf_internal_uuid + ORDER BY subq_28.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_32.mf_internal_uuid AS mf_internal_uuid - , subq_32.buys AS buys + , subq_31.mf_internal_uuid AS mf_internal_uuid + , subq_31.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'metric_time__day', 'metric_time__martian_day', 'user'] SELECT metric_time__martian_day , metric_time__day - , subq_27.user + , subq_26.user , visits FROM ( - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Join to Custom Granularity Dataset SELECT - subq_25.ds__day AS metric_time__day - , subq_25.user AS user - , subq_25.visits AS visits - , subq_26.martian_day AS metric_time__martian_day - FROM ( - -- Read Elements From Semantic Model 'visits_source' - SELECT - 1 AS visits - , DATE_TRUNC('day', ds) AS ds__day - , user_id AS user - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_25 + sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.user AS user + , sma_0_cte.visits AS visits + , subq_25.martian_day AS metric_time__martian_day + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.mf_time_spine subq_26 + ***************************.mf_time_spine subq_25 ON - subq_25.ds__day = subq_26.ds - ) subq_27 + sma_0_cte.metric_time__day = subq_25.ds + ) subq_26 WHERE metric_time__martian_day = '2020-01-01' - ) subq_29 + ) subq_28 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -116,16 +113,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_32 + ) subq_31 ON ( - subq_29.user = subq_32.user + subq_28.user = subq_31.user ) AND ( ( - subq_29.metric_time__day <= subq_32.metric_time__day + subq_28.metric_time__day <= subq_31.metric_time__day ) AND ( - subq_29.metric_time__day > subq_32.metric_time__day - INTERVAL 7 day + subq_28.metric_time__day > subq_31.metric_time__day - INTERVAL 7 day ) ) - ) subq_33 -) subq_36 + ) subq_32 +) subq_35 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 7a46cbd45..39bf76aad 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,30 +6,34 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Order By [] Limit 1 +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant + COALESCE(subq_18.booking__is_instant, subq_23.booking__is_instant) AS booking__is_instant , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 + , MAX(subq_23.derived_shared_alias_2) AS derived_shared_alias_2 FROM ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'booking__is_instant'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT booking__is_instant , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 + FROM sma_0_cte sma_0_cte GROUP BY booking__is_instant ) subq_17 @@ -40,26 +44,20 @@ FULL OUTER JOIN ( booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT booking__is_instant , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 + FROM sma_0_cte sma_0_cte GROUP BY booking__is_instant - ) subq_23 -) subq_24 + ) subq_22 +) subq_23 ON - subq_18.booking__is_instant = subq_24.booking__is_instant + subq_18.booking__is_instant = subq_23.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(subq_18.booking__is_instant, subq_23.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index d7456bd9d..3820b308e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -3,30 +3,33 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day + COALESCE(subq_18.metric_time__day, subq_25.metric_time__day) AS metric_time__day , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + , MAX(subq_25.bookings_at_start_of_month) AS bookings_at_start_of_month FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__day ) subq_18 @@ -36,24 +39,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + subq_21.ds AS metric_time__day + , SUM(sma_0_cte.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + DATE_TRUNC('month', subq_21.ds) = sma_0_cte.metric_time__day GROUP BY - subq_22.ds - ) subq_26 + subq_21.ds + ) subq_25 ON - subq_18.metric_time__day = subq_26.metric_time__day + subq_18.metric_time__day = subq_25.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + COALESCE(subq_18.metric_time__day, subq_25.metric_time__day) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index cfa6d1f5e..93593bb48 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -3,30 +3,34 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__week + metric_time__week AS metric_time__week , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week + COALESCE(subq_18.metric_time__week, subq_25.metric_time__week) AS metric_time__week , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + , MAX(subq_25.bookings_at_start_of_month) AS bookings_at_start_of_month FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__week'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT metric_time__week , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__week ) subq_18 @@ -36,25 +40,19 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + DATE_TRUNC('week', subq_21.ds) AS metric_time__week + , SUM(sma_0_cte.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds + DATE_TRUNC('month', subq_21.ds) = sma_0_cte.metric_time__day + WHERE DATE_TRUNC('week', subq_21.ds) = subq_21.ds GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', subq_21.ds) + ) subq_25 ON - subq_18.metric_time__week = subq_26.metric_time__week + subq_18.metric_time__week = subq_25.metric_time__week GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + COALESCE(subq_18.metric_time__week, subq_25.metric_time__week) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql index 18cde7417..dac02f46d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -3,30 +3,33 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day + COALESCE(subq_18.metric_time__day, subq_25.metric_time__day) AS metric_time__day , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_25.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__day ) subq_18 @@ -36,24 +39,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + subq_21.ds AS metric_time__day + , SUM(sma_0_cte.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day + subq_21.ds - INTERVAL 14 day = sma_0_cte.metric_time__day GROUP BY - subq_22.ds - ) subq_26 + subq_21.ds + ) subq_25 ON - subq_18.metric_time__day = subq_26.metric_time__day + subq_18.metric_time__day = subq_25.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + COALESCE(subq_18.metric_time__day, subq_25.metric_time__day) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 42b79f2c6..b69fb0512 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -3,30 +3,34 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__quarter + metric_time__quarter AS metric_time__quarter , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter + COALESCE(subq_18.metric_time__quarter, subq_25.metric_time__quarter) AS metric_time__quarter , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_25.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT metric_time__quarter , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__quarter ) subq_18 @@ -36,24 +40,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + DATE_TRUNC('quarter', subq_21.ds) AS metric_time__quarter + , SUM(sma_0_cte.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day + subq_21.ds - INTERVAL 14 day = sma_0_cte.metric_time__day GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', subq_21.ds) + ) subq_25 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + subq_18.metric_time__quarter = subq_25.metric_time__quarter GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + COALESCE(subq_18.metric_time__quarter, subq_25.metric_time__quarter) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index cc78d4892..806cdc54a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -3,15 +3,24 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + COALESCE(subq_24.metric_time__day, subq_31.metric_time__day) AS metric_time__day , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago + , MAX(subq_31.bookings_1_month_ago) AS bookings_1_month_ago FROM ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -19,18 +28,12 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings + , SUM(sma_0_cte.bookings) AS month_start_bookings FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 + INNER JOIN + sma_0_cte sma_0_cte ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + DATE_TRUNC('month', subq_20.ds) = sma_0_cte.metric_time__day GROUP BY subq_20.ds ) subq_24 @@ -40,24 +43,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + subq_27.ds AS metric_time__day + , SUM(sma_0_cte.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_27 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day + subq_27.ds - INTERVAL 1 month = sma_0_cte.metric_time__day GROUP BY - subq_28.ds - ) subq_32 + subq_27.ds + ) subq_31 ON - subq_24.metric_time__day = subq_32.metric_time__day + subq_24.metric_time__day = subq_31.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_31.metric_time__day) +) subq_32 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index b31f53052..a3842a3bd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -3,15 +3,24 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__year + metric_time__year AS metric_time__year , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year + COALESCE(subq_24.metric_time__year, subq_31.metric_time__year) AS metric_time__year , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago + , MAX(subq_31.bookings_1_month_ago) AS bookings_1_month_ago FROM ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__year'] @@ -19,18 +28,12 @@ FROM ( -- Compute Metrics via Expressions SELECT DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings + , SUM(sma_0_cte.bookings) AS month_start_bookings FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 + INNER JOIN + sma_0_cte sma_0_cte ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + DATE_TRUNC('month', subq_20.ds) = sma_0_cte.metric_time__day WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds GROUP BY DATE_TRUNC('year', subq_20.ds) @@ -41,24 +44,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + DATE_TRUNC('year', subq_27.ds) AS metric_time__year + , SUM(sma_0_cte.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_27 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day + subq_27.ds - INTERVAL 1 month = sma_0_cte.metric_time__day GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('year', subq_27.ds) + ) subq_31 ON - subq_24.metric_time__year = subq_32.metric_time__year + subq_24.metric_time__year = subq_31.metric_time__year GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + COALESCE(subq_24.metric_time__year, subq_31.metric_time__year) +) subq_32 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 66f5fe7c5..a5627cb81 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -3,15 +3,24 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day + COALESCE(subq_21.metric_time__day, subq_29.metric_time__day) AS metric_time__day , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_29.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -21,12 +30,11 @@ FROM ( metric_time__day , SUM(bookings) AS bookings FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 + metric_time__day + , bookings + FROM sma_0_cte sma_0_cte ) subq_17 WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' GROUP BY @@ -43,26 +51,20 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 + subq_24.ds AS metric_time__day + , sma_0_cte.bookings AS bookings + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_25.ds - INTERVAL 14 day = subq_23.metric_time__day - ) subq_26 + subq_24.ds - INTERVAL 14 day = sma_0_cte.metric_time__day + ) subq_25 WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' GROUP BY metric_time__day - ) subq_30 + ) subq_29 ON - subq_21.metric_time__day = subq_30.metric_time__day + subq_21.metric_time__day = subq_29.metric_time__day GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_21.metric_time__day, subq_29.metric_time__day) +) subq_30 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index d916d9e53..c18f2e728 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -3,15 +3,25 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , booking_value + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - booking__ds__day + booking__ds__day AS booking__ds__day , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day + COALESCE(subq_21.booking__ds__day, subq_25.booking__ds__day) AS booking__ds__day , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + , MAX(subq_25.bookers) AS bookers FROM ( -- Join to Time Spine Dataset -- Pass Only Elements: ['booking_value', 'booking__ds__day'] @@ -19,30 +29,29 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value + , SUM(sma_0_cte.booking_value) AS booking_value FROM ***************************.mf_time_spine subq_17 INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + sma_0_cte sma_0_cte ON - subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + subq_17.ds - INTERVAL 1 week = sma_0_cte.booking__ds__day GROUP BY subq_17.ds ) subq_21 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['bookers', 'booking__ds__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + booking__ds__day + , COUNT(DISTINCT bookers) AS bookers + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 + booking__ds__day + ) subq_25 ON - subq_21.booking__ds__day = subq_26.booking__ds__day + subq_21.booking__ds__day = subq_25.booking__ds__day GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(subq_21.booking__ds__day, subq_25.booking__ds__day) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 101e623ab..72ca6a954 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -3,15 +3,24 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day + COALESCE(subq_24.metric_time__day, subq_29.metric_time__day) AS metric_time__day , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + , MAX(subq_29.booking_fees) AS booking_fees FROM ( -- Join to Time Spine Dataset SELECT @@ -24,17 +33,16 @@ FROM ( metric_time__day , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['booking_value', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS metric_time__day + metric_time__day , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) + metric_time__day ) subq_20 ) subq_21 ON @@ -46,21 +54,20 @@ FROM ( metric_time__day , booking_value * 0.05 AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['booking_value', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS metric_time__day + metric_time__day , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 + metric_time__day + ) subq_28 + ) subq_29 ON - subq_24.metric_time__day = subq_30.metric_time__day + subq_24.metric_time__day = subq_29.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(subq_24.metric_time__day, subq_29.metric_time__day) +) subq_30 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 8fadab17b..0d5bd5294 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -3,30 +3,33 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - booking__ds__day + booking__ds__day AS booking__ds__day , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day + COALESCE(subq_18.booking__ds__day, subq_25.booking__ds__day) AS booking__ds__day , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + , MAX(subq_25.bookings_at_start_of_month) AS bookings_at_start_of_month FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'booking__ds__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT booking__ds__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY booking__ds__day ) subq_18 @@ -36,24 +39,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + subq_21.ds AS booking__ds__day + , SUM(sma_0_cte.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day + DATE_TRUNC('month', subq_21.ds) = sma_0_cte.booking__ds__day GROUP BY - subq_22.ds - ) subq_26 + subq_21.ds + ) subq_25 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + subq_18.booking__ds__day = subq_25.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(subq_18.booking__ds__day, subq_25.booking__ds__day) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 63f543a97..d688425d6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -5,15 +5,26 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , booking_value + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__month + metric_time__month AS metric_time__month , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month + COALESCE(subq_24.metric_time__month, subq_29.metric_time__month) AS metric_time__month , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + , MAX(subq_29.bookers) AS bookers FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['booking_value', 'metric_time__month'] @@ -27,12 +38,12 @@ FROM ( SELECT subq_19.ds AS metric_time__day , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value + , sma_0_cte.booking_value AS booking_value FROM ***************************.mf_time_spine subq_19 INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + sma_0_cte sma_0_cte ON - subq_19.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + subq_19.ds - INTERVAL 1 week = sma_0_cte.metric_time__day ) subq_20 WHERE metric_time__day = '2020-01-01' GROUP BY @@ -47,20 +58,20 @@ FROM ( metric_time__month , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , metric_time__month + , booking_value + , bookers + FROM sma_0_cte sma_0_cte + ) subq_25 WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month - ) subq_30 + ) subq_29 ON - subq_24.metric_time__month = subq_30.metric_time__month + subq_24.metric_time__month = subq_29.metric_time__month GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + COALESCE(subq_24.metric_time__month, subq_29.metric_time__month) +) subq_30 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 0c93f0425..060e7da99 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -5,19 +5,31 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , booking_value + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day - , metric_time__month - , metric_time__year + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year + COALESCE(subq_21.metric_time__day, subq_25.metric_time__day) AS metric_time__day + , COALESCE(subq_21.metric_time__month, subq_25.metric_time__month) AS metric_time__month + , COALESCE(subq_21.metric_time__year, subq_25.metric_time__year) AS metric_time__year , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + , MAX(subq_25.bookers) AS bookers FROM ( -- Join to Time Spine Dataset -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] @@ -27,44 +39,43 @@ FROM ( subq_17.ds AS metric_time__day , DATE_TRUNC('month', subq_17.ds) AS metric_time__month , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value + , SUM(sma_0_cte.booking_value) AS booking_value FROM ***************************.mf_time_spine subq_17 INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + sma_0_cte sma_0_cte ON - subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + subq_17.ds - INTERVAL 1 week = sma_0_cte.metric_time__day GROUP BY subq_17.ds , DATE_TRUNC('month', subq_17.ds) , DATE_TRUNC('year', subq_17.ds) ) subq_21 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + metric_time__day + , metric_time__month + , metric_time__year + , COUNT(DISTINCT bookers) AS bookers + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 + metric_time__day + , metric_time__month + , metric_time__year + ) subq_25 ON ( - subq_21.metric_time__day = subq_26.metric_time__day + subq_21.metric_time__day = subq_25.metric_time__day ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month + subq_21.metric_time__month = subq_25.metric_time__month ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year + subq_21.metric_time__year = subq_25.metric_time__year ) GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(subq_21.metric_time__day, subq_25.metric_time__day) + , COALESCE(subq_21.metric_time__month, subq_25.metric_time__month) + , COALESCE(subq_21.metric_time__year, subq_25.metric_time__year) +) subq_26 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 84c0c9aea..88987d510 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -3,30 +3,33 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - booking__ds__day + booking__ds__day AS booking__ds__day , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day + COALESCE(subq_18.booking__ds__day, subq_25.booking__ds__day) AS booking__ds__day , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_25.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'booking__ds__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT booking__ds__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY booking__ds__day ) subq_18 @@ -36,24 +39,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + subq_21.ds AS booking__ds__day + , SUM(sma_0_cte.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_22.ds - INTERVAL 14 day = subq_20.booking__ds__day + subq_21.ds - INTERVAL 14 day = sma_0_cte.booking__ds__day GROUP BY - subq_22.ds - ) subq_26 + subq_21.ds + ) subq_25 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + subq_18.booking__ds__day = subq_25.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(subq_18.booking__ds__day, subq_25.booking__ds__day) +) subq_26 diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 31ebdbb9f..2f405f96a 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -3,15 +3,24 @@ test_filename: test_fill_nulls_with_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + COALESCE(subq_24.metric_time__day, subq_31.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_31.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( -- Compute Metrics via Expressions SELECT @@ -24,19 +33,13 @@ FROM ( , subq_20.bookings AS bookings FROM ***************************.mf_time_spine subq_22 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__day ) subq_20 @@ -50,24 +53,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + subq_27.ds AS metric_time__day + , SUM(sma_0_cte.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_27 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_28.ds - INTERVAL 14 day = subq_26.metric_time__day + subq_27.ds - INTERVAL 14 day = sma_0_cte.metric_time__day GROUP BY - subq_28.ds - ) subq_32 + subq_27.ds + ) subq_31 ON - subq_24.metric_time__day = subq_32.metric_time__day + subq_24.metric_time__day = subq_31.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_31.metric_time__day) +) subq_32 diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql index 475bffcec..20461667e 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql @@ -3,30 +3,34 @@ test_filename: test_granularity_date_part_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , EXTRACT(isodow FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__extract_dow + metric_time__extract_dow AS metric_time__extract_dow , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow + COALESCE(subq_18.metric_time__extract_dow, subq_25.metric_time__extract_dow) AS metric_time__extract_dow , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_25.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( + -- Read From CTE For node_id=sma_0 + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT metric_time__extract_dow , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(isodow FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 + FROM sma_0_cte sma_0_cte GROUP BY metric_time__extract_dow ) subq_18 @@ -36,24 +40,18 @@ FROM ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT - EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 + EXTRACT(isodow FROM subq_21.ds) AS metric_time__extract_dow + , SUM(sma_0_cte.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_21 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day + subq_21.ds - INTERVAL 14 day = sma_0_cte.metric_time__day GROUP BY - EXTRACT(isodow FROM subq_22.ds) - ) subq_26 + EXTRACT(isodow FROM subq_21.ds) + ) subq_25 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + subq_18.metric_time__extract_dow = subq_25.metric_time__extract_dow GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + COALESCE(subq_18.metric_time__extract_dow, subq_25.metric_time__extract_dow) +) subq_26 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 4c4a00d3b..6245a9aea 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -8,36 +8,45 @@ sql_engine: DuckDB -- Pass Only Elements: ['bookers',] -- Aggregate Measures -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + guest_id AS guest + , booking_value + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COUNT(DISTINCT bookers) AS bookers FROM ( -- Join Standard Outputs SELECT - subq_19.guest__booking_value AS guest__booking_value + subq_18.guest__booking_value AS guest__booking_value , subq_13.bookers AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - guest_id AS guest - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + guest + , booking_value + , bookers + FROM sma_0_cte sma_0_cte ) subq_13 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['booking_value', 'guest'] -- Aggregate Measures -- Compute Metrics via Expressions -- Pass Only Elements: ['guest', 'guest__booking_value'] SELECT - guest_id AS guest + guest , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + FROM sma_0_cte sma_0_cte GROUP BY - guest_id - ) subq_19 + guest + ) subq_18 ON - subq_13.guest = subq_19.guest -) subq_20 + subq_13.guest = subq_18.guest +) subq_19 WHERE guest__booking_value > 1.00 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql index 71eb89003..2d3222e6b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql @@ -6,12 +6,22 @@ sql_engine: DuckDB -- Pass Only Elements: ['listings',] -- Aggregate Measures -- Compute Metrics via Expressions +WITH sma_1_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT SUM(listings) AS listings FROM ( -- Join Standard Outputs SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate + CAST(subq_38.buys AS DOUBLE) / CAST(NULLIF(subq_38.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate , subq_24.listings AS listings FROM ( -- Read Elements From Semantic Model 'listings_latest' @@ -24,72 +34,57 @@ FROM ( LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_28.user, subq_38.user) AS user + COALESCE(subq_28.user, subq_37.user) AS user , MAX(subq_28.visits) AS visits - , MAX(subq_38.buys) AS buys + , MAX(subq_37.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_1 + -- Pass Only Elements: ['visits', 'user'] -- Aggregate Measures SELECT - subq_27.user + sma_1_cte.user , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'user'] - SELECT - user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_27 + FROM sma_1_cte sma_1_cte GROUP BY - subq_27.user + sma_1_cte.user ) subq_28 FULL OUTER JOIN ( -- Find conversions for user within the range of INF -- Pass Only Elements: ['buys', 'user'] -- Aggregate Measures SELECT - subq_35.user + subq_34.user , SUM(buys) AS buys FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_31.visits) OVER ( + FIRST_VALUE(sma_1_cte.visits) OVER ( PARTITION BY - subq_34.user - , subq_34.metric_time__day - , subq_34.mf_internal_uuid - ORDER BY subq_31.metric_time__day DESC + subq_33.user + , subq_33.metric_time__day + , subq_33.mf_internal_uuid + ORDER BY sma_1_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_31.metric_time__day) OVER ( + , FIRST_VALUE(sma_1_cte.metric_time__day) OVER ( PARTITION BY - subq_34.user - , subq_34.metric_time__day - , subq_34.mf_internal_uuid - ORDER BY subq_31.metric_time__day DESC + subq_33.user + , subq_33.metric_time__day + , subq_33.mf_internal_uuid + ORDER BY sma_1_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_31.user) OVER ( + , FIRST_VALUE(sma_1_cte.user) OVER ( PARTITION BY - subq_34.user - , subq_34.metric_time__day - , subq_34.mf_internal_uuid - ORDER BY subq_31.metric_time__day DESC + subq_33.user + , subq_33.metric_time__day + , subq_33.mf_internal_uuid + ORDER BY sma_1_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_34.mf_internal_uuid AS mf_internal_uuid - , subq_34.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_31 + , subq_33.mf_internal_uuid AS mf_internal_uuid + , subq_33.buys AS buys + FROM sma_1_cte sma_1_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -100,23 +95,23 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_34 + ) subq_33 ON ( - subq_31.user = subq_34.user + sma_1_cte.user = subq_33.user ) AND ( - (subq_31.metric_time__day <= subq_34.metric_time__day) + (sma_1_cte.metric_time__day <= subq_33.metric_time__day) ) - ) subq_35 + ) subq_34 GROUP BY - subq_35.user - ) subq_38 + subq_34.user + ) subq_37 ON - subq_28.user = subq_38.user + subq_28.user = subq_37.user GROUP BY - COALESCE(subq_28.user, subq_38.user) - ) subq_39 + COALESCE(subq_28.user, subq_37.user) + ) subq_38 ON - subq_24.user = subq_39.user -) subq_42 + subq_24.user = subq_38.user +) subq_41 WHERE user__visit_buy_conversion_rate > 2 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 53ce7c915..64c93b3d5 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -6,21 +6,24 @@ sql_engine: DuckDB -- Pass Only Elements: ['listings',] -- Aggregate Measures -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT SUM(listings) AS listings FROM ( -- Join Standard Outputs SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings - FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 + subq_26.listing__user__average_booking_value AS user__listing__user__average_booking_value + , sma_0_cte.listings AS listings + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN ( -- Join Standard Outputs -- Pass Only Elements: ['average_booking_value', 'listing__user'] @@ -28,17 +31,17 @@ FROM ( -- Compute Metrics via Expressions -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user + sma_0_cte.user AS listing__user , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_0_cte sma_0_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + bookings_source_src_28000.listing_id = sma_0_cte.listing GROUP BY - listings_latest_src_28000.user_id - ) subq_27 + sma_0_cte.user + ) subq_26 ON - subq_17.user = subq_27.listing__user -) subq_28 + sma_0_cte.user = subq_26.listing__user +) subq_27 WHERE user__listing__user__average_booking_value > 1 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql index b0ce0b1a1..4109fedd7 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql @@ -8,36 +8,43 @@ sql_engine: DuckDB -- Pass Only Elements: ['bookers',] -- Aggregate Measures -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COUNT(DISTINCT bookers) AS bookers FROM ( -- Join Standard Outputs SELECT - subq_19.listing__bookers AS listing__bookers + subq_18.listing__bookers AS listing__bookers , subq_13.bookers AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - listing_id AS listing - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + listing + , bookers + FROM sma_0_cte sma_0_cte ) subq_13 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['bookers', 'listing'] -- Aggregate Measures -- Compute Metrics via Expressions -- Pass Only Elements: ['listing', 'listing__bookers'] SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + listing + , COUNT(DISTINCT bookers) AS listing__bookers + FROM sma_0_cte sma_0_cte GROUP BY - listing_id - ) subq_19 + listing + ) subq_18 ON - subq_13.listing = subq_19.listing -) subq_20 + subq_13.listing = subq_18.listing +) subq_19 WHERE listing__bookers > 1.00 diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 7c9a745ff..212e28605 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -8,13 +8,23 @@ sql_engine: DuckDB -- Pass Only Elements: ['listings',] -- Aggregate Measures -- Compute Metrics via Expressions +WITH sma_1_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT SUM(listings) AS listings FROM ( -- Join Standard Outputs SELECT subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers + , subq_30.listing__bookers AS listing__bookers , subq_19.listings AS listings FROM ( -- Read Elements From Semantic Model 'listings_latest' @@ -25,41 +35,34 @@ FROM ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) subq_19 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_1 + -- Pass Only Elements: ['bookings', 'listing'] -- Aggregate Measures -- Compute Metrics via Expressions -- Pass Only Elements: ['listing', 'listing__bookings'] SELECT listing , SUM(bookings) AS listing__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_22 + FROM sma_1_cte sma_1_cte GROUP BY listing ) subq_25 ON subq_19.listing = subq_25.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_1 -- Pass Only Elements: ['bookers', 'listing'] -- Aggregate Measures -- Compute Metrics via Expressions -- Pass Only Elements: ['listing', 'listing__bookers'] SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + listing + , COUNT(DISTINCT bookers) AS listing__bookers + FROM sma_1_cte sma_1_cte GROUP BY - listing_id - ) subq_31 + listing + ) subq_30 ON - subq_19.listing = subq_31.listing -) subq_32 + subq_19.listing = subq_30.listing +) subq_31 WHERE listing__bookings > 2 AND listing__bookers > 1 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql index c3ae14da6..65aae3ded 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_conversion_metric_query_filters__plan0_optimized.sql @@ -5,17 +5,36 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +, rss_28028_cte AS ( + -- Read Elements From Semantic Model 'users_latest' + SELECT + home_state_latest + , user_id AS user + FROM ***************************.dim_users_latest users_latest_src_28000 +) + SELECT - metric_time__day - , user__home_state_latest + metric_time__day AS metric_time__day + , user__home_state_latest AS user__home_state_latest , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_30.metric_time__day, subq_44.metric_time__day) AS metric_time__day - , COALESCE(subq_30.user__home_state_latest, subq_44.user__home_state_latest) AS user__home_state_latest + COALESCE(subq_30.metric_time__day, subq_43.metric_time__day) AS metric_time__day + , COALESCE(subq_30.user__home_state_latest, subq_43.user__home_state_latest) AS user__home_state_latest , MAX(subq_30.visits) AS visits - , MAX(subq_44.buys) AS buys + , MAX(subq_43.buys) AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'user__home_state_latest', 'metric_time__day'] @@ -27,24 +46,15 @@ FROM ( FROM ( -- Join Standard Outputs SELECT - users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_24.metric_time__day AS metric_time__day - , subq_24.visit__referrer_id AS visit__referrer_id - , subq_24.visits AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_24 + rss_28028_cte.home_state_latest AS user__home_state_latest + , sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.visit__referrer_id AS visit__referrer_id + , sma_0_cte.visits AS visits + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 + rss_28028_cte rss_28028_cte ON - subq_24.user = users_latest_src_28000.user_id + sma_0_cte.user = rss_28028_cte.user ) subq_27 WHERE visit__referrer_id = '123456' GROUP BY @@ -62,82 +72,73 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_37.visits) OVER ( + FIRST_VALUE(subq_36.visits) OVER ( PARTITION BY - subq_40.user - , subq_40.metric_time__day - , subq_40.mf_internal_uuid - ORDER BY subq_37.metric_time__day DESC + subq_39.user + , subq_39.metric_time__day + , subq_39.mf_internal_uuid + ORDER BY subq_36.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_37.visit__referrer_id) OVER ( + , FIRST_VALUE(subq_36.visit__referrer_id) OVER ( PARTITION BY - subq_40.user - , subq_40.metric_time__day - , subq_40.mf_internal_uuid - ORDER BY subq_37.metric_time__day DESC + subq_39.user + , subq_39.metric_time__day + , subq_39.mf_internal_uuid + ORDER BY subq_36.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_37.user__home_state_latest) OVER ( + , FIRST_VALUE(subq_36.user__home_state_latest) OVER ( PARTITION BY - subq_40.user - , subq_40.metric_time__day - , subq_40.mf_internal_uuid - ORDER BY subq_37.metric_time__day DESC + subq_39.user + , subq_39.metric_time__day + , subq_39.mf_internal_uuid + ORDER BY subq_36.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user__home_state_latest - , FIRST_VALUE(subq_37.metric_time__day) OVER ( + , FIRST_VALUE(subq_36.metric_time__day) OVER ( PARTITION BY - subq_40.user - , subq_40.metric_time__day - , subq_40.mf_internal_uuid - ORDER BY subq_37.metric_time__day DESC + subq_39.user + , subq_39.metric_time__day + , subq_39.mf_internal_uuid + ORDER BY subq_36.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_37.user) OVER ( + , FIRST_VALUE(subq_36.user) OVER ( PARTITION BY - subq_40.user - , subq_40.metric_time__day - , subq_40.mf_internal_uuid - ORDER BY subq_37.metric_time__day DESC + subq_39.user + , subq_39.metric_time__day + , subq_39.mf_internal_uuid + ORDER BY subq_36.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_40.mf_internal_uuid AS mf_internal_uuid - , subq_40.buys AS buys + , subq_39.mf_internal_uuid AS mf_internal_uuid + , subq_39.buys AS buys FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['visits', 'visit__referrer_id', 'user__home_state_latest', 'metric_time__day', 'user'] SELECT metric_time__day - , subq_35.user + , subq_34.user , visit__referrer_id , user__home_state_latest , visits FROM ( -- Join Standard Outputs SELECT - users_latest_src_28000.home_state_latest AS user__home_state_latest - , subq_32.metric_time__day AS metric_time__day - , subq_32.user AS user - , subq_32.visit__referrer_id AS visit__referrer_id - , subq_32.visits AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_32 + rss_28028_cte.home_state_latest AS user__home_state_latest + , sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.user AS user + , sma_0_cte.visit__referrer_id AS visit__referrer_id + , sma_0_cte.visits AS visits + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.dim_users_latest users_latest_src_28000 + rss_28028_cte rss_28028_cte ON - subq_32.user = users_latest_src_28000.user_id - ) subq_35 + sma_0_cte.user = rss_28028_cte.user + ) subq_34 WHERE visit__referrer_id = '123456' - ) subq_37 + ) subq_36 INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -148,29 +149,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_40 + ) subq_39 ON ( - subq_37.user = subq_40.user + subq_36.user = subq_39.user ) AND ( ( - subq_37.metric_time__day <= subq_40.metric_time__day + subq_36.metric_time__day <= subq_39.metric_time__day ) AND ( - subq_37.metric_time__day > subq_40.metric_time__day - INTERVAL 7 day + subq_36.metric_time__day > subq_39.metric_time__day - INTERVAL 7 day ) ) - ) subq_41 + ) subq_40 GROUP BY metric_time__day , user__home_state_latest - ) subq_44 + ) subq_43 ON ( - subq_30.user__home_state_latest = subq_44.user__home_state_latest + subq_30.user__home_state_latest = subq_43.user__home_state_latest ) AND ( - subq_30.metric_time__day = subq_44.metric_time__day + subq_30.metric_time__day = subq_43.metric_time__day ) GROUP BY - COALESCE(subq_30.metric_time__day, subq_44.metric_time__day) - , COALESCE(subq_30.user__home_state_latest, subq_44.user__home_state_latest) -) subq_45 + COALESCE(subq_30.metric_time__day, subq_43.metric_time__day) + , COALESCE(subq_30.user__home_state_latest, subq_43.user__home_state_latest) +) subq_44 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 7c8a701bb..d6321c9a9 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -13,15 +13,26 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , booking_value AS max_booking_value + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day + COALESCE(subq_17.metric_time__day, subq_21.metric_time__day) AS metric_time__day , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + , MAX(subq_21.max_booking_value) AS max_booking_value FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] @@ -31,33 +42,32 @@ FROM ( metric_time__day , AVG(average_booking_value) AS average_booking_value FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + metric_time__day + , booking__is_instant + , max_booking_value + , average_booking_value + FROM sma_0_cte sma_0_cte ) subq_13 WHERE booking__is_instant GROUP BY metric_time__day ) subq_17 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + metric_time__day + , MAX(max_booking_value) AS max_booking_value + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 + metric_time__day + ) subq_21 ON - subq_17.metric_time__day = subq_22.metric_time__day + subq_17.metric_time__day = subq_21.metric_time__day GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + COALESCE(subq_17.metric_time__day, subq_21.metric_time__day) +) subq_22 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 353a58c7d..1b9e60b21 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -7,17 +7,37 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + +, sma_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - metric_time__day - , listing__country_latest + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + COALESCE(subq_42.metric_time__day, subq_56.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_56.listing__country_latest) AS listing__country_latest , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + , COALESCE(MAX(subq_56.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( -- Compute Metrics via Expressions SELECT @@ -42,24 +62,15 @@ FROM ( FROM ( -- Join Standard Outputs SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 + sma_1_cte.country_latest AS listing__country_latest + , sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.booking__is_instant AS booking__is_instant + , sma_0_cte.bookings AS bookings + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_1_cte sma_1_cte ON - subq_31.listing = listings_latest_src_28000.listing_id + sma_0_cte.listing = sma_1_cte.listing ) subq_35 WHERE booking__is_instant GROUP BY @@ -79,10 +90,10 @@ FROM ( FROM ( -- Join to Time Spine Dataset SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 + subq_54.ds AS metric_time__day + , subq_52.listing__country_latest AS listing__country_latest + , subq_52.bookings AS bookings + FROM ***************************.mf_time_spine subq_54 LEFT OUTER JOIN ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] @@ -94,52 +105,44 @@ FROM ( FROM ( -- Join Standard Outputs SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings + sma_1_cte.country_latest AS listing__country_latest + , subq_46.metric_time__day AS metric_time__day + , subq_46.booking__is_instant AS booking__is_instant + , subq_46.bookings AS bookings FROM ( -- Join to Time Spine Dataset SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 + subq_45.ds AS metric_time__day + , sma_0_cte.listing AS listing + , sma_0_cte.booking__is_instant AS booking__is_instant + , sma_0_cte.bookings AS bookings + FROM ***************************.mf_time_spine subq_45 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_46.ds - INTERVAL 14 day = subq_44.metric_time__day - ) subq_47 + subq_45.ds - INTERVAL 14 day = sma_0_cte.metric_time__day + ) subq_46 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_1_cte sma_1_cte ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 + subq_46.listing = sma_1_cte.listing + ) subq_49 WHERE booking__is_instant GROUP BY metric_time__day , listing__country_latest - ) subq_54 + ) subq_52 ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 + subq_54.ds = subq_52.metric_time__day + ) subq_55 + ) subq_56 ON ( - subq_42.listing__country_latest = subq_58.listing__country_latest + subq_42.listing__country_latest = subq_56.listing__country_latest ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day + subq_42.metric_time__day = subq_56.metric_time__day ) GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + COALESCE(subq_42.metric_time__day, subq_56.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_56.listing__country_latest) +) subq_57 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql index e8368747a..c6c46601c 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -7,17 +7,37 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) + +, sma_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - metric_time__day - , listing__country_latest + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest + COALESCE(subq_33.metric_time__day, subq_44.metric_time__day) AS metric_time__day + , COALESCE(subq_33.listing__country_latest, subq_44.listing__country_latest) AS listing__country_latest , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + , MAX(subq_44.bookings_2_weeks_ago) AS bookings_2_weeks_ago FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] @@ -30,24 +50,15 @@ FROM ( FROM ( -- Join Standard Outputs SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 + sma_1_cte.country_latest AS listing__country_latest + , sma_0_cte.metric_time__day AS metric_time__day + , sma_0_cte.booking__is_instant AS booking__is_instant + , sma_0_cte.bookings AS bookings + FROM sma_0_cte sma_0_cte LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_1_cte sma_1_cte ON - subq_25.listing = listings_latest_src_28000.listing_id + sma_0_cte.listing = sma_1_cte.listing ) subq_29 WHERE booking__is_instant GROUP BY @@ -66,48 +77,40 @@ FROM ( FROM ( -- Join Standard Outputs SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings + sma_1_cte.country_latest AS listing__country_latest + , subq_37.metric_time__day AS metric_time__day + , subq_37.booking__is_instant AS booking__is_instant + , subq_37.bookings AS bookings FROM ( -- Join to Time Spine Dataset SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 + subq_36.ds AS metric_time__day + , sma_0_cte.listing AS listing + , sma_0_cte.booking__is_instant AS booking__is_instant + , sma_0_cte.bookings AS bookings + FROM ***************************.mf_time_spine subq_36 + INNER JOIN + sma_0_cte sma_0_cte ON - subq_37.ds - INTERVAL 14 day = subq_35.metric_time__day - ) subq_38 + subq_36.ds - INTERVAL 14 day = sma_0_cte.metric_time__day + ) subq_37 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_1_cte sma_1_cte ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 + subq_37.listing = sma_1_cte.listing + ) subq_40 WHERE booking__is_instant GROUP BY metric_time__day , listing__country_latest - ) subq_46 + ) subq_44 ON ( - subq_33.listing__country_latest = subq_46.listing__country_latest + subq_33.listing__country_latest = subq_44.listing__country_latest ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day + subq_33.metric_time__day = subq_44.metric_time__day ) GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(subq_33.metric_time__day, subq_44.metric_time__day) + , COALESCE(subq_33.listing__country_latest, subq_44.listing__country_latest) +) subq_45 diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 766a90fea..034a5c406 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,33 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, subq_64.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(CAST(subq_64.bookings AS DOUBLE) / CAST(NULLIF(subq_64.views, 0) AS DOUBLE)) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest FULL OUTER JOIN ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views - FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing__capacity_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing__capacity_latest - , SUM(views) AS views - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) +) subq_64 ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = subq_64.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, subq_64.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 9af38ae99..3b716dc0b 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -3,15 +3,25 @@ test_filename: test_query_rendering.py sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_0_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day + COALESCE(subq_17.metric_time__day, subq_21.metric_time__day) AS metric_time__day , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + , MAX(subq_21.booking_value) AS booking_value FROM ( -- Constrain Output with WHERE -- Pass Only Elements: ['booking_value', 'metric_time__day'] @@ -21,33 +31,31 @@ FROM ( metric_time__day , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant + metric_time__day + , booking__is_instant , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + FROM sma_0_cte sma_0_cte ) subq_13 WHERE booking__is_instant GROUP BY metric_time__day ) subq_17 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Read From CTE For node_id=sma_0 -- Pass Only Elements: ['booking_value', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', ds) AS metric_time__day + metric_time__day , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + FROM sma_0_cte sma_0_cte GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 + metric_time__day + ) subq_21 ON - subq_17.metric_time__day = subq_22.metric_time__day + subq_17.metric_time__day = subq_21.metric_time__day GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + COALESCE(subq_17.metric_time__day, subq_21.metric_time__day) +) subq_22