From 8434dc861ee1d07fa9d54d5f59cdc566e260586c Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Fri, 8 Nov 2024 13:25:41 -0800 Subject: [PATCH 1/8] /* PR_START p--cte 18 */ Update default optimization level to O5. --- metricflow/engine/metricflow_engine.py | 32 +++++++++---------- metricflow/plan_conversion/dataflow_to_sql.py | 2 +- .../sql/optimizer/optimization_levels.py | 2 +- tests_metricflow/examples/test_node_sql.py | 3 -- .../test_dataflow_to_execution.py | 2 +- .../test_dataflow_to_sql_plan.py | 1 - .../query_rendering/compare_rendered_query.py | 1 - 7 files changed, 19 insertions(+), 24 deletions(-) diff --git a/metricflow/engine/metricflow_engine.py b/metricflow/engine/metricflow_engine.py index 319aa1a2b6..d50d792b9a 100644 --- a/metricflow/engine/metricflow_engine.py +++ b/metricflow/engine/metricflow_engine.py @@ -100,21 +100,21 @@ class MetricFlowQueryRequest: """ request_id: MetricFlowRequestId - saved_query_name: Optional[str] = None - metric_names: Optional[Sequence[str]] = None - metrics: Optional[Sequence[MetricQueryParameter]] = None - group_by_names: Optional[Sequence[str]] = None - group_by: Optional[Tuple[GroupByParameter, ...]] = None - limit: Optional[int] = None - time_constraint_start: Optional[datetime.datetime] = None - time_constraint_end: Optional[datetime.datetime] = None - where_constraints: Optional[Sequence[str]] = None - order_by_names: Optional[Sequence[str]] = None - order_by: Optional[Sequence[OrderByQueryParameter]] = None - min_max_only: bool = False - sql_optimization_level: SqlQueryOptimizationLevel = SqlQueryOptimizationLevel.O4 - dataflow_plan_optimizations: FrozenSet[DataflowPlanOptimization] = DataflowPlanOptimization.enabled_optimizations() - query_type: MetricFlowQueryType = MetricFlowQueryType.METRIC + saved_query_name: Optional[str] + metric_names: Optional[Sequence[str]] + metrics: Optional[Sequence[MetricQueryParameter]] + group_by_names: Optional[Sequence[str]] + group_by: Optional[Tuple[GroupByParameter, ...]] + limit: Optional[int] + time_constraint_start: Optional[datetime.datetime] + time_constraint_end: Optional[datetime.datetime] + where_constraints: Optional[Sequence[str]] + order_by_names: Optional[Sequence[str]] + order_by: Optional[Sequence[OrderByQueryParameter]] + min_max_only: bool + sql_optimization_level: SqlQueryOptimizationLevel + dataflow_plan_optimizations: FrozenSet[DataflowPlanOptimization] + query_type: MetricFlowQueryType @staticmethod def create_with_random_request_id( # noqa: D102 @@ -129,7 +129,7 @@ def create_with_random_request_id( # noqa: D102 where_constraints: Optional[Sequence[str]] = None, order_by_names: Optional[Sequence[str]] = None, order_by: Optional[Sequence[OrderByQueryParameter]] = None, - sql_optimization_level: SqlQueryOptimizationLevel = SqlQueryOptimizationLevel.O4, + sql_optimization_level: SqlQueryOptimizationLevel = SqlQueryOptimizationLevel.default_level(), dataflow_plan_optimizations: FrozenSet[ DataflowPlanOptimization ] = DataflowPlanOptimization.enabled_optimizations(), diff --git a/metricflow/plan_conversion/dataflow_to_sql.py b/metricflow/plan_conversion/dataflow_to_sql.py index a53702e3d2..61d3d510e1 100644 --- a/metricflow/plan_conversion/dataflow_to_sql.py +++ b/metricflow/plan_conversion/dataflow_to_sql.py @@ -182,7 +182,7 @@ def convert_to_sql_query_plan( self, sql_engine_type: SqlEngine, dataflow_plan_node: DataflowPlanNode, - optimization_level: SqlQueryOptimizationLevel = SqlQueryOptimizationLevel.O4, + optimization_level: SqlQueryOptimizationLevel = SqlQueryOptimizationLevel.default_level(), sql_query_plan_id: Optional[DagId] = None, ) -> ConvertToSqlPlanResult: """Create an SQL query plan that represents the computation up to the given dataflow plan node.""" diff --git a/metricflow/sql/optimizer/optimization_levels.py b/metricflow/sql/optimizer/optimization_levels.py index 061e14ca1b..63cedafbcd 100644 --- a/metricflow/sql/optimizer/optimization_levels.py +++ b/metricflow/sql/optimizer/optimization_levels.py @@ -25,7 +25,7 @@ class SqlQueryOptimizationLevel(Enum): @staticmethod def default_level() -> SqlQueryOptimizationLevel: # noqa: D102 - return SqlQueryOptimizationLevel.O4 + return SqlQueryOptimizationLevel.O5 @dataclass(frozen=True) diff --git a/tests_metricflow/examples/test_node_sql.py b/tests_metricflow/examples/test_node_sql.py index c7ac034512..d0a0f281cb 100644 --- a/tests_metricflow/examples/test_node_sql.py +++ b/tests_metricflow/examples/test_node_sql.py @@ -20,7 +20,6 @@ from metricflow.dataset.convert_semantic_model import SemanticModelToDataSetConverter from metricflow.plan_conversion.dataflow_to_sql import DataflowToSqlQueryPlanConverter from metricflow.protocols.sql_client import SqlClient -from metricflow.sql.optimizer.optimization_levels import SqlQueryOptimizationLevel from metricflow.sql.render.sql_plan_renderer import SqlQueryPlanRenderer logger = logging.getLogger(__name__) @@ -57,7 +56,6 @@ def test_view_sql_generated_at_a_node( conversion_result = to_sql_plan_converter.convert_to_sql_query_plan( sql_engine_type=sql_client.sql_engine_type, dataflow_plan_node=read_source_node, - optimization_level=SqlQueryOptimizationLevel.O4, ) sql_plan_at_read_node = conversion_result.sql_plan sql_at_read_node = sql_renderer.render_sql_query_plan(sql_plan_at_read_node).sql @@ -86,7 +84,6 @@ def test_view_sql_generated_at_a_node( conversion_result = to_sql_plan_converter.convert_to_sql_query_plan( sql_engine_type=sql_client.sql_engine_type, dataflow_plan_node=filter_elements_node, - optimization_level=SqlQueryOptimizationLevel.O4, ) sql_plan_at_filter_elements_node = conversion_result.sql_plan sql_at_filter_elements_node = sql_renderer.render_sql_query_plan(sql_plan_at_filter_elements_node).sql diff --git a/tests_metricflow/plan_conversion/test_dataflow_to_execution.py b/tests_metricflow/plan_conversion/test_dataflow_to_execution.py index c5b9503f3c..fa67ffd4c9 100644 --- a/tests_metricflow/plan_conversion/test_dataflow_to_execution.py +++ b/tests_metricflow/plan_conversion/test_dataflow_to_execution.py @@ -31,7 +31,7 @@ def make_execution_plan_converter( # noqa: D103 ), sql_plan_renderer=DefaultSqlQueryPlanRenderer(), sql_client=sql_client, - sql_optimization_level=SqlQueryOptimizationLevel.O4, + sql_optimization_level=SqlQueryOptimizationLevel.default_level(), ) diff --git a/tests_metricflow/plan_conversion/test_dataflow_to_sql_plan.py b/tests_metricflow/plan_conversion/test_dataflow_to_sql_plan.py index a7c45edc0f..23b3e43144 100644 --- a/tests_metricflow/plan_conversion/test_dataflow_to_sql_plan.py +++ b/tests_metricflow/plan_conversion/test_dataflow_to_sql_plan.py @@ -98,7 +98,6 @@ def convert_and_check( sql_engine_type=sql_client.sql_engine_type, sql_query_plan_id=DagId.from_str("plan0_optimized"), dataflow_plan_node=node, - optimization_level=SqlQueryOptimizationLevel.O4, ) sql_query_plan = conversion_result.sql_plan display_graph_if_requested( diff --git a/tests_metricflow/query_rendering/compare_rendered_query.py b/tests_metricflow/query_rendering/compare_rendered_query.py index 1d36ffce46..7f005dc55a 100644 --- a/tests_metricflow/query_rendering/compare_rendered_query.py +++ b/tests_metricflow/query_rendering/compare_rendered_query.py @@ -63,7 +63,6 @@ def render_and_check( conversion_result = dataflow_to_sql_converter.convert_to_sql_query_plan( sql_engine_type=sql_client.sql_engine_type, dataflow_plan_node=optimized_plan.sink_node, - optimization_level=SqlQueryOptimizationLevel.O4, sql_query_plan_id=DagId.from_str("plan0_optimized"), ) sql_query_plan = conversion_result.sql_plan From 0eafcbe57a98910a1d99341ef7aeee00b6775056 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 22:02:39 -0800 Subject: [PATCH 2/8] Update snapshots for DuckDB. --- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- 12 files changed, 453 insertions(+), 455 deletions(-) diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql index d52f20bc31..8f9ca82ad0 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 7221fbdec0..f93bee27a4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > subq_33.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_32.metric_time__day - INTERVAL 7 day ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql index 2375a22b4b..e681c6fd8c 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql index cc21cc6942..c468e4c331 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 12d9bcee09..e3e36d963b 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql index 1e68f84561..6a5a0bb6fd 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/DuckDB/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - INTERVAL 7 day + sma_28019_cte.metric_time__day > subq_23.metric_time__day - INTERVAL 7 day ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql index 6188d8c5fa..d0b2da3568 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 4159b2cece..fc27a90b6e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: DuckDB --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql index 451fcb54db..deb4291897 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: DuckDB --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index c82fb24120..d6e070aa73 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day From e90741be9ad13c4c529336accaa79fbd00a1a00e Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 22:23:57 -0800 Subject: [PATCH 3/8] Update other engine snapshots. --- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 99 ++++++------- .../test_conversion_rate__plan0_optimized.sql | 99 ++++++------- ...h_constant_properties__plan0_optimized.sql | 132 ++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 108 +++++++------- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- ...ount_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...and_fill_nulls_with_0__plan0_optimized.sql | 101 +++++++------ .../test_conversion_rate__plan0_optimized.sql | 101 +++++++------ ...h_constant_properties__plan0_optimized.sql | 136 +++++++++--------- ...rate_with_no_group_by__plan0_optimized.sql | 76 +++++----- ...sion_rate_with_window__plan0_optimized.sql | 112 +++++++-------- .../test_combined_metrics_plan__ep_0.xml | 65 ++++----- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ .../test_combined_metrics_plan__ep_0.xml | 71 +++++---- ...test_small_combined_metrics_plan__ep_0.xml | 85 +++++------ ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 39 +++-- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...t_combine_output_node__plan0_optimized.sql | 33 +++-- ...tiple_semantic_models__plan0_optimized.sql | 47 +++--- .../test_multi_join_node__plan0_optimized.sql | 33 +++-- ...metric_time_dimension__plan0_optimized.sql | 33 +++-- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- ...metric_time_dimension__plan0_optimized.sql | 37 ++--- 72 files changed, 2703 insertions(+), 2715 deletions(-) diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql index 1267ebb049..2949bf60c4 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: BigQuery --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index eab4d41c74..c29b49ee48 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > DATE_SUB(CAST(subq_33.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_32.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY metric_time__day -) subq_41 +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql index a8f6fb72d6..26e66edddf 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 2bbbc75746..3d75853ab1 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 183e1f7b01..364279bcbf 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: BigQuery --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS FLOAT64) / CAST(NULLIF(MAX(subq_18.visits), 0) AS FLOAT64) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS FLOAT64) / CAST(NULLIF(MAX(subq_18.visits), 0) AS FLOAT64) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql index 038bda649f..b9a83bb9d9 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/BigQuery/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , GENERATE_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_SUB(CAST(subq_24.metric_time__day AS DATETIME), INTERVAL 7 day) + sma_28019_cte.metric_time__day > DATE_SUB(CAST(subq_23.metric_time__day AS DATETIME), INTERVAL 7 day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY metric_time__day , visit__referrer_id -) subq_29 +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql index 6b4bf6b28f..b98570fe01 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Databricks --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index f5a58a70f9..054eabc223 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Databricks --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__day) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql index 0c21b78b89..673f1c5940 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Databricks --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 56dc8aa181..f548e1c25a 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Databricks --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 91c5c66528..3092aba633 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Databricks --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql index 4d601ef642..b3a2f09e0a 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Databricks/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Databricks --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql index 53d7571adc..a116b7c877 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Postgres --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 00ff936d0f..ed86788422 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Postgres --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > subq_33.metric_time__day - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_32.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql index 97c2f6f0b4..803bd0f363 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Postgres --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 6e4425814d..109d592bd8 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Postgres --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 625a688fb9..63e7d4caf6 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Postgres --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql index 18cc7b5f6f..6e11ad3d45 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Postgres/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Postgres --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , GEN_RANDOM_UUID() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > subq_24.metric_time__day - MAKE_INTERVAL(days => 7) + sma_28019_cte.metric_time__day > subq_23.metric_time__day - MAKE_INTERVAL(days => 7) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql index 2de7bf5d38..5dd3f9150c 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Redshift --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 896945c98b..3c41d84dcb 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Redshift --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__day) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql index 726b9bb90c..2700910c75 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Redshift --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 805203209e..9d324727cf 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Redshift --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 389b6e71e8..029a4cdd10 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Redshift --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql index b654029282..ab41c8632d 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Redshift/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Redshift --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , CONCAT(CAST(RANDOM()*100000000 AS INT)::VARCHAR,CAST(RANDOM()*100000000 AS INT)::VARCHAR) AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql index 45198ff84a..cb66b10ff1 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Snowflake --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 7a98180760..463efd3a56 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Snowflake --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > DATEADD(day, -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_32.metric_time__day) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql index 198a34d6c1..a2bf58fc4f 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Snowflake --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql index fcd07294d2..8c7b493ad7 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Snowflake --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql index 9165dcef45..f6cea61c4f 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Snowflake --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql index 5289cd3495..11b8ede2a7 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Snowflake/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Snowflake --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , UUID_STRING() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATEADD(day, -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATEADD(day, -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql index ab9c25bc0e..f829f4ca3d 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_count_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Trino --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - COALESCE(MAX(subq_28.buys), 0) AS visit_buy_conversions -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + COALESCE(MAX(subq_27.buys), 0) AS visit_buy_conversions +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql index 5fdf62a899..b354a8d965 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_metric_join_to_timespine_and_fill_nulls_with_0__plan0_optimized.sql @@ -5,15 +5,25 @@ docstring: sql_engine: Trino --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day + metric_time__day AS metric_time__day , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days_fill_nulls_with_0 FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) AS metric_time__day + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) AS metric_time__day , COALESCE(MAX(subq_27.visits), 0) AS visits - , COALESCE(MAX(subq_40.buys), 0) AS buys + , COALESCE(MAX(subq_39.buys), 0) AS buys FROM ( -- Join to Time Spine Dataset SELECT @@ -21,19 +31,13 @@ FROM ( , subq_24.visits AS visits FROM ***************************.mf_time_spine subq_26 LEFT OUTER JOIN ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_23 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day ) subq_24 @@ -43,9 +47,9 @@ FROM ( FULL OUTER JOIN ( -- Join to Time Spine Dataset SELECT - subq_39.ds AS metric_time__day - , subq_37.buys AS buys - FROM ***************************.mf_time_spine subq_39 + subq_38.ds AS metric_time__day + , subq_36.buys AS buys + FROM ***************************.mf_time_spine subq_38 LEFT OUTER JOIN ( -- Find conversions for user within the range of 7 day -- Pass Only Elements: ['buys', 'metric_time__day'] @@ -56,42 +60,33 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_30.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_30.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_30.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_33.user - , subq_33.metric_time__day - , subq_33.mf_internal_uuid - ORDER BY subq_30.metric_time__day DESC + subq_32.user + , subq_32.metric_time__day + , subq_32.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_33.mf_internal_uuid AS mf_internal_uuid - , subq_33.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_30 + , subq_32.mf_internal_uuid AS mf_internal_uuid + , subq_32.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,26 +97,26 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_33 + ) subq_32 ON ( - subq_30.user = subq_33.user + sma_28019_cte.user = subq_32.user ) AND ( ( - subq_30.metric_time__day <= subq_33.metric_time__day + sma_28019_cte.metric_time__day <= subq_32.metric_time__day ) AND ( - subq_30.metric_time__day > DATE_ADD('day', -7, subq_33.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_32.metric_time__day) ) ) - ) subq_34 + ) subq_33 GROUP BY metric_time__day - ) subq_37 + ) subq_36 ON - subq_39.ds = subq_37.metric_time__day - ) subq_40 + subq_38.ds = subq_36.metric_time__day + ) subq_39 ON - subq_27.metric_time__day = subq_40.metric_time__day + subq_27.metric_time__day = subq_39.metric_time__day GROUP BY - COALESCE(subq_27.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(subq_27.metric_time__day, subq_39.metric_time__day) +) subq_40 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql index b4cc1c15bb..ce39c30a21 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate__plan0_optimized.sql @@ -5,29 +5,34 @@ docstring: sql_engine: Trino --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - visit__referrer_id + visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id'] -- Aggregate Measures SELECT visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id'] - SELECT - referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY visit__referrer_id ) subq_18 @@ -41,51 +46,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -96,19 +91,19 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - (subq_21.metric_time__day <= subq_24.metric_time__day) + (sma_28019_cte.metric_time__day <= subq_23.metric_time__day) ) - ) subq_25 + ) subq_24 GROUP BY visit__referrer_id - ) subq_28 + ) subq_27 ON - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id GROUP BY - COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql index 6c39bb9502..772aef9801 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_constant_properties__plan0_optimized.sql @@ -5,33 +5,38 @@ docstring: sql_engine: Trino --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , session_id AS session + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_by_session FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,65 +52,54 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , FIRST_VALUE(subq_21.session) OVER ( + , FIRST_VALUE(sma_28019_cte.session) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - , subq_24.session_id - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + , subq_23.session_id + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS session - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user', 'session'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , session_id AS session - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -117,31 +111,31 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( - subq_21.session = subq_24.session_id + sma_28019_cte.session = subq_23.session_id ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql index cbf2037594..78b24a0a86 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_no_group_by__plan0_optimized.sql @@ -6,16 +6,25 @@ sql_engine: Trino --- -- Combine Aggregated Outputs -- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_28.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days -FROM ( +WITH sma_28019_cte AS ( -- Read Elements From Semantic Model 'visits_source' -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + +SELECT + CAST(MAX(subq_27.buys) AS DOUBLE) / CAST(NULLIF(MAX(subq_18.visits), 0) AS DOUBLE) AS visit_buy_conversion_rate_7days +FROM ( + -- Read From CTE For node_id=sma_28019 -- Pass Only Elements: ['visits',] -- Aggregate Measures SELECT - SUM(1) AS visits - FROM ***************************.fct_visits visits_source_src_28000 + SUM(visits) AS visits + FROM sma_28019_cte sma_28019_cte ) subq_18 CROSS JOIN ( -- Find conversions for user within the range of 7 day @@ -26,42 +35,33 @@ CROSS JOIN ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -72,16 +72,16 @@ CROSS JOIN ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 -) subq_28 + ) subq_24 +) subq_27 diff --git a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql index 3cbcfcfba8..06aaa63ab6 100644 --- a/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_conversion_metrics_to_sql.py/SqlQueryPlan/Trino/test_conversion_rate_with_window__plan0_optimized.sql @@ -5,33 +5,37 @@ docstring: sql_engine: Trino --- -- Compute Metrics via Expressions +WITH sma_28019_cte AS ( + -- Read Elements From Semantic Model 'visits_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , user_id AS user + , referrer_id AS visit__referrer_id + , 1 AS visits + FROM ***************************.fct_visits visits_source_src_28000 +) + SELECT - metric_time__day - , visit__referrer_id + metric_time__day AS metric_time__day + , visit__referrer_id AS visit__referrer_id , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS visit_buy_conversion_rate_7days FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) AS metric_time__day - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) AS visit__referrer_id + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) AS metric_time__day + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) AS visit__referrer_id , MAX(subq_18.visits) AS visits - , MAX(subq_28.buys) AS buys + , MAX(subq_27.buys) AS buys FROM ( + -- Read From CTE For node_id=sma_28019 + -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] -- Aggregate Measures SELECT metric_time__day , visit__referrer_id , SUM(visits) AS visits - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_17 + FROM sma_28019_cte sma_28019_cte GROUP BY metric_time__day , visit__referrer_id @@ -47,51 +51,41 @@ FROM ( FROM ( -- Dedupe the fanout with mf_internal_uuid in the conversion data set SELECT DISTINCT - FIRST_VALUE(subq_21.visits) OVER ( + FIRST_VALUE(sma_28019_cte.visits) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visits - , FIRST_VALUE(subq_21.visit__referrer_id) OVER ( + , FIRST_VALUE(sma_28019_cte.visit__referrer_id) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS visit__referrer_id - , FIRST_VALUE(subq_21.metric_time__day) OVER ( + , FIRST_VALUE(sma_28019_cte.metric_time__day) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS metric_time__day - , FIRST_VALUE(subq_21.user) OVER ( + , FIRST_VALUE(sma_28019_cte.user) OVER ( PARTITION BY - subq_24.user - , subq_24.metric_time__day - , subq_24.mf_internal_uuid - ORDER BY subq_21.metric_time__day DESC + subq_23.user + , subq_23.metric_time__day + , subq_23.mf_internal_uuid + ORDER BY sma_28019_cte.metric_time__day DESC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS user - , subq_24.mf_internal_uuid AS mf_internal_uuid - , subq_24.buys AS buys - FROM ( - -- Read Elements From Semantic Model 'visits_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['visits', 'visit__referrer_id', 'metric_time__day', 'user'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , user_id AS user - , referrer_id AS visit__referrer_id - , 1 AS visits - FROM ***************************.fct_visits visits_source_src_28000 - ) subq_21 + , subq_23.mf_internal_uuid AS mf_internal_uuid + , subq_23.buys AS buys + FROM sma_28019_cte sma_28019_cte INNER JOIN ( -- Read Elements From Semantic Model 'buys_source' -- Metric Time Dimension 'ds' @@ -102,29 +96,29 @@ FROM ( , 1 AS buys , uuid() AS mf_internal_uuid FROM ***************************.fct_buys buys_source_src_28000 - ) subq_24 + ) subq_23 ON ( - subq_21.user = subq_24.user + sma_28019_cte.user = subq_23.user ) AND ( ( - subq_21.metric_time__day <= subq_24.metric_time__day + sma_28019_cte.metric_time__day <= subq_23.metric_time__day ) AND ( - subq_21.metric_time__day > DATE_ADD('day', -7, subq_24.metric_time__day) + sma_28019_cte.metric_time__day > DATE_ADD('day', -7, subq_23.metric_time__day) ) ) - ) subq_25 + ) subq_24 GROUP BY metric_time__day , visit__referrer_id - ) subq_28 + ) subq_27 ON ( - subq_18.visit__referrer_id = subq_28.visit__referrer_id + subq_18.visit__referrer_id = subq_27.visit__referrer_id ) AND ( - subq_18.metric_time__day = subq_28.metric_time__day + subq_18.metric_time__day = subq_27.metric_time__day ) GROUP BY - COALESCE(subq_18.metric_time__day, subq_28.metric_time__day) - , COALESCE(subq_18.visit__referrer_id, subq_28.visit__referrer_id) -) subq_29 + COALESCE(subq_18.metric_time__day, subq_27.metric_time__day) + , COALESCE(subq_18.visit__referrer_id, subq_27.visit__referrer_id) +) subq_28 diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml index 5d5d9d565a..75da5fd0bb 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml @@ -7,80 +7,77 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml index 6f31d9f164..248cf24e49 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml index 6af35e7d73..2d133f4c66 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml @@ -7,83 +7,80 @@ test_filename: test_dataflow_to_execution.py + + + + + + + + + + + + - - + + - - + + + + - - - - - - - - - - + + + - - - - - - - - - - + - + - + - + - - + - + - + - + - + - + - + - - + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml index a9d8f915a0..4da40aaf0e 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml @@ -5,46 +5,49 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql index af7158814b..292362d022 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: BigQuery --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 03e5630df2..dc98ebb10a 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: BigQuery --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,9 +44,9 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY ds__day , listing__country_latest @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY ds__day , listing__country_latest - ) subq_36 + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY ds__day , listing__country_latest -) subq_37 +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql index 22f65187f5..2fced228ea 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: BigQuery --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql index c0bc198917..213e8cbd91 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Databricks --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index d0c28be01e..45487cf27c 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Databricks --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql index 6fc50e372d..e273012100 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Databricks --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql index 983f8f1d1c..bfead25433 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Postgres --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 9a15a293f4..c8d146de34 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Postgres --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql index 7ba2d43929..0d42756ed6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Postgres --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql index 59cf853e56..0ae940a5e7 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Redshift --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 1d695a61aa..3343128d76 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Redshift --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql index e83e60d3ef..697433a5df 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Redshift --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql index 476ce6aea8..76c604e6ad 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Snowflake --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 4ecf068dc0..b14f947b5d 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Snowflake --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql index dbcbd7c6d9..becffa2bd6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Snowflake --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql index 41aeb438e4..9a69082943 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_combine_output_node__plan0_optimized.sql @@ -5,42 +5,41 @@ docstring: sql_engine: Trino --- -- Combine Aggregated Outputs +WITH rss_28001_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + , guest_id AS bookers + , is_instant + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_8.is_instant, subq_11.is_instant) AS is_instant , MAX(subq_8.bookings) AS bookings , COALESCE(MAX(subq_11.instant_bookings), 1) AS instant_bookings , COALESCE(MAX(subq_11.bookers), 1) AS bookers FROM ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['bookings', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['bookings', 'is_instant'] - SELECT - is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_7 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_8 FULL OUTER JOIN ( + -- Read From CTE For node_id=rss_28001 + -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] -- Aggregate Measures SELECT is_instant , SUM(instant_bookings) AS instant_bookings , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Pass Only Elements: ['instant_bookings', 'bookers', 'is_instant'] - SELECT - is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_10 + FROM rss_28001_cte rss_28001_cte GROUP BY is_instant ) subq_11 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index a7ddeb2c1a..84c74729c8 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -5,17 +5,26 @@ docstring: sql_engine: Trino --- -- Compute Metrics via Expressions +WITH sma_28014_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT - ds__day - , listing__country_latest + ds__day AS ds__day + , listing__country_latest AS listing__country_latest , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view FROM ( -- Combine Aggregated Outputs SELECT - COALESCE(subq_27.ds__day, subq_36.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) AS listing__country_latest + COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest , MAX(subq_27.bookings) AS bookings - , MAX(subq_36.views) AS views + , MAX(subq_35.views) AS views FROM ( -- Join Standard Outputs -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] @@ -23,7 +32,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_20.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_20.bookings) AS bookings FROM ( -- Read Elements From Semantic Model 'bookings_source' @@ -35,12 +44,12 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_20 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_20.listing = listings_latest_src_28000.listing_id + subq_20.listing = sma_28014_cte.listing GROUP BY subq_20.ds__day - , listings_latest_src_28000.country + , sma_28014_cte.country_latest ) subq_27 FULL OUTER JOIN ( -- Join Standard Outputs @@ -49,7 +58,7 @@ FROM ( -- Compute Metrics via Expressions SELECT subq_29.ds__day AS ds__day - , listings_latest_src_28000.country AS listing__country_latest + , sma_28014_cte.country_latest AS listing__country_latest , SUM(subq_29.views) AS views FROM ( -- Read Elements From Semantic Model 'views_source' @@ -61,20 +70,20 @@ FROM ( FROM ***************************.fct_views views_source_src_28000 ) subq_29 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + sma_28014_cte sma_28014_cte ON - subq_29.listing = listings_latest_src_28000.listing_id + subq_29.listing = sma_28014_cte.listing GROUP BY subq_29.ds__day - , listings_latest_src_28000.country - ) subq_36 + , sma_28014_cte.country_latest + ) subq_35 ON ( - subq_27.listing__country_latest = subq_36.listing__country_latest + subq_27.listing__country_latest = subq_35.listing__country_latest ) AND ( - subq_27.ds__day = subq_36.ds__day + subq_27.ds__day = subq_35.ds__day ) GROUP BY - COALESCE(subq_27.ds__day, subq_36.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_36.listing__country_latest) -) subq_37 + COALESCE(subq_27.ds__day, subq_35.ds__day) + , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) +) subq_36 diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql index f86a7cbfa3..a49173cc21 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_multi_join_node__plan0_optimized.sql @@ -5,9 +5,18 @@ docstring: sql_engine: Trino --- -- Join Standard Outputs +WITH pfe_1_cte AS ( + -- Read Elements From Semantic Model 'listings_latest' + -- Pass Only Elements: ['country_latest', 'listing'] + SELECT + listing_id AS listing + , country AS country_latest + FROM ***************************.dim_listings_latest listings_latest_src_28000 +) + SELECT subq_9.country_latest AS listing__country_latest - , subq_11.country_latest AS listing__country_latest + , subq_10.country_latest AS listing__country_latest , subq_7.listing AS listing , subq_7.bookings AS bookings FROM ( @@ -19,22 +28,20 @@ FROM ( FROM ***************************.fct_bookings bookings_source_src_28000 ) subq_7 LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte ) subq_9 ON subq_7.listing = subq_9.listing LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'listings_latest' - -- Pass Only Elements: ['country_latest', 'listing'] + -- Read From CTE For node_id=pfe_1 SELECT - listing_id AS listing - , country AS country_latest - FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_11 + listing + , country_latest + FROM pfe_1_cte pfe_1_cte +) subq_10 ON - subq_7.listing = subq_11.listing + subq_7.listing = subq_10.listing diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 36bc8f5765..0b4cc0bffe 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,38 +5,43 @@ docstring: sql_engine: BigQuery --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATETIME_TRUNC(ds, day) AS ds__day + , DATETIME_TRUNC(paid_at, day) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATETIME_TRUNC(paid_at, day) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day ) subq_19 diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index c9e02e625f..4a9171736e 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Databricks --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 02a4e5b929..60600f1541 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Postgres --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 8736f91dfd..cb4519e9f9 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Redshift --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 259fad37d0..477d340b01 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Snowflake --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 663ee19713..afb011f5db 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -5,40 +5,45 @@ docstring: sql_engine: Trino --- -- Combine Aggregated Outputs +WITH rss_28020_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + SELECT + 1 AS bookings + , booking_value AS booking_payments + , DATE_TRUNC('day', ds) AS ds__day + , DATE_TRUNC('day', paid_at) AS paid_at__day + FROM ***************************.fct_bookings bookings_source_src_28000 +) + SELECT COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day , MAX(subq_14.bookings) AS bookings , MAX(subq_19.booking_payments) AS booking_payments FROM ( + -- Read From CTE For node_id=rss_28020 + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - metric_time__day + ds__day AS metric_time__day , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_12 + FROM rss_28020_cte rss_28020_cte GROUP BY - metric_time__day + ds__day ) subq_14 FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] -- Aggregate Measures -- Compute Metrics via Expressions SELECT - DATE_TRUNC('day', paid_at) AS metric_time__day - , SUM(booking_value) AS booking_payments - FROM ***************************.fct_bookings bookings_source_src_28000 + paid_at__day AS metric_time__day + , SUM(booking_payments) AS booking_payments + FROM rss_28020_cte rss_28020_cte GROUP BY - DATE_TRUNC('day', paid_at) + paid_at__day ) subq_19 ON subq_14.metric_time__day = subq_19.metric_time__day From fbca5cf0e200263ddd7bcb8ae153d970bb200efd Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Tue, 12 Nov 2024 21:58:32 -0800 Subject: [PATCH 4/8] Add change log for #1040. --- .changes/unreleased/Features-20241112-215817.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20241112-215817.yaml diff --git a/.changes/unreleased/Features-20241112-215817.yaml b/.changes/unreleased/Features-20241112-215817.yaml new file mode 100644 index 0000000000..aa354ab6fd --- /dev/null +++ b/.changes/unreleased/Features-20241112-215817.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Use CTEs instead of sub-queries in generated SQL. +time: 2024-11-12T21:58:17.127471-08:00 +custom: + Author: plypaul + Issue: "1040" From ada9adc8350856c0f3db5eda8651c3dc0eeb87e6 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 12:47:24 -0800 Subject: [PATCH 5/8] /* PR_START p--cte 19 */ Add method to group nodes by type. --- metricflow/dataflow/dataflow_plan_analyzer.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/metricflow/dataflow/dataflow_plan_analyzer.py b/metricflow/dataflow/dataflow_plan_analyzer.py index 0623277846..370e2a1cad 100644 --- a/metricflow/dataflow/dataflow_plan_analyzer.py +++ b/metricflow/dataflow/dataflow_plan_analyzer.py @@ -1,12 +1,15 @@ from __future__ import annotations from collections import defaultdict +from dataclasses import dataclass from typing import Dict, FrozenSet, Mapping, Sequence, Set +from metricflow_semantics.collection_helpers.merger import Mergeable from typing_extensions import override from metricflow.dataflow.dataflow_plan import DataflowPlan, DataflowPlanNode from metricflow.dataflow.dataflow_plan_visitor import DataflowPlanNodeVisitorWithDefaultHandler +from metricflow.dataflow.nodes.compute_metrics import ComputeMetricsNode class DataflowPlanAnalyzer: @@ -36,6 +39,12 @@ def find_common_branches(dataflow_plan: DataflowPlan) -> Sequence[DataflowPlanNo return tuple(sorted(dataflow_plan.sink_node.accept(common_branches_visitor))) + @staticmethod + def group_nodes_by_type(dataflow_plan: DataflowPlan) -> DataflowPlanNodeSet: + """Grouops dataflow plan nodes by type.""" + grouping_visitor = _GroupNodesByTypeVisitor() + return dataflow_plan.sink_node.accept(grouping_visitor) + class _CountDataflowNodeVisitor(DataflowPlanNodeVisitorWithDefaultHandler[None]): """Helper visitor to build a dict from a node in the plan to the number of times it appears in the plan.""" @@ -77,3 +86,41 @@ def _default_handler(self, node: DataflowPlanNode) -> FrozenSet[DataflowPlanNode common_branch_leaf_nodes.update(parent_node.accept(self)) return frozenset(common_branch_leaf_nodes) + + +@dataclass(frozen=True) +class DataflowPlanNodeSet(Mergeable): + """Contains a set of dataflow plan nodes with fields for different types. + + `ComputeMetricsNode` is the only node of interest for current use cases, but fields for other types can be added + later. + """ + + compute_metric_nodes: FrozenSet[ComputeMetricsNode] + + def merge(self, other: DataflowPlanNodeSet) -> DataflowPlanNodeSet: + return DataflowPlanNodeSet( + compute_metric_nodes=self.compute_metric_nodes.union(other.compute_metric_nodes), + ) + + @classmethod + def empty_instance(cls) -> DataflowPlanNodeSet: + return DataflowPlanNodeSet( + compute_metric_nodes=frozenset(), + ) + + +class _GroupNodesByTypeVisitor(DataflowPlanNodeVisitorWithDefaultHandler[DataflowPlanNodeSet]): + """Groups dataflow nodes by type.""" + + @override + def _default_handler(self, node: DataflowPlanNode) -> DataflowPlanNodeSet: + node_sets = [] + for parent_node in node.parent_nodes: + node_sets.append(parent_node.accept(self)) + + return DataflowPlanNodeSet.merge_iterable(node_sets) + + @override + def visit_compute_metrics_node(self, node: ComputeMetricsNode) -> DataflowPlanNodeSet: + return self._default_handler(node).merge(DataflowPlanNodeSet(frozenset({node}))) From f0c6b9f271c1475f38d254248ff48d90fdf3f8da Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Sat, 9 Nov 2024 12:47:41 -0800 Subject: [PATCH 6/8] Update to convert metric nodes to cte. --- metricflow/dataflow/dataflow_plan_analyzer.py | 4 +++- metricflow/plan_conversion/dataflow_to_sql.py | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/metricflow/dataflow/dataflow_plan_analyzer.py b/metricflow/dataflow/dataflow_plan_analyzer.py index 370e2a1cad..4bcdaa5002 100644 --- a/metricflow/dataflow/dataflow_plan_analyzer.py +++ b/metricflow/dataflow/dataflow_plan_analyzer.py @@ -41,7 +41,7 @@ def find_common_branches(dataflow_plan: DataflowPlan) -> Sequence[DataflowPlanNo @staticmethod def group_nodes_by_type(dataflow_plan: DataflowPlan) -> DataflowPlanNodeSet: - """Grouops dataflow plan nodes by type.""" + """Groups dataflow plan nodes by type.""" grouping_visitor = _GroupNodesByTypeVisitor() return dataflow_plan.sink_node.accept(grouping_visitor) @@ -98,12 +98,14 @@ class DataflowPlanNodeSet(Mergeable): compute_metric_nodes: FrozenSet[ComputeMetricsNode] + @override def merge(self, other: DataflowPlanNodeSet) -> DataflowPlanNodeSet: return DataflowPlanNodeSet( compute_metric_nodes=self.compute_metric_nodes.union(other.compute_metric_nodes), ) @classmethod + @override def empty_instance(cls) -> DataflowPlanNodeSet: return DataflowPlanNodeSet( compute_metric_nodes=frozenset(), diff --git a/metricflow/plan_conversion/dataflow_to_sql.py b/metricflow/plan_conversion/dataflow_to_sql.py index 61d3d510e1..08a8b20b7b 100644 --- a/metricflow/plan_conversion/dataflow_to_sql.py +++ b/metricflow/plan_conversion/dataflow_to_sql.py @@ -273,7 +273,10 @@ def _get_nodes_to_convert_to_cte( """Handles logic for selecting which nodes to convert to CTEs based on the request.""" dataflow_plan = dataflow_plan_node.as_plan() nodes_to_convert_to_cte: Set[DataflowPlanNode] = set(DataflowPlanAnalyzer.find_common_branches(dataflow_plan)) - # Additional nodes will be added later. + + compute_metric_nodes = DataflowPlanAnalyzer.group_nodes_by_type(dataflow_plan).compute_metric_nodes + if len(compute_metric_nodes) > 1: + nodes_to_convert_to_cte.update(compute_metric_nodes) return frozenset(nodes_to_convert_to_cte) From 812a6cc3a229855ce2737f90c426619a6e661db2 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Tue, 12 Nov 2024 22:05:22 -0800 Subject: [PATCH 7/8] Update snapshots with DuckDB. --- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- .../str/DuckDB/test_id_enumeration__query.sql | 25 +- 66 files changed, 3320 insertions(+), 2375 deletions(-) diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index 547cf232b1..2214c5a104 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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 - INTERVAL 2 month + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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 - INTERVAL 2 month - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_derived_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_derived_metric_with_custom_granularity__plan0_optimized.sql index 090fed233c..1f83573863 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_derived_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_derived_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql index 21cbf2586c..75465a1875 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_multiple_metrics_with_custom_granularity__plan0_optimized.sql @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: DuckDB --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 9b358e0937..e84f86d2ec 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index a74e4042e7..02d1ccbca3 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/DuckDB/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/DuckDB/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index fc27a90b6e..ba48e34512 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/DuckDB/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: DuckDB --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 2aaf5a5ff9..39d90fabc8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric__plan0_optimized.sql index 99170ffa98..cecf858644 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 7a46cbd456..8f6b09605d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: DuckDB --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index dedecd5277..00741a3d15 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( subq_10.metric_time__month - INTERVAL 1 month = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index d7456bd9df..680de50a75 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index cfa6d1f5e0..5404e35a80 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql index 18cde7417b..7898d82b0e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 42b79f2c69..eec256cc0f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index cc78d48928..8368601bdb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index b31f530528..669cd4cdce 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + subq_28.ds - INTERVAL 1 month = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 66f5fe7c5e..0c52e9c830 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + subq_25.ds - INTERVAL 14 day = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - subq_25.ds - INTERVAL 14 day = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_cumulative_metric__plan0_optimized.sql index 47db02e9dd..9d77002d7a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( subq_17.ds - INTERVAL 2 day = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index d916d9e538..a8569f1e4a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index 4a3f73badc..8ba0ae1685 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ds - INTERVAL 5 day = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_multi_metric_fill_null__plan0_optimized.sql index d2ae62f58e..575f5ab098 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric__plan0_optimized.sql index 8d9bf1af41..4b8db03776 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index d62752cb4f..14045b183b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + subq_17.ds - INTERVAL 5 day = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - subq_17.ds - INTERVAL 5 day = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - subq_24.ds - INTERVAL 2 day = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_24.ds - INTERVAL 2 day = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 101e623ab7..00626834b5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index 56570a8c16..aa0769adec 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 476ceafc1f..3283b6e875 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_filters__plan0_optimized.sql index d61665f8ea..7b5b19d38f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets__plan0_optimized.sql index ec796be86d..8e9d120783 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - INTERVAL 5 day = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - subq_22.ds - INTERVAL 2 day = subq_20.metric_time__day -) subq_23 + subq_15.ds - INTERVAL 5 day = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_22.ds - INTERVAL 2 day = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0_optimized.sql index 677431f0cc..76a6c10e52 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + subq_15.ds - INTERVAL 5 day = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - INTERVAL 5 day = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - subq_21.metric_time__day - INTERVAL 2 day = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_21.metric_time__day - INTERVAL 2 day = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 5d979eb0ce..34988cc54b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + subq_16.ds - INTERVAL 5 day = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - subq_16.ds - INTERVAL 5 day = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - subq_23.ds - INTERVAL 2 day = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_23.ds - INTERVAL 2 day = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 29518080d5..82f9f7a383 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index d92697dcfc..0bf44f2898 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 8fadab17b1..7b5d27693c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 63f543a976..f2caf380a8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - subq_19.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_19.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 0c93f04252..10b67501af 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - INTERVAL 1 week = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 84c0c9aea0..14e866f917 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - INTERVAL 14 day = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + subq_22.ds - INTERVAL 14 day = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index a4195af29c..ad5a2327e4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/DuckDB/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( subq_10.metric_time__day - INTERVAL 5 day = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 31ebdbb9f8..65ef4ced5c 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/DuckDB/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + subq_28.ds - INTERVAL 14 day = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - subq_28.ds - INTERVAL 14 day = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql index 475bffcecf..179509146e 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(isodow FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day - GROUP BY - EXTRACT(isodow FROM subq_22.ds) - ) subq_26 + EXTRACT(isodow FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + subq_22.ds - INTERVAL 14 day = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(isodow FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index a13ee4f4ce..a1c6798f31 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_window_metric__plan0_optimized.sql index 78923e04e6..661746b7ae 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/DuckDB/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ts - INTERVAL 1 hour = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 4c4a00d3b6..f078bb7bdc 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql index 71eb89003a..71dbda015a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 53ce7c915d..fa3df4c0a3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_multi_hop__plan0_optimized.sql index df66df82d3..b02558c9c9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_single_hop__plan0_optimized.sql index a0ea887e54..8930684618 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql index b0ce0b1a18..5e8fd4ac18 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 6493e6cbc1..5ee18957aa 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index ffeb920a92..511ac91aa9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index 8f88a88e44..49befe8b76 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 7c9a745ff7..fb894d16be 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 5bf661c2aa..db5d4f905b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index c514aae8c2..f102e8dddf 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/DuckDB/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: DuckDB --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index d6e070aa73..69aa23df2f 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/DuckDB/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 7c8a701bb2..b88aa630f6 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 353a58c7db..514627ae39 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + subq_46.ds - INTERVAL 14 day = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - subq_46.ds - INTERVAL 14 day = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql index e8368747af..c0576e9183 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_37.ds - INTERVAL 14 day = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - subq_37.ds - INTERVAL 14 day = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 766a90fea5..77a7b3e421 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/DuckDB/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint__plan0_optimized.sql index f67cf2b467..8668685745 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 9af38ae993..241743c847 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 54f5716b04..9c52b35974 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: DuckDB --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index bff861f120..447f719ab5 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: DuckDB --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 22bd2f889f..850909e634 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/DuckDB/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/DuckDB/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/DuckDB/test_id_enumeration__query.sql index dd9cd665b7..c9b5eb5984 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/DuckDB/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/DuckDB/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: DuckDB --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) From 144f1b4cad9364409b3da340a66fef2f7a91dba0 Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Tue, 12 Nov 2024 22:29:46 -0800 Subject: [PATCH 8/8] Update snapshots for other SQL engines. --- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_non_default_grains__plan0_optimized.sql | 84 +++--- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 23 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- ...th_custom_granularity__plan0_optimized.sql | 29 +- ...th_custom_granularity__plan0_optimized.sql | 25 +- ...th_custom_granularity__plan0_optimized.sql | 28 +- ...ilter_not_in_group_by__plan0_optimized.sql | 28 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 23 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- .../test_combined_metrics_plan__ep_0.xml | 162 ++++++----- ...test_small_combined_metrics_plan__ep_0.xml | 25 +- ...tiple_semantic_models__plan0_optimized.sql | 146 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ...tiple_semantic_models__plan0_optimized.sql | 150 +++++----- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 95 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 109 ++++---- ...grain_and_granularity__plan0_optimized.sql | 111 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 109 ++++---- ...indow_and_granularity__plan0_optimized.sql | 109 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 119 ++++---- ...grain_and_granularity__plan0_optimized.sql | 121 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 117 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 89 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 67 +++-- ...nested_derived_metric__plan0_optimized.sql | 132 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 87 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 109 ++++---- ...fferent_granularities__plan0_optimized.sql | 121 ++++---- ...ultiple_granularities__plan0_optimized.sql | 127 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 109 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._with_time_constraint__plan0_optimized.sql | 28 +- .../test_derived_metric__plan0_optimized.sql | 29 +- ..._different_components__plan0_optimized.sql | 97 ++++--- ...ion_and_offset_window__plan0_optimized.sql | 28 +- ..._with_offset_to_grain__plan0_optimized.sql | 111 ++++---- ...grain_and_granularity__plan0_optimized.sql | 113 ++++---- ...ic_with_offset_window__plan0_optimized.sql | 111 ++++---- ...indow_and_granularity__plan0_optimized.sql | 111 ++++---- ...w_and_offset_to_grain__plan0_optimized.sql | 121 ++++---- ...grain_and_granularity__plan0_optimized.sql | 123 ++++---- ...indow_and_time_filter__plan0_optimized.sql | 119 ++++---- ...set_cumulative_metric__plan0_optimized.sql | 28 +- ...ric_with_agg_time_dim__plan0_optimized.sql | 91 +++--- ...with_one_input_metric__plan0_optimized.sql | 28 +- ...ulti_metric_fill_null__plan0_optimized.sql | 69 +++-- ...nested_derived_metric__plan0_optimized.sql | 134 +++++---- ...nstraint_not_selected__plan0_optimized.sql | 118 ++++---- ...ultiple_input_metrics__plan0_optimized.sql | 140 ++++++---- ...ls_without_time_spine__plan0_optimized.sql | 72 +++-- ...me_spine_multi_metric__plan0_optimized.sql | 89 +++--- .../test_nested_filters__plan0_optimized.sql | 133 +++++---- .../test_nested_offsets__plan0_optimized.sql | 91 +++--- ..._with_time_constraint__plan0_optimized.sql | 99 ++++--- ...with_where_constraint__plan0_optimized.sql | 105 ++++--- ...fferent_granularities__plan0_optimized.sql | 28 +- ...ultiple_granularities__plan0_optimized.sql | 36 ++- ...ain_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ...fferent_granularities__plan0_optimized.sql | 123 ++++---- ...ultiple_granularities__plan0_optimized.sql | 133 +++++---- ...dow_with_agg_time_dim__plan0_optimized.sql | 111 ++++---- ..._with_time_constraint__plan0_optimized.sql | 28 +- ..._for_one_input_metric__plan0_optimized.sql | 142 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ..._for_one_input_metric__plan0_optimized.sql | 144 ++++++---- ...window_with_date_part__plan0_optimized.sql | 109 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...window_with_date_part__plan0_optimized.sql | 111 ++++---- ...ffset_to_grain_metric__plan0_optimized.sql | 28 +- ..._offset_window_metric__plan0_optimized.sql | 28 +- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 140 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...del_as_queried_metric__plan0_optimized.sql | 68 ++--- ...ith_conversion_metric__plan0_optimized.sql | 60 ++-- ...s_local_entity_prefix__plan0_optimized.sql | 75 ++--- ...inner_query_multi_hop__plan0_optimized.sql | 91 +++--- ...nner_query_single_hop__plan0_optimized.sql | 81 +++--- ...ic_filtered_by_itself__plan0_optimized.sql | 68 ++--- ...etric_in_where_filter__plan0_optimized.sql | 88 +++--- ...etric_in_where_filter__plan0_optimized.sql | 77 ++--- ...etric_in_where_filter__plan0_optimized.sql | 142 +++++----- ...ple_metrics_in_filter__plan0_optimized.sql | 109 ++++---- ...etric_in_where_filter__plan0_optimized.sql | 85 +++--- ...etric_in_where_filter__plan0_optimized.sql | 73 ++--- ...metric_time_dimension__plan0_optimized.sql | 23 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...metric_time_dimension__plan0_optimized.sql | 25 +- ...categorical_dimension__plan0_optimized.sql | 93 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 188 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 177 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...categorical_dimension__plan0_optimized.sql | 95 ++++--- ...ic_predicate_pushdown__plan0_optimized.sql | 263 ++++++++++-------- ...th_query_time_filters__plan0_optimized.sql | 192 +++++++------ ...tric_joins_and_filter__plan0_optimized.sql | 181 ++++++------ ...st_measure_constraint__plan0_optimized.sql | 125 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 93 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- ...st_measure_constraint__plan0_optimized.sql | 127 +++++---- ...t_with_reused_measure__plan0_optimized.sql | 95 ++++--- ...single_expr_and_alias__plan0_optimized.sql | 28 +- ...sources_no_dimensions__plan0_optimized.sql | 28 +- ...metrics_no_dimensions__plan0_optimized.sql | 19 +- .../BigQuery/test_id_enumeration__query.sql | 23 +- .../Databricks/test_id_enumeration__query.sql | 25 +- .../Postgres/test_id_enumeration__query.sql | 25 +- .../Redshift/test_id_enumeration__query.sql | 25 +- .../Snowflake/test_id_enumeration__query.sql | 25 +- .../str/Trino/test_id_enumeration__query.sql | 25 +- 396 files changed, 19886 insertions(+), 14216 deletions(-) diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index 1548db4681..06c7e2c0de 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATETIME_TRUNC(subq_13.ds, isoweek) AS metric_time__week + , SUM(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) + ) + GROUP BY + metric_time__day + , metric_time__week + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATETIME_TRUNC(subq_13.ds, isoweek) AS metric_time__week - , SUM(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) - ) - GROUP BY - metric_time__day - , metric_time__week - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index 2c2a13232e..db41ff9b91 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index d43053690a..2d16aaefe6 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index d0ce2c8ec8..7470b53720 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index b0eac1b40c..d16b558aaf 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql index f5070e701c..c783f912b8 100644 --- a/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_cumulative_metric_rendering.py/SqlQueryPlan/Trino/test_derived_cumulative_metric_with_non_default_grains__plan0_optimized.sql @@ -4,44 +4,60 @@ docstring: Test querying a derived metric with a cumulative input metric using non-default grains. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , t2mr - 10 AS trailing_2_months_revenue_sub_10 -FROM ( - -- Re-aggregate Metric via Group By +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , txn_revenue AS t2mr + FROM ( + -- Join Self Over Time Range + -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] + -- Aggregate Measures + SELECT + subq_13.ds AS metric_time__day + , DATE_TRUNC('week', subq_13.ds) AS metric_time__week + , SUM(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) + ) + GROUP BY + subq_13.ds + , DATE_TRUNC('week', subq_13.ds) + ) subq_16 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__week - , t2mr + , t2mr - 10 AS trailing_2_months_revenue_sub_10 FROM ( - -- Compute Metrics via Expressions - -- Window Function for Metric Re-aggregation + -- Re-aggregate Metric via Group By SELECT metric_time__week - , AVG(txn_revenue) OVER (PARTITION BY metric_time__week) AS t2mr + , t2mr FROM ( - -- Join Self Over Time Range - -- Pass Only Elements: ['txn_revenue', 'metric_time__week', 'metric_time__day'] - -- Aggregate Measures + -- Read From CTE For node_id=cm_4 + -- Window Function for Metric Re-aggregation SELECT - subq_13.ds AS metric_time__day - , DATE_TRUNC('week', subq_13.ds) AS metric_time__week - , SUM(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) - ) - GROUP BY - subq_13.ds - , DATE_TRUNC('week', subq_13.ds) - ) subq_16 - ) subq_18 - GROUP BY - metric_time__week - , t2mr -) subq_19 + metric_time__week + , AVG(t2mr) OVER (PARTITION BY metric_time__week) AS t2mr + FROM cm_4_cte cm_4_cte + ) subq_18 + GROUP BY + metric_time__week + , t2mr + ) subq_19 +) + +SELECT + metric_time__week AS metric_time__week + , trailing_2_months_revenue_sub_10 AS trailing_2_months_revenue_sub_10 +FROM cm_5_cte cm_5_cte 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 index 5608d1dfa6..9fcbb5bd73 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATETIME_TRUNC(bookings_source_src_28000.ds, day) = subq_14.ds GROUP BY booking__ds__martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index ec57d54bfb..9186c36934 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: BigQuery --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY metric_time__martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY metric_time__martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY metric_time__martian_day diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql index ac6a6b2162..2a8075f5fc 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY booking__ds__martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 4239dbf288..75c6792e98 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/BigQuery/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte 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 index 33b3c5091a..5b65919082 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index 38f2bc7489..55b66c93e5 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Databricks --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql index cc3c8aeff5..0f427c8e5f 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e4cb94adb0..3ec93b2454 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Databricks/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte 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 index 22a163a260..99a8724927 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index 88f0d5e65a..60e6195237 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Postgres --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 1a03d0f71a..b53cdf5ace 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e5cb95f9b5..212108b242 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Postgres/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte 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 index 0faeff60bc..6410848b96 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index 1c8b925d26..5a4041fd6f 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Redshift --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 70b91bc6a4..2ee8a27911 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 40e9bafc5b..4f63900d9e 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Redshift/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte 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 index dadf4178a1..efa5cd4e32 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index d4164aaf0e..8691ac005d 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Snowflake --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 8d106e8686..cb0db8ef7d 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index e86c0510f4..49755bcde3 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Snowflake/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte 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 index 0396ee4003..22c615b5f4 100644 --- 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 @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , booking_value * 0.05 / bookers AS booking_fees_per_booker -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['booking_value', 'bookers', 'booking__ds__martian_day'] @@ -23,4 +20,24 @@ FROM ( DATE_TRUNC('day', bookings_source_src_28000.ds) = subq_14.ds GROUP BY subq_14.martian_day -) subq_18 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , booking_value * 0.05 / bookers AS booking_fees_per_booker + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + booking__ds__martian_day + , booking_value + , bookers + FROM cm_8_cte cm_8_cte + ) subq_18 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , booking_fees_per_booker AS booking_fees_per_booker +FROM cm_9_cte cm_9_cte 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 index 8f00e938d9..cfbbfc89b5 100644 --- 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 @@ -3,11 +3,7 @@ test_filename: test_custom_granularity.py sql_engine: Trino --- -- 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 ( +WITH cm_4_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'metric_time__martian_day'] @@ -29,8 +25,9 @@ FROM ( subq_12.ds__day = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 -FULL OUTER JOIN ( +) + +, cm_5_cte AS ( -- Metric Time Dimension 'ds' -- Join to Custom Granularity Dataset -- Pass Only Elements: ['listings', 'metric_time__martian_day'] @@ -52,8 +49,16 @@ FULL OUTER JOIN ( subq_18.ds__day = subq_19.ds GROUP BY subq_19.martian_day -) subq_23 +) + +SELECT + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) AS metric_time__martian_day + , MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +FULL OUTER JOIN + cm_5_cte cm_5_cte ON - subq_17.metric_time__martian_day = subq_23.metric_time__martian_day + cm_4_cte.metric_time__martian_day = cm_5_cte.metric_time__martian_day GROUP BY - COALESCE(subq_17.metric_time__martian_day, subq_23.metric_time__martian_day) + COALESCE(cm_4_cte.metric_time__martian_day, cm_5_cte.metric_time__martian_day) diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql index 4ab73ec738..def146c835 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__martian_day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Join to Custom Granularity Dataset -- Pass Only Elements: ['bookings', 'booking__ds__martian_day'] @@ -32,4 +29,23 @@ FROM ( subq_12.ds = subq_13.ds GROUP BY subq_13.martian_day -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__martian_day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + booking__ds__martian_day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + booking__ds__martian_day AS booking__ds__martian_day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql index 2ed4e1008f..61708b5267 100644 --- a/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_custom_granularity.py/SqlQueryPlan/Trino/test_offset_metric_with_custom_granularity_filter_not_in_group_by__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_offset_metric_with_custom_granularity_filter_not_in_group_by test_filename: test_custom_granularity.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -40,4 +37,23 @@ FROM ( WHERE metric_time__martian_day = '2020-01-01' GROUP BY metric_time__day -) subq_19 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_19 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml index 75da5fd0bb..d06c76969f 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml index 248cf24e49..b368cfbdab 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/BigQuery/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,9 +41,17 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Databricks/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Postgres/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Redshift/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Snowflake/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml index 2d133f4c66..24ab5848f0 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_combined_metrics_plan__ep_0.xml @@ -5,82 +5,90 @@ test_filename: test_dataflow_to_execution.py - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml index 4da40aaf0e..1eff644ad2 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml +++ b/tests_metricflow/snapshots/test_dataflow_to_execution.py/ExecutionPlan/Trino/test_small_combined_metrics_plan__ep_0.xml @@ -17,11 +17,7 @@ test_filename: test_dataflow_to_execution.py - - - - - + @@ -32,8 +28,9 @@ test_filename: test_dataflow_to_execution.py - - + + + @@ -44,10 +41,18 @@ test_filename: test_dataflow_to_execution.py - + + + + + + + + + - + - + diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index dc98ebb10a..12d0bb4a60 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/BigQuery/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: BigQuery --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - ds__day - , listing__country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + ds__day + , listing__country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - ds__day - , listing__country_latest - ) subq_35 + DATETIME_TRUNC(ds, day) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY ds__day , listing__country_latest -) subq_36 +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + ds__day + , listing__country_latest + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 45487cf27c..1b0b0ce043 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Databricks/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Databricks --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index c8d146de34..a03d154451 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Postgres/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Postgres --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 3343128d76..27e3d1b3e6 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Redshift/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Redshift --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index b14f947b5d..1d65694e18 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Snowflake/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Snowflake --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql index 84c74729c8..87893cad79 100644 --- a/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_dataflow_to_sql_plan.py/SqlQueryPlan/Trino/test_compute_metrics_node_ratio_from_multiple_semantic_models__plan0_optimized.sql @@ -4,7 +4,7 @@ docstring: Tests the combine metrics node for ratio type metrics. sql_engine: Trino --- --- Compute Metrics via Expressions +-- Read From CTE For node_id=cm_2 WITH sma_28014_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' @@ -14,76 +14,90 @@ WITH sma_28014_cte AS ( FROM ***************************.dim_listings_latest listings_latest_src_28000 ) -SELECT - ds__day AS ds__day - , listing__country_latest AS listing__country_latest - , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view -FROM ( - -- Combine Aggregated Outputs +, cm_0_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_27.ds__day, subq_35.ds__day) AS ds__day - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) AS listing__country_latest - , MAX(subq_27.bookings) AS bookings - , MAX(subq_35.views) AS views + subq_20.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_20.bookings) AS bookings FROM ( - -- Join Standard Outputs - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_20.bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_20.listing = sma_28014_cte.listing - GROUP BY - subq_20.ds__day - , sma_28014_cte.country_latest - ) subq_27 - FULL OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte + ON + subq_20.listing = sma_28014_cte.listing + GROUP BY + subq_20.ds__day + , sma_28014_cte.country_latest +) + +, cm_1_cte AS ( + -- Join Standard Outputs + -- Pass Only Elements: ['views', 'listing__country_latest', 'ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_29.ds__day AS ds__day + , sma_28014_cte.country_latest AS listing__country_latest + , SUM(subq_29.views) AS views + FROM ( + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - subq_29.ds__day AS ds__day - , sma_28014_cte.country_latest AS listing__country_latest - , SUM(subq_29.views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS ds__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - sma_28014_cte sma_28014_cte - ON - subq_29.listing = sma_28014_cte.listing - GROUP BY - subq_29.ds__day - , sma_28014_cte.country_latest - ) subq_35 + DATE_TRUNC('day', ds) AS ds__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + sma_28014_cte sma_28014_cte ON - ( - subq_27.listing__country_latest = subq_35.listing__country_latest - ) AND ( - subq_27.ds__day = subq_35.ds__day - ) + subq_29.listing = sma_28014_cte.listing GROUP BY - COALESCE(subq_27.ds__day, subq_35.ds__day) - , COALESCE(subq_27.listing__country_latest, subq_35.listing__country_latest) -) subq_36 + subq_29.ds__day + , sma_28014_cte.country_latest +) + +, cm_2_cte AS ( + -- Compute Metrics via Expressions + SELECT + ds__day + , listing__country_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) AS ds__day + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.views) AS views + FROM cm_0_cte cm_0_cte + FULL OUTER JOIN + cm_1_cte cm_1_cte + ON + ( + cm_0_cte.listing__country_latest = cm_1_cte.listing__country_latest + ) AND ( + cm_0_cte.ds__day = cm_1_cte.ds__day + ) + GROUP BY + COALESCE(cm_0_cte.ds__day, cm_1_cte.ds__day) + , COALESCE(cm_0_cte.listing__country_latest, cm_1_cte.listing__country_latest) + ) subq_36 +) + +SELECT + ds__day AS ds__day + , listing__country_latest AS listing__country_latest + , bookings_per_view AS bookings_per_view +FROM cm_2_cte cm_2_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index f3a60c71ea..64a2c46ed4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql index b50107ee67..d9c4c7c434 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 9269668a09..6796112fab 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: BigQuery --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY booking__is_instant LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 787df638cf..d5154984b8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATE_SUB(CAST(subq_10.metric_time__month AS DATETIME), INTERVAL 1 month) = DATETIME_TRUNC(monthly_bookings_source_src_16000.ds, month) GROUP BY metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index a3b05090c8..ee67d97e49 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day - GROUP BY - metric_time__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day GROUP BY metric_time__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 69b5a61e8d..a88e1e0846 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATETIME_TRUNC(ds, isoweek) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATETIME_TRUNC(subq_22.ds, isoweek) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day - WHERE DATETIME_TRUNC(subq_22.ds, isoweek) = subq_22.ds - GROUP BY - metric_time__week - ) subq_26 + DATETIME_TRUNC(ds, isoweek) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_22.ds, isoweek) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATETIME_TRUNC(subq_22.ds, month) = subq_20.metric_time__day + WHERE DATETIME_TRUNC(subq_22.ds, isoweek) = subq_22.ds GROUP BY metric_time__week -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + metric_time__week + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql index fa0b0ca3bc..7bad057b52 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index cc6350e6d2..a814257dd7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATETIME_TRUNC(ds, quarter) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATETIME_TRUNC(subq_22.ds, quarter) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__quarter - ) subq_26 + DATETIME_TRUNC(ds, quarter) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_22.ds, quarter) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__quarter -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + metric_time__quarter + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index a0e830b694..0252d5465e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day - GROUP BY - metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day - GROUP BY - metric_time__day - ) subq_32 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day GROUP BY metric_time__day -) subq_33 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 8f7ccace52..c7bcad7917 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(subq_20.ds, year) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATETIME_TRUNC(subq_20.ds, year) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day - WHERE DATETIME_TRUNC(subq_20.ds, year) = subq_20.ds - GROUP BY - metric_time__year - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATETIME_TRUNC(subq_20.ds, month) = subq_18.metric_time__day + WHERE DATETIME_TRUNC(subq_20.ds, year) = subq_20.ds + GROUP BY + metric_time__year +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(subq_28.ds, year) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATETIME_TRUNC(subq_28.ds, year) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day - GROUP BY - metric_time__year - ) subq_32 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 1 month) = subq_26.metric_time__day GROUP BY metric_time__year -) subq_33 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + metric_time__year + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 5fc91ceab3..bdf6f5f3a2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATETIME_TRUNC(ds, day) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATE_SUB(CAST(subq_25.ds AS DATETIME), INTERVAL 14 day) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATE_SUB(CAST(subq_25.ds AS DATETIME), INTERVAL 14 day) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - metric_time__day -) subq_31 + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql index b66ab403f2..14bde3724d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 2 day) = subq_15.metric_time__day GROUP BY metric_time__day -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 3e333677d4..ef48d6bdd7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + booking__ds__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - GROUP BY - booking__ds__day - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY booking__ds__day - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - booking__ds__day -) subq_27 + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index fcc4c15019..8fa6db602b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_SUB(CAST(subq_11.ds AS DATETIME), INTERVAL 5 day) = subq_9.metric_time__day GROUP BY metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql index efc35be36d..ced7070ea8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql index c377f1c476..db3ca0a637 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY metric_time__day -) subq_34 +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 14f557c69d..58961367a8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 5 day) = subq_15.metric_time__day + GROUP BY metric_time__day - , bookings_offset_once + , booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] + SELECT + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 5 day) = subq_15.metric_time__day - GROUP BY - metric_time__day - , booking__is_instant - ) subq_21 - ) subq_22 - ON - DATE_SUB(CAST(subq_24.ds AS DATETIME), INTERVAL 2 day) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_24.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 07eb7ffae8..204e2e60c5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_20 - ) subq_21 - ON - DATETIME_TRUNC(subq_23.ds, month) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - metric_time__day -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATETIME_TRUNC(subq_23.ds, month) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index d915f03038..cf5dac3881 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 276d3da122..70090c447f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql index 0ba29d9919..d04cc125ed 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql index 14d59bc467..336c997aa3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- 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 - ON - DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day - GROUP BY - metric_time__day - ) subq_19 - ) subq_20 + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 2 day) = subq_20.metric_time__day -) subq_23 + DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql index f5654357b9..78505ec3f9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- 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 - ON - DATE_SUB(CAST(subq_15.ds AS DATETIME), INTERVAL 5 day) = subq_13.metric_time__day - GROUP BY - metric_time__day - ) subq_19 - ) subq_20 - ON - DATE_SUB(CAST(subq_21.metric_time__day AS DATETIME), INTERVAL 2 day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_21.metric_time__day AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 974fba1880..d2b729fa7d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- 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_14 + ON + DATE_SUB(CAST(subq_16.ds AS DATETIME), INTERVAL 5 day) = subq_14.metric_time__day + GROUP BY metric_time__day - , bookings_offset_once +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Constrain Output with WHERE + SELECT + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- 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_14 - ON - DATE_SUB(CAST(subq_16.ds AS DATETIME), INTERVAL 5 day) = subq_14.metric_time__day - GROUP BY - metric_time__day - ) subq_20 - ) subq_21 - ON - DATE_SUB(CAST(subq_23.ds AS DATETIME), INTERVAL 2 day) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_SUB(CAST(subq_23.ds AS DATETIME), INTERVAL 2 day) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 9b21410866..ad0abbefe7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 039f084b2c..64ae5c216d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( metric_time__day , metric_time__month , metric_time__year -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index fb74605744..cf7b8b2be9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATETIME_TRUNC(subq_22.ds, month) = subq_20.booking__ds__day - GROUP BY - booking__ds__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATETIME_TRUNC(subq_22.ds, month) = subq_20.booking__ds__day GROUP BY booking__ds__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + booking__ds__day + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 73536fe94b..7e4989455b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATETIME_TRUNC(subq_19.ds, month) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATE_SUB(CAST(subq_19.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATETIME_TRUNC(subq_19.ds, month) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_19.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , DATETIME_TRUNC(ds, month) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATETIME_TRUNC(ds, day) AS metric_time__day + , DATETIME_TRUNC(ds, month) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_31 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + metric_time__month + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index a913e59a6b..283e3cb8e7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATETIME_TRUNC(subq_17.ds, month) AS metric_time__month + , DATETIME_TRUNC(subq_17.ds, year) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) + GROUP BY + metric_time__day + , metric_time__month + , metric_time__year +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + DATETIME_TRUNC(ds, day) AS metric_time__day + , DATETIME_TRUNC(ds, month) AS metric_time__month + , DATETIME_TRUNC(ds, year) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day + , metric_time__month + , metric_time__year +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATETIME_TRUNC(subq_17.ds, month) AS metric_time__month - , DATETIME_TRUNC(subq_17.ds, year) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_SUB(CAST(subq_17.ds AS DATETIME), INTERVAL 1 week) = DATETIME_TRUNC(bookings_source_src_28000.ds, day) - GROUP BY - metric_time__day - , metric_time__month - , metric_time__year - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , DATETIME_TRUNC(ds, month) AS metric_time__month - , DATETIME_TRUNC(ds, year) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY metric_time__day , metric_time__month , metric_time__year - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - metric_time__day - , metric_time__month - , metric_time__year -) subq_27 + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 201002c96a..23c7fc5953 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.booking__ds__day - GROUP BY - booking__ds__day - ) subq_26 + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.booking__ds__day GROUP BY booking__ds__day -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + booking__ds__day + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index af157170a7..1c17fa88dd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/BigQuery/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATE_SUB(CAST(subq_10.metric_time__day AS DATETIME), INTERVAL 5 day) = subq_9.metric_time__day GROUP BY metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 70087237b8..db6f04e801 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql index a8fb9b7986..205ca69925 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 456fd4087d..471e57d5f4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Databricks --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 6c0fe0c3ea..95a98e6aae 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 1e504f0202..ba7c890c90 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 0422c77451..b7ccc6fc62 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql index cf81e784d1..7921c172f8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index f38cb7d2b9..c4c65cd1dc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index be9642ae8d..e0a5b0cdd3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 55bf083f45..4392fcc65c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index f5faf4483c..310e9489f6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql index 46f8a04db2..9c62a830f7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 78ae26a9ed..9b1d0d1661 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index ef9112432e..59e7df52a0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql index e962df7e34..243527b915 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql index 25edde45c3..393f08d959 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 6e2e83b479..704ae2b354 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 3c7b7bd4bd..64e71d72f7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index dc2f86e8b0..5c219c409f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 7848e3fc5d..e48abb3542 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql index 1e23fe4060..fefcc1afc8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql index afed1816e7..1f249ad766 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql index 49e0d1fdd1..ba82d841e3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql index ec6b4352f0..2e79d6bdda 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index a45feb0355..bf0f4dcbd5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 5b6603fdff..bba6b89a43 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index c76a35a7af..064ce82fdc 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 0acbd8cac9..7346ae28c5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 6d98d432d7..5a85516eab 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 38008ad1e4..09dd1173b0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 670d1cb051..fe448492a2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Databricks/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 97593fc289..038fe99c2d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql index d5922afa29..21ba47a49a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 46c9cc3be0..2a3c633652 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Postgres --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 0a45836259..d6fd2f7018 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( subq_10.metric_time__month - MAKE_INTERVAL(months => 1) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index ece4b86e47..17fd879cad 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 32cb49a8ee..fe17b6f084 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql index 9e88eaf124..e1f2a2ebec 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 53a9fe09c8..2fdf14c915 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index 97deaf2d85..6f177f3226 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index d5a61f7a89..102817762c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + subq_28.ds - MAKE_INTERVAL(months => 1) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 2f0e94a1b2..1c8bb796d3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + subq_25.ds - MAKE_INTERVAL(days => 14) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - subq_25.ds - MAKE_INTERVAL(days => 14) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql index 1329a0ade5..fdfd1fbf74 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( subq_17.ds - MAKE_INTERVAL(days => 2) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 2f6371d3fe..8705426350 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index 814f94c4a2..80402d2c3c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ds - MAKE_INTERVAL(days => 5) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql index 49134d7ee0..5472cc1409 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql index a34abf65ef..7e0844f028 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index c11fef9c79..6ad0683da9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + subq_17.ds - MAKE_INTERVAL(days => 5) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - subq_17.ds - MAKE_INTERVAL(days => 5) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - subq_24.ds - MAKE_INTERVAL(days => 2) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_24.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 2f8d1a2c5c..c5fb7d9c2a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index 1811e2491b..9aeff14b78 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 78486ed3b1..825f2815e9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql index 1f99c4edaa..3c7e2f885e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql index ba241866de..0e33a44f58 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - subq_22.ds - MAKE_INTERVAL(days => 2) = subq_20.metric_time__day -) subq_23 + subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_22.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql index 8121928444..20500ef9fd 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - subq_15.ds - MAKE_INTERVAL(days => 5) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - subq_21.metric_time__day - MAKE_INTERVAL(days => 2) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_21.metric_time__day - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 0aa067052a..0112c4e04e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + subq_16.ds - MAKE_INTERVAL(days => 5) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - subq_16.ds - MAKE_INTERVAL(days => 5) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - subq_23.ds - MAKE_INTERVAL(days => 2) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + subq_23.ds - MAKE_INTERVAL(days => 2) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 52f737090e..a53c0c5bc9 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 321cb4ac6d..a8b9acee1e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index e102ca8708..31bab5f6fb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 3934f7c7ba..4ac58c701f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - subq_19.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_19.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 34c12c7bd7..1b7a0b5332 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - subq_17.ds - MAKE_INTERVAL(weeks => 1) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql index 9d3549dd51..2971e204e4 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 904c87a361..a7b8633cca 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Postgres/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( subq_10.metric_time__day - MAKE_INTERVAL(days => 5) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index ac52eb2c8e..639f902e43 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql index 49941151dd..cd85475395 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 71f376db75..755c05e5ec 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Redshift --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index d755bb7d56..42871e8a07 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 464cffe605..e14d687035 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index d9147b7bdb..1211ddea4f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql index cfa62f92ad..d0b975f190 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index b6bc0d71d8..301c8b681c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index f23d4f512a..a2109348a8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index c0e2a731ed..db6b2a9db1 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index 3986620849..d25592a922 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql index 28071a31e6..399cdd7982 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 67fe080a8b..d6fcd75eae 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index 6b9a08d0b2..2b25b163ac 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql index 3fcf40b4d1..7e26643bad 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql index 626212607e..678c373c44 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 606a9fa315..e634f820a7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 38559a01ae..d928c43113 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index 305efccf56..cd299bf77a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index 06a0d6ccc7..a293efeaf5 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql index 0cb09da107..154186c24f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql index 858599a4b1..841859aa4b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql index fef5b71647..0f74441d68 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql index 3d6418dab3..72096c8944 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index e2ef45757b..1bb3519f60 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index e47d7558aa..34b6dc841f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 3aefd9bee6..cf445da590 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 2608a60bae..bf3d689ff7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 22753736dd..21a5403b95 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql index c0f2748032..a23a8a2eb0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index c855be0bd8..b219490368 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Redshift/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 9aae377389..f7390f1cd3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN '2019-12-19' AND '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql index bafa9600fa..600be0f69b 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 024a2779d9..91cae16e37 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Snowflake --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 7b31416074..839d622087 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATEADD(month, -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index f61138cd39..a054addd3e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 9d2b8b9ae0..602dd5f5ec 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql index ce55aa2953..4789c9e00c 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index 8b3679742b..45961a46e1 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index a19241a8b9..4f95d5e7d7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index b8eb112e70..e070709ed7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATEADD(month, -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index f8f10fb732..649e6b95f1 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATEADD(day, -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql index 3776496f57..3e53cd4d0e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATEADD(day, -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index 88b862779c..18d8866cf6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index a603ed6aaf..b1cef9e33d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(day, -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql index ca0e3a9979..5a09db6920 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql index f48b4f9097..a68802f105 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 9388c1fcc3..f5bba9bd43 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATEADD(day, -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATEADD(day, -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 58da2a3cca..4dec1d1bcf 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index a123a96e51..37ade18998 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index ff990f19ba..dd51fae909 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql index c070ddafdf..9011bf50cb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql index f750dc6487..37be9a8fed 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATEADD(day, -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql index dc5ec3de81..86a384f136 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATEADD(day, -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATEADD(day, -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN '2020-01-12' AND '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql index d23fbf4a14..dabae48a72 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATEADD(day, -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATEADD(day, -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATEADD(day, -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 2fec95707b..2df51cb205 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index d270f22808..6c1c35fbe3 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index cc5e1978fc..c8831345b6 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 28159c1513..478f5d5094 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 1e34609202..1815f1c489 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATEADD(week, -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql index bc37fe7312..ca63443fb8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATEADD(day, -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index d1eb7907e5..87c1610977 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Snowflake/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATEADD(day, -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql index 2026fd215e..57d0ce2a87 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_cumulative_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_cumulative_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Constrain Time Range to [2019-12-19T00:00:00, 2020-01-02T00:00:00] -- Pass Only Elements: ['bookers', 'metric_time__day'] @@ -42,4 +39,23 @@ FROM ( WHERE subq_17.metric_time__day BETWEEN timestamp '2019-12-19' AND timestamp '2020-01-02' GROUP BY subq_17.metric_time__day -) subq_23 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql index 3521bdd8f5..6d0092b912 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct -FROM ( +-- Read From CTE For node_id=cm_9 +WITH cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -25,4 +22,24 @@ FROM ( ) subq_13 GROUP BY metric_time__day -) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred_bookings_pct + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , ref_bookings + , bookings + FROM cm_8_cte cm_8_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , non_referred_bookings_pct AS non_referred_bookings_pct +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql index 3a8340d23e..382b266e7f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_that_defines_the_same_alias_in_different_components__plan0_optimized.sql @@ -6,60 +6,81 @@ sql_engine: Trino --- -- Combine Aggregated Outputs -- Order By [] Limit 1 -SELECT - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) AS booking__is_instant - , MAX(subq_18.derived_shared_alias_1a) AS derived_shared_alias_1a - , MAX(subq_24.derived_shared_alias_2) AS derived_shared_alias_2 -FROM ( +WITH cm_8_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + GROUP BY + booking__is_instant +) + +, cm_9_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias - 10 AS derived_shared_alias_1a FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_8 SELECT booking__is_instant - , SUM(bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_8_cte cm_8_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_10_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + booking__is_instant + , SUM(instant_bookings) AS shared_alias + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] + SELECT + is_instant AS booking__is_instant + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_21 + GROUP BY + booking__is_instant +) + +, cm_11_cte AS ( -- Compute Metrics via Expressions SELECT booking__is_instant , shared_alias + 10 AS derived_shared_alias_2 FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read From CTE For node_id=cm_10 SELECT booking__is_instant - , SUM(instant_bookings) AS shared_alias - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'booking__is_instant'] - SELECT - is_instant AS booking__is_instant - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_21 - GROUP BY - booking__is_instant + , shared_alias + FROM cm_10_cte cm_10_cte ) subq_23 -) subq_24 +) + +SELECT + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) AS booking__is_instant + , MAX(cm_9_cte.derived_shared_alias_1a) AS derived_shared_alias_1a + , MAX(cm_11_cte.derived_shared_alias_2) AS derived_shared_alias_2 +FROM cm_9_cte cm_9_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.booking__is_instant = subq_24.booking__is_instant + cm_9_cte.booking__is_instant = cm_11_cte.booking__is_instant GROUP BY - COALESCE(subq_18.booking__is_instant, subq_24.booking__is_instant) + COALESCE(cm_9_cte.booking__is_instant, cm_11_cte.booking__is_instant) LIMIT 1 diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql index 88c79149c8..5b1a9cf3fa 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_month_dimension_and_offset_window__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_metric_with_month_dimension_and_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_last_month AS bookings_last_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings_monthly', 'metric_time__month'] -- Aggregate Measures @@ -28,4 +25,23 @@ FROM ( DATE_ADD('month', -1, subq_10.metric_time__month) = DATE_TRUNC('month', monthly_bookings_source_src_16000.ds) GROUP BY subq_10.metric_time__month -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_last_month AS bookings_last_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_last_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_last_month AS bookings_last_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql index 7c14527adb..a92457cfcb 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql index 20579c6291..2c325cacf2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,59 +2,72 @@ test_name: test_derived_metric_with_offset_to_grain_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__week - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) AS metric_time__week - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + metric_time__week + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__week - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__week'] - SELECT - DATE_TRUNC('week', ds) AS metric_time__week - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__week - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__week'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('week', subq_22.ds) AS metric_time__week - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day - WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds - GROUP BY - DATE_TRUNC('week', subq_22.ds) - ) subq_26 + DATE_TRUNC('week', ds) AS metric_time__week + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__week +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__week'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('week', subq_22.ds) AS metric_time__week + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__week = subq_26.metric_time__week + DATE_TRUNC('month', subq_22.ds) = subq_20.metric_time__day + WHERE DATE_TRUNC('week', subq_22.ds) = subq_22.ds GROUP BY - COALESCE(subq_18.metric_time__week, subq_26.metric_time__week) -) subq_27 + DATE_TRUNC('week', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__week + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) AS metric_time__week + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__week = cm_7_cte.metric_time__week + GROUP BY + COALESCE(cm_6_cte.metric_time__week, cm_7_cte.metric_time__week) + ) subq_27 +) + +SELECT + metric_time__week AS metric_time__week + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql index 97562a5c6a..363d41e3ee 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS metric_time__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__day = subq_26.metric_time__day + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_26.metric_time__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql index a92f248822..fdf498287f 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_granularity__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_derived_metric_with_offset_window_and_granularity test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__quarter - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) AS metric_time__quarter - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__quarter + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__quarter - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - SELECT - DATE_TRUNC('quarter', ds) AS metric_time__quarter - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__quarter - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__quarter'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - DATE_TRUNC('quarter', subq_22.ds) - ) subq_26 + DATE_TRUNC('quarter', ds) AS metric_time__quarter + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__quarter +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__quarter'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('quarter', subq_22.ds) AS metric_time__quarter + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__quarter = subq_26.metric_time__quarter + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__quarter, subq_26.metric_time__quarter) -) subq_27 + DATE_TRUNC('quarter', subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__quarter + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) AS metric_time__quarter + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__quarter = cm_7_cte.metric_time__quarter + GROUP BY + COALESCE(cm_6_cte.metric_time__quarter, cm_7_cte.metric_time__quarter) + ) subq_27 +) + +SELECT + metric_time__quarter AS metric_time__quarter + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql index 320c34869a..3e924f48f0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain__plan0_optimized.sql @@ -2,62 +2,75 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_20.ds AS metric_time__day + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_20.ds AS metric_time__day - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - GROUP BY - subq_20.ds - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + GROUP BY + subq_20.ds +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - subq_28.ds - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__day = subq_32.metric_time__day + DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql index 376523e669..68a5cf6301 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_offset_to_grain_and_granularity__plan0_optimized.sql @@ -2,63 +2,76 @@ test_name: test_derived_metric_with_offset_window_and_offset_to_grain_and_granul test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__year - , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) AS metric_time__year - , MAX(subq_24.month_start_bookings) AS month_start_bookings - , MAX(subq_32.bookings_1_month_ago) AS bookings_1_month_ago - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('year', subq_20.ds) AS metric_time__year + , SUM(subq_18.bookings) AS month_start_bookings + FROM ***************************.mf_time_spine subq_20 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_20.ds) AS metric_time__year - , SUM(subq_18.bookings) AS month_start_bookings - FROM ***************************.mf_time_spine subq_20 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_18 - ON - DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day - WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds - GROUP BY - DATE_TRUNC('year', subq_20.ds) - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_18 + ON + DATE_TRUNC('month', subq_20.ds) = subq_18.metric_time__day + WHERE DATE_TRUNC('year', subq_20.ds) = subq_20.ds + GROUP BY + DATE_TRUNC('year', subq_20.ds) +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('year', subq_28.ds) AS metric_time__year + , SUM(subq_26.bookings) AS bookings_1_month_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - DATE_TRUNC('year', subq_28.ds) AS metric_time__year - , SUM(subq_26.bookings) AS bookings_1_month_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - ON - DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day - GROUP BY - DATE_TRUNC('year', subq_28.ds) - ) subq_32 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 ON - subq_24.metric_time__year = subq_32.metric_time__year + DATE_ADD('month', -1, subq_28.ds) = subq_26.metric_time__day GROUP BY - COALESCE(subq_24.metric_time__year, subq_32.metric_time__year) -) subq_33 + DATE_TRUNC('year', subq_28.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__year + , month_start_bookings - bookings_1_month_ago AS bookings_month_start_compared_to_1_month_prior + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.month_start_bookings) AS month_start_bookings + , MAX(cm_7_cte.bookings_1_month_ago) AS bookings_1_month_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + GROUP BY + COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_33 +) + +SELECT + metric_time__year AS metric_time__year + , bookings_month_start_compared_to_1_month_prior AS bookings_month_start_compared_to_1_month_prior +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql index deaef78743..b5ed9b0f81 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_metric_with_offset_window_and_time_filter__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_derived_metric_with_offset_window_and_time_filter test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_17 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_21.bookings) AS bookings - , MAX(subq_30.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( + subq_25.ds AS metric_time__day + , subq_23.bookings AS bookings + FROM ***************************.mf_time_spine subq_25 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT DATE_TRUNC('day', ds) AS metric_time__day , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_17 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' - GROUP BY - metric_time__day - ) subq_21 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + ) subq_23 + ON + DATE_ADD('day', -14, subq_25.ds) = subq_23.metric_time__day + ) subq_26 + WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_25.ds AS metric_time__day - , subq_23.bookings AS bookings - FROM ***************************.mf_time_spine subq_25 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - ON - DATE_ADD('day', -14, subq_25.ds) = subq_23.metric_time__day - ) subq_26 - WHERE metric_time__day = '2020-01-01' or metric_time__day = '2020-01-14' + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day GROUP BY - metric_time__day - ) subq_30 - ON - subq_21.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_21.metric_time__day, subq_30.metric_time__day) -) subq_31 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql index c9431a6e52..6cec2c51f0 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_cumulative_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_cumulative_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookers', 'metric_time__day'] -- Aggregate Measures @@ -34,4 +31,23 @@ FROM ( DATE_ADD('day', -2, subq_17.ds) = subq_15.metric_time__day GROUP BY subq_17.ds -) subq_21 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , every_2_days_bookers_2_days_ago + FROM cm_4_cte cm_4_cte + ) subq_21 +) + +SELECT + metric_time__day AS metric_time__day + , every_2_days_bookers_2_days_ago AS every_2_days_bookers_2_days_ago +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql index bf41617483..885edc7838 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_agg_time_dim__plan0_optimized.sql @@ -2,47 +2,60 @@ test_name: test_derived_offset_metric_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS booking__ds__day + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + booking__ds__day + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS booking__ds__day - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day GROUP BY - subq_17.ds - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_26 - ON - subq_21.booking__ds__day = subq_26.booking__ds__day - GROUP BY - COALESCE(subq_21.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql index bb78d05bf8..ea8de1996a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_derived_offset_metric_with_one_input_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_derived_offset_metric_with_one_input_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_ADD('day', -5, subq_11.ds) = subq_9.metric_time__day GROUP BY subq_11.ds -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql index 426c0682a5..1a37908020 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_multi_metric_fill_null__plan0_optimized.sql @@ -3,40 +3,45 @@ test_filename: test_derived_metric_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) AS metric_time__day - , MAX(subq_16.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine - , MAX(subq_21.listings) AS listings -FROM ( +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - GROUP BY - metric_time__day - ) subq_14 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 + GROUP BY + metric_time__day + ) subq_14 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_15 -) subq_16 -FULL OUTER JOIN ( +) + +, cm_8_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -53,8 +58,16 @@ FULL OUTER JOIN ( ) subq_19 GROUP BY metric_time__day -) subq_21 +) + +SELECT + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) AS metric_time__day + , MAX(cm_7_cte.twice_bookings_fill_nulls_with_0_without_time_spine) AS twice_bookings_fill_nulls_with_0_without_time_spine + , MAX(cm_8_cte.listings) AS listings +FROM cm_7_cte cm_7_cte +FULL OUTER JOIN + cm_8_cte cm_8_cte ON - subq_16.metric_time__day = subq_21.metric_time__day + cm_7_cte.metric_time__day = cm_8_cte.metric_time__day GROUP BY - COALESCE(subq_16.metric_time__day, subq_21.metric_time__day) + COALESCE(cm_7_cte.metric_time__day, cm_8_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql index d0deaa9a18..a02eddf1e7 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric__plan0_optimized.sql @@ -2,65 +2,87 @@ test_name: test_nested_derived_metric test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_17 +WITH cm_12_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) AS metric_time__day - , MAX(subq_28.non_referred) AS non_referred - , MAX(subq_33.instant) AS instant - , MAX(subq_33.bookings) AS bookings + metric_time__day + , SUM(referred_bookings) AS ref_bookings + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] SELECT - metric_time__day - , (bookings - ref_bookings) * 1.0 / bookings AS non_referred - FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(referred_bookings) AS ref_bookings - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['referred_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - GROUP BY - metric_time__day - ) subq_27 - ) subq_28 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN referrer_id IS NOT NULL THEN 1 ELSE 0 END AS referred_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + GROUP BY + metric_time__day +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , (bookings - ref_bookings) * 1.0 / bookings AS non_referred + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , SUM(instant_bookings) AS instant - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - GROUP BY - metric_time__day - ) subq_33 - ON - subq_28.metric_time__day = subq_33.metric_time__day + , ref_bookings + , bookings + FROM cm_12_cte cm_12_cte + ) subq_27 +) + +, cm_16_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(instant_bookings) AS instant + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['instant_bookings', 'bookings', 'metric_time__day'] + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + , CASE WHEN is_instant THEN 1 ELSE 0 END AS instant_bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 GROUP BY - COALESCE(subq_28.metric_time__day, subq_33.metric_time__day) -) subq_34 + metric_time__day +) + +, cm_17_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , non_referred + (instant * 1.0 / bookings) AS instant_plus_non_referred_bookings_pct + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) AS metric_time__day + , MAX(cm_13_cte.non_referred) AS non_referred + , MAX(cm_16_cte.instant) AS instant + , MAX(cm_16_cte.bookings) AS bookings + FROM cm_13_cte cm_13_cte + FULL OUTER JOIN + cm_16_cte cm_16_cte + ON + cm_13_cte.metric_time__day = cm_16_cte.metric_time__day + GROUP BY + COALESCE(cm_13_cte.metric_time__day, cm_16_cte.metric_time__day) + ) subq_34 +) + +SELECT + metric_time__day AS metric_time__day + , instant_plus_non_referred_bookings_pct AS instant_plus_non_referred_bookings_pct +FROM cm_17_cte cm_17_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql index 8adb624ace..614ec2a121 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_offset_with_joined_where_constraint_not_selected__plan0_optimized.sql @@ -2,57 +2,77 @@ test_name: test_nested_derived_metric_offset_with_joined_where_constraint_not_se test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , subq_15.booking__is_instant AS booking__is_instant + , SUM(subq_15.bookings) AS bookings + FROM ***************************.mf_time_spine subq_17 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_15 + ON + DATE_ADD('day', -5, subq_17.ds) = subq_15.metric_time__day + GROUP BY + subq_17.ds + , subq_15.booking__is_instant +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking__is_instant + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , booking__is_instant + , bookings + FROM cm_6_cte cm_6_cte + ) subq_21 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE + -- Pass Only Elements: ['metric_time__day', 'bookings_offset_once'] SELECT - subq_24.ds AS metric_time__day - , subq_22.booking__is_instant AS booking__is_instant - , subq_22.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_24 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , booking__is_instant - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'booking__is_instant', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_17.ds AS metric_time__day - , subq_15.booking__is_instant AS booking__is_instant - , SUM(subq_15.bookings) AS bookings - FROM ***************************.mf_time_spine subq_17 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_15 - ON - DATE_ADD('day', -5, subq_17.ds) = subq_15.metric_time__day - GROUP BY - subq_17.ds - , subq_15.booking__is_instant - ) subq_21 - ) subq_22 - ON - DATE_ADD('day', -2, subq_24.ds) = subq_22.metric_time__day - ) subq_25 - WHERE booking__is_instant -) subq_27 + subq_24.ds AS metric_time__day + , cm_7_cte.booking__is_instant AS booking__is_instant + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_24 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_24.ds) = cm_7_cte.metric_time__day + ) subq_25 + WHERE booking__is_instant + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql index 0694bbdbd3..c7e77a9c07 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_derived_metric_with_offset_multiple_input_metrics__plan0_optimized.sql @@ -2,65 +2,95 @@ test_name: test_nested_derived_metric_with_offset_multiple_input_metrics test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_14 +WITH cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_11_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) AS metric_time__day - , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month - , MAX(subq_30.booking_fees) AS booking_fees + metric_time__day + , booking_value * 0.05 AS booking_fees_start_of_month FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_10 SELECT - subq_23.ds AS metric_time__day - , subq_21.booking_fees_start_of_month AS booking_fees_start_of_month - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions - SELECT - metric_time__day - , booking_value * 0.05 AS booking_fees_start_of_month - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_20 - ) subq_21 - ON - DATE_TRUNC('month', subq_23.ds) = subq_21.metric_time__day - ) subq_24 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , booking_value + FROM cm_10_cte cm_10_cte + ) subq_20 +) + +, cm_12_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_13_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_value * 0.05 AS booking_fees + FROM ( + -- Read From CTE For node_id=cm_12 SELECT metric_time__day - , booking_value * 0.05 AS booking_fees + , booking_value + FROM cm_12_cte cm_12_cte + ) subq_29 +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , booking_fees - booking_fees_start_of_month AS booking_fees_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) AS metric_time__day + , MAX(subq_24.booking_fees_start_of_month) AS booking_fees_start_of_month + , MAX(cm_13_cte.booking_fees) AS booking_fees FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_29 - ) subq_30 - ON - subq_24.metric_time__day = subq_30.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_30.metric_time__day) -) subq_31 + subq_23.ds AS metric_time__day + , cm_11_cte.booking_fees_start_of_month AS booking_fees_start_of_month + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_11_cte cm_11_cte + ON + DATE_TRUNC('month', subq_23.ds) = cm_11_cte.metric_time__day + ) subq_24 + FULL OUTER JOIN + cm_13_cte cm_13_cte + ON + subq_24.metric_time__day = cm_13_cte.metric_time__day + GROUP BY + COALESCE(subq_24.metric_time__day, cm_13_cte.metric_time__day) + ) subq_31 +) + +SELECT + metric_time__day AS metric_time__day + , booking_fees_since_start_of_month AS booking_fees_since_start_of_month +FROM cm_14_cte cm_14_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql index b62cdb2dde..32085d34f8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine__plan0_optimized.sql @@ -2,36 +2,60 @@ test_name: test_nested_fill_nulls_without_time_spine test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_8 - GROUP BY - metric_time__day - ) subq_9 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_8 + GROUP BY + metric_time__day + ) subq_9 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_6_cte cm_6_cte ) subq_10 -) subq_11 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_7_cte cm_7_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , nested_fill_nulls_without_time_spine AS nested_fill_nulls_without_time_spine +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql index aad7e04f45..3ae0a89f45 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_fill_nulls_without_time_spine_multi_metric__plan0_optimized.sql @@ -3,46 +3,59 @@ test_filename: test_derived_metric_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) AS metric_time__day - , MAX(subq_18.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_8_cte AS ( -- Compute Metrics via Expressions SELECT metric_time__day - , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine FROM ( - -- Compute Metrics via Expressions + -- Aggregate Measures SELECT metric_time__day - , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + , SUM(bookings) AS bookings FROM ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0_without_time_spine - FROM ( - -- Aggregate Measures - SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - GROUP BY - metric_time__day - ) subq_15 - ) subq_16 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + GROUP BY + metric_time__day + ) subq_15 +) + +, cm_9_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_fill_nulls_with_0_without_time_spine AS twice_bookings_fill_nulls_with_0_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_8 + SELECT + metric_time__day + , bookings_fill_nulls_with_0_without_time_spine + FROM cm_8_cte cm_8_cte + ) subq_16 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 3 * twice_bookings_fill_nulls_with_0_without_time_spine AS nested_fill_nulls_without_time_spine + FROM ( + -- Read From CTE For node_id=cm_9 + SELECT + metric_time__day + , twice_bookings_fill_nulls_with_0_without_time_spine + FROM cm_9_cte cm_9_cte ) subq_17 -) subq_18 -FULL OUTER JOIN ( +) + +, cm_11_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -59,8 +72,16 @@ FULL OUTER JOIN ( ) subq_21 GROUP BY metric_time__day -) subq_23 +) + +SELECT + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) AS metric_time__day + , MAX(cm_10_cte.nested_fill_nulls_without_time_spine) AS nested_fill_nulls_without_time_spine + , MAX(cm_11_cte.listings) AS listings +FROM cm_10_cte cm_10_cte +FULL OUTER JOIN + cm_11_cte cm_11_cte ON - subq_18.metric_time__day = subq_23.metric_time__day + cm_10_cte.metric_time__day = cm_11_cte.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__day, subq_23.metric_time__day) + COALESCE(cm_10_cte.metric_time__day, cm_11_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql index d58e04b195..5b5660c6e8 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_filters__plan0_optimized.sql @@ -4,67 +4,86 @@ docstring: Tests derived metric rendering for a nested derived metric with filters on the outer metric spec. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - instant_lux_booking_value_rate AS instant_lux_booking_value_rate -FROM ( +-- Read From CTE For node_id=cm_15 +WITH cm_13_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings'] + -- Aggregate Measures -- Compute Metrics via Expressions SELECT - average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Combine Aggregated Outputs + -- Join Standard Outputs SELECT - MAX(subq_37.average_booking_value) AS average_booking_value - , MAX(subq_37.bookings) AS bookings - , MAX(subq_43.booking_value) AS booking_value + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_29.booking__is_instant AS booking__is_instant + , subq_29.bookings AS bookings + , subq_29.average_booking_value AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_29.booking__is_instant AS booking__is_instant - , subq_29.bookings AS bookings - , subq_29.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_29 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_29.listing = listings_latest_src_28000.listing_id - ) subq_33 - WHERE (listing__is_lux_latest) AND (booking__is_instant) - ) subq_37 - CROSS JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value',] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - SUM(booking_value) AS booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_39 - WHERE booking__is_instant - ) subq_43 + listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_29 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_29.listing = listings_latest_src_28000.listing_id + ) subq_33 + WHERE (listing__is_lux_latest) AND (booking__is_instant) +) + +, cm_12_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(booking_value) AS booking_value + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + is_instant AS booking__is_instant + , booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_39 + WHERE booking__is_instant +) + +, cm_14_cte AS ( + -- Compute Metrics via Expressions + SELECT + average_booking_value * bookings / NULLIF(booking_value, 0) AS instant_lux_booking_value_rate + FROM ( + -- Combine Aggregated Outputs + SELECT + MAX(cm_13_cte.average_booking_value) AS average_booking_value + , MAX(cm_13_cte.bookings) AS bookings + , MAX(cm_12_cte.booking_value) AS booking_value + FROM cm_13_cte cm_13_cte + CROSS JOIN + cm_12_cte cm_12_cte ) subq_44 -) subq_45 +) + +, cm_15_cte AS ( + -- Compute Metrics via Expressions + SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate + FROM ( + -- Read From CTE For node_id=cm_14 + SELECT + instant_lux_booking_value_rate + FROM cm_14_cte cm_14_cte + ) subq_45 +) + +SELECT + instant_lux_booking_value_rate AS instant_lux_booking_value_rate +FROM cm_15_cte cm_15_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql index 02db30d704..a94f823b21 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets__plan0_optimized.sql @@ -2,44 +2,63 @@ test_name: test_nested_offsets test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_22 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_13 ON - DATE_ADD('day', -2, subq_22.ds) = subq_20.metric_time__day -) subq_23 + DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_22.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_22 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_22.ds) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql index d608078631..d814bbd14e 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_time_constraint__plan0_optimized.sql @@ -2,50 +2,69 @@ test_name: test_nested_offsets_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.metric_time__day AS metric_time__day - , subq_20.bookings_offset_once AS bookings_offset_once - FROM ( - -- Time Spine - SELECT - ds AS metric_time__day - FROM ***************************.mf_time_spine subq_22 - WHERE ds BETWEEN timestamp '2020-01-12' AND timestamp '2020-01-13' - ) subq_21 + subq_15.ds AS metric_time__day + , SUM(subq_13.bookings) AS bookings + FROM ***************************.mf_time_spine subq_15 INNER JOIN ( - -- Compute Metrics via Expressions + -- 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 + ON + DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day + GROUP BY + subq_15.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 SELECT metric_time__day - , 2 * bookings AS bookings_offset_once + , bookings + FROM cm_6_cte cm_6_cte + ) subq_19 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings_offset_once AS bookings_offset_twice + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_21.metric_time__day AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Time Spine SELECT - subq_15.ds AS metric_time__day - , SUM(subq_13.bookings) AS bookings - FROM ***************************.mf_time_spine subq_15 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - ON - DATE_ADD('day', -5, subq_15.ds) = subq_13.metric_time__day - GROUP BY - subq_15.ds - ) subq_19 - ) subq_20 - ON - DATE_ADD('day', -2, subq_21.metric_time__day) = subq_20.metric_time__day -) subq_23 + ds AS metric_time__day + FROM ***************************.mf_time_spine subq_22 + WHERE ds BETWEEN timestamp '2020-01-12' AND timestamp '2020-01-13' + ) subq_21 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_21.metric_time__day) = cm_7_cte.metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql index eda2cec264..132b7e2f9a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_nested_offsets_with_where_constraint__plan0_optimized.sql @@ -2,51 +2,70 @@ test_name: test_nested_offsets_with_where_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , 2 * bookings_offset_once AS bookings_offset_twice -FROM ( - -- Constrain Output with WHERE +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_16.ds AS metric_time__day + , SUM(subq_14.bookings) AS bookings + FROM ***************************.mf_time_spine subq_16 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_14 + ON + DATE_ADD('day', -5, subq_16.ds) = subq_14.metric_time__day + GROUP BY + subq_16.ds +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , 2 * bookings AS bookings_offset_once + FROM ( + -- Read From CTE For node_id=cm_6 + SELECT + metric_time__day + , bookings + FROM cm_6_cte cm_6_cte + ) subq_20 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT metric_time__day - , bookings_offset_once + , 2 * bookings_offset_once AS bookings_offset_twice FROM ( - -- Join to Time Spine Dataset + -- Constrain Output with WHERE SELECT - subq_23.ds AS metric_time__day - , subq_21.bookings_offset_once AS bookings_offset_once - FROM ***************************.mf_time_spine subq_23 - INNER JOIN ( - -- Compute Metrics via Expressions + metric_time__day + , bookings_offset_once + FROM ( + -- Join to Time Spine Dataset SELECT - metric_time__day - , 2 * bookings AS bookings_offset_once - FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - subq_16.ds AS metric_time__day - , SUM(subq_14.bookings) AS bookings - FROM ***************************.mf_time_spine subq_16 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_14 - ON - DATE_ADD('day', -5, subq_16.ds) = subq_14.metric_time__day - GROUP BY - subq_16.ds - ) subq_20 - ) subq_21 - ON - DATE_ADD('day', -2, subq_23.ds) = subq_21.metric_time__day - ) subq_24 - WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' -) subq_25 + subq_23.ds AS metric_time__day + , cm_7_cte.bookings_offset_once AS bookings_offset_once + FROM ***************************.mf_time_spine subq_23 + INNER JOIN + cm_7_cte cm_7_cte + ON + DATE_ADD('day', -2, subq_23.ds) = cm_7_cte.metric_time__day + ) subq_24 + WHERE metric_time__day = '2020-01-12' or metric_time__day = '2020-01-13' + ) subq_25 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_offset_twice AS bookings_offset_twice +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index 600f39647f..712d07aee2 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,11 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with one granularity and filtered by a different one. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__month'] -- Aggregate Measures @@ -38,4 +35,23 @@ FROM ( WHERE metric_time__day = '2020-01-01' GROUP BY metric_time__month -) subq_17 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__month + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_17 +) + +SELECT + metric_time__month AS metric_time__month + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql index 88c1ed0b75..b6abc4b2ce 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_metric_multiple_granularities__plan0_optimized.sql @@ -4,13 +4,8 @@ docstring: Test a query where an offset to grain metric is queried with multiple granularities. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , bookings_start_of_month AS bookings_at_start_of_month -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day', 'metric_time__month', 'metric_time__year'] -- Aggregate Measures @@ -35,4 +30,29 @@ FROM ( subq_11.ds , DATE_TRUNC('month', subq_11.ds) , DATE_TRUNC('year', subq_11.ds) -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month AS bookings_at_start_of_month + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , metric_time__month + , metric_time__year + , bookings_start_of_month + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , bookings_at_start_of_month AS bookings_at_start_of_month +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql index 9f5c75d844..bd91dc7221 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_to_grain_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_to_grain_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_at_start_of_month) AS bookings_at_start_of_month + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_at_start_of_month - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_at_start_of_month + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_TRUNC('month', subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_at_start_of_month AS bookings_growth_since_start_of_month + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_at_start_of_month) AS bookings_at_start_of_month + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_since_start_of_month AS bookings_growth_since_start_of_month +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql index c2acb88017..d6d5669b7a 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_filter_and_query_have_different_granularities__plan0_optimized.sql @@ -4,63 +4,76 @@ docstring: Test a query where an offset window metric is queried with one granularity and filtered by a different one. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__month - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) AS metric_time__month - , MAX(subq_24.booking_value) AS booking_value - , MAX(subq_30.bookers) AS bookers + metric_time__month + , SUM(booking_value) AS booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__month - , SUM(booking_value) AS booking_value - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_19.ds AS metric_time__day - , DATE_TRUNC('month', subq_19.ds) AS metric_time__month - , bookings_source_src_28000.booking_value AS booking_value - FROM ***************************.mf_time_spine subq_19 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 - ON - DATE_ADD('week', -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) - ) subq_20 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_24 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookers', 'metric_time__month'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_19.ds AS metric_time__day + , DATE_TRUNC('month', subq_19.ds) AS metric_time__month + , bookings_source_src_28000.booking_value AS booking_value + FROM ***************************.mf_time_spine subq_19 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_19.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ) subq_20 + WHERE metric_time__day = '2020-01-01' + GROUP BY + metric_time__month +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers', 'metric_time__month'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__month + , COUNT(DISTINCT bookers) AS bookers + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - metric_time__month - , COUNT(DISTINCT bookers) AS bookers - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 - WHERE metric_time__day = '2020-01-01' - GROUP BY - metric_time__month - ) subq_30 - ON - subq_24.metric_time__month = subq_30.metric_time__month + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + WHERE metric_time__day = '2020-01-01' GROUP BY - COALESCE(subq_24.metric_time__month, subq_30.metric_time__month) -) subq_31 + metric_time__month +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__month + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + GROUP BY + COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + ) subq_31 +) + +SELECT + metric_time__month AS metric_time__month + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql index 5111961e53..69fdbd8749 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_metric_multiple_granularities__plan0_optimized.sql @@ -4,67 +4,82 @@ docstring: Test a query where an offset window metric is queried with multiple granularities. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , metric_time__month - , metric_time__year - , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_17.ds AS metric_time__day + , DATE_TRUNC('month', subq_17.ds) AS metric_time__month + , DATE_TRUNC('year', subq_17.ds) AS metric_time__year + , SUM(bookings_source_src_28000.booking_value) AS booking_value + FROM ***************************.mf_time_spine subq_17 + INNER JOIN + ***************************.fct_bookings bookings_source_src_28000 + ON + DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + GROUP BY + subq_17.ds + , DATE_TRUNC('month', subq_17.ds) + , DATE_TRUNC('year', subq_17.ds) +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , DATE_TRUNC('month', ds) AS metric_time__month + , DATE_TRUNC('year', ds) AS metric_time__year + , COUNT(DISTINCT guest_id) AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) + , DATE_TRUNC('month', ds) + , DATE_TRUNC('year', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) AS metric_time__day - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) AS metric_time__month - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) AS metric_time__year - , MAX(subq_21.booking_value) AS booking_value - , MAX(subq_26.bookers) AS bookers + metric_time__day + , metric_time__month + , metric_time__year + , booking_value * 0.05 / bookers AS booking_fees_last_week_per_booker_this_week FROM ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['booking_value', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Combine Aggregated Outputs SELECT - subq_17.ds AS metric_time__day - , DATE_TRUNC('month', subq_17.ds) AS metric_time__month - , DATE_TRUNC('year', subq_17.ds) AS metric_time__year - , SUM(bookings_source_src_28000.booking_value) AS booking_value - FROM ***************************.mf_time_spine subq_17 - INNER JOIN - ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) AS metric_time__month + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) AS metric_time__year + , MAX(cm_6_cte.booking_value) AS booking_value + , MAX(cm_7_cte.bookers) AS bookers + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte ON - DATE_ADD('week', -1, subq_17.ds) = DATE_TRUNC('day', bookings_source_src_28000.ds) + ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) AND ( + cm_6_cte.metric_time__month = cm_7_cte.metric_time__month + ) AND ( + cm_6_cte.metric_time__year = cm_7_cte.metric_time__year + ) GROUP BY - subq_17.ds - , DATE_TRUNC('month', subq_17.ds) - , DATE_TRUNC('year', subq_17.ds) - ) subq_21 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'metric_time__day', 'metric_time__month', 'metric_time__year'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , DATE_TRUNC('month', ds) AS metric_time__month - , DATE_TRUNC('year', ds) AS metric_time__year - , COUNT(DISTINCT guest_id) AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - , DATE_TRUNC('month', ds) - , DATE_TRUNC('year', ds) - ) subq_26 - ON - ( - subq_21.metric_time__day = subq_26.metric_time__day - ) AND ( - subq_21.metric_time__month = subq_26.metric_time__month - ) AND ( - subq_21.metric_time__year = subq_26.metric_time__year - ) - GROUP BY - COALESCE(subq_21.metric_time__day, subq_26.metric_time__day) - , COALESCE(subq_21.metric_time__month, subq_26.metric_time__month) - , COALESCE(subq_21.metric_time__year, subq_26.metric_time__year) -) subq_27 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.metric_time__month, cm_7_cte.metric_time__month) + , COALESCE(cm_6_cte.metric_time__year, cm_7_cte.metric_time__year) + ) subq_27 +) + +SELECT + metric_time__day AS metric_time__day + , metric_time__month AS metric_time__month + , metric_time__year AS metric_time__year + , booking_fees_last_week_per_booker_this_week AS booking_fees_last_week_per_booker_this_week +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql index fe12cd0d68..6c4814524d 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_agg_time_dim__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_agg_time_dim test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - booking__ds__day - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) AS booking__ds__day - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + booking__ds__day + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - booking__ds__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'booking__ds__day'] - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - booking__ds__day - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'booking__ds__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - subq_22.ds AS booking__ds__day - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS booking__ds__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.booking__ds__day - GROUP BY - subq_22.ds - ) subq_26 + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + booking__ds__day +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'booking__ds__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_22.ds AS booking__ds__day + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS booking__ds__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.booking__ds__day = subq_26.booking__ds__day + DATE_ADD('day', -14, subq_22.ds) = subq_20.booking__ds__day GROUP BY - COALESCE(subq_18.booking__ds__day, subq_26.booking__ds__day) -) subq_27 + subq_22.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + booking__ds__day + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) AS booking__ds__day + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.booking__ds__day = cm_7_cte.booking__ds__day + GROUP BY + COALESCE(cm_6_cte.booking__ds__day, cm_7_cte.booking__ds__day) + ) subq_27 +) + +SELECT + booking__ds__day AS booking__ds__day + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql index 0d06d24598..689b484308 100644 --- a/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_derived_metric_rendering.py/SqlQueryPlan/Trino/test_time_offset_metric_with_time_constraint__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_time_offset_metric_with_time_constraint test_filename: test_derived_metric_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_5_days_ago AS bookings_5_day_lag -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -33,4 +30,23 @@ FROM ( DATE_ADD('day', -5, subq_10.metric_time__day) = subq_9.metric_time__day GROUP BY subq_10.metric_time__day -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_5_days_ago AS bookings_5_day_lag + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , bookings_5_days_ago + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_5_day_lag AS bookings_5_day_lag +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 576edf2281..cf457d3966 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/BigQuery/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 14 day) = subq_26.metric_time__day + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATE_SUB(CAST(subq_28.ds AS DATETIME), INTERVAL 14 day) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY metric_time__day - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - metric_time__day -) subq_33 + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index ba9e282ecb..df20b2521f 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Databricks/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 44671f8248..915e9dd91f 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Postgres/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + subq_28.ds - MAKE_INTERVAL(days => 14) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - subq_28.ds - MAKE_INTERVAL(days => 14) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 25e33fd181..c78ebbf2d8 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Redshift/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 34a698d3f4..8bbeeb4858 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Snowflake/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATEADD(day, -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql index 45f332582e..e5ac2806f1 100644 --- a/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_fill_nulls_with_rendering.py/SqlQueryPlan/Trino/test_derived_fill_nulls_for_one_input_metric__plan0_optimized.sql @@ -2,72 +2,96 @@ test_name: test_derived_fill_nulls_for_one_input_metric test_filename: test_fill_nulls_with_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day - , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_22.ds AS metric_time__day + , subq_20.bookings AS bookings + FROM ***************************.mf_time_spine subq_22 + LEFT OUTER JOIN ( + -- Aggregate Measures SELECT - subq_22.ds AS metric_time__day - , subq_20.bookings AS bookings - FROM ***************************.mf_time_spine subq_22 - LEFT OUTER JOIN ( - -- Aggregate Measures + metric_time__day + , SUM(bookings) AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'metric_time__day'] SELECT - metric_time__day - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__day'] - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_19 - GROUP BY - metric_time__day - ) subq_20 - ON - subq_22.ds = subq_20.metric_time__day - ) subq_23 - ) subq_24 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset - -- Pass Only Elements: ['bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_19 + GROUP BY + metric_time__day + ) subq_20 + ON + subq_22.ds = subq_20.metric_time__day + ) subq_23 +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + subq_28.ds AS metric_time__day + , SUM(subq_26.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_28 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_26 + ON + DATE_ADD('day', -14, subq_28.ds) = subq_26.metric_time__day + GROUP BY + subq_28.ds +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset + FROM ( + -- Combine Aggregated Outputs SELECT - subq_28.ds AS metric_time__day - , SUM(subq_26.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_28 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) AS metric_time__day + , COALESCE(MAX(subq_24.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , MAX(subq_32.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM ( + -- Read From CTE For node_id=cm_6 SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_26 + metric_time__day + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_24 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_32 ON - DATE_ADD('day', -14, subq_28.ds) = subq_26.metric_time__day + subq_24.metric_time__day = subq_32.metric_time__day GROUP BY - subq_28.ds - ) subq_32 - ON - subq_24.metric_time__day = subq_32.metric_time__day - GROUP BY - COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) -) subq_33 + COALESCE(subq_24.metric_time__day, subq_32.metric_time__day) + ) subq_33 +) + +SELECT + metric_time__day AS metric_time__day + , bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset AS bookings_growth_2_weeks_fill_nulls_with_0_for_non_offset +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql index 6aad32a7f6..caea2a8e82 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - IF(EXTRACT(dayofweek FROM ds) = 1, 7, EXTRACT(dayofweek FROM ds) - 1) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - IF(EXTRACT(dayofweek FROM subq_22.ds) = 1, 7, EXTRACT(dayofweek FROM subq_22.ds) - 1) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day - GROUP BY - metric_time__extract_dow - ) subq_26 + IF(EXTRACT(dayofweek FROM ds) = 1, 7, EXTRACT(dayofweek FROM ds) - 1) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + IF(EXTRACT(dayofweek FROM subq_22.ds) = 1, 7, EXTRACT(dayofweek FROM subq_22.ds) - 1) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATE_SUB(CAST(subq_22.ds AS DATETIME), INTERVAL 14 day) = subq_20.metric_time__day GROUP BY metric_time__extract_dow -) subq_27 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + metric_time__extract_dow + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 5a9219415d..e1335a1bab 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATETIME_TRUNC(subq_11.ts, hour) = subq_9.metric_time__hour GROUP BY metric_time__hour -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql index 7997b120d8..2dcbef3223 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/BigQuery/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_SUB(CAST(subq_11.ts AS DATETIME), INTERVAL 1 hour) = subq_9.metric_time__hour GROUP BY metric_time__hour -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql index fcbbcc1ba0..262cb6056a 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(DAYOFWEEK_ISO FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) - ) subq_26 + EXTRACT(DAYOFWEEK_ISO FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(DAYOFWEEK_ISO FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index a2be431b34..101fdc2864 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql index 33ac7b7e64..19a4ec44f8 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Databricks/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql index 5e0bf2bbcd..193e9d00db 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(isodow FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day - GROUP BY - EXTRACT(isodow FROM subq_22.ds) - ) subq_26 + EXTRACT(isodow FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(isodow FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + subq_22.ds - MAKE_INTERVAL(days => 14) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(isodow FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 4ebee052c5..c707d79891 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql index 41dd8e3274..7338a4b1c3 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Postgres/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( subq_11.ts - MAKE_INTERVAL(hours => 1) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql index c2b6382245..f19c76ea7b 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - CASE WHEN EXTRACT(dow FROM ds) = 0 THEN EXTRACT(dow FROM ds) + 7 ELSE EXTRACT(dow FROM ds) END AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END - ) subq_26 + CASE WHEN EXTRACT(dow FROM ds) = 0 THEN EXTRACT(dow FROM ds) + 7 ELSE EXTRACT(dow FROM ds) END AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + CASE WHEN EXTRACT(dow FROM subq_22.ds) = 0 THEN EXTRACT(dow FROM subq_22.ds) + 7 ELSE EXTRACT(dow FROM subq_22.ds) END +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 7b1ea188a2..609d525d18 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql index eeb440b35e..4bd81e4c0d 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Redshift/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql index 656edaf9fe..709b77c99f 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(dayofweekiso FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(dayofweekiso FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(dayofweekiso FROM subq_22.ds) - ) subq_26 + EXTRACT(dayofweekiso FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(dayofweekiso FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATEADD(day, -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(dayofweekiso FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 0ac8b25258..6ed276d78b 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql index 0ee8b61dbc..4a52434fb0 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Snowflake/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATEADD(hour, -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql index 4def88a76d..1eca1185c0 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_offset_window_with_date_part__plan0_optimized.sql @@ -2,58 +2,71 @@ test_name: test_offset_window_with_date_part test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__extract_dow - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) AS metric_time__extract_dow - , MAX(subq_18.bookings) AS bookings - , MAX(subq_26.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__extract_dow + , SUM(bookings) AS bookings FROM ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__extract_dow - , SUM(bookings) AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - SELECT - EXTRACT(DAY_OF_WEEK FROM ds) AS metric_time__extract_dow - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - metric_time__extract_dow - ) subq_18 - FULL OUTER JOIN ( - -- Join to Time Spine Dataset + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT - EXTRACT(DAY_OF_WEEK FROM subq_22.ds) AS metric_time__extract_dow - , SUM(subq_20.bookings) AS bookings_2_weeks_ago - FROM ***************************.mf_time_spine subq_22 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_20 - ON - DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day - GROUP BY - EXTRACT(DAY_OF_WEEK FROM subq_22.ds) - ) subq_26 + EXTRACT(DAY_OF_WEEK FROM ds) AS metric_time__extract_dow + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + metric_time__extract_dow +) + +, cm_7_cte AS ( + -- Join to Time Spine Dataset + -- Pass Only Elements: ['bookings', 'metric_time__extract_dow'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + EXTRACT(DAY_OF_WEEK FROM subq_22.ds) AS metric_time__extract_dow + , SUM(subq_20.bookings) AS bookings_2_weeks_ago + FROM ***************************.mf_time_spine subq_22 + INNER JOIN ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_20 ON - subq_18.metric_time__extract_dow = subq_26.metric_time__extract_dow + DATE_ADD('day', -14, subq_22.ds) = subq_20.metric_time__day GROUP BY - COALESCE(subq_18.metric_time__extract_dow, subq_26.metric_time__extract_dow) -) subq_27 + EXTRACT(DAY_OF_WEEK FROM subq_22.ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__extract_dow + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) AS metric_time__extract_dow + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__extract_dow = cm_7_cte.metric_time__extract_dow + GROUP BY + COALESCE(cm_6_cte.metric_time__extract_dow, cm_7_cte.metric_time__extract_dow) + ) subq_27 +) + +SELECT + metric_time__extract_dow AS metric_time__extract_dow + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql index 6eca0ddc3f..64979d6fd9 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_to_grain_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_to_grain_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_grain_to_date_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_TRUNC('hour', subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_grain_to_date_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_grain_to_date_metric AS subdaily_offset_grain_to_date_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql index f51d27ebec..4af6555151 100644 --- a/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_granularity_date_part_rendering.py/SqlQueryPlan/Trino/test_subdaily_offset_window_metric__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_subdaily_offset_window_metric test_filename: test_granularity_date_part_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__hour - , archived_users AS subdaily_offset_window_metric -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Join to Time Spine Dataset -- Pass Only Elements: ['archived_users', 'metric_time__hour'] -- Aggregate Measures @@ -27,4 +24,23 @@ FROM ( DATE_ADD('hour', -1, subq_11.ts) = subq_9.metric_time__hour GROUP BY subq_11.ts -) subq_15 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__hour + , archived_users AS subdaily_offset_window_metric + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__hour + , archived_users + FROM cm_4_cte cm_4_cte + ) subq_15 +) + +SELECT + metric_time__hour AS metric_time__hour + , subdaily_offset_window_metric AS subdaily_offset_window_metric +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 5166802d9e..e68b861497 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql index 72abea6402..d1208e5b91 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS FLOAT64) / CAST(NULLIF(subq_39.visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS FLOAT64) / CAST(NULLIF(visits, 0) AS FLOAT64) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY user ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 74a410722c..75c501cb9d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listing__user +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listing__user - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql index 4871deb408..01d43bd8f3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATETIME_TRUNC(bridge_table_src_22000.ds_partitioned, day) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATETIME_TRUNC(bridge_table_src_22000.ds_partitioned, day) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATETIME_TRUNC(account_month_txns_src_22000.ds_partitioned, day) = subq_30.ds_partitioned__day - ) - GROUP BY - account_id__customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATETIME_TRUNC(account_month_txns_src_22000.ds_partitioned, day) = subq_30.ds_partitioned__day + ) + GROUP BY + account_id__customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql index e5c386cf63..93abe72de4 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql index eb9387e67f..157bc71345 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 5cb94bbb5c..86cb323885 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATETIME_TRUNC(created_at, day) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATETIME_TRUNC(created_at, day) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 4fe1878955..fc3d11be2b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index f5815e8615..8b11a76c78 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY listing - , booking_value * views AS listing__views_times_booking_value + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - listing - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 4917aea695..e61eb998d0 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index f4d3811512..0ffa4c9da1 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS FLOAT64) / CAST(NULLIF(subq_25.bookers, 0) AS FLOAT64) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS FLOAT64) / CAST(NULLIF(bookers, 0) AS FLOAT64) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 1315f9edf3..aefe125f0b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/BigQuery/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: BigQuery --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 66dbc3dcec..baf3eafdd3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql index b022e93fa1..bec116d973 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 6ae5114cd4..2f450ddd87 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql index ae0625a961..e2f526bdae 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql index 32d0d4fd48..10a88382d9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql index 8dd5d7b831..ee6d32d2c9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 27e287e276..08db85ffc7 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index e792136fa4..2ea48964e7 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index 924ebfbad4..66141875f8 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 355684fa4f..f82138ee34 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 7007d806d5..35ef8d482b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 2db347fc95..61e2f4e785 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Databricks/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Databricks --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index f97c479704..8b36291e91 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql index f6ef6f9ef4..ffda9d300a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 557149d12f..0bbe2c9674 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql index 5ab04bcc99..a64abb1d6b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql index 8fb6eae175..f5083603de 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql index 7e92b16499..4f6cba2d8a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 74bc392a56..94ea4b491b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 9b3ce5aaad..436298fc41 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index b0a0b216f3..0befd979b6 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index cdb2978e01..8a0c51dc5d 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 038d123f1a..b4fea3dda4 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 38f0a21af7..1bcdd67653 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Postgres/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Postgres --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 25bcd2eb73..b1fbf8f8aa 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql index 1c4dc8b06e..922d7fdb00 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE PRECISION) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE PRECISION) / CAST(NULLIF(visits, 0) AS DOUBLE PRECISION) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql index f4f1f7429a..91c697b800 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql index c89509a597..251bc76375 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql index f0195e62a7..ab3e7cffeb 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql index 3c0a42954a..791e5f10cf 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 40a62e3467..5c207a35e6 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index ed899a05b5..b63cdb18db 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index fe3105003d..5309037194 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index fe7d7cbb30..b421b76156 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index 5b859eca22..1be791e974 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(bookers, 0) AS DOUBLE PRECISION) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index a7832d3cba..66df6046da 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Redshift/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Redshift --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index 4cf1666583..e1b25e29b4 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql index 149f48bbb5..e8c3b0bffd 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 7f9be66b9c..516eb1221a 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql index b2b219ffcf..6e9f5bb07b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql index b79ca66b2a..ec5eacc32c 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql index 1d6bf9b6c8..c2d19be940 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 0d5d30e3fb..e6dd287e27 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index fab73b5c99..7737d0bb9e 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index a372ba649c..5fe4ecabcd 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 813fadcf96..2552bc6c6b 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index e8b9b0edec..19e2f381cb 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index d2803d2c39..386a55f6f0 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Snowflake/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Snowflake --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql index f1d00ba859..3a935e1e38 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_by_metric_in_same_semantic_model_as_queried_metric__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'guest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + guest_id AS guest + , SUM(booking_value) AS guest__booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + guest_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.guest__booking_value AS guest__booking_value - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - guest_id AS guest - , guest_id AS bookers + cm_3_cte.guest__booking_value AS guest__booking_value + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'guest'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['guest', 'guest__booking_value'] - SELECT - guest_id AS guest - , SUM(booking_value) AS guest__booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - guest_id - ) subq_19 - ON - subq_13.guest = subq_19.guest -) subq_20 -WHERE guest__booking_value > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.guest_id = cm_3_cte.guest + ) subq_20 + WHERE guest__booking_value > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql index a2f8c8563f..e5f6c52dd2 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_filter_with_conversion_metric__plan0_optimized.sql @@ -2,26 +2,13 @@ test_name: test_filter_with_conversion_metric test_filename: test_metric_filter_rendering.py sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Compute Metrics via Expressions SELECT - CAST(subq_39.buys AS DOUBLE) / CAST(NULLIF(subq_39.visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate - , subq_24.listings AS listings + subq_39.user + , CAST(buys AS DOUBLE) / CAST(NULLIF(visits, 0) AS DOUBLE) AS user__visit_buy_conversion_rate FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_24 - LEFT OUTER JOIN ( -- Combine Aggregated Outputs SELECT COALESCE(subq_28.user, subq_38.user) AS user @@ -116,7 +103,36 @@ FROM ( GROUP BY COALESCE(subq_28.user, subq_38.user) ) subq_39 - ON - subq_24.user = subq_39.user -) subq_42 -WHERE user__visit_buy_conversion_rate > 2 +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.user__visit_buy_conversion_rate AS user__visit_buy_conversion_rate + , subq_24.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_24 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_24.user = cm_3_cte.user + ) subq_42 + WHERE user__visit_buy_conversion_rate > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql index 380085c354..24726788f4 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_group_by_has_local_entity_prefix__plan0_optimized.sql @@ -2,43 +2,52 @@ test_name: test_group_by_has_local_entity_prefix test_filename: test_metric_filter_rendering.py sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['average_booking_value', 'listing__user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listings_latest_src_28000.user_id AS listing__user + , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id + GROUP BY + listings_latest_src_28000.user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_27.listing__user__average_booking_value AS user__listing__user__average_booking_value - , subq_17.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' - SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_17 - LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['average_booking_value', 'listing__user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing__user', 'listing__user__average_booking_value'] SELECT - listings_latest_src_28000.user_id AS listing__user - , AVG(bookings_source_src_28000.booking_value) AS listing__user__average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + cm_3_cte.listing__user__average_booking_value AS user__listing__user__average_booking_value + , subq_17.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_17 LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + cm_3_cte cm_3_cte ON - bookings_source_src_28000.listing_id = listings_latest_src_28000.listing_id - GROUP BY - listings_latest_src_28000.user_id - ) subq_27 - ON - subq_17.user = subq_27.listing__user -) subq_28 -WHERE user__listing__user__average_booking_value > 1 + subq_17.user = cm_3_cte.listing__user + ) subq_28 + WHERE user__listing__user__average_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql index e44112b9f6..8d1244e1b9 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_multi_hop__plan0_optimized.sql @@ -4,51 +4,60 @@ docstring: Tests rendering for a metric filter using a two-hop join in the inner query. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( -- Join Standard Outputs + -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_35.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 + subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id + , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count + FROM ***************************.account_month_txns account_month_txns_src_22000 LEFT OUTER JOIN ( -- Join Standard Outputs - -- Pass Only Elements: ['txn_count', 'account_id__customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['account_id__customer_id__customer_third_hop_id', 'account_id__customer_id__customer_third_hop_id__txn_count'] + -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] SELECT - subq_30.customer_id__customer_third_hop_id AS account_id__customer_id__customer_third_hop_id - , SUM(account_month_txns_src_22000.txn_count) AS account_id__customer_id__customer_third_hop_id__txn_count - FROM ***************************.account_month_txns account_month_txns_src_22000 - LEFT OUTER JOIN ( - -- Join Standard Outputs - -- Pass Only Elements: ['ds_partitioned__day', 'account_id', 'customer_id__customer_third_hop_id'] - SELECT - DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day - , bridge_table_src_22000.account_id AS account_id - , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id - FROM ***************************.bridge_table bridge_table_src_22000 - LEFT OUTER JOIN - ***************************.customer_other_data customer_other_data_src_22000 - ON - bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id - ) subq_30 + DATE_TRUNC('day', bridge_table_src_22000.ds_partitioned) AS ds_partitioned__day + , bridge_table_src_22000.account_id AS account_id + , customer_other_data_src_22000.customer_third_hop_id AS customer_id__customer_third_hop_id + FROM ***************************.bridge_table bridge_table_src_22000 + LEFT OUTER JOIN + ***************************.customer_other_data customer_other_data_src_22000 ON - ( - account_month_txns_src_22000.account_id = subq_30.account_id - ) AND ( - DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day - ) - GROUP BY - subq_30.customer_id__customer_third_hop_id - ) subq_35 + bridge_table_src_22000.customer_id = customer_other_data_src_22000.customer_id + ) subq_30 ON - third_hop_table_src_22000.customer_third_hop_id = subq_35.account_id__customer_id__customer_third_hop_id -) subq_36 -WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 + ( + account_month_txns_src_22000.account_id = subq_30.account_id + ) AND ( + DATE_TRUNC('day', account_month_txns_src_22000.ds_partitioned) = subq_30.ds_partitioned__day + ) + GROUP BY + subq_30.customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs + SELECT + cm_3_cte.account_id__customer_id__customer_third_hop_id__txn_count AS customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.account_id__customer_id__customer_third_hop_id + ) subq_36 + WHERE customer_third_hop_id__account_id__customer_id__customer_third_hop_id__txn_count > 2 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql index 6b05cc265d..e81da75e68 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_inner_query_single_hop__plan0_optimized.sql @@ -4,41 +4,50 @@ docstring: Tests rendering for a metric filter using a one-hop join in the inner query. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['third_hop_count',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT third_hop_count) AS third_hop_count -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + customer_id__customer_third_hop_id + , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers + FROM ( + -- Read Elements From Semantic Model 'customer_other_data' + -- Metric Time Dimension 'acquired_ds' + SELECT + customer_third_hop_id AS customer_id__customer_third_hop_id + , country AS customer_id__country + , 1 AS customers_with_other_data + FROM ***************************.customer_other_data customer_other_data_src_22000 + ) subq_16 + WHERE customer_id__country = 'paraguay' + GROUP BY + customer_id__customer_third_hop_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['third_hop_count',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_21.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers - , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count - FROM ***************************.third_hop_table third_hop_table_src_22000 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['customers_with_other_data', 'customer_id__customer_third_hop_id'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['customer_id__customer_third_hop_id', 'customer_id__customer_third_hop_id__paraguayan_customers'] + COUNT(DISTINCT third_hop_count) AS third_hop_count + FROM ( + -- Join Standard Outputs SELECT - customer_id__customer_third_hop_id - , SUM(customers_with_other_data) AS customer_id__customer_third_hop_id__paraguayan_customers - FROM ( - -- Read Elements From Semantic Model 'customer_other_data' - -- Metric Time Dimension 'acquired_ds' - SELECT - customer_third_hop_id AS customer_id__customer_third_hop_id - , country AS customer_id__country - , 1 AS customers_with_other_data - FROM ***************************.customer_other_data customer_other_data_src_22000 - ) subq_16 - WHERE customer_id__country = 'paraguay' - GROUP BY - customer_id__customer_third_hop_id - ) subq_21 - ON - third_hop_table_src_22000.customer_third_hop_id = subq_21.customer_id__customer_third_hop_id -) subq_22 -WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 + cm_3_cte.customer_id__customer_third_hop_id__paraguayan_customers AS customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers + , third_hop_table_src_22000.customer_third_hop_id AS third_hop_count + FROM ***************************.third_hop_table third_hop_table_src_22000 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + third_hop_table_src_22000.customer_third_hop_id = cm_3_cte.customer_id__customer_third_hop_id + ) subq_22 + WHERE customer_third_hop_id__customer_id__customer_third_hop_id__paraguayan_customers > 0 +) + +SELECT + third_hop_count AS third_hop_count +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql index 2a95d127c1..8fec6d158f 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_filtered_by_itself__plan0_optimized.sql @@ -4,40 +4,42 @@ docstring: Tests a query for a metric that filters by the same metric. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['bookers',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - COUNT(DISTINCT bookers) AS bookers -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookers',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookers AS listing__bookers - , subq_13.bookers AS bookers + COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - listing_id AS listing - , guest_id AS bookers + cm_3_cte.listing__bookers AS listing__bookers + , bookings_source_src_28000.guest_id AS bookers FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookers > 1.00 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + bookings_source_src_28000.listing_id = cm_3_cte.listing + ) subq_20 + WHERE listing__bookers > 1.00 +) + +SELECT + bookers AS bookers +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql index 9863eed9db..c148be7772 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_metric_with_metric_in_where_filter__plan0_optimized.sql @@ -4,50 +4,60 @@ docstring: Tests a query with a metric in the metric-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings', 'metric_time__day'] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - metric_time__day - , SUM(listings) AS active_listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.metric_time__day AS metric_time__day - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT - DATE_TRUNC('day', created_at) AS metric_time__day - , listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , SUM(listings) AS active_listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.metric_time__day AS metric_time__day + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT - listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 -GROUP BY - metric_time__day + DATE_TRUNC('day', created_at) AS metric_time__day + , listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 + GROUP BY + metric_time__day +) + +SELECT + metric_time__day AS metric_time__day + , active_listings AS active_listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql index 530cc6437e..f126461cfc 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_cumulative_metric_in_where_filter__plan0_optimized.sql @@ -6,40 +6,49 @@ docstring: Note this cumulative metric has no window / grain to date. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Read Elements From Semantic Model 'revenue' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['txn_revenue', 'user'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + user_id AS user + , SUM(revenue) AS user__revenue_all_time + FROM ***************************.fct_revenue revenue_src_28000 + GROUP BY + user_id +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.user__revenue_all_time AS user__revenue_all_time - , subq_13.listings AS listings + SUM(listings) AS listings FROM ( - -- Read Elements From Semantic Model 'listings_latest' - -- Metric Time Dimension 'ds' + -- Join Standard Outputs SELECT - user_id AS user - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'revenue' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['txn_revenue', 'user'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['user', 'user__revenue_all_time'] - SELECT - user_id AS user - , SUM(revenue) AS user__revenue_all_time - FROM ***************************.fct_revenue revenue_src_28000 - GROUP BY - user_id - ) subq_19 - ON - subq_13.user = subq_19.user -) subq_20 -WHERE user__revenue_all_time > 1 + cm_3_cte.user__revenue_all_time AS user__revenue_all_time + , subq_13.listings AS listings + FROM ( + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' + SELECT + user_id AS user + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.user = cm_3_cte.user + ) subq_20 + WHERE user__revenue_all_time > 1 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql index fedb762290..db6cf323b3 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_derived_metric_in_where_filter__plan0_optimized.sql @@ -4,75 +4,89 @@ docstring: Tests a query with a derived metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_33.listing__views_times_booking_value AS listing__views_times_booking_value - , subq_20.listings AS listings + listing + , SUM(views) AS views FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'views_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['views', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__views_times_booking_value'] + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_28 + GROUP BY + listing +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing + , booking_value * views AS listing__views_times_booking_value + FROM ( + -- Combine Aggregated Outputs SELECT - listing - , booking_value * views AS listing__views_times_booking_value + COALESCE(cm_5_cte.listing, cm_6_cte.listing) AS listing + , MAX(cm_5_cte.booking_value) AS booking_value + , MAX(cm_6_cte.views) AS views + FROM cm_5_cte cm_5_cte + FULL OUTER JOIN + cm_6_cte cm_6_cte + ON + cm_5_cte.listing = cm_6_cte.listing + GROUP BY + COALESCE(cm_5_cte.listing, cm_6_cte.listing) + ) subq_31 +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs + SELECT + cm_7_cte.listing__views_times_booking_value AS listing__views_times_booking_value + , subq_20.listings AS listings FROM ( - -- Combine Aggregated Outputs + -- Read Elements From Semantic Model 'listings_latest' + -- Metric Time Dimension 'ds' SELECT - COALESCE(subq_25.listing, subq_30.listing) AS listing - , MAX(subq_25.booking_value) AS booking_value - , MAX(subq_30.views) AS views - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing_id AS listing - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_25 - FULL OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - listing - , SUM(views) AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['views', 'listing'] - SELECT - listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_28 - GROUP BY - listing - ) subq_30 - ON - subq_25.listing = subq_30.listing - GROUP BY - COALESCE(subq_25.listing, subq_30.listing) - ) subq_31 - ) subq_33 - ON - subq_20.listing = subq_33.listing -) subq_34 -WHERE listing__views_times_booking_value > 1 + listing_id AS listing + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_7_cte cm_7_cte + ON + subq_20.listing = cm_7_cte.listing + ) subq_34 + WHERE listing__views_times_booking_value > 1 +) + +SELECT + listings AS listings +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql index 0d390ad774..47b2bf7ed2 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_multiple_metrics_in_filter__plan0_optimized.sql @@ -4,62 +4,73 @@ docstring: Tests a query with 2 simple metrics in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_6 +WITH cm_4_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_25.listing__bookings AS listing__bookings - , subq_31.listing__bookers AS listing__bookers - , subq_19.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_19 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_22 + GROUP BY + listing +) + +, cm_5_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookers', 'listing'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing_id AS listing + , COUNT(DISTINCT guest_id) AS listing__bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + listing_id +) + +, cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_4_cte.listing__bookings AS listing__bookings + , cm_5_cte.listing__bookers AS listing__bookers + , subq_19.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_22 - GROUP BY - listing - ) subq_25 - ON - subq_19.listing = subq_25.listing - LEFT OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookers', 'listing'] - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookers'] - SELECT - listing_id AS listing - , COUNT(DISTINCT guest_id) AS listing__bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - listing_id - ) subq_31 - ON - subq_19.listing = subq_31.listing -) subq_32 -WHERE listing__bookings > 2 AND listing__bookers > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_19 + LEFT OUTER JOIN + cm_4_cte cm_4_cte + ON + subq_19.listing = cm_4_cte.listing + LEFT OUTER JOIN + cm_5_cte cm_5_cte + ON + subq_19.listing = cm_5_cte.listing + ) subq_32 + WHERE listing__bookings > 2 AND listing__bookers > 1 +) + +SELECT + listings AS listings +FROM cm_6_cte cm_6_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql index de93123c56..73e29727ac 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_ratio_metric_in_where_filter__plan0_optimized.sql @@ -4,46 +4,65 @@ docstring: Tests a query with a ratio metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_9 +WITH cm_7_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - CAST(subq_25.bookings AS DOUBLE) / CAST(NULLIF(subq_25.bookers, 0) AS DOUBLE) AS listing__bookings_per_booker - , subq_20.listings AS listings + listing + , SUM(bookings) AS bookings + , COUNT(DISTINCT bookers) AS bookers FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_20 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions + , 1 AS bookings + , guest_id AS bookers + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_23 + GROUP BY + listing +) + +, cm_8_cte AS ( + -- Read From CTE For node_id=cm_7 + -- Compute Metrics via Expressions + SELECT + listing + , CAST(bookings AS DOUBLE) / CAST(NULLIF(bookers, 0) AS DOUBLE) AS listing__bookings_per_booker + FROM cm_7_cte cm_7_cte +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS bookings - , COUNT(DISTINCT bookers) AS bookers + cm_8_cte.listing__bookings_per_booker AS listing__bookings_per_booker + , subq_20.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'bookers', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - , guest_id AS bookers - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_23 - GROUP BY - listing - ) subq_25 - ON - subq_20.listing = subq_25.listing -) subq_28 -WHERE listing__bookings_per_booker > 1 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_20 + LEFT OUTER JOIN + cm_8_cte cm_8_cte + ON + subq_20.listing = cm_8_cte.listing + ) subq_28 + WHERE listing__bookings_per_booker > 1 +) + +SELECT + listings AS listings +FROM cm_9_cte cm_9_cte diff --git a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql index 8752d079c0..275cd77a30 100644 --- a/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_filter_rendering.py/SqlQueryPlan/Trino/test_query_with_simple_metric_in_where_filter__plan0_optimized.sql @@ -4,45 +4,54 @@ docstring: Tests a query with a simple metric in the query-level where filter. sql_engine: Trino --- --- Constrain Output with WHERE --- Pass Only Elements: ['listings',] --- Aggregate Measures --- Compute Metrics via Expressions -SELECT - SUM(listings) AS listings -FROM ( - -- Join Standard Outputs +-- Read From CTE For node_id=cm_4 +WITH cm_3_cte AS ( + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - subq_19.listing__bookings AS listing__bookings - , subq_13.listings AS listings + listing + , SUM(bookings) AS listing__bookings FROM ( - -- Read Elements From Semantic Model 'listings_latest' + -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS listings - FROM ***************************.dim_listings_latest listings_latest_src_28000 - ) subq_13 - LEFT OUTER JOIN ( - -- Aggregate Measures - -- Compute Metrics via Expressions - -- Pass Only Elements: ['listing', 'listing__bookings'] + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_16 + GROUP BY + listing +) + +, cm_4_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['listings',] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + SUM(listings) AS listings + FROM ( + -- Join Standard Outputs SELECT - listing - , SUM(bookings) AS listing__bookings + cm_3_cte.listing__bookings AS listing__bookings + , subq_13.listings AS listings FROM ( - -- Read Elements From Semantic Model 'bookings_source' + -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['bookings', 'listing'] SELECT listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_16 - GROUP BY - listing - ) subq_19 - ON - subq_13.listing = subq_19.listing -) subq_20 -WHERE listing__bookings > 2 + , 1 AS listings + FROM ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_13 + LEFT OUTER JOIN + cm_3_cte cm_3_cte + ON + subq_13.listing = cm_3_cte.listing + ) subq_20 + WHERE listing__bookings > 2 +) + +SELECT + listings AS listings +FROM cm_4_cte cm_4_cte diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 0b4cc0bffe..becd9fa003 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/BigQuery/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY metric_time__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 4a9171736e..44bb3aa0b7 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Databricks/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 60600f1541..a3f15fdae1 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Postgres/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index cb4519e9f9..4abb45763e 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Redshift/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index 477d340b01..cbc3ec60ad 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Snowflake/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql index afb011f5db..c1a3ecf105 100644 --- a/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_metric_time_dimension_to_sql.py/SqlQueryPlan/Trino/test_simple_query_with_metric_time_dimension__plan0_optimized.sql @@ -15,11 +15,7 @@ WITH rss_28020_cte AS ( FROM ***************************.fct_bookings bookings_source_src_28000 ) -SELECT - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) AS metric_time__day - , MAX(subq_14.bookings) AS bookings - , MAX(subq_19.booking_payments) AS booking_payments -FROM ( +, cm_0_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings', 'metric_time__day'] @@ -31,8 +27,9 @@ FROM ( FROM rss_28020_cte rss_28020_cte GROUP BY ds__day -) subq_14 -FULL OUTER JOIN ( +) + +, cm_1_cte AS ( -- Read From CTE For node_id=rss_28020 -- Metric Time Dimension 'paid_at' -- Pass Only Elements: ['booking_payments', 'metric_time__day'] @@ -44,8 +41,16 @@ FULL OUTER JOIN ( FROM rss_28020_cte rss_28020_cte GROUP BY paid_at__day -) subq_19 +) + +SELECT + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) AS metric_time__day + , MAX(cm_0_cte.bookings) AS bookings + , MAX(cm_1_cte.booking_payments) AS booking_payments +FROM cm_0_cte cm_0_cte +FULL OUTER JOIN + cm_1_cte cm_1_cte ON - subq_14.metric_time__day = subq_19.metric_time__day + cm_0_cte.metric_time__day = cm_1_cte.metric_time__day GROUP BY - COALESCE(subq_14.metric_time__day, subq_19.metric_time__day) + COALESCE(cm_0_cte.metric_time__day, cm_1_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 71d8872b72..27a2309f11 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS FLOAT64) / CAST(NULLIF(max_booking_value, 0) AS FLOAT64) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATETIME_TRUNC(ds, day) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant GROUP BY metric_time__day -) subq_23 +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS FLOAT64) / CAST(NULLIF(max_booking_value, 0) AS FLOAT64) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index c0df4693cf..6368e329aa 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATE_SUB(CAST(subq_46.ds AS DATETIME), INTERVAL 14 day) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATE_SUB(CAST(subq_46.ds AS DATETIME), INTERVAL 14 day) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - metric_time__day - , listing__country_latest -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + metric_time__day + , listing__country_latest + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 649c633500..9ac3203b59 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_SUB(CAST(subq_37.ds AS DATETIME), INTERVAL 14 day) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATE_SUB(CAST(subq_37.ds AS DATETIME), INTERVAL 14 day) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY metric_time__day , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - metric_time__day - , listing__country_latest -) subq_47 + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index be3f4b4464..9fa671c44e 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/BigQuery/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS FLOAT64) / CAST(NULLIF(subq_82.views, 0) AS FLOAT64)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_82 +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS FLOAT64) / CAST(NULLIF(views, 0) AS FLOAT64) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + listing__capacity_latest + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY listing__capacity_latest diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 92979c0076..0d6fa19c3b 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 9daa5092a0..a86cca091e 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 3916fbc8d1..394dd1880e 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 605bb59d1b..9cf6d13312 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Databricks/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 5fc69eaebc..82bb20018d 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 15ac247a92..32364e7af4 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + subq_46.ds - MAKE_INTERVAL(days => 14) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - subq_46.ds - MAKE_INTERVAL(days => 14) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 637b25a3f4..6da79aa4b4 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + subq_37.ds - MAKE_INTERVAL(days => 14) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - subq_37.ds - MAKE_INTERVAL(days => 14) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index c8ab77479e..d78d9be6b1 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Postgres/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE PRECISION)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 1f5468866c..e5207ba823 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE PRECISION) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 26fd818dd1..2dfeb14491 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 360555b7ca..334d61d843 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 907567ef0b..5d31af894a 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Redshift/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE PRECISION) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE PRECISION)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE PRECISION) / CAST(NULLIF(views, 0) AS DOUBLE PRECISION) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 3a8e14329a..1649272e0b 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 0910734c31..5add6d3c91 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATEADD(day, -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 0fbcd96ac6..bfcf06a48d 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATEADD(day, -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 4076772c08..b2cc1d5d48 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Snowflake/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql index 88489a386f..59e7c04db6 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_different_filters_on_same_measure_source_categorical_dimension__plan0_optimized.sql @@ -12,52 +12,65 @@ docstring: the source input for the latter input must NOT have the filter applied to it. sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.average_booking_value) AS average_booking_value - , MAX(subq_22.max_booking_value) AS max_booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , MAX(booking_value) AS max_booking_value + , is_instant AS booking__is_instant + , booking_value AS average_booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['max_booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , MAX(booking_value) AS max_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(average_booking_value AS DOUBLE) / CAST(NULLIF(max_booking_value, 0) AS DOUBLE) AS instant_booking_fraction_of_max_value + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.average_booking_value) AS average_booking_value + , MAX(cm_7_cte.max_booking_value) AS max_booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_fraction_of_max_value AS instant_booking_fraction_of_max_value +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql index 0a8042dc50..893e586337 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_fill_nulls_time_spine_metric_predicate_pushdown__plan0_optimized.sql @@ -6,47 +6,97 @@ docstring: TODO: support metric time filters sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Compute Metrics via Expressions SELECT - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest - , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 - , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 FROM ( - -- Compute Metrics via Expressions + -- Join to Time Spine Dataset SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_fill_nulls_with_0 - FROM ( - -- Join to Time Spine Dataset + subq_40.ds AS metric_time__day + , subq_38.listing__country_latest AS listing__country_latest + , subq_38.bookings AS bookings + FROM ***************************.mf_time_spine subq_40 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures SELECT - subq_40.ds AS metric_time__day - , subq_38.listing__country_latest AS listing__country_latest - , subq_38.bookings AS bookings - FROM ***************************.mf_time_spine subq_40 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_31.metric_time__day AS metric_time__day + , subq_31.booking__is_instant AS booking__is_instant + , subq_31.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_31.metric_time__day AS metric_time__day - , subq_31.booking__is_instant AS booking__is_instant - , subq_31.bookings AS bookings - FROM ( + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_31 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_31.listing = listings_latest_src_28000.listing_id + ) subq_35 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_38 + ON + subq_40.ds = subq_38.metric_time__day + ) subq_41 +) + +, cm_7_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , COALESCE(bookings, 0) AS bookings_2_weeks_ago + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_56.ds AS metric_time__day + , subq_54.listing__country_latest AS listing__country_latest + , subq_54.bookings AS bookings + FROM ***************************.mf_time_spine subq_56 + LEFT OUTER JOIN ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_47.metric_time__day AS metric_time__day + , subq_47.booking__is_instant AS booking__is_instant + , subq_47.bookings AS bookings + FROM ( + -- Join to Time Spine Dataset + SELECT + subq_46.ds AS metric_time__day + , subq_44.listing AS listing + , subq_44.booking__is_instant AS booking__is_instant + , subq_44.bookings AS bookings + FROM ***************************.mf_time_spine subq_46 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -55,91 +105,68 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_31 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_44 ON - subq_31.listing = listings_latest_src_28000.listing_id - ) subq_35 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_38 - ON - subq_40.ds = subq_38.metric_time__day - ) subq_41 - ) subq_42 - FULL OUTER JOIN ( - -- Compute Metrics via Expressions + DATE_ADD('day', -14, subq_46.ds) = subq_44.metric_time__day + ) subq_47 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_47.listing = listings_latest_src_28000.listing_id + ) subq_51 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest + ) subq_54 + ON + subq_56.ds = subq_54.metric_time__day + ) subq_57 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 - bookings_2_weeks_ago AS bookings_growth_2_weeks_fill_nulls_with_0 + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , COALESCE(bookings, 0) AS bookings_2_weeks_ago + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) AS metric_time__day + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) AS listing__country_latest + , COALESCE(MAX(subq_42.bookings_fill_nulls_with_0), 0) AS bookings_fill_nulls_with_0 + , COALESCE(MAX(subq_58.bookings_2_weeks_ago), 0) AS bookings_2_weeks_ago FROM ( - -- Join to Time Spine Dataset + -- Read From CTE For node_id=cm_6 SELECT - subq_56.ds AS metric_time__day - , subq_54.listing__country_latest AS listing__country_latest - , subq_54.bookings AS bookings - FROM ***************************.mf_time_spine subq_56 - LEFT OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_47.metric_time__day AS metric_time__day - , subq_47.booking__is_instant AS booking__is_instant - , subq_47.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_46.ds AS metric_time__day - , subq_44.listing AS listing - , subq_44.booking__is_instant AS booking__is_instant - , subq_44.bookings AS bookings - FROM ***************************.mf_time_spine subq_46 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_44 - ON - DATE_ADD('day', -14, subq_46.ds) = subq_44.metric_time__day - ) subq_47 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_47.listing = listings_latest_src_28000.listing_id - ) subq_51 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_54 - ON - subq_56.ds = subq_54.metric_time__day - ) subq_57 - ) subq_58 - ON - ( - subq_42.listing__country_latest = subq_58.listing__country_latest - ) AND ( - subq_42.metric_time__day = subq_58.metric_time__day - ) - GROUP BY - COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) - , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) -) subq_59 + metric_time__day + , listing__country_latest + , bookings_fill_nulls_with_0 + FROM cm_6_cte cm_6_cte + ) subq_42 + FULL OUTER JOIN ( + -- Read From CTE For node_id=cm_7 + SELECT + metric_time__day + , listing__country_latest + , bookings_2_weeks_ago + FROM cm_7_cte cm_7_cte + ) subq_58 + ON + ( + subq_42.listing__country_latest = subq_58.listing__country_latest + ) AND ( + subq_42.metric_time__day = subq_58.metric_time__day + ) + GROUP BY + COALESCE(subq_42.metric_time__day, subq_58.metric_time__day) + , COALESCE(subq_42.listing__country_latest, subq_58.listing__country_latest) + ) subq_59 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks_fill_nulls_with_0 AS bookings_growth_2_weeks_fill_nulls_with_0 +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql index 2187c07082..bc7a2e5f4c 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_offset_metric_with_query_time_filters__plan0_optimized.sql @@ -6,35 +6,69 @@ docstring: TODO: support metric time filters sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , listing__country_latest - , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings + FROM ( + -- Join Standard Outputs + SELECT + listings_latest_src_28000.country AS listing__country_latest + , subq_25.metric_time__day AS metric_time__day + , subq_25.booking__is_instant AS booking__is_instant + , subq_25.bookings AS bookings + FROM ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , is_instant AS booking__is_instant + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_25 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_25.listing = listings_latest_src_28000.listing_id + ) subq_29 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_7_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) AS metric_time__day - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) AS listing__country_latest - , MAX(subq_33.bookings) AS bookings - , MAX(subq_46.bookings_2_weeks_ago) AS bookings_2_weeks_ago + metric_time__day + , listing__country_latest + , SUM(bookings) AS bookings_2_weeks_ago FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.country AS listing__country_latest + , subq_38.metric_time__day AS metric_time__day + , subq_38.booking__is_instant AS booking__is_instant + , subq_38.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Join to Time Spine Dataset SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_25.metric_time__day AS metric_time__day - , subq_25.booking__is_instant AS booking__is_instant - , subq_25.bookings AS bookings - FROM ( + subq_37.ds AS metric_time__day + , subq_35.listing AS listing + , subq_35.booking__is_instant AS booking__is_instant + , subq_35.bookings AS bookings + FROM ***************************.mf_time_spine subq_37 + INNER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' SELECT @@ -43,71 +77,51 @@ FROM ( , is_instant AS booking__is_instant , 1 AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_25 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 + ) subq_35 ON - subq_25.listing = listings_latest_src_28000.listing_id - ) subq_29 - WHERE booking__is_instant - GROUP BY - metric_time__day - , listing__country_latest - ) subq_33 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__country_latest', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_ADD('day', -14, subq_37.ds) = subq_35.metric_time__day + ) subq_38 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_38.listing = listings_latest_src_28000.listing_id + ) subq_42 + WHERE booking__is_instant + GROUP BY + metric_time__day + , listing__country_latest +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , listing__country_latest + , bookings - bookings_2_weeks_ago AS bookings_growth_2_weeks + FROM ( + -- Combine Aggregated Outputs SELECT - metric_time__day - , listing__country_latest - , SUM(bookings) AS bookings_2_weeks_ago - FROM ( - -- Join Standard Outputs - SELECT - listings_latest_src_28000.country AS listing__country_latest - , subq_38.metric_time__day AS metric_time__day - , subq_38.booking__is_instant AS booking__is_instant - , subq_38.bookings AS bookings - FROM ( - -- Join to Time Spine Dataset - SELECT - subq_37.ds AS metric_time__day - , subq_35.listing AS listing - , subq_35.booking__is_instant AS booking__is_instant - , subq_35.bookings AS bookings - FROM ***************************.mf_time_spine subq_37 - INNER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , is_instant AS booking__is_instant - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_35 - ON - DATE_ADD('day', -14, subq_37.ds) = subq_35.metric_time__day - ) subq_38 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_38.listing = listings_latest_src_28000.listing_id - ) subq_42 - WHERE booking__is_instant + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) AS listing__country_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.bookings_2_weeks_ago) AS bookings_2_weeks_ago + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + ( + cm_6_cte.listing__country_latest = cm_7_cte.listing__country_latest + ) AND ( + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + ) GROUP BY - metric_time__day - , listing__country_latest - ) subq_46 - ON - ( - subq_33.listing__country_latest = subq_46.listing__country_latest - ) AND ( - subq_33.metric_time__day = subq_46.metric_time__day - ) - GROUP BY - COALESCE(subq_33.metric_time__day, subq_46.metric_time__day) - , COALESCE(subq_33.listing__country_latest, subq_46.listing__country_latest) -) subq_47 + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + , COALESCE(cm_6_cte.listing__country_latest, cm_7_cte.listing__country_latest) + ) subq_47 +) + +SELECT + metric_time__day AS metric_time__day + , listing__country_latest AS listing__country_latest + , bookings_growth_2_weeks AS bookings_growth_2_weeks +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql index 91f21bfa1b..ae24f5d9be 100644 --- a/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_predicate_pushdown_rendering.py/SqlQueryPlan/Trino/test_saved_query_with_metric_joins_and_filter__plan0_optimized.sql @@ -7,12 +7,7 @@ docstring: sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_51.bookings) AS bookings - , MAX(subq_61.views) AS views - , MAX(CAST(subq_82.bookings AS DOUBLE) / CAST(NULLIF(subq_82.views, 0) AS DOUBLE)) AS bookings_per_view -FROM ( +WITH cm_6_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] -- Aggregate Measures @@ -44,8 +39,9 @@ FROM ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_51 -FULL OUTER JOIN ( +) + +, cm_7_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['views', 'listing__capacity_latest'] -- Aggregate Measures @@ -77,87 +73,110 @@ FULL OUTER JOIN ( WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY listing__capacity_latest -) subq_61 -ON - subq_51.listing__capacity_latest = subq_61.listing__capacity_latest -FULL OUTER JOIN ( - -- Combine Aggregated Outputs +) + +, cm_8_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) AS listing__capacity_latest - , MAX(subq_71.bookings) AS bookings - , MAX(subq_81.views) AS views + listing__capacity_latest + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['bookings', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_63.metric_time__day AS metric_time__day + , subq_63.bookings AS bookings FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_63.metric_time__day AS metric_time__day - , subq_63.bookings AS bookings - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_63 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_63.listing = listings_latest_src_28000.listing_id - ) subq_67 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_71 - FULL OUTER JOIN ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['views', 'listing__capacity_latest'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_63 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_63.listing = listings_latest_src_28000.listing_id + ) subq_67 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') + GROUP BY + listing__capacity_latest +) + +, cm_9_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['views', 'listing__capacity_latest'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , SUM(views) AS views + FROM ( + -- Join Standard Outputs SELECT - listing__capacity_latest - , SUM(views) AS views + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , listings_latest_src_28000.capacity AS listing__capacity_latest + , subq_73.metric_time__day AS metric_time__day + , subq_73.views AS views FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'views_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , listings_latest_src_28000.capacity AS listing__capacity_latest - , subq_73.metric_time__day AS metric_time__day - , subq_73.views AS views - FROM ( - -- Read Elements From Semantic Model 'views_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS views - FROM ***************************.fct_views views_source_src_28000 - ) subq_73 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_73.listing = listings_latest_src_28000.listing_id - ) subq_77 - WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') - GROUP BY - listing__capacity_latest - ) subq_81 - ON - subq_71.listing__capacity_latest = subq_81.listing__capacity_latest + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS views + FROM ***************************.fct_views views_source_src_28000 + ) subq_73 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_73.listing = listings_latest_src_28000.listing_id + ) subq_77 + WHERE (listing__is_lux_latest) AND (metric_time__day >= '2020-01-02') GROUP BY - COALESCE(subq_71.listing__capacity_latest, subq_81.listing__capacity_latest) -) subq_82 + listing__capacity_latest +) + +, cm_10_cte AS ( + -- Compute Metrics via Expressions + SELECT + listing__capacity_latest + , CAST(bookings AS DOUBLE) / CAST(NULLIF(views, 0) AS DOUBLE) AS bookings_per_view + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_8_cte.bookings) AS bookings + , MAX(cm_9_cte.views) AS views + FROM cm_8_cte cm_8_cte + FULL OUTER JOIN + cm_9_cte cm_9_cte + ON + cm_8_cte.listing__capacity_latest = cm_9_cte.listing__capacity_latest + GROUP BY + COALESCE(cm_8_cte.listing__capacity_latest, cm_9_cte.listing__capacity_latest) + ) subq_82 +) + +SELECT + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) AS listing__capacity_latest + , MAX(cm_6_cte.bookings) AS bookings + , MAX(cm_7_cte.views) AS views + , MAX(cm_10_cte.bookings_per_view) AS bookings_per_view +FROM cm_6_cte cm_6_cte +FULL OUTER JOIN + cm_7_cte cm_7_cte +ON + cm_6_cte.listing__capacity_latest = cm_7_cte.listing__capacity_latest +FULL OUTER JOIN + cm_10_cte cm_10_cte ON - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest) = subq_82.listing__capacity_latest + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest) = cm_10_cte.listing__capacity_latest GROUP BY - COALESCE(subq_51.listing__capacity_latest, subq_61.listing__capacity_latest, subq_82.listing__capacity_latest) + COALESCE(cm_6_cte.listing__capacity_latest, cm_7_cte.listing__capacity_latest, cm_10_cte.listing__capacity_latest) diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql index 407adbf902..affb382b1a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATETIME_TRUNC(ds, day) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + metric_time__day +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY metric_time__day - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - metric_time__day -) subq_41 + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql index df236298a5..c0649bd6aa 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS FLOAT64) / CAST(NULLIF(booking_value, 0) AS FLOAT64) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATETIME_TRUNC(ds, day) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATETIME_TRUNC(ds, day) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - metric_time__day - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATETIME_TRUNC(ds, day) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY metric_time__day -) subq_23 +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS FLOAT64) / CAST(NULLIF(booking_value, 0) AS FLOAT64) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + metric_time__day + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 1d74c8190d..5efd2ee15a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 62a40bc24a..0ba7b73d4a 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: BigQuery --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS FLOAT64) / CAST(NULLIF(MAX(subq_20.listings), 0) AS FLOAT64) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS FLOAT64) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS FLOAT64) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql index e273bc2e0a..93b068558e 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/BigQuery/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATETIME_TRUNC(ds, day) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATETIME_TRUNC(created_at, day) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql index 32b76b94d5..c41c0dc2c6 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 0fb229c3c5..66f52b7e43 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 5be1cd7ccd..07e76472f2 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Databricks --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 35ffb7205f..4b5f0de2ac 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Databricks --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 43de92366e..0942923b67 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Databricks/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql index 4479ea1c94..bee53c6bf7 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql index e170240396..a7ba8abafa 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index dd13a61238..01892c5d6b 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Postgres --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index f956ae8c4a..91a2ea57e4 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Postgres --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 3e92a120ce..11e08ea2cb 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Postgres/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql index b7754d372a..72d8ce81e9 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql index bc2d185163..a5bf9def3d 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE PRECISION) / CAST(NULLIF(booking_value, 0) AS DOUBLE PRECISION) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 24fc0afc5f..223d225ef3 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Redshift --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index ba39460c0f..a316d223a3 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Redshift --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE PRECISION) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE PRECISION) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql index b56c02e168..1cbfdd46f8 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Redshift/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql index ade62236e9..cc108961fd 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql index 34d2c9ea44..8ae213be83 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 5a05e1279c..5e3f75cf18 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index c9596e6310..0956be1e75 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Snowflake --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql index fe083b5cd0..9e5a02bcb1 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Snowflake/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN '2020-01-01' AND '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql index b6a5a7744a..971722545d 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint__plan0_optimized.sql @@ -2,67 +2,80 @@ test_name: test_measure_constraint test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_12 +WITH cm_11_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) AS metric_time__day - , MAX(subq_35.average_booking_value) AS average_booking_value - , MAX(subq_35.bookings) AS bookings - , MAX(subq_40.booking_value) AS booking_value + metric_time__day + , AVG(average_booking_value) AS average_booking_value + , SUM(bookings) AS bookings FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['average_booking_value', 'bookings', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + -- Join Standard Outputs SELECT - metric_time__day - , AVG(average_booking_value) AS average_booking_value - , SUM(bookings) AS bookings + listings_latest_src_28000.is_lux AS listing__is_lux_latest + , subq_27.metric_time__day AS metric_time__day + , subq_27.bookings AS bookings + , subq_27.average_booking_value AS average_booking_value FROM ( - -- Join Standard Outputs + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' SELECT - listings_latest_src_28000.is_lux AS listing__is_lux_latest - , subq_27.metric_time__day AS metric_time__day - , subq_27.bookings AS bookings - , subq_27.average_booking_value AS average_booking_value - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , listing_id AS listing - , 1 AS bookings - , booking_value AS average_booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_27 - LEFT OUTER JOIN - ***************************.dim_listings_latest listings_latest_src_28000 - ON - subq_27.listing = listings_latest_src_28000.listing_id - ) subq_31 - WHERE listing__is_lux_latest - GROUP BY - metric_time__day - ) subq_35 - FULL OUTER JOIN ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions + DATE_TRUNC('day', ds) AS metric_time__day + , listing_id AS listing + , 1 AS bookings + , booking_value AS average_booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + ) subq_27 + LEFT OUTER JOIN + ***************************.dim_listings_latest listings_latest_src_28000 + ON + subq_27.listing = listings_latest_src_28000.listing_id + ) subq_31 + WHERE listing__is_lux_latest + GROUP BY + metric_time__day +) + +, cm_10_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 + GROUP BY + DATE_TRUNC('day', ds) +) + +, cm_12_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , average_booking_value * bookings / NULLIF(booking_value, 0) AS lux_booking_value_rate_expr + FROM ( + -- Combine Aggregated Outputs SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) AS metric_time__day + , MAX(cm_11_cte.average_booking_value) AS average_booking_value + , MAX(cm_11_cte.bookings) AS bookings + , MAX(cm_10_cte.booking_value) AS booking_value + FROM cm_11_cte cm_11_cte + FULL OUTER JOIN + cm_10_cte cm_10_cte + ON + cm_11_cte.metric_time__day = cm_10_cte.metric_time__day GROUP BY - DATE_TRUNC('day', ds) - ) subq_40 - ON - subq_35.metric_time__day = subq_40.metric_time__day - GROUP BY - COALESCE(subq_35.metric_time__day, subq_40.metric_time__day) -) subq_41 + COALESCE(cm_11_cte.metric_time__day, cm_10_cte.metric_time__day) + ) subq_41 +) + +SELECT + metric_time__day AS metric_time__day + , lux_booking_value_rate_expr AS lux_booking_value_rate_expr +FROM cm_12_cte cm_12_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql index ffb73d97a2..186d8c6730 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_reused_measure__plan0_optimized.sql @@ -2,52 +2,65 @@ test_name: test_measure_constraint_with_reused_measure test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio -FROM ( - -- Combine Aggregated Outputs +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( + -- Constrain Output with WHERE + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions SELECT - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) AS metric_time__day - , MAX(subq_17.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint - , MAX(subq_22.booking_value) AS booking_value + metric_time__day + , SUM(booking_value) AS booking_value_with_is_instant_constraint FROM ( - -- Constrain Output with WHERE - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions - SELECT - metric_time__day - , SUM(booking_value) AS booking_value_with_is_instant_constraint - FROM ( - -- Read Elements From Semantic Model 'bookings_source' - -- Metric Time Dimension 'ds' - SELECT - DATE_TRUNC('day', ds) AS metric_time__day - , is_instant AS booking__is_instant - , booking_value - FROM ***************************.fct_bookings bookings_source_src_28000 - ) subq_13 - WHERE booking__is_instant - GROUP BY - metric_time__day - ) subq_17 - FULL OUTER JOIN ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' - -- Pass Only Elements: ['booking_value', 'metric_time__day'] - -- Aggregate Measures - -- Compute Metrics via Expressions SELECT DATE_TRUNC('day', ds) AS metric_time__day - , SUM(booking_value) AS booking_value + , is_instant AS booking__is_instant + , booking_value FROM ***************************.fct_bookings bookings_source_src_28000 - GROUP BY - DATE_TRUNC('day', ds) - ) subq_22 - ON - subq_17.metric_time__day = subq_22.metric_time__day + ) subq_13 + WHERE booking__is_instant + GROUP BY + metric_time__day +) + +, cm_7_cte AS ( + -- Read Elements From Semantic Model 'bookings_source' + -- Metric Time Dimension 'ds' + -- Pass Only Elements: ['booking_value', 'metric_time__day'] + -- Aggregate Measures + -- Compute Metrics via Expressions + SELECT + DATE_TRUNC('day', ds) AS metric_time__day + , SUM(booking_value) AS booking_value + FROM ***************************.fct_bookings bookings_source_src_28000 GROUP BY - COALESCE(subq_17.metric_time__day, subq_22.metric_time__day) -) subq_23 + DATE_TRUNC('day', ds) +) + +, cm_8_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , CAST(booking_value_with_is_instant_constraint AS DOUBLE) / CAST(NULLIF(booking_value, 0) AS DOUBLE) AS instant_booking_value_ratio + FROM ( + -- Combine Aggregated Outputs + SELECT + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) AS metric_time__day + , MAX(cm_6_cte.booking_value_with_is_instant_constraint) AS booking_value_with_is_instant_constraint + , MAX(cm_7_cte.booking_value) AS booking_value + FROM cm_6_cte cm_6_cte + FULL OUTER JOIN + cm_7_cte cm_7_cte + ON + cm_6_cte.metric_time__day = cm_7_cte.metric_time__day + GROUP BY + COALESCE(cm_6_cte.metric_time__day, cm_7_cte.metric_time__day) + ) subq_23 +) + +SELECT + metric_time__day AS metric_time__day + , instant_booking_value_ratio AS instant_booking_value_ratio +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql index 0752177f56..b8f6d0f9f0 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_measure_constraint_with_single_expr_and_alias__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_measure_constraint_with_single_expr_and_alias test_filename: test_query_rendering.py sql_engine: Trino --- --- Compute Metrics via Expressions -SELECT - metric_time__day - , delayed_bookings * 2 AS double_counted_delayed_bookings -FROM ( +-- Read From CTE For node_id=cm_5 +WITH cm_4_cte AS ( -- Constrain Output with WHERE -- Pass Only Elements: ['bookings', 'metric_time__day'] -- Aggregate Measures @@ -26,4 +23,23 @@ FROM ( WHERE NOT booking__is_instant GROUP BY metric_time__day -) subq_11 +) + +, cm_5_cte AS ( + -- Compute Metrics via Expressions + SELECT + metric_time__day + , delayed_bookings * 2 AS double_counted_delayed_bookings + FROM ( + -- Read From CTE For node_id=cm_4 + SELECT + metric_time__day + , delayed_bookings + FROM cm_4_cte cm_4_cte + ) subq_11 +) + +SELECT + metric_time__day AS metric_time__day + , double_counted_delayed_bookings AS double_counted_delayed_bookings +FROM cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql index 20a4074e4b..2df9ec6d00 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_metric_with_measures_from_multiple_sources_no_dimensions__plan0_optimized.sql @@ -2,11 +2,8 @@ test_name: test_metric_with_measures_from_multiple_sources_no_dimensions test_filename: test_query_rendering.py sql_engine: Trino --- --- Combine Aggregated Outputs --- Compute Metrics via Expressions -SELECT - CAST(MAX(subq_15.bookings) AS DOUBLE) / CAST(NULLIF(MAX(subq_20.listings), 0) AS DOUBLE) AS bookings_per_listing -FROM ( +-- Read From CTE For node_id=cm_8 +WITH cm_6_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['bookings',] @@ -15,8 +12,9 @@ FROM ( SELECT SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 -) subq_15 -CROSS JOIN ( +) + +, cm_7_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Pass Only Elements: ['listings',] @@ -25,4 +23,18 @@ CROSS JOIN ( SELECT SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 -) subq_20 +) + +, cm_8_cte AS ( + -- Combine Aggregated Outputs + -- Compute Metrics via Expressions + SELECT + CAST(MAX(cm_6_cte.bookings) AS DOUBLE) / CAST(NULLIF(MAX(cm_7_cte.listings), 0) AS DOUBLE) AS bookings_per_listing + FROM cm_6_cte cm_6_cte + CROSS JOIN + cm_7_cte cm_7_cte +) + +SELECT + bookings_per_listing AS bookings_per_listing +FROM cm_8_cte cm_8_cte diff --git a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql index 47112df5d5..3ece8d6305 100644 --- a/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql +++ b/tests_metricflow/snapshots/test_query_rendering.py/SqlQueryPlan/Trino/test_multiple_metrics_no_dimensions__plan0_optimized.sql @@ -3,10 +3,7 @@ test_filename: test_query_rendering.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - MAX(subq_17.bookings) AS bookings - , MAX(subq_23.listings) AS listings -FROM ( +WITH cm_4_cte AS ( -- Read Elements From Semantic Model 'bookings_source' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -17,8 +14,9 @@ FROM ( SUM(1) AS bookings FROM ***************************.fct_bookings bookings_source_src_28000 WHERE DATE_TRUNC('day', ds) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-01' -) subq_17 -CROSS JOIN ( +) + +, cm_5_cte AS ( -- Read Elements From Semantic Model 'listings_latest' -- Metric Time Dimension 'ds' -- Constrain Time Range to [2020-01-01T00:00:00, 2020-01-01T00:00:00] @@ -29,4 +27,11 @@ CROSS JOIN ( SUM(1) AS listings FROM ***************************.dim_listings_latest listings_latest_src_28000 WHERE DATE_TRUNC('day', created_at) BETWEEN timestamp '2020-01-01' AND timestamp '2020-01-01' -) subq_23 +) + +SELECT + MAX(cm_4_cte.bookings) AS bookings + , MAX(cm_5_cte.listings) AS listings +FROM cm_4_cte cm_4_cte +CROSS JOIN + cm_5_cte cm_5_cte diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql index 3c7a4e4722..55e79e4c2b 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/BigQuery/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: BigQuery --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY metric_time__day diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql index 5c1346422b..4f6f90d73a 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Databricks/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Databricks --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql index 8b32d1151e..8795f616f6 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Postgres/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Postgres --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql index 27a52649d7..3e897c4f0f 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Redshift/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Redshift --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql index 0041085981..69a46525fa 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Snowflake/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Snowflake --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) diff --git a/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql b/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql index aa7df86eb9..884253f0f0 100644 --- a/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql +++ b/tests_metricflow/snapshots/test_rendered_query.py/str/Trino/test_id_enumeration__query.sql @@ -3,11 +3,7 @@ test_filename: test_rendered_query.py sql_engine: Trino --- -- Combine Aggregated Outputs -SELECT - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) AS metric_time__day - , MAX(subq_4.bookings) AS bookings - , MAX(subq_9.listings) AS listings -FROM ( +WITH cm_2_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -24,8 +20,9 @@ FROM ( ) subq_2 GROUP BY metric_time__day -) subq_4 -FULL OUTER JOIN ( +) + +, cm_3_cte AS ( -- Aggregate Measures -- Compute Metrics via Expressions SELECT @@ -42,8 +39,16 @@ FULL OUTER JOIN ( ) subq_7 GROUP BY metric_time__day -) subq_9 +) + +SELECT + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day) AS metric_time__day + , MAX(cm_2_cte.bookings) AS bookings + , MAX(cm_3_cte.listings) AS listings +FROM cm_2_cte cm_2_cte +FULL OUTER JOIN + cm_3_cte cm_3_cte ON - subq_4.metric_time__day = subq_9.metric_time__day + cm_2_cte.metric_time__day = cm_3_cte.metric_time__day GROUP BY - COALESCE(subq_4.metric_time__day, subq_9.metric_time__day) + COALESCE(cm_2_cte.metric_time__day, cm_3_cte.metric_time__day)