From 64398acf697ebf1d45b3e86f2003c403a93a6e27 Mon Sep 17 00:00:00 2001 From: Will Deng Date: Fri, 15 Dec 2023 00:44:13 -0500 Subject: [PATCH] updated conversion metric snapshots --- ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../BigQuery/test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../DuckDB/test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../Postgres/test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../Redshift/test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.sql | 346 +++++ ...ount_with_no_group_by__plan0_optimized.sql | 63 + .../Snowflake/test_conversion_rate__plan0.sql | 355 +++++ .../test_conversion_rate__plan0_optimized.sql | 83 + ...n_rate_with_constant_properties__plan0.sql | 389 +++++ ...h_constant_properties__plan0_optimized.sql | 111 ++ ...onversion_rate_with_no_group_by__plan0.sql | 346 +++++ ...rate_with_no_group_by__plan0_optimized.sql | 63 + ...est_conversion_rate_with_window__plan0.sql | 379 +++++ ...sion_rate_with_window__plan0_optimized.sql | 101 ++ ...nversion_count_with_no_group_by__plan0.xml | 1220 +++++++++++++++ .../test_conversion_rate__plan0.xml | 1268 ++++++++++++++++ ...n_rate_with_constant_properties__plan0.xml | 1333 +++++++++++++++++ ...onversion_rate_with_no_group_by__plan0.xml | 1220 +++++++++++++++ ...est_conversion_rate_with_window__plan0.xml | 1316 ++++++++++++++++ 65 files changed, 19773 insertions(+) create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_count_with_no_group_by__plan0.xml create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate__plan0.xml create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_constant_properties__plan0.xml create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_no_group_by__plan0.xml create mode 100644 metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_window__plan0.xml diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..546e925870 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC(buys_source_src_10002.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC(buys_source_src_10002.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS buy__ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATE_SUB(CAST(subq_9.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..28bb0ddbe1 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATE_SUB(CAST(subq_24.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..587c85a1bc --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS FLOAT64) / CAST(NULLIF(subq_14.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC(buys_source_src_10002.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC(buys_source_src_10002.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS buy__ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + visit__referrer_id +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..caaf0f7af1 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + visit__referrer_id +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..5c23e10b41 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS FLOAT64) / CAST(NULLIF(subq_14.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC(buys_source_src_10002.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC(buys_source_src_10002.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS buy__ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATE_SUB(CAST(subq_9.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + metric_time__day + , visit__referrer_id +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..66d687d5fa --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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(ds, day) AS metric_time__day + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , DATE_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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , session_id + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATE_SUB(CAST(subq_24.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.metric_time__day + ) + GROUP BY + metric_time__day + , visit__referrer_id +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..77017a8336 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS FLOAT64) / CAST(NULLIF(subq_14.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC(buys_source_src_10002.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC(buys_source_src_10002.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS buy__ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATE_SUB(CAST(subq_9.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..5c7c59b67b --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATE_SUB(CAST(subq_24.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..dde073e211 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS FLOAT64) / CAST(NULLIF(subq_14.visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC(visits_source_src_10011.ds, day) AS ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC(visits_source_src_10011.ds, day) AS visit__ds__day + , DATE_TRUNC(visits_source_src_10011.ds, isoweek) AS visit__ds__week + , DATE_TRUNC(visits_source_src_10011.ds, month) AS visit__ds__month + , DATE_TRUNC(visits_source_src_10011.ds, quarter) AS visit__ds__quarter + , DATE_TRUNC(visits_source_src_10011.ds, year) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , IF(EXTRACT(dayofweek FROM visits_source_src_10011.ds) = 1, 7, EXTRACT(dayofweek FROM visits_source_src_10011.ds) - 1) AS visit__ds__extract_dow + , EXTRACT(dayofyear FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GENERATE_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC(buys_source_src_10002.ds, day) AS ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC(buys_source_src_10002.ds, day) AS buy__ds__day + , DATE_TRUNC(buys_source_src_10002.ds, isoweek) AS buy__ds__week + , DATE_TRUNC(buys_source_src_10002.ds, month) AS buy__ds__month + , DATE_TRUNC(buys_source_src_10002.ds, quarter) AS buy__ds__quarter + , DATE_TRUNC(buys_source_src_10002.ds, year) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , IF(EXTRACT(dayofweek FROM buys_source_src_10002.ds) = 1, 7, EXTRACT(dayofweek FROM buys_source_src_10002.ds) - 1) AS buy__ds__extract_dow + , EXTRACT(dayofyear FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATE_SUB(CAST(subq_9.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + metric_time__day + , visit__referrer_id +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..4018dba08d --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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(ds, day) AS metric_time__day + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC(ds, day) AS ds__day + , DATE_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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC(ds, day) AS ds__day + , user_id AS user + , 1 AS buys + , GENERATE_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATE_SUB(CAST(subq_24.ds__day AS DATETIME), INTERVAL 7 day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.metric_time__day + ) + GROUP BY + metric_time__day + , visit__referrer_id +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..eb81d68ccf --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..4ac5a7fa98 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..b174560416 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..303854bfc2 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..387b53e17d --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..c30e5717e3 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , session_id + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..56b0e1f1d4 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..3424a583fd --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..a024196e84 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..1dc4eb6577 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..fa2765f4ac --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - INTERVAL 7 day + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..8145602268 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - INTERVAL 7 day + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..65b0be4db3 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..c6f0db4a9d --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..6ff5982f77 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - INTERVAL 7 day + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..69307c6460 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , session_id + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - INTERVAL 7 day + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..c171a1dd03 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - INTERVAL 7 day + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..e675ff1bd1 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - INTERVAL 7 day + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..e51c9c2bbc --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - INTERVAL 7 day + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..39c91227d4 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - INTERVAL 7 day + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..62a9f82bb8 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..1a39061116 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..d38660c2de --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..3b9417b39b --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..9001802c50 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..1920013f86 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , session_id + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..9d72654c3d --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..0c705f7c68 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..e4e5b5e60b --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(isodow FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(isodow FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > subq_9.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..21a544cb79 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , GEN_RANDOM_UUID() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > subq_24.ds__day - MAKE_INTERVAL(days => 7) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..8adc59881d --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..c3ef936388 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 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_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..72a18d2709 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..41f2e795ed --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 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_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..85d6a609a6 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..c62876722b --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , session_id + , 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_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..5907b26435 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..f4eda45477 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 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_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..29b9d88cbe --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , CASE WHEN EXTRACT(dow FROM visits_source_src_10011.ds) = 0 THEN EXTRACT(dow FROM visits_source_src_10011.ds) + 7 ELSE EXTRACT(dow FROM visits_source_src_10011.ds) END AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , CASE WHEN EXTRACT(dow FROM buys_source_src_10002.ds) = 0 THEN EXTRACT(dow FROM buys_source_src_10002.ds) + 7 ELSE EXTRACT(dow FROM buys_source_src_10002.ds) END AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..28a76b1a77 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 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_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..06ae0b08c6 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.buys AS visit_buy_conversions +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , COALESCE(MAX(subq_13.buys), 0) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..c15e2a94b2 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- Combine Aggregated Outputs +-- Compute Metrics via Expressions +SELECT + COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions +FROM ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0.sql new file mode 100644 index 0000000000..93cd4a376a --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0.sql @@ -0,0 +1,355 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id'] + SELECT + subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + SELECT + subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of INF + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + (subq_6.user = subq_9.user) AND ((subq_6.ds__day <= subq_9.ds__day)) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.visit__referrer_id + ) subq_13 + ON + subq_3.visit__referrer_id = subq_13.visit__referrer_id + GROUP BY + COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql new file mode 100644 index 0000000000..ec693820c5 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql @@ -0,0 +1,83 @@ +-- Compute Metrics via Expressions +SELECT + 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of INF + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id'] + -- Aggregate Measures + SELECT + visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + (subq_21.ds__day <= subq_24.ds__day) + ) + ) subq_25 + GROUP BY + visit__referrer_id + ) subq_28 + ON + subq_18.visit__referrer_id = subq_28.visit__referrer_id + GROUP BY + COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) +) subq_29 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0.sql new file mode 100644 index 0000000000..421aeff629 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0.sql @@ -0,0 +1,389 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.session + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS user + , first_value(subq_6.session) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid, subq_9.session_id ORDER BY subq_6.ds__day DESC) AS session + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', + -- 'visit__referrer_id', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.session + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + subq_6.session = subq_9.session_id + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql new file mode 100644 index 0000000000..705ff75915 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -0,0 +1,111 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) AS user + , first_value(subq_21.session) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid, subq_24.session_id ORDER BY subq_21.ds__day DESC) 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', + -- 'ds__day', + -- 'metric_time__day', + -- 'user', + -- 'session'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , session_id + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + subq_21.session = subq_24.session_id + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0.sql new file mode 100644 index 0000000000..62c6f08f6e --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0.sql @@ -0,0 +1,346 @@ +-- Compute Metrics via Expressions +SELECT + CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits'] + SELECT + subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + ) subq_3 + CROSS JOIN ( + -- Aggregate Measures + SELECT + SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys'] + SELECT + subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.user + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'ds__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.user + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + ) subq_13 +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql new file mode 100644 index 0000000000..f9041f8fee --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -0,0 +1,63 @@ +-- 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 ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: + -- ['visits'] + -- Aggregate Measures + SELECT + SUM(1) AS visits + FROM ***************************.fct_visits visits_source_src_10011 +) subq_18 +CROSS JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys'] + -- Aggregate Measures + SELECT + SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 +) subq_28 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0.sql new file mode 100644 index 0000000000..5143864534 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0.sql @@ -0,0 +1,379 @@ +-- Compute Metrics via Expressions +SELECT + subq_14.metric_time__day + , subq_14.visit__referrer_id + , CAST(subq_14.buys AS DOUBLE) / CAST(NULLIF(subq_14.visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) AS metric_time__day + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) AS visit__referrer_id + , MAX(subq_3.visits) AS visits + , MAX(subq_13.buys) AS buys + FROM ( + -- Aggregate Measures + SELECT + subq_2.metric_time__day + , subq_2.visit__referrer_id + , SUM(subq_2.visits) AS visits + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_1.metric_time__day + , subq_1.visit__referrer_id + , subq_1.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_0.ds__day + , subq_0.ds__week + , subq_0.ds__month + , subq_0.ds__quarter + , subq_0.ds__year + , subq_0.ds__extract_year + , subq_0.ds__extract_quarter + , subq_0.ds__extract_month + , subq_0.ds__extract_day + , subq_0.ds__extract_dow + , subq_0.ds__extract_doy + , subq_0.visit__ds__day + , subq_0.visit__ds__week + , subq_0.visit__ds__month + , subq_0.visit__ds__quarter + , subq_0.visit__ds__year + , subq_0.visit__ds__extract_year + , subq_0.visit__ds__extract_quarter + , subq_0.visit__ds__extract_month + , subq_0.visit__ds__extract_day + , subq_0.visit__ds__extract_dow + , subq_0.visit__ds__extract_doy + , subq_0.ds__day AS metric_time__day + , subq_0.ds__week AS metric_time__week + , subq_0.ds__month AS metric_time__month + , subq_0.ds__quarter AS metric_time__quarter + , subq_0.ds__year AS metric_time__year + , subq_0.ds__extract_year AS metric_time__extract_year + , subq_0.ds__extract_quarter AS metric_time__extract_quarter + , subq_0.ds__extract_month AS metric_time__extract_month + , subq_0.ds__extract_day AS metric_time__extract_day + , subq_0.ds__extract_dow AS metric_time__extract_dow + , subq_0.ds__extract_doy AS metric_time__extract_doy + , subq_0.user + , subq_0.session + , subq_0.visit__user + , subq_0.visit__session + , subq_0.referrer_id + , subq_0.visit__referrer_id + , subq_0.visits + , subq_0.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_0 + ) subq_1 + ) subq_2 + GROUP BY + subq_2.metric_time__day + , subq_2.visit__referrer_id + ) subq_3 + FULL OUTER JOIN ( + -- Aggregate Measures + SELECT + subq_12.metric_time__day + , subq_12.visit__referrer_id + , SUM(subq_12.buys) AS buys + FROM ( + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + SELECT + subq_11.metric_time__day + , subq_11.visit__referrer_id + , subq_11.buys + FROM ( + -- Find conversions for user within the range of 7 day + SELECT + subq_10.ds__day + , subq_10.metric_time__day + , subq_10.user + , subq_10.visit__referrer_id + , subq_10.buys + , subq_10.visits + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_6.visits) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visits + , first_value(subq_6.visit__referrer_id) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS visit__referrer_id + , first_value(subq_6.ds__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS ds__day + , first_value(subq_6.metric_time__day) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS metric_time__day + , first_value(subq_6.user) OVER (PARTITION BY subq_9.user, subq_9.ds__day, subq_9.mf_internal_uuid ORDER BY subq_6.ds__day DESC) AS user + , subq_9.mf_internal_uuid AS mf_internal_uuid + , subq_9.buys AS buys + FROM ( + -- Pass Only Elements: + -- ['visits', 'visit__referrer_id', 'ds__day', 'metric_time__day', 'user'] + SELECT + subq_5.ds__day + , subq_5.metric_time__day + , subq_5.user + , subq_5.visit__referrer_id + , subq_5.visits + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_4.ds__day + , subq_4.ds__week + , subq_4.ds__month + , subq_4.ds__quarter + , subq_4.ds__year + , subq_4.ds__extract_year + , subq_4.ds__extract_quarter + , subq_4.ds__extract_month + , subq_4.ds__extract_day + , subq_4.ds__extract_dow + , subq_4.ds__extract_doy + , subq_4.visit__ds__day + , subq_4.visit__ds__week + , subq_4.visit__ds__month + , subq_4.visit__ds__quarter + , subq_4.visit__ds__year + , subq_4.visit__ds__extract_year + , subq_4.visit__ds__extract_quarter + , subq_4.visit__ds__extract_month + , subq_4.visit__ds__extract_day + , subq_4.visit__ds__extract_dow + , subq_4.visit__ds__extract_doy + , subq_4.ds__day AS metric_time__day + , subq_4.ds__week AS metric_time__week + , subq_4.ds__month AS metric_time__month + , subq_4.ds__quarter AS metric_time__quarter + , subq_4.ds__year AS metric_time__year + , subq_4.ds__extract_year AS metric_time__extract_year + , subq_4.ds__extract_quarter AS metric_time__extract_quarter + , subq_4.ds__extract_month AS metric_time__extract_month + , subq_4.ds__extract_day AS metric_time__extract_day + , subq_4.ds__extract_dow AS metric_time__extract_dow + , subq_4.ds__extract_doy AS metric_time__extract_doy + , subq_4.user + , subq_4.session + , subq_4.visit__user + , subq_4.visit__session + , subq_4.referrer_id + , subq_4.visit__referrer_id + , subq_4.visits + , subq_4.visitors + FROM ( + -- Read Elements From Semantic Model 'visits_source' + SELECT + 1 AS visits + , visits_source_src_10011.user_id AS visitors + , DATE_TRUNC('day', visits_source_src_10011.ds) AS ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS ds__extract_doy + , visits_source_src_10011.referrer_id + , DATE_TRUNC('day', visits_source_src_10011.ds) AS visit__ds__day + , DATE_TRUNC('week', visits_source_src_10011.ds) AS visit__ds__week + , DATE_TRUNC('month', visits_source_src_10011.ds) AS visit__ds__month + , DATE_TRUNC('quarter', visits_source_src_10011.ds) AS visit__ds__quarter + , DATE_TRUNC('year', visits_source_src_10011.ds) AS visit__ds__year + , EXTRACT(year FROM visits_source_src_10011.ds) AS visit__ds__extract_year + , EXTRACT(quarter FROM visits_source_src_10011.ds) AS visit__ds__extract_quarter + , EXTRACT(month FROM visits_source_src_10011.ds) AS visit__ds__extract_month + , EXTRACT(day FROM visits_source_src_10011.ds) AS visit__ds__extract_day + , EXTRACT(dayofweekiso FROM visits_source_src_10011.ds) AS visit__ds__extract_dow + , EXTRACT(doy FROM visits_source_src_10011.ds) AS visit__ds__extract_doy + , visits_source_src_10011.referrer_id AS visit__referrer_id + , visits_source_src_10011.user_id AS user + , visits_source_src_10011.session_id AS session + , visits_source_src_10011.user_id AS visit__user + , visits_source_src_10011.session_id AS visit__session + FROM ***************************.fct_visits visits_source_src_10011 + ) subq_4 + ) subq_5 + ) subq_6 + INNER JOIN ( + -- Add column with generated UUID + SELECT + subq_8.ds__day + , subq_8.ds__week + , subq_8.ds__month + , subq_8.ds__quarter + , subq_8.ds__year + , subq_8.ds__extract_year + , subq_8.ds__extract_quarter + , subq_8.ds__extract_month + , subq_8.ds__extract_day + , subq_8.ds__extract_dow + , subq_8.ds__extract_doy + , subq_8.buy__ds__day + , subq_8.buy__ds__week + , subq_8.buy__ds__month + , subq_8.buy__ds__quarter + , subq_8.buy__ds__year + , subq_8.buy__ds__extract_year + , subq_8.buy__ds__extract_quarter + , subq_8.buy__ds__extract_month + , subq_8.buy__ds__extract_day + , subq_8.buy__ds__extract_dow + , subq_8.buy__ds__extract_doy + , subq_8.metric_time__day + , subq_8.metric_time__week + , subq_8.metric_time__month + , subq_8.metric_time__quarter + , subq_8.metric_time__year + , subq_8.metric_time__extract_year + , subq_8.metric_time__extract_quarter + , subq_8.metric_time__extract_month + , subq_8.metric_time__extract_day + , subq_8.metric_time__extract_dow + , subq_8.metric_time__extract_doy + , subq_8.user + , subq_8.session_id + , subq_8.buy__user + , subq_8.buy__session_id + , subq_8.buys + , subq_8.buyers + , UUID_STRING() AS mf_internal_uuid + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_7.ds__day + , subq_7.ds__week + , subq_7.ds__month + , subq_7.ds__quarter + , subq_7.ds__year + , subq_7.ds__extract_year + , subq_7.ds__extract_quarter + , subq_7.ds__extract_month + , subq_7.ds__extract_day + , subq_7.ds__extract_dow + , subq_7.ds__extract_doy + , subq_7.buy__ds__day + , subq_7.buy__ds__week + , subq_7.buy__ds__month + , subq_7.buy__ds__quarter + , subq_7.buy__ds__year + , subq_7.buy__ds__extract_year + , subq_7.buy__ds__extract_quarter + , subq_7.buy__ds__extract_month + , subq_7.buy__ds__extract_day + , subq_7.buy__ds__extract_dow + , subq_7.buy__ds__extract_doy + , subq_7.ds__day AS metric_time__day + , subq_7.ds__week AS metric_time__week + , subq_7.ds__month AS metric_time__month + , subq_7.ds__quarter AS metric_time__quarter + , subq_7.ds__year AS metric_time__year + , subq_7.ds__extract_year AS metric_time__extract_year + , subq_7.ds__extract_quarter AS metric_time__extract_quarter + , subq_7.ds__extract_month AS metric_time__extract_month + , subq_7.ds__extract_day AS metric_time__extract_day + , subq_7.ds__extract_dow AS metric_time__extract_dow + , subq_7.ds__extract_doy AS metric_time__extract_doy + , subq_7.user + , subq_7.session_id + , subq_7.buy__user + , subq_7.buy__session_id + , subq_7.buys + , subq_7.buyers + FROM ( + -- Read Elements From Semantic Model 'buys_source' + SELECT + 1 AS buys + , buys_source_src_10002.user_id AS buyers + , DATE_TRUNC('day', buys_source_src_10002.ds) AS ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS ds__extract_doy + , DATE_TRUNC('day', buys_source_src_10002.ds) AS buy__ds__day + , DATE_TRUNC('week', buys_source_src_10002.ds) AS buy__ds__week + , DATE_TRUNC('month', buys_source_src_10002.ds) AS buy__ds__month + , DATE_TRUNC('quarter', buys_source_src_10002.ds) AS buy__ds__quarter + , DATE_TRUNC('year', buys_source_src_10002.ds) AS buy__ds__year + , EXTRACT(year FROM buys_source_src_10002.ds) AS buy__ds__extract_year + , EXTRACT(quarter FROM buys_source_src_10002.ds) AS buy__ds__extract_quarter + , EXTRACT(month FROM buys_source_src_10002.ds) AS buy__ds__extract_month + , EXTRACT(day FROM buys_source_src_10002.ds) AS buy__ds__extract_day + , EXTRACT(dayofweekiso FROM buys_source_src_10002.ds) AS buy__ds__extract_dow + , EXTRACT(doy FROM buys_source_src_10002.ds) AS buy__ds__extract_doy + , buys_source_src_10002.user_id AS user + , buys_source_src_10002.session_id + , buys_source_src_10002.user_id AS buy__user + , buys_source_src_10002.session_id AS buy__session_id + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_7 + ) subq_8 + ) subq_9 + ON + ( + subq_6.user = subq_9.user + ) AND ( + ( + subq_6.ds__day <= subq_9.ds__day + ) AND ( + subq_6.ds__day > DATEADD(day, -7, subq_9.ds__day) + ) + ) + ) subq_10 + ) subq_11 + ) subq_12 + GROUP BY + subq_12.metric_time__day + , subq_12.visit__referrer_id + ) subq_13 + ON + ( + subq_3.visit__referrer_id = subq_13.visit__referrer_id + ) AND ( + subq_3.metric_time__day = subq_13.metric_time__day + ) + GROUP BY + COALESCE(subq_3.metric_time__day, subq_13.metric_time__day) + , COALESCE(subq_3.visit__referrer_id, subq_13.visit__referrer_id) +) subq_14 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql new file mode 100644 index 0000000000..f8ec4cb785 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql @@ -0,0 +1,101 @@ +-- Compute Metrics via Expressions +SELECT + metric_time__day + , 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 + , MAX(subq_18.visits) AS visits + , MAX(subq_28.buys) AS buys + FROM ( + -- 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_10011 + ) subq_17 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_18 + FULL OUTER JOIN ( + -- Find conversions for user within the range of 7 day + -- Pass Only Elements: + -- ['buys', 'visit__referrer_id', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , visit__referrer_id + , SUM(buys) AS buys + FROM ( + -- Dedupe the fanout with mf_internal_uuid in the conversion data set + SELECT DISTINCT + first_value(subq_21.visits) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visits + , first_value(subq_21.visit__referrer_id) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS visit__referrer_id + , first_value(subq_21.ds__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS ds__day + , first_value(subq_21.metric_time__day) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) AS metric_time__day + , first_value(subq_21.user) OVER (PARTITION BY subq_24.user, subq_24.ds__day, subq_24.mf_internal_uuid ORDER BY subq_21.ds__day DESC) 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', 'ds__day', 'metric_time__day', 'user'] + SELECT + DATE_TRUNC('day', ds) AS ds__day + , 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_10011 + ) subq_21 + INNER JOIN ( + -- Read Elements From Semantic Model 'buys_source' + -- Metric Time Dimension 'ds' + -- Add column with generated UUID + SELECT + DATE_TRUNC('day', ds) AS ds__day + , user_id AS user + , 1 AS buys + , UUID_STRING() AS mf_internal_uuid + FROM ***************************.fct_buys buys_source_src_10002 + ) subq_24 + ON + ( + subq_21.user = subq_24.user + ) AND ( + ( + subq_21.ds__day <= subq_24.ds__day + ) AND ( + subq_21.ds__day > DATEADD(day, -7, subq_24.ds__day) + ) + ) + ) subq_25 + GROUP BY + metric_time__day + , visit__referrer_id + ) subq_28 + ON + ( + subq_18.visit__referrer_id = subq_28.visit__referrer_id + ) AND ( + subq_18.metric_time__day = subq_28.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 diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_count_with_no_group_by__plan0.xml b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_count_with_no_group_by__plan0.xml new file mode 100644 index 0000000000..a97d0bd26b --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_count_with_no_group_by__plan0.xml @@ -0,0 +1,1220 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate__plan0.xml b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate__plan0.xml new file mode 100644 index 0000000000..46d0c44803 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate__plan0.xml @@ -0,0 +1,1268 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_constant_properties__plan0.xml b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_constant_properties__plan0.xml new file mode 100644 index 0000000000..8f6cb21ac2 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_constant_properties__plan0.xml @@ -0,0 +1,1333 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_no_group_by__plan0.xml b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_no_group_by__plan0.xml new file mode 100644 index 0000000000..e649d811ba --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_no_group_by__plan0.xml @@ -0,0 +1,1220 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_window__plan0.xml b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_window__plan0.xml new file mode 100644 index 0000000000..423a387c10 --- /dev/null +++ b/metricflow/test/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/test_conversion_rate_with_window__plan0.xml @@ -0,0 +1,1316 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +