From 69ed6a8de09d5819a9873402a46f8b5a37d66614 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 22:23:57 -0800 Subject: [PATCH] Update other engine snapshots. --- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 99 ++++++------- .../test_conversion_rate__plan0_optimized.sql | 99 ++++++------- ...h_constant_properties__plan0_optimized.sql | 132 ++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 108 +++++++------- ...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 +++++++-------- ...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 +++++++-------- ...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 +++++++-------- ...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 +++++++-------- ...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 | 65 ++++----- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../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 | 39 +++-- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...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 | 33 +++-- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- 72 files changed, 2703 insertions(+), 2715 deletions(-) diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql index 1267ebb04..2949bf60c 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: BigQuery --- -- 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 + DATETIME_TRUNC(ds, day) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/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/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index eab4d41c7..c29b49ee4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/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/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) 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 FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) 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 - DATETIME_TRUNC(ds, day) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 > DATE_SUB(CAST(subq_33.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_32.metric_time__day AS DATETIME), 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 metric_time__day -) subq_41 +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql index a8f6fb72d..26e66eddd 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) 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 FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 2bbbc7574..3d75853ab 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) 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 FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) 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 - DATETIME_TRUNC(ds, day) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), 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 metric_time__day , visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 183e1f7b0..364279bcb 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: BigQuery --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS FLOAT64) / CAST(NULLIF(MAX(subq_18.visits), 0) AS FLOAT64) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) 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 FLOAT64) / CAST(NULLIF(MAX(subq_18.visits), 0) AS FLOAT64) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql index 038bda649..b9a83bb9d 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) 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 FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) 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 - DATETIME_TRUNC(ds, day) 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 - DATETIME_TRUNC(ds, day) 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 , GENERATE_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 > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), 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 metric_time__day , visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql index 6b4bf6b28..b98570fe0 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Databricks --- -- 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 , 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/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/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index f5a58a70f..054eabc22 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/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/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Databricks --- -- 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 , 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 > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__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/Databricks/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql index 0c21b78b8..673f1c594 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Databricks --- -- 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 , 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/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 56dc8aa18..f548e1c25 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Databricks --- -- 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 , 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 91c5c6652..3092aba63 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Databricks --- -- 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 , 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql index 4d601ef64..b3a2f09e0 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Databricks --- -- 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 , 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql index 53d7571ad..a116b7c87 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Postgres --- -- 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 - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/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/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 00ff936d0..ed8678842 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/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/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Postgres --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_32.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) 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/Postgres/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql index 97c2f6f0b..803bd0f36 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Postgres --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 6e4425814..109d592bd 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Postgres --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) 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/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 625a688fb..63e7d4caf 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Postgres --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) 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 PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) 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 - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql index 18cc7b5f6..6e11ad3d4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Postgres --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) 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/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql index 2de7bf5d3..5dd3f9150 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Redshift --- -- 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/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/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 896945c98..3c41d84dc 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/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/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Redshift --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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 > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__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/Redshift/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql index 726b9bb90..2700910c7 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Redshift --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 805203209..9d324727c 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Redshift --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 389b6e71e..029a4cdd1 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Redshift --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) 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 PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql index b65402928..ab41c8632 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Redshift --- -- 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 PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) 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 , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql index 45198ff84..cb66b10ff 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Snowflake --- -- 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 , UUID_STRING() 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/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/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 7a9818076..463efd3a5 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/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/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Snowflake --- -- 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 , UUID_STRING() 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 > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__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/Snowflake/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql index 198a34d6c..a2bf58fc4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Snowflake --- -- 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 , UUID_STRING() 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/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql index fcd07294d..8c7b493ad 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Snowflake --- -- 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 , UUID_STRING() 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 9165dcef4..f6cea61c4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Snowflake --- -- 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 , UUID_STRING() 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql index 5289cd349..11b8ede2a 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Snowflake --- -- 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 , UUID_STRING() 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 > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__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/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql index ab9c25bc0..f829f4ca3 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Trino --- -- 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 , 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 > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/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/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 5fdf62a89..b354a8d96 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/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/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Trino --- -- 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 , 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 > DATE_ADD('day', -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_32.metric_time__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/Trino/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql index b4cc1c15b..ce39c30a2 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Trino --- -- 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 , 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/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 6c39bb950..772aef980 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Trino --- -- 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 , 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 > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__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/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql index cbf203759..78b24a0a8 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Trino --- -- 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 , 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 > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql index 3cbcfcfba..06aaa63ab 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Trino --- -- 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 , 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 > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__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/BigQuery/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml index 5d5d9d565..75da5fd0b 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml @@ -7,80 +7,77 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml index 6f31d9f16..248cf24e4 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/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_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/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/Databricks/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/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_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/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/Postgres/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/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_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/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/Redshift/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/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_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/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/Snowflake/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/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_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml index 6af35e7d7..2d133f4c6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/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/Trino/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a..4da40aaf0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/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/BigQuery/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql index af7158814..292362d02 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: BigQuery --- -- 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/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 03e5630df..dc98ebb10 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: BigQuery --- -- 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 FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) 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,9 +44,9 @@ 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 ds__day , listing__country_latest @@ -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 ds__day , listing__country_latest - ) subq_36 + ) 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 ds__day , listing__country_latest -) subq_37 +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql index 22f65187f..2fced228e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: BigQuery --- -- 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_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql index c0bc19891..213e8cbd9 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Databricks --- -- 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/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index d0c28be01..45487cf27 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Databricks --- -- 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/Databricks/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql index 6fc50e372..e27301210 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Databricks --- -- 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_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql index 983f8f1d1..bfead2543 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Postgres --- -- 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/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 9a15a293f..c8d146de3 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Postgres --- -- 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 PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) 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/Postgres/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql index 7ba2d4392..0d42756ed 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Postgres --- -- 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_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql index 59cf853e5..0ae940a5e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Redshift --- -- 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/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 1d695a61a..3343128d7 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Redshift --- -- 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 PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) 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/Redshift/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql index e83e60d3e..697433a5d 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Redshift --- -- 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_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql index 476ce6aea..76c604e6a 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Snowflake --- -- 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/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 4ecf068dc..b14f947b5 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Snowflake --- -- 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/Snowflake/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql index dbcbd7c6d..becffa2bd 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Snowflake --- -- 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_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql index 41aeb438e..9a6908294 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Trino --- -- 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/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index a7ddeb2c1..84c74729c 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Trino --- -- 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/Trino/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql index f86a7cbfa..a49173cc2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Trino --- -- 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/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 36bc8f576..0b4cc0bff 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,38 +5,43 @@ docstring: sql_engine: BigQuery --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATETIME_TRUNC(ds, day) AS ds__day + , DATETIME_TRUNC(paid_at, day) 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 - DATETIME_TRUNC(ds, day) 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 ) 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 - DATETIME_TRUNC(paid_at, day) 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 metric_time__day ) subq_19 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index c9e02e625..4a9171736 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Databricks --- -- 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 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 02a4e5b92..60600f154 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Postgres --- -- 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 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 8736f91df..cb4519e9f 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Redshift --- -- 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 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 259fad37d..477d340b0 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Snowflake --- -- 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 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 663ee1971..afb011f5d 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Trino --- -- 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