From 0eafcbe57a98910a1d99341ef7aeee00b6775056 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 22:02:39 -0800 Subject: [PATCH] Update snapshots for DuckDB. --- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- 12 files changed, 453 insertions(+), 455 deletions(-) diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql index d52f20bc3..8f9ca82ad 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_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 + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -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_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.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_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , 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_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 7221fbdec..f93bee27a 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_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_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.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_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > subq_33.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_32.metric_time__day - INTERVAL 7 day ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql index 2375a22b4..e681c6fd8 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_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 - 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_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - 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_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , 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_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql index cc21cc694..c468e4c33 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_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 + , session_id AS session + , 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_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - 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 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , 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_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 12d9bcee0..e3e36d963 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_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_27.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -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_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.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_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , 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_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql index 1e68f8456..6a5a0bb6f 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_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_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - 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 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - 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_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , 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_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql index 6188d8c5f..d0b2da356 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 4159b2cec..fc27a90b6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28014_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 - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql index 451fcb54d..deb429189 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: DuckDB --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index c82fb2412..d6e070aa7 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS 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_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day