From 24d77fd602529b7b7f27bd13294a2115c04213ed Mon Sep 17 00:00:00 2001 From: Courtney Holcomb Date: Wed, 18 Sep 2024 14:59:08 -0700 Subject: [PATCH] Update SQL engine snapshots --- ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ..._metric_with_custom_granularity__plan0.sql | 164 +++++++ ...th_custom_granularity__plan0_optimized.sql | 45 ++ ..._metric_with_custom_granularity__plan0.sql | 455 ++++++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + ...ity_joined_to_non_default_grain__plan0.sql | 159 ++++++ ..._to_non_default_grain__plan0_optimized.sql | 24 + ...metrics_with_custom_granularity__plan0.sql | 386 +++++++++++++++ ...th_custom_granularity__plan0_optimized.sql | 55 +++ ..._metric_with_custom_granularity__plan0.sql | 218 +++++++++ ...th_custom_granularity__plan0_optimized.sql | 21 + 60 files changed, 9288 insertions(+) create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0.sql create mode 100644 tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0_optimized.sql diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..072ca2742b --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATETIME_TRUNC(revenue_src_28000.created_at, day) AS ds__day + , DATETIME_TRUNC(revenue_src_28000.created_at, isoweek) AS ds__week + , DATETIME_TRUNC(revenue_src_28000.created_at, month) AS ds__month + , DATETIME_TRUNC(revenue_src_28000.created_at, quarter) AS ds__quarter + , DATETIME_TRUNC(revenue_src_28000.created_at, year) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM revenue_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM revenue_src_28000.created_at) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATETIME_TRUNC(revenue_src_28000.created_at, day) AS revenue_instance__ds__day + , DATETIME_TRUNC(revenue_src_28000.created_at, isoweek) AS revenue_instance__ds__week + , DATETIME_TRUNC(revenue_src_28000.created_at, month) AS revenue_instance__ds__month + , DATETIME_TRUNC(revenue_src_28000.created_at, quarter) AS revenue_instance__ds__quarter + , DATETIME_TRUNC(revenue_src_28000.created_at, year) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , IF(EXTRACT(dayofweek FROM revenue_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM revenue_src_28000.created_at) - 1) AS revenue_instance__ds__extract_dow + , EXTRACT(dayofyear FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > DATE_SUB(CAST(subq_2.metric_time__day AS DATETIME), INTERVAL 2 month) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + metric_time__martian_day + , metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..6d6ae4bf41 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATETIME_TRUNC(revenue_src_28000.created_at, day) <= subq_13.ds + ) AND ( + DATETIME_TRUNC(revenue_src_28000.created_at, day) > DATE_SUB(CAST(subq_13.ds AS DATETIME), INTERVAL 2 month) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + metric_time__martian_day + , metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..95a6ef4500 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS booking__ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS booking__ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS booking__ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS booking__ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS booking__ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS booking__ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS booking__ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS booking__ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS booking__ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS booking__ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS booking__paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS booking__paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS booking__paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS booking__paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS booking__paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS booking__ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS booking__ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS booking__ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS booking__ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS booking__ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS booking__ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS booking__ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS booking__ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS booking__ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS booking__ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS booking__paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS booking__paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS booking__paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS booking__paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS booking__paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + booking__ds__martian_day +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..58272febbf --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATETIME_TRUNC(bookings_source_src_28000.ds, day) = subq_15.ds + GROUP BY + booking__ds__martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..c2e895b286 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS ds__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS ds__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS ds__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS ds__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS created_at__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS created_at__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS created_at__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS created_at__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS listing__ds__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__ds__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS listing__ds__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__ds__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS listing__created_at__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__created_at__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS listing__created_at__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__created_at__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + metric_time__martian_day + , listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..0f07ff0f45 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(created_at, month) AS listing__ds__month + , DATETIME_TRUNC(created_at, day) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + metric_time__martian_day + , listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..5f1450d118 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS booking__ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS booking__ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS booking__ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS booking__ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS booking__ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS booking__ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS booking__ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS booking__ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS booking__ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS booking__ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS booking__paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS booking__paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS booking__paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS booking__paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS booking__paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS ds__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS ds__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS ds__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS ds__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS created_at__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS created_at__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS created_at__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS created_at__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS listing__ds__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__ds__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS listing__ds__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__ds__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__ds__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATETIME_TRUNC(listings_latest_src_28000.created_at, day) AS listing__created_at__day + , DATETIME_TRUNC(listings_latest_src_28000.created_at, isoweek) AS listing__created_at__week + , DATETIME_TRUNC(listings_latest_src_28000.created_at, month) AS listing__created_at__month + , DATETIME_TRUNC(listings_latest_src_28000.created_at, quarter) AS listing__created_at__quarter + , DATETIME_TRUNC(listings_latest_src_28000.created_at, year) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , IF(EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) = 1, 7, EXTRACT(dayofweek FROM listings_latest_src_28000.created_at) - 1) AS listing__created_at__extract_dow + , EXTRACT(dayofyear FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + metric_time__martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..9886651d2c --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + metric_time__martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(created_at, day) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + metric_time__martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + metric_time__martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..c9cb523f5c --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATETIME_TRUNC(bookings_source_src_28000.ds, day) AS booking__ds__day + , DATETIME_TRUNC(bookings_source_src_28000.ds, isoweek) AS booking__ds__week + , DATETIME_TRUNC(bookings_source_src_28000.ds, month) AS booking__ds__month + , DATETIME_TRUNC(bookings_source_src_28000.ds, quarter) AS booking__ds__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds, year) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds) - 1) AS booking__ds__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, day) AS booking__ds_partitioned__day + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, isoweek) AS booking__ds_partitioned__week + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, month) AS booking__ds_partitioned__month + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, quarter) AS booking__ds_partitioned__quarter + , DATETIME_TRUNC(bookings_source_src_28000.ds_partitioned, year) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.ds_partitioned) - 1) AS booking__ds_partitioned__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, day) AS booking__paid_at__day + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, isoweek) AS booking__paid_at__week + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, month) AS booking__paid_at__month + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, quarter) AS booking__paid_at__quarter + , DATETIME_TRUNC(bookings_source_src_28000.paid_at, year) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , IF(EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) = 1, 7, EXTRACT(dayofweek FROM bookings_source_src_28000.paid_at) - 1) AS booking__paid_at__extract_dow + , EXTRACT(dayofyear FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..f1dc0aa255 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + booking__ds__martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..15729313b4 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM revenue_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > DATEADD(month, -2, subq_2.metric_time__day) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + subq_6.metric_time__martian_day + , subq_6.metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..4ff01dd587 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + subq_15.martian_day + , subq_14.metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..44fa56eb22 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..a7aa3173bb --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_15.ds + GROUP BY + subq_15.martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..e0252b235e --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + , subq_3.listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..a0fd837dea --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', created_at) AS listing__ds__month + , DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + subq_7.martian_day + , subq_6.listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..a2deb0d052 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..b6ace5e0ef --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + subq_14.martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + subq_20.martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..6c80fd230e --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAYOFWEEK_ISO FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..0d4bd34046 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + subq_7.martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..bdce4b1d9b --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , EXTRACT(isodow FROM revenue_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , EXTRACT(isodow FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > subq_2.metric_time__day - MAKE_INTERVAL(months => 2) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + subq_6.metric_time__martian_day + , subq_6.metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..cbb86235b5 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > subq_13.ds - MAKE_INTERVAL(months => 2) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + subq_15.martian_day + , subq_14.metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..f9c8f2d31b --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..a7aa3173bb --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_15.ds + GROUP BY + subq_15.martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..8f86d8b418 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + , subq_3.listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..a0fd837dea --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', created_at) AS listing__ds__month + , DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + subq_7.martian_day + , subq_6.listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..6a8f2d80f4 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(isodow FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..b6ace5e0ef --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + subq_14.martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + subq_20.martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..7566699dc9 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(isodow FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..0d4bd34046 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + subq_7.martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..6e54afe885 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM revenue_src_28000.created_at) = 0 THEN EXTRACT(dow FROM revenue_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM revenue_src_28000.created_at) END AS ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , CASE WHEN EXTRACT(dow FROM revenue_src_28000.created_at) = 0 THEN EXTRACT(dow FROM revenue_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM revenue_src_28000.created_at) END AS revenue_instance__ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > DATEADD(month, -2, subq_2.metric_time__day) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + subq_6.metric_time__martian_day + , subq_6.metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..4ff01dd587 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + subq_15.martian_day + , subq_14.metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..2a5711f1b4 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..a7aa3173bb --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_15.ds + GROUP BY + subq_15.martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..e9e2589f36 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + , subq_3.listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..a0fd837dea --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', created_at) AS listing__ds__month + , DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + subq_7.martian_day + , subq_6.listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..9bbe91870d --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , CASE WHEN EXTRACT(dow FROM listings_latest_src_28000.created_at) = 0 THEN EXTRACT(dow FROM listings_latest_src_28000.created_at) + 7 ELSE EXTRACT(dow FROM listings_latest_src_28000.created_at) END AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..b6ace5e0ef --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + subq_14.martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + subq_20.martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..efec060855 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds) END AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.ds_partitioned) END AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , CASE WHEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) = 0 THEN EXTRACT(dow FROM bookings_source_src_28000.paid_at) + 7 ELSE EXTRACT(dow FROM bookings_source_src_28000.paid_at) END AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..0d4bd34046 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + subq_7.martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..f925262a11 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , EXTRACT(dayofweekiso FROM revenue_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , EXTRACT(dayofweekiso FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > DATEADD(month, -2, subq_2.metric_time__day) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + subq_6.metric_time__martian_day + , subq_6.metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..4ff01dd587 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATEADD(month, -2, subq_13.ds) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + subq_15.martian_day + , subq_14.metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..6967066362 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..a7aa3173bb --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_15.ds + GROUP BY + subq_15.martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..85e7a3dbbe --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + , subq_3.listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..a0fd837dea --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', created_at) AS listing__ds__month + , DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + subq_7.martian_day + , subq_6.listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..b532ba4f7a --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(dayofweekiso FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..b6ace5e0ef --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + subq_14.martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + subq_20.martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..15da532a72 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(dayofweekiso FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..0d4bd34046 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + subq_7.martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..a2520c5552 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,164 @@ +-- Re-aggregate Metric via Group By +SELECT + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue +FROM ( + -- Window Function for Metric Re-aggregation + SELECT + subq_8.metric_time__martian_day + , AVG(subq_8.trailing_2_months_revenue) OVER (PARTITION BY subq_8.metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_7.metric_time__martian_day + , subq_7.metric_time__day + , subq_7.txn_revenue AS trailing_2_months_revenue + FROM ( + -- Aggregate Measures + SELECT + subq_6.metric_time__martian_day + , subq_6.metric_time__day + , SUM(subq_6.txn_revenue) AS txn_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + SELECT + subq_4.metric_time__day AS metric_time__day + , subq_4.txn_revenue AS txn_revenue + , subq_5.martian_day AS metric_time__martian_day + FROM ( + -- Join Self Over Time Range + SELECT + subq_2.metric_time__day AS metric_time__day + , subq_1.ds__day AS ds__day + , subq_1.ds__week AS ds__week + , subq_1.ds__month AS ds__month + , subq_1.ds__quarter AS ds__quarter + , subq_1.ds__year AS ds__year + , subq_1.ds__extract_year AS ds__extract_year + , subq_1.ds__extract_quarter AS ds__extract_quarter + , subq_1.ds__extract_month AS ds__extract_month + , subq_1.ds__extract_day AS ds__extract_day + , subq_1.ds__extract_dow AS ds__extract_dow + , subq_1.ds__extract_doy AS ds__extract_doy + , subq_1.revenue_instance__ds__day AS revenue_instance__ds__day + , subq_1.revenue_instance__ds__week AS revenue_instance__ds__week + , subq_1.revenue_instance__ds__month AS revenue_instance__ds__month + , subq_1.revenue_instance__ds__quarter AS revenue_instance__ds__quarter + , subq_1.revenue_instance__ds__year AS revenue_instance__ds__year + , subq_1.revenue_instance__ds__extract_year AS revenue_instance__ds__extract_year + , subq_1.revenue_instance__ds__extract_quarter AS revenue_instance__ds__extract_quarter + , subq_1.revenue_instance__ds__extract_month AS revenue_instance__ds__extract_month + , subq_1.revenue_instance__ds__extract_day AS revenue_instance__ds__extract_day + , subq_1.revenue_instance__ds__extract_dow AS revenue_instance__ds__extract_dow + , subq_1.revenue_instance__ds__extract_doy AS revenue_instance__ds__extract_doy + , subq_1.metric_time__week AS metric_time__week + , subq_1.metric_time__month AS metric_time__month + , subq_1.metric_time__quarter AS metric_time__quarter + , subq_1.metric_time__year AS metric_time__year + , subq_1.metric_time__extract_year AS metric_time__extract_year + , subq_1.metric_time__extract_quarter AS metric_time__extract_quarter + , subq_1.metric_time__extract_month AS metric_time__extract_month + , subq_1.metric_time__extract_day AS metric_time__extract_day + , subq_1.metric_time__extract_dow AS metric_time__extract_dow + , subq_1.metric_time__extract_doy AS metric_time__extract_doy + , subq_1.user AS user + , subq_1.revenue_instance__user AS revenue_instance__user + , subq_1.txn_revenue AS txn_revenue + FROM ( + -- Time Spine + SELECT + subq_3.ds AS metric_time__day + FROM ***************************.mf_time_spine subq_3 + ) subq_2 + INNER JOIN ( + -- 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.revenue_instance__ds__day + , subq_0.revenue_instance__ds__week + , subq_0.revenue_instance__ds__month + , subq_0.revenue_instance__ds__quarter + , subq_0.revenue_instance__ds__year + , subq_0.revenue_instance__ds__extract_year + , subq_0.revenue_instance__ds__extract_quarter + , subq_0.revenue_instance__ds__extract_month + , subq_0.revenue_instance__ds__extract_day + , subq_0.revenue_instance__ds__extract_dow + , subq_0.revenue_instance__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.revenue_instance__user + , subq_0.txn_revenue + FROM ( + -- Read Elements From Semantic Model 'revenue' + SELECT + revenue_src_28000.revenue AS txn_revenue + , DATE_TRUNC('day', revenue_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM revenue_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', revenue_src_28000.created_at) AS revenue_instance__ds__day + , DATE_TRUNC('week', revenue_src_28000.created_at) AS revenue_instance__ds__week + , DATE_TRUNC('month', revenue_src_28000.created_at) AS revenue_instance__ds__month + , DATE_TRUNC('quarter', revenue_src_28000.created_at) AS revenue_instance__ds__quarter + , DATE_TRUNC('year', revenue_src_28000.created_at) AS revenue_instance__ds__year + , EXTRACT(year FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_year + , EXTRACT(quarter FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_quarter + , EXTRACT(month FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_month + , EXTRACT(day FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_dow + , EXTRACT(doy FROM revenue_src_28000.created_at) AS revenue_instance__ds__extract_doy + , revenue_src_28000.user_id AS user + , revenue_src_28000.user_id AS revenue_instance__user + FROM ***************************.fct_revenue revenue_src_28000 + ) subq_0 + ) subq_1 + ON + ( + subq_1.metric_time__day <= subq_2.metric_time__day + ) AND ( + subq_1.metric_time__day > DATE_ADD('month', -2, subq_2.metric_time__day) + ) + ) subq_4 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_5 + ON + subq_4.metric_time__day = subq_5.ds + ) subq_6 + GROUP BY + subq_6.metric_time__martian_day + , subq_6.metric_time__day + ) subq_7 + ) subq_8 +) subq_9 +GROUP BY + subq_9.metric_time__martian_day + , subq_9.trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..8f9d82f087 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_cumulative_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,45 @@ +-- Re-aggregate Metric via Group By +SELECT + metric_time__martian_day + , trailing_2_months_revenue +FROM ( + -- Compute Metrics via Expressions + -- Window Function for Metric Re-aggregation + SELECT + metric_time__martian_day + , AVG(txn_revenue) OVER (PARTITION BY metric_time__martian_day) AS trailing_2_months_revenue + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['txn_revenue', 'metric_time__day', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_15.martian_day AS metric_time__martian_day + , subq_14.metric_time__day AS metric_time__day + , SUM(subq_14.txn_revenue) AS txn_revenue + FROM ( + -- Join Self Over Time Range + SELECT + subq_13.ds AS metric_time__day + , revenue_src_28000.revenue AS txn_revenue + FROM ***************************.mf_time_spine subq_13 + INNER JOIN + ***************************.fct_revenue revenue_src_28000 + ON + ( + DATE_TRUNC('day', revenue_src_28000.created_at) <= subq_13.ds + ) AND ( + DATE_TRUNC('day', revenue_src_28000.created_at) > DATE_ADD('month', -2, subq_13.ds) + ) + ) subq_14 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + subq_14.metric_time__day = subq_15.ds + GROUP BY + subq_15.martian_day + , subq_14.metric_time__day + ) subq_17 +) subq_19 +GROUP BY + metric_time__martian_day + , trailing_2_months_revenue diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..a58ebce2ed --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,455 @@ +-- Compute Metrics via Expressions +SELECT + subq_12.booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) AS booking__ds__martian_day + , MAX(subq_5.booking_value) AS booking_value + , MAX(subq_11.bookers) AS bookers + FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.booking__ds__martian_day + , subq_4.booking_value + FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.booking_value) AS booking_value + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + SELECT + subq_1.booking_value AS booking_value + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day + ) subq_4 + ) subq_5 + FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.booking__ds__martian_day + , subq_10.bookers + FROM ( + -- Aggregate Measures + SELECT + subq_9.booking__ds__martian_day + , COUNT(DISTINCT subq_9.bookers) AS bookers + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + SELECT + subq_7.bookers AS bookers + , subq_8.martian_day AS booking__ds__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.ds_partitioned__day + , subq_6.ds_partitioned__week + , subq_6.ds_partitioned__month + , subq_6.ds_partitioned__quarter + , subq_6.ds_partitioned__year + , subq_6.ds_partitioned__extract_year + , subq_6.ds_partitioned__extract_quarter + , subq_6.ds_partitioned__extract_month + , subq_6.ds_partitioned__extract_day + , subq_6.ds_partitioned__extract_dow + , subq_6.ds_partitioned__extract_doy + , subq_6.paid_at__day + , subq_6.paid_at__week + , subq_6.paid_at__month + , subq_6.paid_at__quarter + , subq_6.paid_at__year + , subq_6.paid_at__extract_year + , subq_6.paid_at__extract_quarter + , subq_6.paid_at__extract_month + , subq_6.paid_at__extract_day + , subq_6.paid_at__extract_dow + , subq_6.paid_at__extract_doy + , subq_6.booking__ds__day + , subq_6.booking__ds__week + , subq_6.booking__ds__month + , subq_6.booking__ds__quarter + , subq_6.booking__ds__year + , subq_6.booking__ds__extract_year + , subq_6.booking__ds__extract_quarter + , subq_6.booking__ds__extract_month + , subq_6.booking__ds__extract_day + , subq_6.booking__ds__extract_dow + , subq_6.booking__ds__extract_doy + , subq_6.booking__ds_partitioned__day + , subq_6.booking__ds_partitioned__week + , subq_6.booking__ds_partitioned__month + , subq_6.booking__ds_partitioned__quarter + , subq_6.booking__ds_partitioned__year + , subq_6.booking__ds_partitioned__extract_year + , subq_6.booking__ds_partitioned__extract_quarter + , subq_6.booking__ds_partitioned__extract_month + , subq_6.booking__ds_partitioned__extract_day + , subq_6.booking__ds_partitioned__extract_dow + , subq_6.booking__ds_partitioned__extract_doy + , subq_6.booking__paid_at__day + , subq_6.booking__paid_at__week + , subq_6.booking__paid_at__month + , subq_6.booking__paid_at__quarter + , subq_6.booking__paid_at__year + , subq_6.booking__paid_at__extract_year + , subq_6.booking__paid_at__extract_quarter + , subq_6.booking__paid_at__extract_month + , subq_6.booking__paid_at__extract_day + , subq_6.booking__paid_at__extract_dow + , subq_6.booking__paid_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.guest + , subq_6.host + , subq_6.booking__listing + , subq_6.booking__guest + , subq_6.booking__host + , subq_6.is_instant + , subq_6.booking__is_instant + , subq_6.bookings + , subq_6.instant_bookings + , subq_6.booking_value + , subq_6.max_booking_value + , subq_6.min_booking_value + , subq_6.bookers + , subq_6.average_booking_value + , subq_6.referred_bookings + , subq_6.median_booking_value + , subq_6.booking_value_p99 + , subq_6.discrete_booking_value_p99 + , subq_6.approximate_continuous_booking_value_p99 + , subq_6.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.booking__ds__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.booking__ds__martian_day + ) subq_10 + ) subq_11 + ON + subq_5.booking__ds__martian_day = subq_11.booking__ds__martian_day + GROUP BY + COALESCE(subq_5.booking__ds__martian_day, subq_11.booking__ds__martian_day) +) subq_12 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..a7aa3173bb --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Compute Metrics via Expressions +SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_15.martian_day AS booking__ds__martian_day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + , COUNT(DISTINCT bookings_source_src_28000.guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_15 + ON + DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_15.ds + GROUP BY + subq_15.martian_day +) subq_18 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql new file mode 100644 index 0000000000..70d797ac7e --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0.sql @@ -0,0 +1,159 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.metric_time__martian_day + , subq_4.listing__ds__month + , subq_4.listings +FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , subq_3.listing__ds__month + , SUM(subq_3.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] + SELECT + subq_1.listing__ds__month AS listing__ds__month + , subq_1.listings AS listings + , subq_2.martian_day AS metric_time__martian_day + 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.created_at__day + , subq_0.created_at__week + , subq_0.created_at__month + , subq_0.created_at__quarter + , subq_0.created_at__year + , subq_0.created_at__extract_year + , subq_0.created_at__extract_quarter + , subq_0.created_at__extract_month + , subq_0.created_at__extract_day + , subq_0.created_at__extract_dow + , subq_0.created_at__extract_doy + , subq_0.listing__ds__day + , subq_0.listing__ds__week + , subq_0.listing__ds__month + , subq_0.listing__ds__quarter + , subq_0.listing__ds__year + , subq_0.listing__ds__extract_year + , subq_0.listing__ds__extract_quarter + , subq_0.listing__ds__extract_month + , subq_0.listing__ds__extract_day + , subq_0.listing__ds__extract_dow + , subq_0.listing__ds__extract_doy + , subq_0.listing__created_at__day + , subq_0.listing__created_at__week + , subq_0.listing__created_at__month + , subq_0.listing__created_at__quarter + , subq_0.listing__created_at__year + , subq_0.listing__created_at__extract_year + , subq_0.listing__created_at__extract_quarter + , subq_0.listing__created_at__extract_month + , subq_0.listing__created_at__extract_day + , subq_0.listing__created_at__extract_dow + , subq_0.listing__created_at__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.listing + , subq_0.user + , subq_0.listing__user + , subq_0.country_latest + , subq_0.is_lux_latest + , subq_0.capacity_latest + , subq_0.listing__country_latest + , subq_0.listing__is_lux_latest + , subq_0.listing__capacity_latest + , subq_0.listings + , subq_0.largest_listing + , subq_0.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + , subq_3.listing__ds__month +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql new file mode 100644 index 0000000000..a0fd837dea --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_metric_custom_granularity_joined_to_non_default_grain__plan0_optimized.sql @@ -0,0 +1,24 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['listings', 'metric_time__day', 'listing__ds__month'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS metric_time__martian_day + , subq_6.listing__ds__month AS listing__ds__month + , SUM(subq_6.listings) AS listings +FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('month', created_at) AS listing__ds__month + , DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.metric_time__day = subq_7.ds +GROUP BY + subq_7.martian_day + , subq_6.listing__ds__month diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..17253d3dc1 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0.sql @@ -0,0 +1,386 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_5.bookings) AS bookings + , MAX(subq_11.listings) AS listings +FROM ( + -- Compute Metrics via Expressions + SELECT + subq_4.metric_time__martian_day + , subq_4.bookings + FROM ( + -- Aggregate Measures + SELECT + subq_3.metric_time__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS metric_time__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.metric_time__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.metric_time__martian_day + ) subq_4 +) subq_5 +FULL OUTER JOIN ( + -- Compute Metrics via Expressions + SELECT + subq_10.metric_time__martian_day + , subq_10.listings + FROM ( + -- Aggregate Measures + SELECT + subq_9.metric_time__martian_day + , SUM(subq_9.listings) AS listings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + SELECT + subq_7.listings AS listings + , subq_8.martian_day AS metric_time__martian_day + FROM ( + -- Metric Time Dimension 'ds' + SELECT + subq_6.ds__day + , subq_6.ds__week + , subq_6.ds__month + , subq_6.ds__quarter + , subq_6.ds__year + , subq_6.ds__extract_year + , subq_6.ds__extract_quarter + , subq_6.ds__extract_month + , subq_6.ds__extract_day + , subq_6.ds__extract_dow + , subq_6.ds__extract_doy + , subq_6.created_at__day + , subq_6.created_at__week + , subq_6.created_at__month + , subq_6.created_at__quarter + , subq_6.created_at__year + , subq_6.created_at__extract_year + , subq_6.created_at__extract_quarter + , subq_6.created_at__extract_month + , subq_6.created_at__extract_day + , subq_6.created_at__extract_dow + , subq_6.created_at__extract_doy + , subq_6.listing__ds__day + , subq_6.listing__ds__week + , subq_6.listing__ds__month + , subq_6.listing__ds__quarter + , subq_6.listing__ds__year + , subq_6.listing__ds__extract_year + , subq_6.listing__ds__extract_quarter + , subq_6.listing__ds__extract_month + , subq_6.listing__ds__extract_day + , subq_6.listing__ds__extract_dow + , subq_6.listing__ds__extract_doy + , subq_6.listing__created_at__day + , subq_6.listing__created_at__week + , subq_6.listing__created_at__month + , subq_6.listing__created_at__quarter + , subq_6.listing__created_at__year + , subq_6.listing__created_at__extract_year + , subq_6.listing__created_at__extract_quarter + , subq_6.listing__created_at__extract_month + , subq_6.listing__created_at__extract_day + , subq_6.listing__created_at__extract_dow + , subq_6.listing__created_at__extract_doy + , subq_6.ds__day AS metric_time__day + , subq_6.ds__week AS metric_time__week + , subq_6.ds__month AS metric_time__month + , subq_6.ds__quarter AS metric_time__quarter + , subq_6.ds__year AS metric_time__year + , subq_6.ds__extract_year AS metric_time__extract_year + , subq_6.ds__extract_quarter AS metric_time__extract_quarter + , subq_6.ds__extract_month AS metric_time__extract_month + , subq_6.ds__extract_day AS metric_time__extract_day + , subq_6.ds__extract_dow AS metric_time__extract_dow + , subq_6.ds__extract_doy AS metric_time__extract_doy + , subq_6.listing + , subq_6.user + , subq_6.listing__user + , subq_6.country_latest + , subq_6.is_lux_latest + , subq_6.capacity_latest + , subq_6.listing__country_latest + , subq_6.listing__is_lux_latest + , subq_6.listing__capacity_latest + , subq_6.listings + , subq_6.largest_listing + , subq_6.smallest_listing + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + SELECT + 1 AS listings + , listings_latest_src_28000.capacity AS largest_listing + , listings_latest_src_28000.capacity AS smallest_listing + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS created_at__extract_doy + , listings_latest_src_28000.country AS country_latest + , listings_latest_src_28000.is_lux AS is_lux_latest + , listings_latest_src_28000.capacity AS capacity_latest + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__ds__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__ds__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__ds__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__ds__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__ds__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__ds__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__ds__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__ds__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__ds__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__ds__extract_doy + , DATE_TRUNC('day', listings_latest_src_28000.created_at) AS listing__created_at__day + , DATE_TRUNC('week', listings_latest_src_28000.created_at) AS listing__created_at__week + , DATE_TRUNC('month', listings_latest_src_28000.created_at) AS listing__created_at__month + , DATE_TRUNC('quarter', listings_latest_src_28000.created_at) AS listing__created_at__quarter + , DATE_TRUNC('year', listings_latest_src_28000.created_at) AS listing__created_at__year + , EXTRACT(year FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_year + , EXTRACT(quarter FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_quarter + , EXTRACT(month FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_month + , EXTRACT(day FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_dow + , EXTRACT(doy FROM listings_latest_src_28000.created_at) AS listing__created_at__extract_doy + , listings_latest_src_28000.country AS listing__country_latest + , listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , listings_latest_src_28000.listing_id AS listing + , listings_latest_src_28000.user_id AS user + , listings_latest_src_28000.user_id AS listing__user + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_6 + ) subq_7 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_8 + ON + subq_7.metric_time__day = subq_8.ds + ) subq_9 + GROUP BY + subq_9.metric_time__martian_day + ) subq_10 +) subq_11 +ON + subq_5.metric_time__martian_day = subq_11.metric_time__martian_day +GROUP BY + COALESCE(subq_5.metric_time__martian_day, subq_11.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..b6ace5e0ef --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,55 @@ +-- Combine Aggregated Outputs +SELECT + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) AS metric_time__martian_day + , MAX(subq_17.bookings) AS bookings + , MAX(subq_23.listings) AS listings +FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_14.martian_day AS metric_time__martian_day + , SUM(subq_13.bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_14 + ON + subq_13.metric_time__day = subq_14.ds + GROUP BY + subq_14.martian_day +) subq_17 +FULL OUTER JOIN ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_20.martian_day AS metric_time__martian_day + , SUM(subq_19.listings) AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', created_at) AS metric_time__day + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_20 + ON + subq_19.metric_time__day = subq_20.ds + GROUP BY + subq_20.martian_day +) subq_23 +ON + subq_17.metric_time__martian_day = subq_23.metric_time__martian_day +GROUP BY + COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0.sql new file mode 100644 index 0000000000..645febfdc9 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0.sql @@ -0,0 +1,218 @@ +-- Compute Metrics via Expressions +SELECT + subq_4.booking__ds__martian_day + , subq_4.bookings +FROM ( + -- Aggregate Measures + SELECT + subq_3.booking__ds__martian_day + , SUM(subq_3.bookings) AS bookings + FROM ( + -- Join to Custom Granularity Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + SELECT + subq_1.bookings AS bookings + , subq_2.martian_day AS booking__ds__martian_day + 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.ds_partitioned__day + , subq_0.ds_partitioned__week + , subq_0.ds_partitioned__month + , subq_0.ds_partitioned__quarter + , subq_0.ds_partitioned__year + , subq_0.ds_partitioned__extract_year + , subq_0.ds_partitioned__extract_quarter + , subq_0.ds_partitioned__extract_month + , subq_0.ds_partitioned__extract_day + , subq_0.ds_partitioned__extract_dow + , subq_0.ds_partitioned__extract_doy + , subq_0.paid_at__day + , subq_0.paid_at__week + , subq_0.paid_at__month + , subq_0.paid_at__quarter + , subq_0.paid_at__year + , subq_0.paid_at__extract_year + , subq_0.paid_at__extract_quarter + , subq_0.paid_at__extract_month + , subq_0.paid_at__extract_day + , subq_0.paid_at__extract_dow + , subq_0.paid_at__extract_doy + , subq_0.booking__ds__day + , subq_0.booking__ds__week + , subq_0.booking__ds__month + , subq_0.booking__ds__quarter + , subq_0.booking__ds__year + , subq_0.booking__ds__extract_year + , subq_0.booking__ds__extract_quarter + , subq_0.booking__ds__extract_month + , subq_0.booking__ds__extract_day + , subq_0.booking__ds__extract_dow + , subq_0.booking__ds__extract_doy + , subq_0.booking__ds_partitioned__day + , subq_0.booking__ds_partitioned__week + , subq_0.booking__ds_partitioned__month + , subq_0.booking__ds_partitioned__quarter + , subq_0.booking__ds_partitioned__year + , subq_0.booking__ds_partitioned__extract_year + , subq_0.booking__ds_partitioned__extract_quarter + , subq_0.booking__ds_partitioned__extract_month + , subq_0.booking__ds_partitioned__extract_day + , subq_0.booking__ds_partitioned__extract_dow + , subq_0.booking__ds_partitioned__extract_doy + , subq_0.booking__paid_at__day + , subq_0.booking__paid_at__week + , subq_0.booking__paid_at__month + , subq_0.booking__paid_at__quarter + , subq_0.booking__paid_at__year + , subq_0.booking__paid_at__extract_year + , subq_0.booking__paid_at__extract_quarter + , subq_0.booking__paid_at__extract_month + , subq_0.booking__paid_at__extract_day + , subq_0.booking__paid_at__extract_dow + , subq_0.booking__paid_at__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.listing + , subq_0.guest + , subq_0.host + , subq_0.booking__listing + , subq_0.booking__guest + , subq_0.booking__host + , subq_0.is_instant + , subq_0.booking__is_instant + , subq_0.bookings + , subq_0.instant_bookings + , subq_0.booking_value + , subq_0.max_booking_value + , subq_0.min_booking_value + , subq_0.bookers + , subq_0.average_booking_value + , subq_0.referred_bookings + , subq_0.median_booking_value + , subq_0.booking_value_p99 + , subq_0.discrete_booking_value_p99 + , subq_0.approximate_continuous_booking_value_p99 + , subq_0.approximate_discrete_booking_value_p99 + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , bookings_source_src_28000.booking_value + , bookings_source_src_28000.booking_value AS max_booking_value + , bookings_source_src_28000.booking_value AS min_booking_value + , bookings_source_src_28000.guest_id AS bookers + , bookings_source_src_28000.booking_value AS average_booking_value + , bookings_source_src_28000.booking_value AS booking_payments + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + , bookings_source_src_28000.booking_value AS median_booking_value + , bookings_source_src_28000.booking_value AS booking_value_p99 + , bookings_source_src_28000.booking_value AS discrete_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_continuous_booking_value_p99 + , bookings_source_src_28000.booking_value AS approximate_discrete_booking_value_p99 + , bookings_source_src_28000.is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS paid_at__extract_doy + , bookings_source_src_28000.is_instant AS booking__is_instant + , DATE_TRUNC('day', bookings_source_src_28000.ds) AS booking__ds__day + , DATE_TRUNC('week', bookings_source_src_28000.ds) AS booking__ds__week + , DATE_TRUNC('month', bookings_source_src_28000.ds) AS booking__ds__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds) AS booking__ds__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds) AS booking__ds__year + , EXTRACT(year FROM bookings_source_src_28000.ds) AS booking__ds__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds) AS booking__ds__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds) AS booking__ds__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds) AS booking__ds__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds) AS booking__ds__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds) AS booking__ds__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__day + , DATE_TRUNC('week', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__week + , DATE_TRUNC('month', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__month + , DATE_TRUNC('quarter', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__quarter + , DATE_TRUNC('year', bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__year + , EXTRACT(year FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_month + , EXTRACT(day FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.ds_partitioned) AS booking__ds_partitioned__extract_doy + , DATE_TRUNC('day', bookings_source_src_28000.paid_at) AS booking__paid_at__day + , DATE_TRUNC('week', bookings_source_src_28000.paid_at) AS booking__paid_at__week + , DATE_TRUNC('month', bookings_source_src_28000.paid_at) AS booking__paid_at__month + , DATE_TRUNC('quarter', bookings_source_src_28000.paid_at) AS booking__paid_at__quarter + , DATE_TRUNC('year', bookings_source_src_28000.paid_at) AS booking__paid_at__year + , EXTRACT(year FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_year + , EXTRACT(quarter FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_quarter + , EXTRACT(month FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_month + , EXTRACT(day FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_day + , EXTRACT(DAY_OF_WEEK FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_dow + , EXTRACT(doy FROM bookings_source_src_28000.paid_at) AS booking__paid_at__extract_doy + , bookings_source_src_28000.listing_id AS listing + , bookings_source_src_28000.guest_id AS guest + , bookings_source_src_28000.host_id AS host + , bookings_source_src_28000.listing_id AS booking__listing + , bookings_source_src_28000.guest_id AS booking__guest + , bookings_source_src_28000.host_id AS booking__host + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_0 + ) subq_1 + LEFT OUTER JOIN + ***************************.mf_time_spine subq_2 + ON + subq_1.booking__ds__day = subq_2.ds + ) subq_3 + GROUP BY + subq_3.booking__ds__martian_day +) subq_4 diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0_optimized.sql new file mode 100644 index 0000000000..0d4bd34046 --- /dev/null +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_simple_metric_with_custom_granularity__plan0_optimized.sql @@ -0,0 +1,21 @@ +-- Join to Custom Granularity Dataset +-- Pass Only Elements: ['bookings', 'booking__ds__day'] +-- Aggregate Measures +-- Compute Metrics via Expressions +SELECT + subq_7.martian_day AS booking__ds__martian_day + , SUM(subq_6.bookings) AS bookings +FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 +) subq_6 +LEFT OUTER JOIN + ***************************.mf_time_spine subq_7 +ON + subq_6.booking__ds__day = subq_7.ds +GROUP BY + subq_7.martian_day